|
|
@@ -4,8 +4,9 @@
|
|
|
#include "switch_ctrl.h"
|
|
|
#include "cascade.h"
|
|
|
#include "sqlite_handle.h"
|
|
|
-
|
|
|
-
|
|
|
+#include "sys.h"
|
|
|
+#include "thread.h"
|
|
|
+#include "sensor_handle.h"
|
|
|
|
|
|
#define DEV_CHN_REG (4000)
|
|
|
#define OUT_INFO_REG (4001)
|
|
|
@@ -76,17 +77,18 @@ enum {
|
|
|
|
|
|
|
|
|
#define DEV_CHN_REG (4000)
|
|
|
-#define SWITCH_STATUE_REG (4001)
|
|
|
-#define POWER_INFO_REG (4002)
|
|
|
-#define TEPM_HUM_REG (4016)
|
|
|
-#define SENCOR_STATUS (4018)
|
|
|
+#define DEV_IP_MAC (4001)
|
|
|
+#define POWER_INFO_REG (4007)
|
|
|
+#define OVER_POWER_ALL (4101)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
enum{
|
|
|
- IP_DEV_CHN,
|
|
|
- IP_SWITCH_STATUE,
|
|
|
- IP_POWER_INFO,
|
|
|
- IP_TEMP_HUM,
|
|
|
- IP_SENCOR_STATUS,
|
|
|
+ DS_IP_DEV_CHN,
|
|
|
+ DS_IP_MAC,
|
|
|
+ DS_POWER_INFO_REG,
|
|
|
};
|
|
|
|
|
|
|
|
|
@@ -131,6 +133,13 @@ static mmap_modbus_t modbus_three[] = {
|
|
|
[TREE_W_ALL_CHN_STA ]{.offset = TREE_ALL_SET_SWITCH_REG }
|
|
|
};
|
|
|
|
|
|
+static mmap_modbus_t ip[] ={
|
|
|
+ [DS_IP_DEV_CHN ]{.offset = DEV_CHN_REG },
|
|
|
+ [DS_IP_MAC ]{.offset = DEV_IP_MAC },
|
|
|
+ [DS_POWER_INFO_REG ]{.offset = POWER_INFO_REG },
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
typedef struct {
|
|
|
modbus_mapping_t *map;
|
|
|
}slave_handle_t;
|
|
|
@@ -660,4 +669,546 @@ void cascade_slave_write(uint32_t addr, uint16_t val)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#endif
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int cascade_slave_init(void)
|
|
|
+{
|
|
|
+ slave_handle_t *sh=&slHandle;
|
|
|
+ GlobalDeviceManager* mgr=get_dm();
|
|
|
+ if(sh->map)
|
|
|
+ {
|
|
|
+ free(sh->map);
|
|
|
+ sh->map = NULL;
|
|
|
+ }
|
|
|
+ sh->map = modbus_mapping_new_start_address(0,0,0,0,DEV_CHN_REG,1000,0,0); //3000 ge
|
|
|
+ ip[DS_IP_DEV_CHN ].addr = &mgr->mmp->dev_chn;
|
|
|
+ ip[DS_IP_MAC ].addr = &mgr->mmp->mac[0];
|
|
|
+ ip[DS_POWER_INFO_REG ].addr = &mgr->mmp->data[0];
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void cascade_slave_read(uint32_t addr,uint32_t lenth)
|
|
|
+{
|
|
|
+ slave_handle_t *sh=&slHandle;
|
|
|
+ switch(addr)
|
|
|
+ {
|
|
|
+ case DEV_CHN_REG:
|
|
|
+ {
|
|
|
+ uint16_t *base_addr = (uint16_t *)ip[DS_IP_DEV_CHN].addr;
|
|
|
+ uint32_t step = addr - sh->map->start_registers;
|
|
|
+ base_addr = base_addr + (addr - ip[DS_IP_DEV_CHN].offset);
|
|
|
+ uint16_t *map_addr = (uint16_t *)&(sh->map->tab_registers[step]);
|
|
|
+ if(lenth < 126)
|
|
|
+ {
|
|
|
+ memcpy(map_addr, base_addr,2*lenth);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case DEV_IP_MAC:
|
|
|
+ {
|
|
|
+ uint16_t *base_addr = (uint16_t *)ip[DS_IP_MAC].addr;
|
|
|
+ uint32_t step = addr - sh->map->start_registers;
|
|
|
+ base_addr = base_addr + (addr - ip[DS_IP_MAC].offset);
|
|
|
+ uint16_t *map_addr = (uint16_t *)&(sh->map->tab_registers[step]);
|
|
|
+ if(lenth < 126)
|
|
|
+ {
|
|
|
+ memcpy(map_addr, base_addr,2*lenth);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case POWER_INFO_REG ... OVER_POWER_ALL:
|
|
|
+ {
|
|
|
+ uint16_t *base_addr = (uint16_t *)ip[DS_POWER_INFO_REG].addr;
|
|
|
+ uint32_t step = addr - sh->map->start_registers;
|
|
|
+ base_addr = base_addr + (addr - ip[DS_POWER_INFO_REG].offset);
|
|
|
+ uint16_t *map_addr = (uint16_t *)&(sh->map->tab_registers[step]);
|
|
|
+ if(lenth < 126)
|
|
|
+ {
|
|
|
+ memcpy(map_addr, base_addr,2*lenth);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static void memswap(uint8_t *buf, int len)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ uint8_t tmp;
|
|
|
+
|
|
|
+ for(i=0; i<len; i+=2) {
|
|
|
+ tmp = buf[i];
|
|
|
+ buf[i] = buf[i+1];
|
|
|
+ buf[i+1] = tmp;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static int _mb_hdr(uint8_t *data, int datalen, mb_hdr_t *h)
|
|
|
+{
|
|
|
+ h->addr = data[0];
|
|
|
+ h->func = data[1];
|
|
|
+ h->reg = data[2]<<8 | data[3];
|
|
|
+ h->regcnt = data[4]<<8 | data[5];
|
|
|
+
|
|
|
+ h->dlen = 0;
|
|
|
+ h->data = NULL;
|
|
|
+ if(datalen>8) {
|
|
|
+ h->dlen = data[6];
|
|
|
+ h->data = data+7;
|
|
|
+
|
|
|
+ memswap(h->data, h->dlen+h->dlen%2);
|
|
|
+ }
|
|
|
+ //print_hdr("sss", h);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+int cascade_set_ip_modbus(ModbusInfo_t *info)
|
|
|
+{
|
|
|
+ GlobalDeviceManager *_globalDeviceManager = (GlobalDeviceManager *)&__globalDeviceManage;
|
|
|
+ g_modbus_deinit(&_globalDeviceManager->sensor_modbus_manager);
|
|
|
+ if(info->product_modbus_type > 0)
|
|
|
+ {
|
|
|
+ int ret = g_modbus_init(&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ INTERL_SENSOR_MODBUS_PORT, // _globalSensorMangerTemp->sensor_port_name,
|
|
|
+ info->product_modbus_baud, // _globalSensorMangerTemp->sensor_baud,
|
|
|
+ info->product_modbus_type,
|
|
|
+ info->product_modbus_addr,
|
|
|
+ "ip to user",
|
|
|
+ 1);
|
|
|
+ g_modbus_set_slave(&_globalDeviceManager->sensor_modbus_manager,info->product_modbus_addr);
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ int ret = g_modbus_init(&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ INTERL_SENSOR_MODBUS_PORT, // _globalSensorMangerTemp->sensor_port_name,
|
|
|
+ info->product_modbus_baud, // _globalSensorMangerTemp->sensor_baud,
|
|
|
+ info->product_modbus_type,
|
|
|
+ info->product_modbus_addr,
|
|
|
+ "sensor",
|
|
|
+ 1);
|
|
|
+
|
|
|
+ }
|
|
|
+ _globalDeviceManager->_globalDevInfo._gmodbus_info.product_modbus_type = info->product_modbus_type;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void* ip_cascade_thread(void *arg)
|
|
|
+{
|
|
|
+ GlobalDeviceManager *_globalDeviceManager = (GlobalDeviceManager *)&__globalDeviceManage;
|
|
|
+ GlobalDeviceInfo *devInfo = &_globalDeviceManager->_globalDevInfo;
|
|
|
+ uint8_t query[512] = {0};
|
|
|
+
|
|
|
+ int ret = g_modbus_init(&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ INTERL_SENSOR_MODBUS_PORT, // _globalSensorMangerTemp->sensor_port_name,
|
|
|
+ devInfo->_gmodbus_info.product_modbus_baud, // _globalSensorMangerTemp->sensor_baud,
|
|
|
+ devInfo->_gmodbus_info.product_modbus_type,
|
|
|
+ devInfo->_gmodbus_info.product_modbus_addr,
|
|
|
+ "ip to user",
|
|
|
+ 1);
|
|
|
+ g_modbus_set_slave(&_globalDeviceManager->sensor_modbus_manager,devInfo->_gmodbus_info.product_modbus_addr);
|
|
|
+ cascade_slave_init();
|
|
|
+ int r = 0;
|
|
|
+ mb_hdr_t h;
|
|
|
+ while (_globalDeviceManager->dev_samp_flag)
|
|
|
+ {
|
|
|
+ r = g_modbus_receive(&_globalDeviceManager->sensor_modbus_manager,query);
|
|
|
+ if(r > 0)
|
|
|
+ {
|
|
|
+ _mb_hdr(query,r,&h);
|
|
|
+ if(h.func==MODBUS_FC_READ_HOLDING_REGISTERS) {
|
|
|
+ cascade_slave_read(h.reg, h.regcnt);
|
|
|
+ g_modbus_reply(&_globalDeviceManager->sensor_modbus_manager,query,r,cascade_slave_map());
|
|
|
+ }
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ log_d("modbus err!!!!\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ pthread_exit(NULL);
|
|
|
+}
|
|
|
+
|
|
|
+int tcp_modbus_receive(Modbus_Manger_Tcp *tcp, uint8_t *req)
|
|
|
+{
|
|
|
+ int ret = 0 ;
|
|
|
+ pthread_mutex_lock(&tcp->mutex);
|
|
|
+ ret = modbus_receive(tcp->ctx, req);
|
|
|
+ pthread_mutex_unlock(&tcp->mutex);
|
|
|
+ return ret ;
|
|
|
+}
|
|
|
+int tcp_modbus_reply(Modbus_Manger_Tcp *tcp, uint8_t *req, int reqlen, modbus_mapping_t *map)
|
|
|
+{
|
|
|
+ int ret = 0 ;
|
|
|
+ pthread_mutex_lock(&tcp->mutex);
|
|
|
+ ret = modbus_reply(tcp->ctx, req, reqlen, map);
|
|
|
+ pthread_mutex_unlock(&tcp->mutex);
|
|
|
+ return ret ;
|
|
|
+}
|
|
|
+int tcp_modbus_set_slave(Modbus_Manger_Tcp *tcp, int saddr)
|
|
|
+{
|
|
|
+ int ret = 0 ;
|
|
|
+ pthread_mutex_lock(&tcp->mutex);
|
|
|
+ ret = modbus_set_slave(tcp->ctx, saddr);
|
|
|
+ pthread_mutex_unlock(&tcp->mutex);
|
|
|
+ return ret ;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+Modbus_Manger_Tcp* tcp_modbus_init(const char* ip, uint16_t port,uint8_t addr,int power_type)
|
|
|
+{
|
|
|
+ Modbus_Manger_Tcp* tcp = NULL;
|
|
|
+ tcp = (Modbus_Manger_Tcp*)malloc(sizeof(Modbus_Manger_Tcp));
|
|
|
+ if(!tcp)
|
|
|
+ {
|
|
|
+ log_d("malloc modubus manger faield!!!\n");
|
|
|
+ goto err1;
|
|
|
+ }
|
|
|
+ memset(tcp, 0, sizeof(Modbus_Manger_Tcp));
|
|
|
+
|
|
|
+ tcp->ctx = modbus_new_tcp(ip, port);
|
|
|
+ if(!tcp->ctx)
|
|
|
+ {
|
|
|
+ log_d("malloc modbus_tcp ctx faield!!!\n");
|
|
|
+ goto err2;
|
|
|
+ }
|
|
|
+
|
|
|
+ struct timeval response_timeout;
|
|
|
+ response_timeout.tv_sec = 1;
|
|
|
+ response_timeout.tv_usec = 0;
|
|
|
+ modbus_set_response_timeout(tcp->ctx, 0,800000);
|
|
|
+ modbus_set_byte_timeout(tcp->ctx,0,50000);
|
|
|
+ modbus_set_error_recovery(tcp->ctx,MODBUS_ERROR_RECOVERY_PROTOCOL);
|
|
|
+ modbus_set_slave(tcp->ctx,addr);
|
|
|
+ pthread_mutex_init(&tcp->mutex,NULL);
|
|
|
+ strcpy(tcp->ip,ip);
|
|
|
+ tcp->port = port;
|
|
|
+ tcp->slave_id = addr;
|
|
|
+ tcp->power_type = power_type;
|
|
|
+ return tcp;
|
|
|
+err2:
|
|
|
+ free(tcp);
|
|
|
+err1:
|
|
|
+ return tcp;
|
|
|
+}
|
|
|
+
|
|
|
+void tcp_modbus_deinit(Modbus_Manger_Tcp **tcp)
|
|
|
+{
|
|
|
+ Modbus_Manger_Tcp** tmp = tcp;
|
|
|
+ if(tmp)
|
|
|
+ {
|
|
|
+ if(*tmp)
|
|
|
+ {
|
|
|
+ modbus_close((*tmp)->ctx);
|
|
|
+ modbus_free((*tmp)->ctx);
|
|
|
+
|
|
|
+ free(*tmp);
|
|
|
+ *tmp = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void* tcp_modbus_thread(void *arg)
|
|
|
+{
|
|
|
+ //GlobalDeviceManager *mgr = (GlobalDeviceManager *)(arg);
|
|
|
+
|
|
|
+ GlobalDeviceManager *mgr = get_dm();
|
|
|
+ Modbus_Manger_Tcp *tcp = mgr->md_tcp;
|
|
|
+ tcp->connection = modbus_tcp_listen(tcp->ctx,CASCADE_SLAVE_CONNECTION);
|
|
|
+ modbus_tcp_accept(tcp->ctx,&tcp->connection);
|
|
|
+ uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH] = {0};
|
|
|
+ mb_hdr_t h;
|
|
|
+ log_d("start tcp modbus thread!!!\n");
|
|
|
+ int rc = 0;
|
|
|
+ while(1)
|
|
|
+ {
|
|
|
+ if(tcp->ctx){
|
|
|
+ rc = tcp_modbus_receive(tcp,query);
|
|
|
+ if(rc > 0)
|
|
|
+ {
|
|
|
+ _mb_hdr(&query[6],rc,&h);
|
|
|
+ if(h.func==MODBUS_FC_READ_HOLDING_REGISTERS) {
|
|
|
+ cascade_slave_read(h.reg, h.regcnt);
|
|
|
+ tcp_modbus_reply(tcp,query,rc,cascade_slave_map());
|
|
|
+ }
|
|
|
+
|
|
|
+ //r = _mb_reply(cas, buff, rc,cascade_slave_map());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void sensor_or_cascade_init()
|
|
|
+{
|
|
|
+ //GlobalDeviceManager* dm =get_dm();
|
|
|
+ GlobalDeviceManager *_globalDeviceManager = (GlobalDeviceManager *)&__globalDeviceManage;
|
|
|
+ INIT_LIST_HEAD(&_globalDeviceManager->_globalSensorManger.list);
|
|
|
+ //INIT_LIST_HEAD(&dm->_globalSensorManger.list);
|
|
|
+ GlobalDeviceInfo *devInfo = &_globalDeviceManager->_globalDevInfo;
|
|
|
+ //uint8_t query[512] = {0};
|
|
|
+ int ret = 0;
|
|
|
+ ret = dev_get_sensor_manage_info(_globalDeviceManager->db,
|
|
|
+ _globalDeviceManager->_globalDevInfo.product_id,
|
|
|
+ &_globalDeviceManager->_globalSensorManger);
|
|
|
+
|
|
|
+ dev_Alarm_Run_message(_globalDeviceManager,language_alarm_Init_Success[0],"sensor");
|
|
|
+ cascade_slave_init();
|
|
|
+ if(devInfo->_gmodbus_info.product_modbus_type > 0)
|
|
|
+ {
|
|
|
+ int ret = g_modbus_init(&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ INTERL_SENSOR_MODBUS_PORT, // _globalSensorMangerTemp->sensor_port_name,
|
|
|
+ devInfo->_gmodbus_info.product_modbus_baud, // _globalSensorMangerTemp->sensor_baud,
|
|
|
+ devInfo->_gmodbus_info.product_modbus_type,
|
|
|
+ devInfo->_gmodbus_info.product_modbus_addr,
|
|
|
+ "ip to user",
|
|
|
+ 1);
|
|
|
+ g_modbus_set_slave(&_globalDeviceManager->sensor_modbus_manager,devInfo->_gmodbus_info.product_modbus_addr);
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ int ret = g_modbus_init(&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ INTERL_SENSOR_MODBUS_PORT, // _globalSensorMangerTemp->sensor_port_name,
|
|
|
+ INTERL_SENSOR_MODBUS_BAUD, // _globalSensorMangerTemp->sensor_baud,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ "ip to user",
|
|
|
+ 1);
|
|
|
+ }
|
|
|
+ pthread_mutex_init(&_globalDeviceManager->_sensor_update,NULL);
|
|
|
+
|
|
|
+
|
|
|
+ thread_start(THREAD_ID_SENSOR, thread_sensor_or_485, _globalDeviceManager, 5*MB, 0);
|
|
|
+}
|
|
|
+
|
|
|
+extern void sensor_alarm(GlobalSensorManger *_globalSensorMangerTemp, int nAlarmType);
|
|
|
+
|
|
|
+void * thread_sensor_or_485(void*arg)
|
|
|
+{
|
|
|
+ thread_handle_t *h=(thread_handle_t*)arg;
|
|
|
+ //GlobalDeviceManager* _globalDeviceManager = (GlobalDeviceManager*)h->arg;
|
|
|
+ GlobalDeviceManager *_globalDeviceManager = (GlobalDeviceManager *)&__globalDeviceManage;
|
|
|
+ GlobalSensorManger *_globalSensorMangerTemp;
|
|
|
+ int ret = 0;
|
|
|
+ GlobalSensorInfo _globalSensorInfo;
|
|
|
+ struct tm *t;
|
|
|
+ struct timeval tv;
|
|
|
+ struct timezone tz;
|
|
|
+ int r = 0;
|
|
|
+ mb_hdr_t mb_h;
|
|
|
+ GlobalDeviceInfo *devInfo = &_globalDeviceManager->_globalDevInfo;
|
|
|
+ uint8_t query[512] = {0};
|
|
|
+
|
|
|
+ float temperature = 0.0;
|
|
|
+ float humidity = 0.0;
|
|
|
+ int nSaveIndex = 1;
|
|
|
+ char strTable[] = "Table_SensorInfo";
|
|
|
+ char strTemp[64] = {0};
|
|
|
+ dev_search_last_Index(_globalDeviceManager->db, &nSaveIndex, strTable); // 查询日志最大点
|
|
|
+ int nMaxIndex = 2000000000;
|
|
|
+ int nDeleteNumber = 1000 / 120000 * 3600 * 24 * 30 * 10; // 1/60s;
|
|
|
+
|
|
|
+ while(_globalDeviceManager->dev_samp_flag)
|
|
|
+ {
|
|
|
+ if(devInfo->_gmodbus_info.product_modbus_type > 0)
|
|
|
+ {
|
|
|
+ //printf("12323451541545135435\n");
|
|
|
+ dev_search_last_Index(_globalDeviceManager->db, &nSaveIndex, strTable);
|
|
|
+ pthread_mutex_lock(&__globalDeviceManage._sensor_update);
|
|
|
+
|
|
|
+ pthread_mutex_unlock(&__globalDeviceManage._sensor_update);
|
|
|
+ list_for_each_entry(_globalSensorMangerTemp, &_globalDeviceManager->_globalSensorManger.list, list)
|
|
|
+ {
|
|
|
+ switch (_globalSensorMangerTemp->sensor_type)
|
|
|
+ {
|
|
|
+ case SENSOR_TYPE_TEMPERATURE:
|
|
|
+ {
|
|
|
+ _globalSensorInfo.val1 = 0.0;
|
|
|
+ _globalSensorInfo.val2 = 0.0;
|
|
|
+ _globalSensorMangerTemp->Cur_sensor_info=_globalSensorInfo;
|
|
|
+ _globalDeviceManager->g_modebus_read.temprature=0.0;
|
|
|
+ _globalDeviceManager->g_modebus_read.humidity=0.0;
|
|
|
+ _globalSensorMangerTemp->sensor_status = 1;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ r = g_modbus_receive(&_globalDeviceManager->sensor_modbus_manager,query);
|
|
|
+ if(r > 0)
|
|
|
+ {
|
|
|
+ _mb_hdr(query,r,&mb_h);
|
|
|
+ if(mb_h.func==MODBUS_FC_READ_HOLDING_REGISTERS) {
|
|
|
+ cascade_slave_read(mb_h.reg, mb_h.regcnt);
|
|
|
+ g_modbus_reply(&_globalDeviceManager->sensor_modbus_manager,query,r,cascade_slave_map());
|
|
|
+ }
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ log_d("modbus err!!!!\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ //printf("--------------------------\n");
|
|
|
+ if (nSaveIndex > nMaxIndex)
|
|
|
+ {
|
|
|
+ dev_change_last_Index(_globalDeviceManager->db, &nSaveIndex, nDeleteNumber, strTable);
|
|
|
+ }
|
|
|
+ pthread_mutex_lock(&__globalDeviceManage._sensor_update);
|
|
|
+ list_for_each_entry(_globalSensorMangerTemp, &_globalDeviceManager->_globalSensorManger.list, list)
|
|
|
+ {
|
|
|
+ switch (_globalSensorMangerTemp->sensor_type)
|
|
|
+ {
|
|
|
+ case SENSOR_TYPE_TEMPERATURE: // 温湿度
|
|
|
+ {
|
|
|
+ // printf("get \n");
|
|
|
+ float temperature = 0.0;
|
|
|
+ float humidity = 0.0;
|
|
|
+#ifdef CUST_ANDERSON
|
|
|
+ ret = _g_sensor_get_temperature_value_ANDERSON((void *)&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ _globalSensorMangerTemp->sensor_addr,
|
|
|
+ &temperature,
|
|
|
+ &humidity);
|
|
|
+#else
|
|
|
+ ret = _g_sensor_get_temperature_value((void *)&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ _globalSensorMangerTemp->sensor_addr,
|
|
|
+ &temperature,
|
|
|
+ &humidity);
|
|
|
+#endif
|
|
|
+ if (ret == -1)
|
|
|
+ {
|
|
|
+ _globalSensorMangerTemp->sensor_status = 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _globalSensorMangerTemp->sensor_status = 0;
|
|
|
+ }
|
|
|
+ // log_d("sensor val:%0.2f %0.2f\n",temperature,humidity);
|
|
|
+ // printf("get temperature=%f humidity=%f\n",temperature,humidity);
|
|
|
+ gettimeofday(&tv, &tz);
|
|
|
+ t = localtime(&tv.tv_sec);
|
|
|
+ sprintf(_globalSensorInfo.samp_time, "%04d-%02d-%02d %02d:%02d:%02d.%03ld",
|
|
|
+ t->tm_year + 1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
|
|
|
+ _globalSensorInfo.product_id = __globalDeviceManage._globalDevInfo.product_id;
|
|
|
+ _globalSensorInfo.sensor_id = _globalSensorMangerTemp->sensor_id;
|
|
|
+ _globalSensorInfo.val1 = temperature;
|
|
|
+ _globalSensorInfo.val2 = humidity;
|
|
|
+
|
|
|
+ _globalSensorInfo.product_log_time=_globalSensorMangerTemp->Cur_sensor_info.product_log_time;
|
|
|
+ _globalSensorMangerTemp->Cur_sensor_info=_globalSensorInfo;
|
|
|
+ _globalDeviceManager->g_modebus_read.temprature=temperature *1000;
|
|
|
+ _globalDeviceManager->g_modebus_read.humidity=humidity *1000;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SENSOR_TYPE_SMOKE: // 烟雾
|
|
|
+ case SENSOR_TYPE_WATER: // 水浸
|
|
|
+ case SENSOR_TYPE_DOOR: // 门禁 IO信号
|
|
|
+ {
|
|
|
+ if (_globalSensorMangerTemp->sensor_interface_type==2)//干节点传感器
|
|
|
+ {
|
|
|
+ int nStatus=0;
|
|
|
+ g_switch_get_gpio_status(_globalSensorMangerTemp->sensor_addr+GPIO_PD11,&nStatus);
|
|
|
+ _globalSensorMangerTemp->Cur_sensor_info.val1=nStatus;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SENSOR_TYPE_GAS: // 气体
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SENSOR_TYPE_AIR_PRESS: // 气流
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case SENSOR_TYPE_DoubleT://双头传感器
|
|
|
+ {
|
|
|
+ float temperature = 0.0;
|
|
|
+ float temperature2 = 0.0;
|
|
|
+ ret = _g_sensor_get_D_temperature_value_ANDERSON((void *)&_globalDeviceManager->sensor_modbus_manager,
|
|
|
+ _globalSensorMangerTemp->sensor_addr,
|
|
|
+ &temperature,
|
|
|
+ &temperature2);
|
|
|
+
|
|
|
+ if (ret == -1)
|
|
|
+ {
|
|
|
+ //log_w("Relay modbus Error:%s\n", modbus_strerror(errno));
|
|
|
+ _globalSensorMangerTemp->sensor_status = 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _globalSensorMangerTemp->sensor_status = 0;
|
|
|
+ }
|
|
|
+ // log_d("sensor val:%0.2f %0.2f\n",temperature,humidity);
|
|
|
+ // printf("get temperature=%f humidity=%f\n",temperature,humidity);
|
|
|
+ gettimeofday(&tv, &tz);
|
|
|
+ t = localtime(&tv.tv_sec);
|
|
|
+ sprintf(_globalSensorInfo.samp_time, "%04d-%02d-%02d %02d:%02d:%02d.%03ld",
|
|
|
+ t->tm_year + 1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec / 1000);
|
|
|
+ _globalSensorInfo.product_id = __globalDeviceManage._globalDevInfo.product_id;
|
|
|
+ _globalSensorInfo.sensor_id = _globalSensorMangerTemp->sensor_id;
|
|
|
+ _globalSensorInfo.val1 = temperature;
|
|
|
+ _globalSensorInfo.val2 = temperature2;
|
|
|
+ _globalSensorInfo.product_log_time=_globalSensorMangerTemp->Cur_sensor_info.product_log_time;;
|
|
|
+ _globalSensorMangerTemp->Cur_sensor_info=_globalSensorInfo;
|
|
|
+ _globalDeviceManager->g_modebus_read.temprature=temperature;
|
|
|
+ _globalDeviceManager->g_modebus_read.temprature2=temperature2;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 通用功能
|
|
|
+ // 写入数据库
|
|
|
+ gettimeofday(&tv, NULL);
|
|
|
+ time_t now;
|
|
|
+ time(&now);
|
|
|
+ t = localtime(&now);
|
|
|
+ char buffer[80];
|
|
|
+ strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
|
|
|
+ memset(_globalSensorMangerTemp->Cur_sensor_info.samp_time, 0, sizeof(_globalSensorMangerTemp->Cur_sensor_info.samp_time));
|
|
|
+ strftime(_globalSensorMangerTemp->Cur_sensor_info.samp_time, sizeof(_globalSensorMangerTemp->Cur_sensor_info.samp_time), "%Y-%m-%d %H:%M:%S", t);
|
|
|
+ sprintf(buffer, ".%03ld", tv.tv_usec / 1000);
|
|
|
+ strcat(_globalSensorMangerTemp->Cur_sensor_info.samp_time, buffer);
|
|
|
+ if (difftime(now, _globalSensorMangerTemp->Cur_sensor_info.product_log_time) > (30000 / 1000))
|
|
|
+ {
|
|
|
+ _globalSensorMangerTemp->Cur_sensor_info.product_log_time = now;
|
|
|
+ // 插入数据库
|
|
|
+ ret = dev_insert_sensor_info(_globalDeviceManager->db, &_globalSensorMangerTemp->Cur_sensor_info, &nSaveIndex);
|
|
|
+ if (ret != 0)
|
|
|
+ {
|
|
|
+ log_e("sensor_temp_write database error.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (_globalSensorMangerTemp->sensor_status == 0)
|
|
|
+ {
|
|
|
+ sensor_alarm(_globalSensorMangerTemp, SENSOR_VAL1_UPPER);
|
|
|
+ sensor_alarm(_globalSensorMangerTemp, SENSOR_VAL1_LOWER);
|
|
|
+ sensor_alarm(_globalSensorMangerTemp, SENSOR_VAL2_UPPER);
|
|
|
+ sensor_alarm(_globalSensorMangerTemp, SENSOR_VAL2_LOWER);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pthread_mutex_unlock(&__globalDeviceManage._sensor_update);
|
|
|
+ sleep(3);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pthread_exit(NULL);
|
|
|
+ return NULL;
|
|
|
+}
|