|
|
@@ -5,6 +5,9 @@
|
|
|
#include "common.h"
|
|
|
#include "sys.h"
|
|
|
#include "thread.h"
|
|
|
+#include "sqlite_handle.h"
|
|
|
+#include "json_handle.h"
|
|
|
+#include "cascade.h"
|
|
|
|
|
|
#define WS_MAX 10
|
|
|
|
|
|
@@ -21,18 +24,127 @@ typedef struct {
|
|
|
|
|
|
int inited;
|
|
|
int ws_cnt;
|
|
|
- pthread_mutex_t mutex;
|
|
|
}ws_handle_t;
|
|
|
static ws_handle_t wsHandle;
|
|
|
|
|
|
+static int ws_broadcast(ws_handle_t *wh, void *data, int len)
|
|
|
+{
|
|
|
+ mg_conn_t *c;
|
|
|
+
|
|
|
+ for (c=wh->mgr.conns; c!=NULL; c=c->next) {
|
|
|
+ mg_ws_send(c, data, len, WEBSOCKET_OP_TEXT);
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
static void read_ipaddr(ws_handle_t *h)
|
|
|
{
|
|
|
sprintf(h->wpath,"ws://%s:6785", getLocalIpAddress("eth0"));
|
|
|
printf("____ ws path: %s\n", h->wpath);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+static void web_update(ws_handle_t *wh)
|
|
|
+{
|
|
|
+ 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 ;
|
|
|
+ _OverAllPwrAckInfo allInfo; //设备电源信息
|
|
|
+ _OverChnPwrAckInfo chInfo;
|
|
|
+ GlobalDeviceManager* gdm= &__globalDeviceManage;
|
|
|
+ GlobalDeviceManager* gdm2=&__globalDeviceManage2;
|
|
|
+
|
|
|
+ if(wh->ws_cnt>0)
|
|
|
+ {
|
|
|
+ //if(gdm->view_page==1)
|
|
|
+ {
|
|
|
+ if(gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_Tree ||
|
|
|
+ gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_One)
|
|
|
+ {
|
|
|
+ //三相
|
|
|
+ _OverAllPwrAckInfo l1,l2,l3;
|
|
|
+ if (cur_dev_addr == 0)
|
|
|
+ {
|
|
|
+ pthread_mutex_lock(&__globalDeviceManage._power_update);
|
|
|
+ dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm);
|
|
|
+ dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm);
|
|
|
+ dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm);
|
|
|
+ pthread_mutex_unlock(&__globalDeviceManage._power_update);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ cascade_lock();
|
|
|
+ dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm2);
|
|
|
+ dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm2);
|
|
|
+ dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm2);
|
|
|
+ cascade_unlock();
|
|
|
+ }
|
|
|
+ json_str = over_all_pwr_monitor_Tree_AC_to_json(&l1, &l2, &l3);
|
|
|
+
|
|
|
+ ws_broadcast(wh, json_str, strlen(json_str));
|
|
|
+ cJSON_free((void*)json_str);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(cur_dev_addr==0) {
|
|
|
+ pthread_mutex_lock(&__globalDeviceManage._power_update);
|
|
|
+ dev_search_latest_power_statistic_info(gdm, &allInfo);
|
|
|
+ pthread_mutex_unlock(&__globalDeviceManage._power_update);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cascade_lock();
|
|
|
+ dev_search_latest_power_statistic_info(gdm2, &allInfo);
|
|
|
+ cascade_unlock();
|
|
|
+ }
|
|
|
+ json_str = ws_over_status_ack_to_json(0, &allInfo);
|
|
|
+
|
|
|
+ ws_broadcast(wh, json_str, strlen(json_str));
|
|
|
+ cJSON_free(json_str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //推送通道信息
|
|
|
+ //else if(gdm->view_page==2)
|
|
|
+ {
|
|
|
+ INIT_LIST_HEAD(&chInfo.list);
|
|
|
+
|
|
|
+ if(cur_dev_addr==0) {
|
|
|
+ pthread_mutex_lock(&__globalDeviceManage._power_update);
|
|
|
+ dev_search_latest_power_All_info(gdm,&chInfo);
|
|
|
+ pthread_mutex_unlock(&__globalDeviceManage._power_update);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cascade_lock();
|
|
|
+ dev_search_latest_power_All_info(gdm2,&chInfo);
|
|
|
+ cascade_unlock();
|
|
|
+ }
|
|
|
+
|
|
|
+ json_str = ws_chn_status_ack_to_json(1,&chInfo);
|
|
|
+ ws_broadcast(wh, json_str, strlen(json_str));
|
|
|
+
|
|
|
+ //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
+ cJSON_free((void*)json_str);
|
|
|
+ //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
+ _OverChnPwrAckInfo *node,*next;
|
|
|
+ list_for_each_entry_safe(node, next, &chInfo.list, list)
|
|
|
+ {
|
|
|
+ list_del(&node->list);
|
|
|
+ free(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //else if (gdm->view_page==3)
|
|
|
+ {
|
|
|
+ json_str = ws_sensor_status_ack_to_json(&__globalDeviceManage._globalSensorManger);
|
|
|
+ ws_broadcast(wh, json_str, strlen(json_str));
|
|
|
+ cJSON_free(json_str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
static void fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
{
|
|
|
ws_handle_t *wh=&wsHandle;
|
|
|
@@ -94,9 +206,9 @@ void* websocket_thread(void* arg)
|
|
|
wh->inited = 1;
|
|
|
|
|
|
while(h->quit==0) {
|
|
|
- pthread_mutex_lock(&wh->mutex);
|
|
|
- mg_mgr_poll(&wh->mgr, 200); // Infinite event loop
|
|
|
- pthread_mutex_unlock(&wh->mutex);
|
|
|
+ mg_mgr_poll(&wh->mgr, 1000); // Infinite event loop
|
|
|
+
|
|
|
+ web_update(wh);
|
|
|
}
|
|
|
wh->inited = 0;
|
|
|
mg_mgr_free(&wh->mgr);
|
|
|
@@ -109,11 +221,6 @@ int websocket_init(void)
|
|
|
{
|
|
|
ws_handle_t *wh=&wsHandle;
|
|
|
|
|
|
- if (pthread_mutex_init(&wh->mutex, NULL) != 0) {
|
|
|
- log_e("websocket mutex init failed!\n");
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
memset(wh, 0, sizeof(ws_handle_t));
|
|
|
read_ipaddr(wh);
|
|
|
|
|
|
@@ -122,63 +229,5 @@ int websocket_init(void)
|
|
|
}
|
|
|
|
|
|
|
|
|
-int websocket_send(void *conn, void *data, int len)
|
|
|
-{
|
|
|
- int i;
|
|
|
- ws_handle_t *wh=&wsHandle;
|
|
|
-
|
|
|
- if(!wh->inited || !conn) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- pthread_mutex_lock(&wh->mutex);
|
|
|
- mg_ws_send(conn, data, len, WEBSOCKET_OP_TEXT);
|
|
|
- pthread_mutex_unlock(&wh->mutex);
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int websocket_broadcast(void *data, int len)
|
|
|
-{
|
|
|
- int i;
|
|
|
- mg_conn_t *c;
|
|
|
- ws_handle_t *wh=&wsHandle;
|
|
|
-
|
|
|
- if(!wh->inited) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- pthread_mutex_lock(&wh->mutex);
|
|
|
- for (c=wh->mgr.conns; c!=NULL; c=c->next) {
|
|
|
- mg_ws_send(c, data, len, WEBSOCKET_OP_TEXT);
|
|
|
- }
|
|
|
- pthread_mutex_unlock(&wh->mutex);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int websocket_is_online(void)
|
|
|
-{
|
|
|
- ws_handle_t *wh=&wsHandle;
|
|
|
- return (wh->ws_cnt>0)?1:0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-int websocket_free(void)
|
|
|
-{
|
|
|
- int r;
|
|
|
- ws_handle_t *wh=&wsHandle;
|
|
|
-
|
|
|
- if (!wh->inited) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- thread_stop(THREAD_ID_WS);
|
|
|
- pthread_mutex_destroy(&wh->mutex);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|