#include "rh811ad053f.h" #include "plt.h" #include "rh811ad053f.h" int rh811ad053f_nbr; struct rh811ad053f_t rh811ad053f[RH811AD053F_NBR_MAX + 1]; static int rh811ad053f_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[]) { int i; struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para; struct rh811ad053f_t *dev = NULL; pcbparam->nrow++; log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn); dev = &rh811ad053f[pcbparam->nrow]; 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("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__); } } } pcbparam->ret = 0; log_dbg("%s, --,ret:%d", __func__, pcbparam->ret); return 0; } int rh811ad053f_get_addr(int idx) { return rh811ad053f[idx].comm.adr; } static void aux(int idx) { static int cnt = 0; if (cnt < rh811ad053f_nbr && mqtt_get_state() == SMST_READY) { if (idx == rh811ad053f_nbr) { cloud.th_init = 0; } char buf[4096] = {0}; rh811ad053f_get_init_data(ctn_get_idx_in_ess(), idx, buf); cloud_send_init(buf); cnt++; } } void* rh811ad053f_thrd_main(void *param) { struct rh811ad053f_t *dev = NULL; int idx; log_dbg("%s, ++", __func__); for (idx = 1; idx <= rh811ad053f_nbr; idx++) { rh811ad053f_comm_init(idx); rh811ad053f_sm_init(idx); } while (1) { for (idx = 1; idx <= rh811ad053f_nbr; idx++) { rh811ad053f_comm_dac(idx); rh811ad053f_sm(idx); aux(idx); sleep(1); // 1s } } log_dbg("%s, --, idx:%d", __func__, idx); return NULL; } int rh811ad053f_chk_stat_all(int stat) { int idx; for (idx = 1; idx <= rh811ad053f_nbr; idx++) { if (rh811ad053f_get_state(idx) != stat) { return -1; } } return 0; } int rh811ad053f_send_sm_cmd_all(int cmd) { int ret = 0; int idx; for (idx = 1; idx <= rh811ad053f_nbr; idx++) { rh811ad053f_send_sm_cmd(idx, cmd); } log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, cmd, ret); return ret; } int rh811ad053f_init() { pthread_t thrd; int result = 0; int ret = 0; int idx = 0; struct rh811ad053f_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 rh811ad053f"); cbparam.nrow = 0; result = sqlite3_exec(db, sql, rh811ad053f_dbcb_0, (void *)&cbparam, &errmsg); plt_unlock_ctndb(); if (result != SQLITE_OK) { ret = -1; } else if (cbparam.ret != 0) { ret = -2; } else { rh811ad053f_nbr = cbparam.nrow; cbparam.nrow = 0; db = plt_get_devdb(); plt_lock_devdb(); sprintf(sql, "select * from rh811ad053f"); result = sqlite3_exec(db, sql, rh811ad053f_dbcb_0, (void *)&cbparam, &errmsg); plt_unlock_devdb(); for (int i = 1; i <= rh811ad053f_nbr; ++i) { if (strcmp(rh811ad053f[i].szdev_id, "") == 0) { if (get_snow_id(rh811ad053f[i].szdev_id) != 0) { ret = -4; } else { char sql[1024]; char *errmsg = NULL; plt_lock_devdb(); sprintf(sql, "update rh811ad053f set devid=\"%s\" where idx=%d", rh811ad053f[i].szdev_id, rh811ad053f[i].idx); plt_unlock_devdb(); int rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg); 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__, rh811ad053f[i].szdev_id); } } } } if (pthread_create(&thrd, NULL, rh811ad053f_thrd_main, NULL) != 0) { log_dbg("%s, create rh811ad053f thrd main fail", __func__); ret = -1; } } leave: log_dbg("%s--, ret:%d", __func__, ret); return ret; } int rh811ad053f_get_state(int idx) { return rh811ad053f[idx].sm.state; } char *rh811ad053f_get_state_str(int idx) { return rh811ad053f[idx].sm.szState; } int rh811ad053f_get_cmd(int idx) { return rh811ad053f[idx].cmd; } void rh811ad053f_reset_cmd(int idx) { rh811ad053f[idx].cmd = CMD_SM_DONE; } double rh811ad053f_get_temp(int idx) { return rh811ad053f[idx].temp; } double rh811ad053f_get_humi(int idx) { return rh811ad053f[idx].humi; } int rh811ad053f_send_sm_cmd(int idx, int val) { int ret = 0; struct rh811ad053f_t *dev = &rh811ad053f[idx]; dev->cmd = val; leave: log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, val, ret); return ret; } int rh811ad053f_get_comm_st(int idx) { struct rh811ad053f_t *dev = &rh811ad053f[idx]; struct comm_t *comm = &dev->comm; return comm_get_state(comm); } int rh811ad053f_get_chan_idx(int idx) { struct rh811ad053f_t *dev = &rh811ad053f[idx]; struct comm_t *comm = &dev->comm; return comm_get_chan_idx(comm); } int rh811ad053f_get_tick(int idx) { return rh811ad053f[idx].sm.tick; } char *rh811ad053f_get_comm_state_str(int idx) { struct rh811ad053f_t *dev = &rh811ad053f[idx]; struct comm_t *comm = &dev->comm; return comm_get_state_str(comm); } int rh811ad053f_get_nbr() { return rh811ad053f_nbr; } struct rh811ad053f_t *rh811ad053f_get_all_data_ptr() { return rh811ad053f; } int rh811ad053f_get_tool_data(int idx, char *buf) { if (idx < 1 || idx > rh811ad053f_nbr || buf == NULL) return -1; struct rh811ad053f_t *dev = &rh811ad053f[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 temp:" L_GREEN "%.1lf" NONE " humi:%.1lf\n", dev->szdev_id, buf_temp, dev->temp, dev->humi); return 0; } int rh811ad053f_get_tbmqtt_data(int idx, char *buf) { sprintf(buf, "'env%d_state':'%s','env%d_t':%.1f, 'env%d_h':%.1f", idx, rh811ad053f_get_state_str(idx), idx, rh811ad053f_get_temp(idx), idx, rh811ad053f_get_humi(idx)); } int rh811ad053f_get_cloud_data(int ctn_idx, int idx, char *buf) { sprintf( buf, "{\"device_id\":\"%s\",\"type\":6, \"temp\":%.1f, \"humi\":%.1f, \"state\":\"%d\"}", rh811ad053f[idx].szdev_id, rh811ad053f_get_temp(idx), rh811ad053f_get_humi(idx), rh811ad053f_get_state(idx)); } int rh811ad053f_get_init_data(int ctn_idx, int idx, char *buf) { sprintf(buf, "{\"device_id\":\"%s\", \"type\":6, \"idx\":%d, \"pid\":\"%s\", \"config\":{ \ \"chan_idx\":%d, \"adr\":%d}}", rh811ad053f[idx].szdev_id, idx, ctn_get_dev_id(), rh811ad053f_get_chan_idx(idx), rh811ad053f[idx].comm.adr); return 0; } int rh811ad053f_get_modbus_data(int idx,unsigned short* data) { struct rh811ad053f_t *dev = &rh811ad053f[idx]; data[0] = (unsigned short)DEVM_RH811AD053F; 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->temp; data[8] = (unsigned short)dev->humi; return 0; }