|
|
@@ -20,7 +20,7 @@
|
|
|
#define MB_MAX_LEN 250
|
|
|
|
|
|
|
|
|
-//#define MB_PARA_SIM
|
|
|
+#define MB_PARA_SIM
|
|
|
//#define SLAVE_DATA_SIM
|
|
|
|
|
|
|
|
|
@@ -112,9 +112,9 @@ static int slave_get_info(cascade_handle_t *cas, int hardread)
|
|
|
|
|
|
#ifdef SLAVE_DATA_SIM
|
|
|
for(j=1; j<10; j++) {
|
|
|
- info->board[info->cnt].addr = 1;
|
|
|
- info->board[info->cnt].type = 3;
|
|
|
- info->board[info->cnt].ch_addr = 4;
|
|
|
+ info->channel[info->cnt].addr = 1;
|
|
|
+ info->channel[info->cnt].type = 3;
|
|
|
+ info->channel[info->cnt].ch_addr = 4;
|
|
|
info->cnt++;
|
|
|
}
|
|
|
#else
|
|
|
@@ -124,9 +124,9 @@ static int slave_get_info(cascade_handle_t *cas, int hardread)
|
|
|
dm->_global_device_info->product_pwr_type);
|
|
|
if(r==0) {
|
|
|
for(j=0; j<maxChn; j++) {
|
|
|
- info->board[info->cnt].addr = i;
|
|
|
- info->board[info->cnt].type = type;
|
|
|
- info->board[info->cnt].ch_addr = j+1;
|
|
|
+ info->channel[info->cnt].addr = i;
|
|
|
+ info->channel[info->cnt].type = type;
|
|
|
+ info->channel[info->cnt].ch_addr = j+1;
|
|
|
info->cnt++;
|
|
|
}
|
|
|
}
|
|
|
@@ -139,9 +139,9 @@ static int slave_get_info(cascade_handle_t *cas, int hardread)
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- info->board[info->cnt].addr = tmp->product_saddr;
|
|
|
- info->board[info->cnt].type = tmp->product_ch_type;
|
|
|
- info->board[info->cnt].ch_addr = tmp->product_ch_addr;
|
|
|
+ info->channel[info->cnt].addr = tmp->product_saddr;
|
|
|
+ info->channel[info->cnt].type = tmp->product_ch_type;
|
|
|
+ info->channel[info->cnt].ch_addr = tmp->product_ch_addr;
|
|
|
info->cnt++;
|
|
|
}
|
|
|
}
|
|
|
@@ -150,17 +150,54 @@ static int slave_get_info(cascade_handle_t *cas, int hardread)
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
+static int get_ac_info(int nCh, int nType, PowerInfo *info)
|
|
|
+{
|
|
|
+ GlobalDeviceManager *dm=get_dm();
|
|
|
+ GlobalTreeACManager *tmp = NULL;
|
|
|
+
|
|
|
+ list_for_each_entry(tmp, &dm->_globalPowerManger.list_Tree_AC, list_Tree_AC)
|
|
|
+ {
|
|
|
+ if (tmp->product_ch_id != nCh ||tmp->product_ph_type!=nType){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ *info = tmp->_PowerInfo;
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return -1;
|
|
|
+}
|
|
|
static int slave_get_all_data(cascade_handle_t *cas)
|
|
|
+{
|
|
|
+ GlobalPowerManger *tmp=NULL;
|
|
|
+ GlobalDeviceManager *dm=get_dm();
|
|
|
+
|
|
|
+ cas->allInfo.type = dm->_global_device_info->product_pwr_type;
|
|
|
+ if (dm->_global_device_info->product_pwr_type == SmartPDU_Tree_AC_Tree ||
|
|
|
+ dm->_global_device_info->product_pwr_type == SmartPDU_Tree_AC_One)
|
|
|
+ {
|
|
|
+ get_ac_info(0, 0, &cas->allInfo.info[0]);
|
|
|
+ get_ac_info(0, 1, &cas->allInfo.info[1]);
|
|
|
+ get_ac_info(0, 2, &cas->allInfo.info[2]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ dev_search_latest_power_info(dm->db, dm->_global_device_info->product_id, 0, &cas->allInfo.info[0]);
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static int slave_get_ch_data(cascade_handle_t *cas)
|
|
|
{
|
|
|
GlobalDeviceManager *dm=get_dm();
|
|
|
GlobalPowerManger *tmp=NULL;
|
|
|
|
|
|
- cas->pwrInfo.cnt=0;
|
|
|
+ cas->chInfo.cnt=0;
|
|
|
list_for_each_entry(tmp, &dm->_globalPowerManger.list, list)
|
|
|
{
|
|
|
if(tmp) {
|
|
|
- cas->pwrInfo.info[cas->pwrInfo.cnt] = tmp->_PowerInfo;
|
|
|
- cas->pwrInfo.cnt++;
|
|
|
+ cas->chInfo.info[cas->chInfo.cnt] = tmp->_PowerInfo;
|
|
|
+ cas->chInfo.cnt++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -325,7 +362,7 @@ static int mb_read(cascade_handle_t *cas, int addr, data_t *d)
|
|
|
//LOGE("___ mb_read dlen %d is wrong!\n", d->dlen);
|
|
|
return -1;
|
|
|
}
|
|
|
- LOGD("___ mb_read dlen: %d\n", d->dlen);
|
|
|
+ //LOGD("___ mb_read dlen: %d\n", d->dlen);
|
|
|
|
|
|
while(1) {
|
|
|
if(rlen+oncelen>d->dlen) {
|
|
|
@@ -369,7 +406,7 @@ static int mb_write(cascade_handle_t *cas, int addr, data_t *d)
|
|
|
//LOGE("___ mb_write dlen %d is wrong!\n", d->dlen);
|
|
|
return -1;
|
|
|
}
|
|
|
- LOGD("___ mb_write dlen: %d\n", d->dlen);
|
|
|
+ //LOGD("___ mb_write dlen: %d\n", d->dlen);
|
|
|
|
|
|
while(1) {
|
|
|
if(wlen+oncelen>d->dlen) {
|
|
|
@@ -444,8 +481,6 @@ static int mb_receive(cascade_handle_t *cas)
|
|
|
}
|
|
|
|
|
|
if(h.reg==CASCADE_REG_WRITE) {
|
|
|
-
|
|
|
- LOGD("____ CASCADE_REG_WRITE, %d\n", cas->cmd.cmd);
|
|
|
switch(cas->cmd.cmd) {
|
|
|
case CASCADE_CMD_OPEN:
|
|
|
case CASCADE_CMD_CLOSE:
|
|
|
@@ -454,11 +489,18 @@ static int mb_receive(cascade_handle_t *cas)
|
|
|
char buff[80];
|
|
|
board_info_t *info=&cas->info;
|
|
|
|
|
|
+ if(cas->cmd.cmd==CASCADE_CMD_OPEN) {
|
|
|
+ LOGD("____ slave CASCADE_CMD_OPEN\n");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ LOGD("____ slave CASCADE_CMD_CLOSE\n");
|
|
|
+ }
|
|
|
+
|
|
|
for(i=0; i<info->cnt; i++) {
|
|
|
- if(info->board[i].addr>0) {
|
|
|
- t = g_switch_set_all_ctrl(&get_dm()->_globalRelaySampManger, info->board[i].type, info->board[i].addr, (cas->cmd.cmd==CASCADE_CMD_OPEN)?1:0);
|
|
|
+ if(info->channel[i].addr>0) {
|
|
|
+ t = g_switch_set_all_ctrl(&get_dm()->_globalRelaySampManger, info->channel[i].type, info->channel[i].addr, (cas->cmd.cmd==CASCADE_CMD_OPEN)?1:0);
|
|
|
if(t) {
|
|
|
- LOGE("___ %d %s failed\n", info->board[i].addr, (cas->cmd.cmd==CASCADE_CMD_OPEN)?"open":"close");
|
|
|
+ LOGE("___ %d %s failed\n", info->channel[i].addr, (cas->cmd.cmd==CASCADE_CMD_OPEN)?"open":"close");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -503,9 +545,21 @@ static int mb_receive(cascade_handle_t *cas)
|
|
|
|
|
|
case CASCADE_CMD_QUERY_CH:
|
|
|
{
|
|
|
- LOGD("___ slave CASCADE_CMD_QUERY_CH\n");
|
|
|
- slave_get_all_data(cas);
|
|
|
- memcpy(cas->map2->tab_registers+(h.reg-CASCADE_REG_OFFSET), ((char*)cas->pwrInfo.info)+sendlen, h.regcnt*2);
|
|
|
+ #if 0
|
|
|
+ if(cas->cmd.obj==OBJ_OVERALL) {
|
|
|
+ LOGD("___ slave CASCADE_CMD_QUERY_CH, overall\n");
|
|
|
+
|
|
|
+ slave_get_all_data(cas);
|
|
|
+ memcpy(cas->map2->tab_registers+(h.reg-CASCADE_REG_OFFSET), ((char*)&cas->allInfo)+sendlen, h.regcnt*2);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ #endif
|
|
|
+ {
|
|
|
+ LOGD("___ slave CASCADE_CMD_QUERY_CH, channel\n");
|
|
|
+
|
|
|
+ slave_get_ch_data(cas);
|
|
|
+ memcpy(cas->map2->tab_registers+(h.reg-CASCADE_REG_OFFSET), ((char*)&cas->chInfo)+sendlen, h.regcnt*2);
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -560,7 +614,7 @@ static int cascade_scan(cascade_handle_t *cas)
|
|
|
slave_add(cas, cas->scanAddr);
|
|
|
}
|
|
|
else {
|
|
|
- LOGW("____ scan %d fail, %s\n", cas->scanAddr, modbus_strerror(errno));
|
|
|
+ //LOGW("____ scan %d fail, %s\n", cas->scanAddr, modbus_strerror(errno));
|
|
|
}
|
|
|
}
|
|
|
cas->scanAddr++;
|
|
|
@@ -590,23 +644,27 @@ static void power_init(void)
|
|
|
}
|
|
|
static int power_clear(cascade_handle_t *cas)
|
|
|
{
|
|
|
- GlobalPowerManger *tmp=NULL;
|
|
|
+ GlobalPowerManger *pos,*tmp=NULL;
|
|
|
GlobalDeviceManager *dm2=get_dm2();
|
|
|
|
|
|
- if(list_empty(&dm2->_globalPowerManger.list))
|
|
|
- return;
|
|
|
+ if(list_empty(&dm2->_globalPowerManger.list)) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
- list_for_each_entry(tmp, &dm2->_globalPowerManger.list, list)
|
|
|
+#if 0
|
|
|
+ list_for_each_entry(tmp,&dm2->_globalPowerManger.list,list)
|
|
|
{
|
|
|
- if(tmp) {
|
|
|
- if(tmp->global_over_manager) {
|
|
|
- free(tmp->global_over_manager);
|
|
|
- }
|
|
|
-
|
|
|
- list_del(&tmp->list);
|
|
|
- free(tmp);
|
|
|
- }
|
|
|
+ list_del(&tmp->list);
|
|
|
+ free(tmp);
|
|
|
}
|
|
|
+#else
|
|
|
+ list_for_each_entry_safe(pos,tmp,&dm2->_globalPowerManger.list,list)
|
|
|
+ {
|
|
|
+ list_del(&pos->list);
|
|
|
+ free(pos);
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
static int power_add(cascade_handle_t *cas)
|
|
|
@@ -627,11 +685,11 @@ static int power_add(cascade_handle_t *cas)
|
|
|
}
|
|
|
|
|
|
pm->product_id = dm2->_global_device_info->product_id;
|
|
|
- pm->product_saddr = info->board[i].addr;
|
|
|
+ pm->product_saddr = info->channel[i].addr;
|
|
|
pm->product_ch_id = i+1;
|
|
|
- pm->product_ch_addr = info->board[i].ch_addr;
|
|
|
+ pm->product_ch_addr = info->channel[i].ch_addr;
|
|
|
sprintf(pm->product_ch_name,"CH%d",pm->product_ch_id);
|
|
|
- pm->product_ch_type = info->board[i].type;
|
|
|
+ pm->product_ch_type = info->channel[i].type;
|
|
|
pm->product_ch_status = 0;
|
|
|
|
|
|
pm->product_ch_start_delay = 1000 * (chn%nGroups!=0?chn%nGroups:nGroups);
|
|
|
@@ -645,31 +703,24 @@ static int power_add(cascade_handle_t *cas)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-static int power_overall_update(cascade_handle_t *cas)
|
|
|
+static int power_all_update(cascade_handle_t *cas)
|
|
|
{
|
|
|
GlobalPowerManger *tmp=NULL;
|
|
|
GlobalDeviceManager *dm2=get_dm2();
|
|
|
- _OverAllPwrAckInfo *info=&cas->allInfo;
|
|
|
-
|
|
|
- if (dm2->_global_device_info->product_pwr_type == SmartPDU_Tree_AC_Tree ||
|
|
|
- dm2->_global_device_info->product_pwr_type == SmartPDU_Tree_AC_One)
|
|
|
- {
|
|
|
- dev_search_latest_t_ac_power_statistic_info(0, 0, info);
|
|
|
- dev_search_latest_t_ac_power_statistic_info(0, 1, info);
|
|
|
- dev_search_latest_t_ac_power_statistic_info(0, 2, info);
|
|
|
+ int type=cas->allInfo.type;
|
|
|
+ PowerInfo *info=cas->allInfo.info;
|
|
|
+
|
|
|
+ if(list_empty(&dm2->_globalPowerManger.list)) {
|
|
|
+ return -1;
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ list_for_each_entry(tmp, &dm2->_globalPowerManger.list, list)
|
|
|
{
|
|
|
- GlobalPowerInfo pwrInfo;
|
|
|
- dev_search_latest_power_info(dm2->db, dm2->_global_device_info->product_id, 0, &pwrInfo);
|
|
|
- info->voltage = pwrInfo._power_info.voltage ;
|
|
|
- info->current = pwrInfo._power_info.current ;
|
|
|
- info->power = pwrInfo._power_info.power ;
|
|
|
- info->freq = pwrInfo._power_info.freq ;
|
|
|
- info->consumption = pwrInfo._power_info.consumption ;
|
|
|
- info->factor = pwrInfo._power_info.factor ;
|
|
|
+ if(tmp) {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -678,15 +729,16 @@ static int power_ch_update(cascade_handle_t *cas)
|
|
|
int cnt=0;
|
|
|
GlobalPowerManger *tmp=NULL;
|
|
|
GlobalDeviceManager *dm2=get_dm2();
|
|
|
- PowerInfo *info=cas->pwrInfo.info;
|
|
|
+ PowerInfo *info=cas->chInfo.info;
|
|
|
|
|
|
- if(cas->pwrInfo.cnt==0) {
|
|
|
+ if(cas->chInfo.cnt==0 || list_empty(&dm2->_globalPowerManger.list)) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
+ LOGD("_____ power_ch_update\n");
|
|
|
list_for_each_entry(tmp, &dm2->_globalPowerManger.list, list)
|
|
|
{
|
|
|
- if(cnt>cas->pwrInfo.cnt) {
|
|
|
+ if(cnt>cas->chInfo.cnt) {
|
|
|
break;
|
|
|
}
|
|
|
tmp->_PowerInfo = info[cnt];
|
|
|
@@ -773,9 +825,11 @@ static int master_request(cascade_handle_t *cas, cmd_data_t *cmd)
|
|
|
case CASCADE_CMD_SAVE:
|
|
|
case CASCADE_CMD_SAVE3:
|
|
|
{
|
|
|
+ LOGD("____ master CMD: %d\n", cmd->cmd);
|
|
|
+
|
|
|
wdata.dlen = sizeof(*cmd);
|
|
|
wdata.data = (uint8_t*)cmd;
|
|
|
- r = mb_write(&cas->m, cur_dev_addr, &wdata);
|
|
|
+ r = mb_write(cas, cur_dev_addr, &wdata);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -824,15 +878,24 @@ static int master_query(cascade_handle_t *cas)
|
|
|
|
|
|
case CASCADE_CMD_QUERY_CH:
|
|
|
{
|
|
|
- LOGD("____ master query CASCADE_CMD_QUERY_CH\n");
|
|
|
+ //LOGD("____ master query CASCADE_CMD_QUERY_CH\n");
|
|
|
if(cas->info.cnt==0) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- rdata.dlen = sizeof(PowerInfo)*cas->info.cnt;
|
|
|
- rdata.data = (uint8_t*)cas->pwrInfo.info;
|
|
|
+#if 0
|
|
|
+ if(cmd->obj==OBJ_OVERALL) {
|
|
|
+ rdata.dlen = sizeof(PowerInfo);
|
|
|
+ rdata.data = (uint8_t*)cas->allInfo.info;
|
|
|
+ }
|
|
|
+ else
|
|
|
+#endif
|
|
|
+ {
|
|
|
+ rdata.dlen = sizeof(uint8_t)+sizeof(PowerInfo)*cas->info.cnt;
|
|
|
+ rdata.data = (uint8_t*)&cas->chInfo;
|
|
|
+ }
|
|
|
+
|
|
|
r = mb_write_read(cas, cur_dev_addr, &wdata, &rdata);
|
|
|
- LOGD("____ master mb_write_read CASCADE_CMD_QUERY_CH %d\n", r);
|
|
|
if(r==0) {
|
|
|
power_ch_update(cas);
|
|
|
}
|
|
|
@@ -1023,6 +1086,7 @@ static int set_modbus(cascade_handle_t *cas, ModbusInfo_t *info)
|
|
|
int cascade_init(void)
|
|
|
{
|
|
|
int r=0;
|
|
|
+ list_cfg_t lc;
|
|
|
cascade_handle_t *cas=&casHandle;
|
|
|
|
|
|
memset(cas, 0, sizeof(casHandle));
|
|
|
@@ -1032,7 +1096,6 @@ int cascade_init(void)
|
|
|
pthread_mutex_init(&cas->mutex, NULL);
|
|
|
|
|
|
cas->scanAddr = 1;
|
|
|
- cas->pwrInfo.info = malloc(128*sizeof(PowerInfo));
|
|
|
//cas->map = modbus_mapping_new(0,0,MAX_READ_REGS,MAX_WRITE_REGS);
|
|
|
cas->map2 = modbus_mapping_new_start_address(0,0,0,0,
|
|
|
CASCADE_REG_READ, MAX_READ_REGS2,
|
|
|
@@ -1040,7 +1103,14 @@ int cascade_init(void)
|
|
|
|
|
|
set_modbus(cas, get_mb());
|
|
|
slave_add(cas, 0);
|
|
|
-
|
|
|
+
|
|
|
+#if 0
|
|
|
+ lc.log = 0;
|
|
|
+ lc.max = 200;
|
|
|
+ lc.mode = LIST_FULL_FIFO;
|
|
|
+ cas->list = xlist_init(&lc);
|
|
|
+#endif
|
|
|
+
|
|
|
thread_start(THREAD_ID_CASCADE, cascade_thread, cas, 4*MB, 0);
|
|
|
thread_start(THREAD_ID_SCAN, scan_thread, cas, 4*MB, 0);
|
|
|
|