#include "plt.h" int cd1f3300_nbr; struct cd1f3300_t cd1f3300[CD1F3300_NBR_MAX + 1]; static int cd1f3300_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[]) { int i; struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para; struct cd1f3300_t *dev = NULL; pcbparam->nrow++; log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn); dev = &cd1f3300[pcbparam->nrow]; dev->threshold = INT_MIN; for (i = 0; i < ncolumn; i++) { if (strcmp("chan_idx", columnname[i]) == 0) { dev->comm.chanidx = atoi(columnvalue[i]); } else if (strcmp("info", columnname[i]) == 0) { strcpy(dev->szinfo, columnvalue[i]); } else if (strcmp("adr", columnname[i]) == 0) { dev->comm.adr = atoi(columnvalue[i]); } else if (strcmp("thres", columnname[i]) == 0) { dev->threshold = atoi(columnvalue[i]); } else if (strcmp("idx", columnname[i]) == 0) { dev->idx = atoi(columnvalue[i]); } else if (strcmp("devid", columnname[i]) == 0) { if (columnvalue[i] != NULL && strcmp(columnvalue[i], "") != 0) { strcpy(dev->szdev_id, columnvalue[i]); } else { log_dbg("%s, devid is null", __func__); } } } if (dev->threshold == INT_MIN) { dev->threshold = 40; } pcbparam->ret = 0; log_dbg("%s, --,ret:%d", __func__, pcbparam->ret); return 0; } int cd1f3300_get_addr(int idx) { return cd1f3300[idx].comm.adr; } int cd1f3300_get_co_density(int idx) { return cd1f3300[idx].co_density; } int cd1f3300_get_co_threshold(int idx) { return cd1f3300[idx].threshold; } static void aux(int idx) { static int cnt = 0; if (cnt < cd1f3300_nbr && mqtt_get_state() == SMST_READY) { cnt++; if (idx == cd1f3300_get_nbr()) { cloud.co_init = 0; } char buf[4096] = {0}; cd1f3300_get_init_data(ctn_get_idx_in_ess(), idx, buf); cloud_send_init(buf); } } void* cd1f3300_thrd_main(void *param) { struct cd1f3300_t *dev = NULL; int idx; log_dbg("%s, ++", __func__); for (idx = 1; idx <= cd1f3300_nbr; idx++) { cd1f3300_comm_init(idx); cd1f3300_sm_init(idx); } while (1) { for (idx = 1; idx <= cd1f3300_nbr; idx++) { cd1f3300_comm_dac(idx); cd1f3300_sm(idx); aux(idx); sleep(1); // 1s } } log_dbg("%s, --, idx:%d", __func__, idx); return NULL; } int cd1f3300_chk_stat_all(int stat) { int idx; for (idx = 1; idx <= cd1f3300_nbr; idx++) { if (cd1f3300_get_state(idx) != stat) { return -1; } } return 0; } int cd1f3300_send_sm_cmd_all(int cmd) { int ret = 0; int idx; for (idx = 1; idx <= cd1f3300_nbr; idx++) { cd1f3300_send_sm_cmd(idx, cmd); } log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, cmd, ret); return ret; } int cd1f3300_init() { pthread_t thrd; int result = 0; int ret = 0; int idx = 0; struct cd1f3300_t *dev = NULL; char *errmsg = NULL; char sql[1024]; struct dbcbparam_t cbparam; sqlite3 *db = NULL; log_dbg("%s, ++", __func__); plt_lock_ctndb(); db = plt_get_ctndb(); sprintf(sql, "select * from cd1f3300"); cbparam.nrow = 0; result = sqlite3_exec(db, sql, cd1f3300_dbcb_0, (void *)&cbparam, &errmsg); plt_unlock_ctndb(); if (result != SQLITE_OK) { ret = -1; } else if (cbparam.ret != 0) { ret = -2; } else { cd1f3300_nbr = cbparam.nrow; cbparam.nrow = 0; db = plt_get_devdb(); plt_lock_devdb(); sprintf(sql, "select * from cd1f3300"); result = sqlite3_exec(db, sql, cd1f3300_dbcb_0, (void *)&cbparam, &errmsg); plt_unlock_devdb(); for (int i = 1; i <= cd1f3300_nbr; ++i) { if (strcmp(cd1f3300[i].szdev_id, "") == 0) { if (get_snow_id(cd1f3300[i].szdev_id) != 0) { ret = -4; } else { char sql[1024]; char *errmsg = NULL; sprintf(sql, "update cd1f3300 set devid=\"%s\" where idx=%d", cd1f3300[i].szdev_id, cd1f3300[i].idx); plt_lock_devdb(); int rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg); plt_unlock_devdb(); if (rc != SQLITE_OK) { log_dbg("%s, failed to update level device id:%s", __func__, errmsg); ret = -4; goto leave; } else { log_dbg("%s, update device id:%s", __func__, cd1f3300[i].szdev_id); } } } } if (pthread_create(&thrd, NULL, cd1f3300_thrd_main, NULL) != 0) { log_dbg("%s, create cd1f3300 thrd main fail", __func__); ret = -1; } } leave: log_dbg("%s--, ret:%d", __func__, ret); return ret; } int cd1f3300_get_state(int idx) { return cd1f3300[idx].sm.state; } char *cd1f3300_get_state_str(int idx) { return cd1f3300[idx].sm.szState; } int cd1f3300_get_cmd(int idx) { return cd1f3300[idx].cmd; } void cd1f3300_reset_cmd(int idx) { cd1f3300[idx].cmd = CMD_SM_DONE; } // int cd1f3300_get_humi( int idx ) // { // return cd1f3300[idx].humi; // } int cd1f3300_send_sm_cmd(int idx, int val) { int ret = 0; struct cd1f3300_t *dev = &cd1f3300[idx]; dev->cmd = val; leave: log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, val, ret); return ret; } int cd1f3300_get_comm_st(int idx) { struct cd1f3300_t *dev = &cd1f3300[idx]; struct comm_t *comm = &dev->comm; return comm_get_state(comm); } int cd1f3300_get_chan_idx(int idx) { struct cd1f3300_t *dev = &cd1f3300[idx]; struct comm_t *comm = &dev->comm; return comm_get_chan_idx(comm); } int cd1f3300_get_tick(int idx) { return cd1f3300[idx].sm.tick; } char *cd1f3300_get_comm_state_str(int idx) { struct cd1f3300_t *dev = &cd1f3300[idx]; struct comm_t *comm = &dev->comm; return comm_get_state_str(comm); } int cd1f3300_get_nbr() { return cd1f3300_nbr; } struct cd1f3300_t *cd1f3300_get_all_data_ptr() { return cd1f3300; } int cd1f3300_get_tool_data(int idx, char *buf) { if (idx < 1 || idx > cd1f3300_nbr || buf == NULL) return -1; struct cd1f3300_t *dev = &cd1f3300[idx]; struct statemachine_t *sm = &dev->sm; char buf_temp[1024]; sm_get_summary(sm, buf_temp, sizeof(buf_temp)); sprintf(buf, "dev id :%s %s CO Density:" L_GREEN "%d" NONE ", CO Warn Threshold: " L_GREEN "%d" NONE " state:%s\n", dev->szdev_id, buf_temp, dev->co_density, dev->threshold, dev->state_str); return 0; } double cd1f3300_get_co(int idx) { return cd1f3300[idx].co_density; } int cd1f3300_get_tbmqtt_data(int idx, char *buf) { sprintf(buf, "'env%d_state':'%s','env%d_co':%.1f", idx, cd1f3300_get_state_str(idx), idx, cd1f3300_get_co(idx)); } int cd1f3300_get_cloud_data(int ctn_idx, int idx, char *buf) { sprintf(buf, "{\"device_id\":\"%s\",\"type\":8, \"co\":%.1f, \"state\":\"%d\"}", cd1f3300[idx].szdev_id, cd1f3300_get_co(idx), cd1f3300_get_state(idx)); } int cd1f3300_get_init_data(int ctn_idx, int idx, char *buf) { sprintf(buf, "{\"device_id\":\"%s\", \"type\":8, \"idx\":%d, \"pid\":\"%s\", \"config\":{ \ \"chan_idx\":%d, \"adr\":%d}}", cd1f3300[idx].szdev_id, idx, ctn_get_dev_id(), cd1f3300[idx].comm.chanidx, cd1f3300[idx].comm.adr); return 0; } int cd1f3300_get_modbus_data(int idx,unsigned short* data) { struct cd1f3300_t *dev = &cd1f3300[idx]; data[0] = (unsigned short)DEVM_CD1F3300; data[1] = (unsigned short)1; data[2] = (unsigned short)dev->sm.state; data[3] = (unsigned short)dev->sm.step; data[4] = (unsigned short)dev->sm.err; data[5] = (unsigned short)dev->comm.adr; data[6] = (unsigned short)dev->comm.state; data[7] = (unsigned short)dev->co_density; data[8] = (unsigned short)dev->threshold; data[9] = (unsigned short)dev->state; return 0; }