|
|
|
@ -43,6 +43,16 @@
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
|
|
/* 私有头文件 */
|
|
|
|
|
#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 ****/
|
|
|
|
|
|
|
|
|
|