|
|
@@ -10,6 +10,7 @@
|
|
|
#include "sys.h"
|
|
|
#include "cfg.h"
|
|
|
#include "xlist.h"
|
|
|
+#include "websocket_handle.h"
|
|
|
#include "switch_ctrl.h"
|
|
|
#include "sqlite_handle.h"
|
|
|
|
|
|
@@ -24,8 +25,10 @@
|
|
|
#endif
|
|
|
|
|
|
|
|
|
+#define CONN_PERIOD 5000 //ms
|
|
|
#define SEND_PERIOD 5000 //ms
|
|
|
|
|
|
+
|
|
|
#ifdef USE_MQTT
|
|
|
|
|
|
char *topic_sub[MQTT_SUB_MAX]={
|
|
|
@@ -80,9 +83,9 @@ static mqtt_handle_t mqHandle={0};
|
|
|
static int my_recv(char *topic, char *data);
|
|
|
static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data);
|
|
|
static void timer_start(mqtt_handle_t *h);
|
|
|
-static int post_stat(int type, int id);
|
|
|
+static int send_stat(mqtt_handle_t *h, int type);
|
|
|
static uint32_t get_serv(char *json);
|
|
|
-static void post_once(void);
|
|
|
+static void send_once(mqtt_handle_t *h);
|
|
|
|
|
|
enum {
|
|
|
SERV_NTP=0,
|
|
|
@@ -101,31 +104,7 @@ enum {
|
|
|
#define BGET(flag,mask) ((flag)&(1<<(mask)))
|
|
|
#define BSET(flag,mask) ((flag)|=(1<<(mask)))
|
|
|
|
|
|
-static int ser_cmp(mqtt_server_t *a, mqtt_server_t *b)
|
|
|
-{
|
|
|
- if(strcmp(a->server, b->server) ||
|
|
|
- strcmp(a->ip, b->ip) ||
|
|
|
- strcmp(a->port, b->port) ||
|
|
|
- strcmp(a->user, b->user) ||
|
|
|
- strcmp(a->password, b->password) ||
|
|
|
- (a->mode!=b->mode)) {
|
|
|
- return 1;
|
|
|
- }
|
|
|
|
|
|
- return 0;
|
|
|
-}
|
|
|
-static int get_id(mqtt_handle_t *h, mqtt_server_t *ser)
|
|
|
-{
|
|
|
- int i,r=-1;
|
|
|
- mqtt_conn_t *conn=h->conn;
|
|
|
-
|
|
|
- for(i=0; i<MQTT_SER_MAX; i++) {
|
|
|
- if(conn[i].c && ser_cmp(ser, &conn[i].ser)==0) {
|
|
|
- return i;
|
|
|
- }
|
|
|
- }
|
|
|
- return -1;
|
|
|
-}
|
|
|
static char *get_int_str(int n)
|
|
|
{
|
|
|
static char tmp[32];
|
|
|
@@ -135,7 +114,7 @@ static char *get_int_str(int n)
|
|
|
static char *get_float_str(float n)
|
|
|
{
|
|
|
static char tmp[32];
|
|
|
- snprintf(tmp, sizeof(tmp), "%f", n);
|
|
|
+ snprintf(tmp, sizeof(tmp), "%0.3f", n);
|
|
|
return tmp;
|
|
|
}
|
|
|
static int get_date_time(char *d, char *t)
|
|
|
@@ -287,7 +266,7 @@ static int get_url(mqtt_server_t *ser, char *url)
|
|
|
int port=1883;
|
|
|
char *head="mqtts";
|
|
|
|
|
|
- if((!ser->server[0] && !ser->ip[0]) || !ser->port[0]) {
|
|
|
+ if(!ser->server[0] || !ser->port[0]) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
@@ -305,8 +284,7 @@ static int get_url(mqtt_server_t *ser, char *url)
|
|
|
head = "wss";
|
|
|
}
|
|
|
|
|
|
- char *server=(ser->server[0]?ser->server:ser->ip);
|
|
|
- sprintf(url, "%s://%s:%d", head, server, port);
|
|
|
+ sprintf(url, "%s://%s:%d", head, ser->server, port);
|
|
|
return 0;
|
|
|
}
|
|
|
static int my_conn(mqtt_handle_t *h)
|
|
|
@@ -323,6 +301,8 @@ static int my_conn(mqtt_handle_t *h)
|
|
|
.qos = 1,
|
|
|
.version = 4,
|
|
|
.keepalive = 60,
|
|
|
+ .topic = mg_str("hello"),
|
|
|
+ .message = mg_str("bye"),
|
|
|
.client_id = mg_str(ser[i].cid),
|
|
|
.user = mg_str(ser[i].user),
|
|
|
.pass = mg_str(ser[i].password),
|
|
|
@@ -353,17 +333,17 @@ static int my_send(mqtt_handle_t *h)
|
|
|
|
|
|
return r;
|
|
|
}
|
|
|
-static void post_once(void)
|
|
|
+static void send_once(mqtt_handle_t *h)
|
|
|
{
|
|
|
- post_stat(MQTT_PUB_INFO_DEVICE, 0);
|
|
|
- post_stat(MQTT_PUB_STAT_NETWORK, 0);
|
|
|
- post_stat(MQTT_PUB_STAT_SENSOR, 0);
|
|
|
- post_stat(MQTT_PUB_STAT_SERVICE, 0);
|
|
|
+ send_stat(h, MQTT_PUB_INFO_DEVICE);
|
|
|
+ send_stat(h, MQTT_PUB_STAT_NETWORK);
|
|
|
+ send_stat(h, MQTT_PUB_STAT_SENSOR);
|
|
|
+ send_stat(h, MQTT_PUB_STAT_SERVICE);
|
|
|
}
|
|
|
-static void post_period(void)
|
|
|
+static void send_period(mqtt_handle_t *h)
|
|
|
{
|
|
|
- post_stat(MQTT_PUB_STAT_POWER_ALL, 0);
|
|
|
- post_stat(MQTT_PUB_STAT_POWER_CHN, 0);
|
|
|
+ send_stat(h, MQTT_PUB_STAT_POWER_ALL);
|
|
|
+ send_stat(h, MQTT_PUB_STAT_POWER_CHN);
|
|
|
}
|
|
|
|
|
|
static void timer_conn_fn(void *arg)
|
|
|
@@ -372,19 +352,15 @@ static void timer_conn_fn(void *arg)
|
|
|
my_conn(h);
|
|
|
}
|
|
|
static void timer_period_fn(void *arg)
|
|
|
-{
|
|
|
- post_period();
|
|
|
-}
|
|
|
-static void timer_send_fn(void *arg)
|
|
|
{
|
|
|
mqtt_handle_t *h=(mqtt_handle_t*)arg;
|
|
|
- my_send(h);
|
|
|
+ send_period(h);
|
|
|
}
|
|
|
+
|
|
|
static void timer_start(mqtt_handle_t *h)
|
|
|
{
|
|
|
- mg_timer_add(&h->mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_conn_fn, h);
|
|
|
- mg_timer_add(&h->mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_period_fn, h);
|
|
|
- mg_timer_add(&h->mgr, 2000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_send_fn, h);
|
|
|
+ mg_timer_add(&h->mgr, CONN_PERIOD, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_conn_fn, h);
|
|
|
+ mg_timer_add(&h->mgr, SEND_PERIOD, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_period_fn, h);
|
|
|
}
|
|
|
|
|
|
static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
@@ -401,10 +377,9 @@ static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
|
|
|
case MG_EV_CONNECT:
|
|
|
{
|
|
|
- //char path[200];
|
|
|
- //sys_get_path(path, "emqxsl-ca.crt");
|
|
|
- if (mg_url_is_ssl(mc->ser.server)) {
|
|
|
-
|
|
|
+ char url[1024];
|
|
|
+ get_url(&mc->ser, url);
|
|
|
+ if (mg_url_is_ssl(url)) {
|
|
|
struct mg_tls_opts opts = {.ca = mg_str(mc->ser.cert),
|
|
|
.name = mg_url_host(mc->ser.server)};
|
|
|
mg_tls_init(c, &opts);
|
|
|
@@ -414,10 +389,16 @@ static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
|
|
|
|
|
|
case MG_EV_MQTT_OPEN:
|
|
|
{
|
|
|
- post_once();
|
|
|
+ send_once(h);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
+ case MG_EV_POLL:
|
|
|
+ {
|
|
|
+ my_send(h);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
case MG_EV_MQTT_MSG:
|
|
|
{
|
|
|
struct mg_mqtt_message *mm=(struct mg_mqtt_message*)ev_data;
|
|
|
@@ -516,43 +497,44 @@ int mqtt_deinit(void)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int post_stat(int type, int id)
|
|
|
+static int send_stat(mqtt_handle_t *h, int type)
|
|
|
{
|
|
|
- int r=-1;
|
|
|
+ int i,r=-1;
|
|
|
|
|
|
#ifdef USE_MQTT
|
|
|
+ float p1,p2,p3;
|
|
|
char topic[256];
|
|
|
char* content=NULL;
|
|
|
- mqtt_handle_t *h=&mqHandle;
|
|
|
- char date[40],time[40];
|
|
|
+ char buf[20],date[40],time[40];
|
|
|
|
|
|
if(!h->inited || type<0 || type>=MQTT_PUB_MAX) {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- if(type==MQTT_PUB_STAT_POWER_CHN || type==MQTT_PUB_STAT_SENSOR) {
|
|
|
- snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id, id);
|
|
|
- }
|
|
|
- else {
|
|
|
+ if(type!=MQTT_PUB_STAT_POWER_CHN && type!=MQTT_PUB_STAT_SENSOR) {
|
|
|
snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id);
|
|
|
}
|
|
|
- get_date_time(date, time);
|
|
|
|
|
|
switch(type) {
|
|
|
case MQTT_PUB_INFO_DEVICE:
|
|
|
{
|
|
|
cJSON* root=cJSON_CreateObject();
|
|
|
if(root) {
|
|
|
- cJSON_AddStringToObject(root,"id", get_int_str(id));
|
|
|
+ cJSON_AddStringToObject(root,"id", get_int_str(h->prod_id));
|
|
|
cJSON_AddStringToObject(root,"name", "Gowone smartPDU");
|
|
|
cJSON_AddStringToObject(root,"type", "smartPDU AC");
|
|
|
cJSON_AddStringToObject(root,"firmware", VERSION);
|
|
|
cJSON_AddStringToObject(root,"channel_num", "8");
|
|
|
cJSON_AddStringToObject(root,"sensor_num", "10");
|
|
|
+
|
|
|
+ get_date_time(date, time);
|
|
|
cJSON_AddStringToObject(root,"date", date);
|
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
content = cJSON_Print(root);
|
|
|
+ my_pub(h, type, topic, content);
|
|
|
+
|
|
|
+ cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
|
}
|
|
|
}
|
|
|
@@ -603,10 +585,15 @@ static int post_stat(int type, int id)
|
|
|
cJSON_AddStringToObject(root,"modbus_baud", get_int_str(h->dm->_globalDevInfo._gmodbus_info.product_modbus_baud));
|
|
|
cJSON_AddStringToObject(root,"modbus_mode", get_int_str(h->dm->_globalDevInfo._gmodbus_info.product_modbus_type));
|
|
|
cJSON_AddStringToObject(root,"vpn_enable", "0");
|
|
|
+
|
|
|
+ get_date_time(date, time);
|
|
|
cJSON_AddStringToObject(root,"date", date);
|
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
content = cJSON_Print(root);
|
|
|
+ my_pub(h, type, topic, content);
|
|
|
+
|
|
|
+ cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
|
}
|
|
|
}
|
|
|
@@ -616,18 +603,38 @@ static int post_stat(int type, int id)
|
|
|
{
|
|
|
cJSON* root=cJSON_CreateObject();
|
|
|
if(root) {
|
|
|
- //h->dm->_globalPowerManger
|
|
|
- cJSON_AddStringToObject(root,"voltage", "");
|
|
|
- cJSON_AddStringToObject(root,"current", "");
|
|
|
- cJSON_AddStringToObject(root,"power", "");
|
|
|
- cJSON_AddStringToObject(root,"consumption", "");
|
|
|
- cJSON_AddStringToObject(root,"pactive_power", "");
|
|
|
- cJSON_AddStringToObject(root,"reactive_power", "");
|
|
|
- cJSON_AddStringToObject(root,"apparent_power", "");
|
|
|
- cJSON_AddStringToObject(root,"date", date);
|
|
|
- cJSON_AddStringToObject(root,"time", time);
|
|
|
+ int cnt=1;
|
|
|
+ pwrall_info_t *pall=websocket_get_pwrall();
|
|
|
|
|
|
- content = cJSON_Print(root);
|
|
|
+ if(pall->ph3) {
|
|
|
+ cnt = 3;
|
|
|
+ }
|
|
|
+
|
|
|
+ for(i=0; i<cnt; i++) {
|
|
|
+ _OverAllPwrAckInfo *info=&pall->ch[i];
|
|
|
+
|
|
|
+ sprintf(buf, "L%d", i+1);
|
|
|
+ cJSON_AddStringToObject(root,"phase", buf);
|
|
|
+ cJSON_AddStringToObject(root,"voltage", get_float_str(info->voltage));
|
|
|
+ cJSON_AddStringToObject(root,"current", get_float_str(info->current));
|
|
|
+ cJSON_AddStringToObject(root,"power", get_float_str(info->power/1000));
|
|
|
+ cJSON_AddStringToObject(root,"consumption", get_float_str(info->consumption));
|
|
|
+
|
|
|
+ p1 = info->power;
|
|
|
+ p3 = (info->factor?(p1/info->factor):0.0f);
|
|
|
+ p2 = p3 - p1;
|
|
|
+ cJSON_AddStringToObject(root,"pactive_power", get_float_str(p1));
|
|
|
+ cJSON_AddStringToObject(root,"reactive_power", get_float_str(p2));
|
|
|
+ cJSON_AddStringToObject(root,"apparent_power", get_float_str(p3));
|
|
|
+
|
|
|
+ get_date_time(date, time);
|
|
|
+ cJSON_AddStringToObject(root,"date", date);
|
|
|
+ cJSON_AddStringToObject(root,"time", time);
|
|
|
+
|
|
|
+ content = cJSON_Print(root);
|
|
|
+ my_pub(h, type, topic, content);
|
|
|
+ cJSON_free(content);
|
|
|
+ }
|
|
|
cJSON_Delete(root);
|
|
|
}
|
|
|
}
|
|
|
@@ -635,11 +642,20 @@ static int post_stat(int type, int id)
|
|
|
|
|
|
case MQTT_PUB_STAT_POWER_CHN:
|
|
|
{
|
|
|
- cJSON* root=cJSON_CreateObject();
|
|
|
- if(root) {
|
|
|
- GlobalPowerManger *tmp=get_power(h, id);
|
|
|
- if(tmp) {
|
|
|
- cJSON_AddStringToObject(root,"name", "app server channel");
|
|
|
+ int quit=0;
|
|
|
+ GlobalPowerManger *tmp=NULL;
|
|
|
+
|
|
|
+ lock_s_hold(LOCK_ID_POWER_UPDATE);
|
|
|
+ list_for_each_entry(tmp, &h->dm->_globalPowerManger.list, list)
|
|
|
+ {
|
|
|
+ if(tmp==NULL) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ cJSON* root=cJSON_CreateObject();
|
|
|
+ if(root) {
|
|
|
+ sprintf(buf, "CH%d", tmp->product_ch_id);
|
|
|
+ cJSON_AddStringToObject(root,"name", buf);
|
|
|
cJSON_AddStringToObject(root,"status", get_int_str(tmp->_PowerInfo.status));
|
|
|
cJSON_AddStringToObject(root,"voltage", get_float_str(tmp->_PowerInfo.voltage));
|
|
|
cJSON_AddStringToObject(root,"current", get_float_str(tmp->_PowerInfo.current));
|
|
|
@@ -647,25 +663,44 @@ static int post_stat(int type, int id)
|
|
|
cJSON_AddStringToObject(root,"power_freq", get_float_str(tmp->_PowerInfo.freq));
|
|
|
cJSON_AddStringToObject(root,"consumption", get_float_str(tmp->_PowerInfo.consumption));
|
|
|
cJSON_AddStringToObject(root,"power_factor", get_float_str(tmp->_PowerInfo.factor));
|
|
|
- cJSON_AddStringToObject(root,"pactive_power", "");
|
|
|
- cJSON_AddStringToObject(root,"reactive_power", "");
|
|
|
- cJSON_AddStringToObject(root,"apparent_power", "");
|
|
|
- }
|
|
|
- cJSON_AddStringToObject(root,"date", date);
|
|
|
- cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
- content = cJSON_Print(root);
|
|
|
- cJSON_Delete(root);
|
|
|
+
|
|
|
+ p1 = tmp->_PowerInfo.power;
|
|
|
+ p3 = p1 / tmp->_PowerInfo.factor;
|
|
|
+ p2 = p3 - p1;
|
|
|
+ cJSON_AddStringToObject(root,"pactive_power", get_float_str(p1));
|
|
|
+ cJSON_AddStringToObject(root,"reactive_power", get_float_str(p2));
|
|
|
+ cJSON_AddStringToObject(root,"apparent_power", get_float_str(p3));
|
|
|
+
|
|
|
+ get_date_time(date, time);
|
|
|
+ cJSON_AddStringToObject(root,"date", date);
|
|
|
+ cJSON_AddStringToObject(root,"time", time);
|
|
|
+
|
|
|
+ snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id, tmp->product_ch_id);
|
|
|
+ content = cJSON_Print(root);
|
|
|
+ my_pub(h, type, topic, content);
|
|
|
+
|
|
|
+ cJSON_free(content);
|
|
|
+ cJSON_Delete(root);
|
|
|
+ }
|
|
|
}
|
|
|
+ lock_s_release(LOCK_ID_POWER_UPDATE);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case MQTT_PUB_STAT_SENSOR:
|
|
|
{
|
|
|
- cJSON* root=cJSON_CreateObject();
|
|
|
- if(root) {
|
|
|
- GlobalSensorManger *tmp=get_sensor(h, id);
|
|
|
- if(tmp) {
|
|
|
+ GlobalSensorManger *tmp=NULL;
|
|
|
+
|
|
|
+ lock_s_hold(LOCK_ID_SENSOR);
|
|
|
+ list_for_each_entry(tmp, &h->dm->_globalSensorManger.list, list)
|
|
|
+ {
|
|
|
+ if(tmp==NULL) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ cJSON* root=cJSON_CreateObject();
|
|
|
+ if(root) {
|
|
|
cJSON_AddStringToObject(root,"name", "temp sensor");
|
|
|
cJSON_AddStringToObject(root,"type", get_int_str(tmp->sensor_type));
|
|
|
cJSON_AddStringToObject(root,"modbus_address", get_int_str(tmp->sensor_addr));
|
|
|
@@ -674,13 +709,20 @@ static int post_stat(int type, int id)
|
|
|
cJSON_AddStringToObject(root,"value_num", get_int_str(tmp->sensor_val_count));
|
|
|
cJSON_AddStringToObject(root,"value1", get_float_str(tmp->Cur_sensor_info.val1));
|
|
|
cJSON_AddStringToObject(root,"value2", get_float_str(tmp->Cur_sensor_info.val2));
|
|
|
- }
|
|
|
- cJSON_AddStringToObject(root,"date", date);
|
|
|
- cJSON_AddStringToObject(root,"time", time);
|
|
|
+
|
|
|
+ get_date_time(date, time);
|
|
|
+ cJSON_AddStringToObject(root,"date", date);
|
|
|
+ cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
- content = cJSON_Print(root);
|
|
|
- cJSON_Delete(root);
|
|
|
+ snprintf(topic, sizeof(topic), topic_pub[type], h->prod_id, tmp->sensor_id);
|
|
|
+ content = cJSON_Print(root);
|
|
|
+ my_pub(h, type, topic, content);
|
|
|
+
|
|
|
+ cJSON_free(content);
|
|
|
+ cJSON_Delete(root);
|
|
|
+ }
|
|
|
}
|
|
|
+ lock_s_release(LOCK_ID_SENSOR);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
@@ -700,10 +742,15 @@ static int post_stat(int type, int id)
|
|
|
cJSON_AddStringToObject(root,"mqtt", BGET(flag, SERV_MQTT)?"1":"0");
|
|
|
cJSON_AddStringToObject(root,"cloud", BGET(flag, SERV_CLOUD)?"1":"0");
|
|
|
cJSON_AddStringToObject(root,"ntp", BGET(flag, SERV_NTP)?"1":"0");
|
|
|
+
|
|
|
+ get_date_time(date, time);
|
|
|
cJSON_AddStringToObject(root,"date", date);
|
|
|
cJSON_AddStringToObject(root,"time", time);
|
|
|
|
|
|
content = cJSON_Print(root);
|
|
|
+ my_pub(h, type, topic, content);
|
|
|
+
|
|
|
+ cJSON_free(content);
|
|
|
cJSON_Delete(root);
|
|
|
}
|
|
|
}
|
|
|
@@ -713,12 +760,8 @@ static int post_stat(int type, int id)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- mqtt_pkt_t pkt;
|
|
|
- pkt.id = type;
|
|
|
- snprintf(pkt.topic, sizeof(pkt.topic), "%s", topic);
|
|
|
- snprintf(pkt.content, sizeof(pkt.content), "%s", content);
|
|
|
- r = xlist_append(h->list, 0, &pkt, sizeof(pkt));
|
|
|
- cJSON_free(content);
|
|
|
+
|
|
|
+
|
|
|
#endif
|
|
|
|
|
|
return r;
|
|
|
@@ -888,7 +931,7 @@ static int set_serv(uint32_t flag)
|
|
|
{
|
|
|
if(BGET(flag,SERV_NTP)) {
|
|
|
if(!thread_is_running(THREAD_ID_NTP)) {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
@@ -1057,9 +1100,8 @@ int mqtt_test(void)
|
|
|
.id = 0,
|
|
|
.mode = 1,
|
|
|
.cid = "mqtlx_e92334545tet3465",
|
|
|
-
|
|
|
+ .name = "serverX",
|
|
|
.server = "192.168.1.12",
|
|
|
- .ip = "",
|
|
|
.port = "1883",
|
|
|
|
|
|
//root
|
|
|
@@ -1076,7 +1118,7 @@ int mqtt_test(void)
|
|
|
|
|
|
my_conn(h);
|
|
|
while(1) {
|
|
|
- post_period();
|
|
|
+ send_period(h);
|
|
|
sleep(3);
|
|
|
}
|
|
|
|