|
|
@@ -31,7 +31,6 @@ typedef struct {
|
|
|
mg_tls_t tls;
|
|
|
|
|
|
mg_conn_t *c;
|
|
|
- mg_conn_t *upg;
|
|
|
|
|
|
int inited;
|
|
|
int ws_cnt;
|
|
|
@@ -85,158 +84,175 @@ static int ws_send_data(ws_handle_t *wh, void *data, int len)
|
|
|
return ws_broadcast(wh, data, len, 0);
|
|
|
#endif
|
|
|
}
|
|
|
-
|
|
|
-static int get_board_info(board_info_t *info)
|
|
|
+//////////////////////////////////////////////////////////////////////
|
|
|
+static int ws_all_update(ws_handle_t *h)
|
|
|
{
|
|
|
+ char *json;
|
|
|
+ int r,r1,r2,r3;
|
|
|
+ GlobalDeviceManager* gdm= &__globalDeviceManage;
|
|
|
+ GlobalDeviceManager* gdm2=&__globalDeviceManage2;
|
|
|
+ GlobalDeviceManager* pdm=NULL;
|
|
|
+
|
|
|
+ pwrall_info_t *pall=&h->pwrall;
|
|
|
+ if(gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_Tree ||
|
|
|
+ gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_One ||
|
|
|
+ gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_One_B)
|
|
|
+ {
|
|
|
+ pall->ph3 = 1;
|
|
|
+
|
|
|
+ if (cur_dev_addr == 0)
|
|
|
+ {
|
|
|
+ lock_s_hold(LOCK_ID_POWER_UPDATE);
|
|
|
+ r1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &pall->ch[0], gdm);
|
|
|
+ r2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &pall->ch[1], gdm);
|
|
|
+ r3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &pall->ch[2], gdm);
|
|
|
+ lock_s_release(LOCK_ID_POWER_UPDATE);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ cascade_lock();
|
|
|
+ r1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &pall->ch[0], gdm2);
|
|
|
+ r2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &pall->ch[1], gdm2);
|
|
|
+ r3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &pall->ch[2], gdm2);
|
|
|
+ cascade_unlock();
|
|
|
+ }
|
|
|
+ if (0 == r1 && 0 == r2 && 0 == r3)
|
|
|
+ {
|
|
|
+ json = over_all_pwr_monitor_Tree_AC_to_json(&pall->ch[0], &pall->ch[1], &pall->ch[2]);
|
|
|
+ ws_send_data(h, json, strlen(json));
|
|
|
+ cJSON_free(json);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ pall->ph3 = 0;
|
|
|
+ if(cur_dev_addr==0) {
|
|
|
+ lock_s_hold(LOCK_ID_POWER_UPDATE);
|
|
|
+ r = dev_search_latest_power_statistic_info(gdm, &pall->ch[0]);
|
|
|
+ lock_s_release(LOCK_ID_POWER_UPDATE);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cascade_lock();
|
|
|
+ r = dev_search_latest_power_statistic_info(gdm2, &pall->ch[0]);
|
|
|
+ cascade_unlock();
|
|
|
+ }
|
|
|
+ if (0==r)
|
|
|
+ {
|
|
|
+ json = ws_over_status_ack_to_json(0, &pall->ch[0]);
|
|
|
+ ws_send_data(h, json, strlen(json));
|
|
|
+ cJSON_free(json);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return 0;
|
|
|
}
|
|
|
+static int ws_chn_update(ws_handle_t *h)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+ char *json;
|
|
|
+ _OverChnPwrAckInfo chInfo;
|
|
|
+ GlobalDeviceManager* gdm= &__globalDeviceManage;
|
|
|
+ GlobalDeviceManager* gdm2=&__globalDeviceManage2;
|
|
|
+ GlobalDeviceManager* pdm=NULL;
|
|
|
+
|
|
|
+ INIT_LIST_HEAD(&chInfo.list);
|
|
|
+ if(cur_dev_addr==0) {
|
|
|
+ lock_s_hold(LOCK_ID_POWER_UPDATE);
|
|
|
+ r = dev_search_latest_power_All_info(gdm,&chInfo, cur_dev_addr);
|
|
|
+ lock_s_release(LOCK_ID_POWER_UPDATE);
|
|
|
+ pdm = gdm;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cascade_lock();
|
|
|
+ r = dev_search_latest_power_All_info(gdm2,&chInfo, cur_dev_addr);
|
|
|
+ cascade_unlock();
|
|
|
+ pdm = gdm2;
|
|
|
+ }
|
|
|
+ if (0 == r)
|
|
|
+ {
|
|
|
+ json = ws_chn_status_ack_to_json(1, &chInfo, pdm);
|
|
|
+ ws_send_data(h, json, strlen(json));
|
|
|
+ cJSON_free(json);
|
|
|
+ }
|
|
|
+ _OverChnPwrAckInfo *node, *next;
|
|
|
+ list_for_each_entry_safe(node, next, &chInfo.list, list)
|
|
|
+ {
|
|
|
+ list_del(&node->list);
|
|
|
+ free(node);
|
|
|
+ }
|
|
|
|
|
|
-static int ws_upg_update(ws_handle_t *wh)
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static int ws_sensor_update(ws_handle_t *h)
|
|
|
{
|
|
|
- if((!wh->upg)) {
|
|
|
- return -1;
|
|
|
+ char *json;
|
|
|
+ GlobalDeviceManager* gdm= &__globalDeviceManage;
|
|
|
+
|
|
|
+ //lock_s_hold(LOCK_ID_SENSOR);
|
|
|
+ json = ws_sensor_status_ack_to_json(&gdm->_globalSensorManger);
|
|
|
+ //lock_s_release(LOCK_ID_SENSOR);
|
|
|
+ if(json) {
|
|
|
+ ws_send_data(h, json, strlen(json));
|
|
|
+ cJSON_free(json);
|
|
|
}
|
|
|
-
|
|
|
- extern board_list_t *get_board_list(void);
|
|
|
- board_list_t *l=get_board_list();
|
|
|
- char* json = board_to_json(l);
|
|
|
|
|
|
- mg_ws_send2(wh->upg, json, strlen(json), WEBSOCKET_OP_TEXT, WS_SEND_TLEN_MAX);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+static int ws_breaker_update(ws_handle_t *h)
|
|
|
+{
|
|
|
+ char *json;
|
|
|
+ GlobalDeviceManager* gdm= &__globalDeviceManage;
|
|
|
+ GlobalDeviceManager* gdm2=&__globalDeviceManage2;
|
|
|
+
|
|
|
+ if(cur_dev_addr==0) {
|
|
|
+ json = breaker_status_ack_to_json(gdm,0);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ json = breaker_status_ack_to_json(gdm2,cur_dev_addr);
|
|
|
+ }
|
|
|
+ ws_send_data(h, json, strlen(json));
|
|
|
+ cJSON_free(json);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+static int ws_upg_update(ws_handle_t *h)
|
|
|
+{
|
|
|
+ extern board_list_t *get_board_list(void);
|
|
|
+ board_list_t *l=get_board_list();
|
|
|
|
|
|
+ char* json = board_to_json(l);
|
|
|
+ ws_send_data(h, json, strlen(json));
|
|
|
+ cJSON_free(json);
|
|
|
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+/////////////////////////////////////////////////////////////
|
|
|
|
|
|
-static void web_update(ws_handle_t *wh)
|
|
|
+static int ws_update(ws_handle_t *h)
|
|
|
{
|
|
|
- GlobalSensorManger* _globalSensorMangerTemp;
|
|
|
- int ret = 0 ;
|
|
|
- GlobalSensorInfo _globalSensorInfo;
|
|
|
- struct tm* t ;
|
|
|
- struct timeval tv;
|
|
|
- struct timezone tz ;
|
|
|
- char* json_str = NULL ;
|
|
|
- GlobalPowerInfo _powerInfo;
|
|
|
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
|
|
|
- _OverChnPwrAckInfo chInfo;
|
|
|
- GlobalDeviceManager* gdm= &__globalDeviceManage;
|
|
|
- GlobalDeviceManager* gdm2=&__globalDeviceManage2;
|
|
|
- GlobalDeviceManager* pdm=NULL;
|
|
|
- int r,r1,r2,r3;
|
|
|
+ int r;
|
|
|
|
|
|
- if(ws_is_online(wh))
|
|
|
- {
|
|
|
- //overall info
|
|
|
- pwrall_info_t *pall=&wh->pwrall;
|
|
|
- if(gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_Tree ||
|
|
|
- gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_One ||
|
|
|
- gdm->_globalDevInfo.product.pwr_type==SmartPDU_Tree_AC_One_B)
|
|
|
- {
|
|
|
- pall->ph3 = 1;
|
|
|
-
|
|
|
- if (cur_dev_addr == 0)
|
|
|
- {
|
|
|
- lock_s_hold(LOCK_ID_POWER_UPDATE);
|
|
|
- r1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &pall->ch[0], gdm);
|
|
|
- r2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &pall->ch[1], gdm);
|
|
|
- r3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &pall->ch[2], gdm);
|
|
|
- lock_s_release(LOCK_ID_POWER_UPDATE);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- cascade_lock();
|
|
|
- r1 = dev_search_latest_t_ac_power_statistic_info(0, 0, &pall->ch[0], gdm2);
|
|
|
- r2 = dev_search_latest_t_ac_power_statistic_info(0, 1, &pall->ch[1], gdm2);
|
|
|
- r3 = dev_search_latest_t_ac_power_statistic_info(0, 2, &pall->ch[2], gdm2);
|
|
|
- cascade_unlock();
|
|
|
- }
|
|
|
- if (0 == r1 && 0 == r2 && 0 == r3)
|
|
|
- {
|
|
|
- json_str = over_all_pwr_monitor_Tree_AC_to_json(&pall->ch[0], &pall->ch[1], &pall->ch[2]);
|
|
|
- ws_send_data(wh, json_str, strlen(json_str));
|
|
|
- cJSON_free((void *)json_str);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- pall->ph3 = 0;
|
|
|
-
|
|
|
- if(cur_dev_addr==0) {
|
|
|
- lock_s_hold(LOCK_ID_POWER_UPDATE);
|
|
|
- r = dev_search_latest_power_statistic_info(gdm, &pall->ch[0]);
|
|
|
- lock_s_release(LOCK_ID_POWER_UPDATE);
|
|
|
- }
|
|
|
- else {
|
|
|
- cascade_lock();
|
|
|
- r = dev_search_latest_power_statistic_info(gdm2, &pall->ch[0]);
|
|
|
- cascade_unlock();
|
|
|
- }
|
|
|
- if (0==r)
|
|
|
- {
|
|
|
- json_str = ws_over_status_ack_to_json(0, &pall->ch[0]);
|
|
|
- ws_send_data(wh, json_str, strlen(json_str));
|
|
|
- cJSON_free(json_str);
|
|
|
- }
|
|
|
- }
|
|
|
+ if(!ws_is_online(h)) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
- //channel info
|
|
|
- {
|
|
|
- INIT_LIST_HEAD(&chInfo.list);
|
|
|
-
|
|
|
- if(cur_dev_addr==0) {
|
|
|
- lock_s_hold(LOCK_ID_POWER_UPDATE);
|
|
|
- r = dev_search_latest_power_All_info(gdm,&chInfo, cur_dev_addr);
|
|
|
- lock_s_release(LOCK_ID_POWER_UPDATE);
|
|
|
- pdm = gdm;
|
|
|
- }
|
|
|
- else {
|
|
|
- cascade_lock();
|
|
|
- r = dev_search_latest_power_All_info(gdm2,&chInfo, cur_dev_addr);
|
|
|
- cascade_unlock();
|
|
|
- pdm = gdm2;
|
|
|
- }
|
|
|
- if (0 == r)
|
|
|
- {
|
|
|
- json_str = ws_chn_status_ack_to_json(1, &chInfo, pdm);
|
|
|
- ws_send_data(wh, json_str, strlen(json_str));
|
|
|
- cJSON_free((void *)json_str);
|
|
|
- }
|
|
|
- _OverChnPwrAckInfo *node, *next;
|
|
|
- list_for_each_entry_safe(node, next, &chInfo.list, list)
|
|
|
- {
|
|
|
- list_del(&node->list);
|
|
|
- free(node);
|
|
|
- }
|
|
|
- }
|
|
|
+ //overall refresh
|
|
|
+ r = ws_all_update(h);
|
|
|
+
|
|
|
+ //channel refresh
|
|
|
+ r = ws_chn_update(h);
|
|
|
|
|
|
- //sensor info
|
|
|
- {
|
|
|
- //lock_s_hold(LOCK_ID_SENSOR);
|
|
|
- json_str = ws_sensor_status_ack_to_json(&gdm->_globalSensorManger);
|
|
|
- //lock_s_release(LOCK_ID_SENSOR);
|
|
|
- if(json_str) {
|
|
|
- ws_send_data(wh, json_str, strlen(json_str));
|
|
|
- cJSON_free(json_str);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // breaker info
|
|
|
- {
|
|
|
- if(cur_dev_addr==0) {
|
|
|
- json_str = breaker_status_ack_to_json(gdm,0);
|
|
|
- }else
|
|
|
- {
|
|
|
- json_str = breaker_status_ack_to_json(gdm2,cur_dev_addr);
|
|
|
- }
|
|
|
- ws_send_data(wh, json_str, strlen(json_str));
|
|
|
- cJSON_free(json_str);
|
|
|
- }
|
|
|
+ //sensor refresh
|
|
|
+ r = ws_sensor_update(h);
|
|
|
+
|
|
|
+ // breaker refresh
|
|
|
+ r = ws_breaker_update(h);
|
|
|
|
|
|
- //upgrade refresh
|
|
|
+ //upgrade refresh
|
|
|
+ r = ws_upg_update(h);
|
|
|
|
|
|
- }
|
|
|
+ return r;
|
|
|
}
|
|
|
static void fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
{
|
|
|
@@ -260,10 +276,6 @@ static void fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
if(c==wh->c) {
|
|
|
wh->c = NULL;
|
|
|
}
|
|
|
-
|
|
|
- if(c==wh->upg) {
|
|
|
- wh->upg = NULL;
|
|
|
- }
|
|
|
}
|
|
|
log_i("ws upgrade del connection ID:%d\n", c->id);
|
|
|
}
|
|
|
@@ -291,10 +303,6 @@ static void fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
wh->c = c;
|
|
|
log_i("ws upgrade new connection ID:%d\n", c->id);
|
|
|
}
|
|
|
- else if (mg_match(hm->uri, mg_str("/websocket/controlBoardUpgrade"), NULL)) {
|
|
|
- mg_ws_upgrade(c, hm, NULL);
|
|
|
- wh->upg = c;
|
|
|
- }
|
|
|
else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
|
|
|
mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
|
|
|
}
|
|
|
@@ -310,33 +318,36 @@ static void fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+static void timer_fn(void *arg)
|
|
|
+{
|
|
|
+ ws_handle_t *h=(ws_handle_t*)arg;
|
|
|
+ ws_update(h);
|
|
|
+}
|
|
|
+
|
|
|
static void* ws_thread(void* arg)
|
|
|
{
|
|
|
- int r;
|
|
|
- thread_handle_t *h=(thread_handle_t*)arg;
|
|
|
- ws_handle_t *wh=(ws_handle_t*)h->arg;
|
|
|
+ int r=0;
|
|
|
+ thread_handle_t *th=(thread_handle_t*)arg;
|
|
|
+ ws_handle_t *h=(ws_handle_t*)th->arg;
|
|
|
const char *ws_addr="ws://[::]:6785";
|
|
|
const char *wss_addr="wss://[::]:6786";
|
|
|
|
|
|
//mg_log_set(MG_LL_DEBUG);
|
|
|
- mg_mgr_init(&wh->mgr); // Initialise event manager
|
|
|
- mg_http_listen(&wh->mgr, ws_addr, fn, NULL); // Create HTTP listener
|
|
|
- mg_http_listen(&wh->mgr, wss_addr, fn, (void*)1);
|
|
|
+ mg_mgr_init(&h->mgr); // Initialise event manager
|
|
|
+ mg_http_listen(&h->mgr, ws_addr, fn, NULL); // Create HTTP listener
|
|
|
+ mg_http_listen(&h->mgr, wss_addr, fn, (void*)1);
|
|
|
|
|
|
- wh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
|
|
|
- wh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
|
|
|
- wh->inited = 1;
|
|
|
+ h->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
|
|
|
+ h->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
|
|
|
+ h->inited = 1;
|
|
|
|
|
|
- while(h->quit==0) {
|
|
|
- if (wh->c)
|
|
|
- {
|
|
|
- web_update(wh);
|
|
|
- }
|
|
|
- mg_mgr_poll(&wh->mgr, 1000); // Infinite event loop
|
|
|
- usleep(500000);
|
|
|
+ mg_timer_add(&h->mgr, 1000, MG_TIMER_REPEAT, timer_fn, h);
|
|
|
+ while(th->quit==0) {
|
|
|
+ mg_mgr_poll(&h->mgr, 100); // Infinite event loop
|
|
|
}
|
|
|
- wh->inited = 0;
|
|
|
- mg_mgr_free(&wh->mgr);
|
|
|
+ h->inited = 0;
|
|
|
+ mg_mgr_free(&h->mgr);
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
}
|
|
|
@@ -347,8 +358,6 @@ int websocket_init(void)
|
|
|
ws_handle_t *wh=&wsHandle;
|
|
|
|
|
|
memset(wh, 0, sizeof(ws_handle_t));
|
|
|
- //read_ipaddr(wh);
|
|
|
-
|
|
|
thread_start(THREAD_ID_WS, ws_thread, wh);
|
|
|
return 0;
|
|
|
}
|