From d5e587258b1545891cfb65eeab8c69a12d631307 Mon Sep 17 00:00:00 2001 From: wangbo Date: Tue, 11 Mar 2025 05:25:22 +0000 Subject: [PATCH] =?UTF-8?q?1.=E5=8F=96=E6=B6=88=E6=B5=8B=E7=82=B9=E6=80=BB?= =?UTF-8?q?=E6=8E=A5=E5=9C=B0=E7=94=B5=E6=B5=81=E5=92=8C=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=94=B5=E6=B5=81;2.=E7=94=B5=E6=B5=81=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=B0=8F=E6=95=B0=E7=82=B9=E5=90=8E=E4=B8=A4=E4=BD=8D;3.?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8C=E6=AD=A54G=E6=97=B6=E9=97=B4;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/include/ca_mqtt.h | 7 +- app/lib/a_process/ca_collect.c | 2 +- app/lib/a_process/ca_mqtt.c | 222 +++++++++++++++++++++++++++++++-- build/Makefile | 2 +- 4 files changed, 218 insertions(+), 15 deletions(-) diff --git a/app/include/ca_mqtt.h b/app/include/ca_mqtt.h index f2e68f8..7a5cb52 100755 --- a/app/include/ca_mqtt.h +++ b/app/include/ca_mqtt.h @@ -50,11 +50,7 @@ #define DEVICE_MODEL "SD_JK_LD_FBDMX"// //#define DEVICE_MODEL "DL_WHLD_HL" #define MANUFACTURERID "WHLD" -#define DEVICE_CODE "03MW00000000001" -#define DEVICE_POINT_A "03MCD00000000001" -#define DEVICE_POINT_B "03MCD00000000002" -#define DEVICE_POINT_C "03MCD00000000003" - +#define AT_COMMOND_TTY "/dev/ttyUSB2" @@ -105,6 +101,7 @@ extern void _ca_mqtt_json_ground_current(ca_coll_dev_data_t *data, char *outmsg, extern int _ca_mqtt_data_report_child_device(ca_coll_dev_data_t *data, char *device_id, int idx); extern int _ca_mqtt_update_device_status(char *device_id); extern int _ca_mqtt_add_sub_device(node_info_t *node_info); +extern int ca_read_4g_time(); #endif #endif /************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/ diff --git a/app/lib/a_process/ca_collect.c b/app/lib/a_process/ca_collect.c index c2deb60..ff63632 100755 --- a/app/lib/a_process/ca_collect.c +++ b/app/lib/a_process/ca_collect.c @@ -1010,7 +1010,7 @@ void *_ca_coll_handle_send(void *arg) /* 判断采集间隔 */ now = time(NULL); - if (now - utc >= ca_coll_ctrl.cfg.coll_inr) + if (abs(now - utc) >= ca_coll_ctrl.cfg.coll_inr) { utc = now; diff --git a/app/lib/a_process/ca_mqtt.c b/app/lib/a_process/ca_mqtt.c index fc7667f..eb582fb 100755 --- a/app/lib/a_process/ca_mqtt.c +++ b/app/lib/a_process/ca_mqtt.c @@ -43,6 +43,16 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* 私有头文件 */ #include "vty.h" @@ -538,7 +548,7 @@ void _ca_mqtt_json_ground_current(ca_coll_dev_data_t *data, char *outmsg, int id if (idx == 0) { bzero(str, sizeof(str)); - sprintf(str, "%f", data->elec[0] / 1000.0); + sprintf(str, "%.2f", data->elec[0] / 1000.0); //sprintf(str, "%f", 1111 / 1000.0); cJSON_AddStringToObject(pdataJson, "pointPhase", "A"); cJSON_AddStringToObject(pdataJson, "ShieldEarthCurrentPhase", str); @@ -546,7 +556,7 @@ void _ca_mqtt_json_ground_current(ca_coll_dev_data_t *data, char *outmsg, int id else if (idx == 1) { bzero(str, sizeof(str)); - sprintf(str, "%f", data->elec[1] / 1000.0); + sprintf(str, "%.2f", data->elec[1] / 1000.0); //sprintf(str, "%f", 1122 / 1000.0); cJSON_AddStringToObject(pdataJson, "pointPhase", "B"); cJSON_AddStringToObject(pdataJson, "ShieldEarthCurrentPhase", str); @@ -554,7 +564,7 @@ void _ca_mqtt_json_ground_current(ca_coll_dev_data_t *data, char *outmsg, int id else if (idx == 2) { bzero(str, sizeof(str)); - sprintf(str, "%f", data->elec[2] / 1000.0); + sprintf(str, "%.2f", data->elec[2] / 1000.0); //sprintf(str, "%f", 1133 / 1000.0); cJSON_AddStringToObject(pdataJson, "pointPhase", "C"); cJSON_AddStringToObject(pdataJson, "ShieldEarthCurrentPhase", str); @@ -562,7 +572,7 @@ void _ca_mqtt_json_ground_current(ca_coll_dev_data_t *data, char *outmsg, int id else if (idx == 3) { bzero(str, sizeof(str)); - sprintf(str, "%f", data->elec[3] / 1000.0); + sprintf(str, "%.2f", data->elec[3] / 1000.0); //sprintf(str, "%f", 1523 / 1000.0); cJSON_AddStringToObject(pdataJson, "pointPhase", "R"); cJSON_AddStringToObject(pdataJson, "ShieldEarthCurrentPhase", str); @@ -572,7 +582,7 @@ void _ca_mqtt_json_ground_current(ca_coll_dev_data_t *data, char *outmsg, int id else if (idx == 4) { bzero(str, sizeof(str)); - sprintf(str, "%f", data->elec[4] / 1000.0); + sprintf(str, "%.2f", data->elec[4] / 1000.0); //sprintf(str, "%f", 1423 / 1000.0); cJSON_AddStringToObject(pdataJson, "pointPhase", "T"); cJSON_AddStringToObject(pdataJson, "ShieldEarthCurrentPhase", str); @@ -692,7 +702,6 @@ int _ca_mqtt_data_report_child_device(ca_coll_dev_data_t *data, char *device_id, printf("Failed to start sendMessage, return code %d\n", rc); exit(EXIT_FAILURE); } - printf("send child device data.....\r\n"); return 0; } @@ -729,6 +738,7 @@ int _ca_mqtt_connect(char *brokeAddr, char *clientId, char *username, char *pass #endif DBG(DBG_M_CA_MQTT, " Destroy Mqtt, Relogin\r\n"); MQTTAsync_destroy(&ca_mqtt_ctrl.client); + ca_mqtt_ctrl.client = NULL; } if ((rc = MQTTAsync_create(&ca_mqtt_ctrl.client, brokeAddr, clientId, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTASYNC_SUCCESS) @@ -800,8 +810,8 @@ void *_ca_mqtt_handle(void *param) _ca_mqtt_data_report_child_device(recv_msg->data, ca_mqtt_ctrl.info.dev_id, 0); _ca_mqtt_data_report_child_device(recv_msg->data, ca_mqtt_ctrl.info.dev_id, 1); _ca_mqtt_data_report_child_device(recv_msg->data, ca_mqtt_ctrl.info.dev_id, 2); - _ca_mqtt_data_report_child_device(recv_msg->data, ca_mqtt_ctrl.info.dev_id, 3); - _ca_mqtt_data_report_child_device(recv_msg->data, ca_mqtt_ctrl.info.dev_id, 4); + //_ca_mqtt_data_report_child_device(recv_msg->data, ca_mqtt_ctrl.info.dev_id, 3); + //_ca_mqtt_data_report_child_device(recv_msg->data, ca_mqtt_ctrl.info.dev_id, 4); break; default: break; @@ -811,6 +821,8 @@ void *_ca_mqtt_handle(void *param) /* 释放数据 */ XFREE(MTYPE_CA_MQTT, recv_msg->data); fifo_push(ca_mqtt_ctrl.fifo_id); + + ca_read_4g_time(); } return NULL; @@ -975,6 +987,200 @@ void ca_mqtt_state_show(void) printh("Passwd %s\r\n", ca_mqtt_ctrl.info.passwd); printh("Connect: %s\r\n\n", state->is_connect ? "yes" : "no"); } + +// +CCLK: "25/03/10,05:37:04+00" +int _ca_read_cclk_time(char *string, time_t *ptime) +{ + struct tm tm = {0}; + char *p1 = NULL; + char *p2 = NULL; + if (string == NULL) + return -1; + if ((p1 = strstr(string, "+CCLK:")) == NULL) + return -1; + + p1 += strlen("+CCLK:"); + p1 += 2; + if ((p2 = strchr(p1, '/')) == NULL) + return -1; + *p2 = '\0'; + tm.tm_year = atoi(p1) + 100; + + p1 += 3; + if ((p2 = strchr(p1, '/')) == NULL) + return -1; + *p2 = '\0'; + tm.tm_mon = atoi(p1) -1; + + p1 += 3; + if ((p2 = strchr(p1, ',')) == NULL) + return -1; + *p2 = '\0'; + tm.tm_mday = atoi(p1); + + p1 += 3; + if ((p2 = strchr(p1, ':')) == NULL) + return -1; + *p2 = '\0'; + tm.tm_hour = (atoi(p1) + 8) % 24; + + p1 += 3; + if ((p2 = strchr(p1, ':')) == NULL) + return -1; + *p2 = '\0'; + tm.tm_min = atoi(p1); + + + p1 += 3; + if ((p2 = strchr(p1, '+')) == NULL) + return -1; + *p2 = '\0'; + tm.tm_sec = atoi(p1); + + *ptime = mktime(&tm); + + DBG(DBG_M_CA_MQTT_ERR, "%d-%d-%d %d:%d:%d\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); + + return 0; + +} + +int ca_chat(int fd, const char *at, const char *expect, int timeout, char **response) +{ + int ret; + int read_len = 0; + static char buf[512]; + + if (response) + { + *response = NULL; + } + + tcflush(fd, TCIOFLUSH); + + + //DBG(DBG_M_CA_MQTT_ERR, "chat --> %s\r\n", at); + do + { + ret = write(fd, at, strlen(at)); + } + while (ret < 0 && errno == EINTR); + + if (ret <= 0) + { + DBG(DBG_M_CA_MQTT_ERR, "chat write error on stdout: %s(%d) \r\n", strerror(errno), errno); + return errno ? errno : EINVAL; + } + + while (timeout > 0) + { + struct pollfd poll_fd = {fd, POLLIN, 0}; + if (poll(&poll_fd, 1, 200) <= 0) + { + if (errno == ETIMEDOUT) + { + if (timeout >= 200) + { + timeout -= 200; + } + else + { + timeout = 0; + } + continue; + } + else if (errno != EINTR) + { + DBG(DBG_M_CA_MQTT_ERR, "chat poll error on stdin: %s(%d) \r\n", strerror(errno), errno); + return errno ? errno : EINVAL; + } + } + if (poll_fd.revents && (poll_fd.revents & POLLIN)) + { + memset(buf, 0, sizeof(buf)); + usleep(100 * 1000); + if ((read_len = read(fd, buf, sizeof(buf) - 1)) <= 0) + { + DBG(DBG_M_CA_MQTT_ERR, "chat read error on stdin: %s(%d) \r\n", strerror(errno), errno); + return errno ? errno : EINVAL; + } + + DBG(DBG_M_CA_MQTT_ERR, "chat return string.len = %d <----- %s;expect:%s\r\n", strlen(buf), buf, expect); + + if (read_len >= 512) + { + printf("chat read max len [read:%d--max:%d]\n", read_len, 512); + return -1; + } + + if (strstr(buf, expect)) + { + + if (response) + { + *response = strstr(buf, expect); + } + return 0; + } + else + { + if (response) + { + *response = buf; + } + } + } + } + + return errno ? errno : EINVAL; +} + +void ca_modify_system_time(char *response) +{ + time_t time4g = 0; + if (_ca_read_cclk_time(response, &time4g) == 0) + { + if (abs(time4g - time(NULL)) > 10) + { + time_set(time4g); + } + } +} + +int ca_read_4g_time() +{ + int modem_fd, fdflags; + struct termios ios; + char *response; + int modembits = TIOCM_DTR; + modem_fd = open(AT_COMMOND_TTY, O_RDWR | O_NONBLOCK); + if (modem_fd == -1 ) + { + DBG(DBG_M_CA_MQTT_ERR, " open %s failed\r\n", AT_COMMOND_TTY); + return -1; + } + + fdflags = fcntl(modem_fd, F_GETFL); + if (fdflags != -1) + { + fcntl(modem_fd, F_SETFL, fdflags | O_NONBLOCK); + } + /* disable echo on serial ports */ + tcgetattr( modem_fd, &ios ); + cfmakeraw(&ios); + ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */ + cfsetispeed(&ios, B115200); + cfsetospeed(&ios, B115200); + tcsetattr( modem_fd, TCSANOW, &ios ); + ioctl(modem_fd, (0 ? TIOCMBIS : TIOCMBIC), &modembits); //clear DTR + if (ca_chat(modem_fd, "AT+CCLK?\r\n", "+CCLK:", 1000, &response) == 0) + { + ca_modify_system_time(response); + } + close(modem_fd); + return 0; +} + #endif /************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/ diff --git a/build/Makefile b/build/Makefile index 51142f9..923d47e 100755 --- a/build/Makefile +++ b/build/Makefile @@ -31,7 +31,7 @@ CONFIG_FILE := $(SOURCE_DIR)/include/config.h VERSION_FILE := $(SOURCE_DIR)/include/version.h VERSION_LIB := version.a DATE_STRING := `date "+%Y.%m.%d %k:%M:%S"` -VERSION_STRING := "3.2.1.6" +VERSION_STRING := "3.2.1.7" MV := mv -f RM := rm -rf