#include "yf2825.h" #include "plt.h" #include "yf2825_comm.h" #include "yf2825_sm.h" int yf2825_nbr; struct yf2825_t yf2825[YF2825_NBR_MAX + 1]; int yf2825_send_sm_cmd(int idx, int cmd) { int ret = 0; struct yf2825_t *dev = &yf2825[idx]; dev->cmd = cmd; log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, cmd, ret); return ret; } int yf2825_send_sm_cmd_all(int cmd) { int ret = 0; int idx; struct yf2825_t *dev = NULL; for (idx = 1; idx <= yf2825_nbr; idx++) { yf2825_send_sm_cmd(idx, cmd); } return ret; } int yf2825_chk_state_all(int stat) { int idx; for (idx = 1; idx <= yf2825_nbr; idx++) { if (yf2825_get_state(idx) != stat) { return -1; } } return 0; } int yf2825_get_cmd(int idx) { return yf2825[idx].cmd; } void yf2825_reset_cmd(int idx) { yf2825[idx].cmd = CMD_SM_DONE; } int yf2825_get_state(int idx) { return yf2825[idx].sm.state; } static int yf2825_aux(int idx) { static int cnt = 0; if (cnt < yf2825_nbr && mqtt_get_state() == SMST_READY) { cnt++; if (idx == yf2825_nbr) { cloud.fe_init = 0; } char buf[4096] = {0}; yf2825_get_init_data(ctn_get_idx_in_ess(), idx, buf); cloud_send_init(buf); } } static void *yf2825_thrd_main(void *param) { struct yf2825_t *dev = NULL; int idx; log_dbg("%s, ++", __func__); for (idx = 1; idx <= yf2825_nbr; idx++) { yf2825_sm_init(idx); yf2825_comm_init(idx); } while (1) { for (idx = 1; idx <= yf2825_nbr; idx++) { yf2825_comm_dac(idx); yf2825_sm(idx); yf2825_aux(idx); // usleep(100000); /* 100ms */ sleep(1); } } log_dbg("%s, --, idx:%d", __func__, idx); } static int yf2825_dbcb(void *para, int ncolumn, char **columnvalue, char *columnname[]) { int i; struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para; struct yf2825_t *pdev; int idx = 0; pcbparam->nrow++; log_dbg("%s, ++, row:%d, col:%d", __func__, pcbparam->nrow, ncolumn); pdev = &yf2825[pcbparam->nrow]; for (i = 0; i < ncolumn; i++) { if (strcmp("chan_idx", columnname[i]) == 0) { pdev->comm.chanidx = atoi(columnvalue[i]); } else if (strcmp("info", columnname[i]) == 0) { strcpy(pdev->szinfo, columnvalue[i]); } else if (strcmp("adr", columnname[i]) == 0) { pdev->comm.adr = atoi(columnvalue[i]); } else if (strcmp("idx", columnname[i]) == 0) { pdev->idx = atoi(columnvalue[i]); } else if (strcmp("devid", columnname[i]) == 0) { if (columnvalue[i] != NULL && strcmp(columnvalue[i], "") != 0) { strcpy(pdev->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 yf2825_init(void) { pthread_t thrd; int result = 0; int ret = 0; int idx = 0; struct yf2825_t *dev = NULL; char *errmsg = NULL; char sql[1024]; struct dbcbparam_t cbparam; sqlite3 *db = STA.cfg_db; log_dbg("%s, ++", __func__); plt_lock_ctndb(); sprintf(sql, "select * from yf2825"); cbparam.nrow = 0; result = sqlite3_exec(db, sql, yf2825_dbcb, (void *)&cbparam, &errmsg); plt_unlock_ctndb(); if (result != SQLITE_OK) { log_dbg("%s, result != SQLITE_OK : %d", __func__, result); ret = -1; } else if (cbparam.ret != 0) { log_dbg("%s, cbparam.ret != 0 : %d", __func__, cbparam.ret); ret = -2; } else { yf2825_nbr = cbparam.nrow; cbparam.nrow = 0; db = plt_get_devdb(); plt_lock_devdb(); sprintf(sql, "select * from yf2825"); result = sqlite3_exec(db, sql, yf2825_dbcb, (void *)&cbparam, &errmsg); plt_unlock_devdb(); for (int i = 1; i <= yf2825_nbr; ++i) { if (strcmp(yf2825[i].szdev_id, "") == 0) { if (get_snow_id(yf2825[i].szdev_id) != 0) { ret = -4; } else { char sql[1024]; char *errmsg = NULL; sprintf(sql, "update yf2825 set devid=\"%s\" where idx=%d", yf2825[i].szdev_id, yf2825[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__, yf2825[i].szdev_id); } } } } if (pthread_create(&thrd, NULL, yf2825_thrd_main, NULL) != 0) { ret = -1; log_dbg("%s, create yf2825 thrd main fail", __func__); } } leave: log_dbg("%s--, ret:%d", __func__, ret); return ret; } int yf2825_get_addr(int idx) { return yf2825[idx].comm.adr; } int yf2825_get_comm_st(int idx) { struct yf2825_t *dev = &yf2825[idx]; struct comm_t *comm = &dev->comm; return comm_get_state(comm); } int yf2825_get_chan_idx(int idx) { struct yf2825_t *dev = &yf2825[idx]; struct comm_t *comm = &dev->comm; return comm_get_chan_idx(comm); } char *yf2825_get_comm_st_str(int idx) { struct yf2825_t *dev = &yf2825[idx]; struct comm_t *comm = &dev->comm; return comm_get_state_str(comm); } int yf2825_get_tick(int idx) { return yf2825[idx].sm.tick; } int yf2825_get_nbr() { return yf2825_nbr; } int yf2825_get_tool_data(int idx, char *buf) { if (idx < 1 || idx > yf2825_nbr || buf == NULL) return -1; struct yf2825_t *dev = &yf2825[idx]; struct statemachine_t *sm = &dev->sm; struct comm_t *comm = &dev->comm; char buf_temp[1024]; sm_get_summary(sm, buf_temp, sizeof(buf_temp)); sprintf(buf, " [%d] %s ", idx, buf_temp); comm_get_summary(comm, buf_temp, sizeof(buf_temp)); strcat(buf, buf_temp); sprintf(buf_temp, "dev id :%s smoke_detector_alarm:%d temperature_alarm:%d press_alarm:%d spurt_flag:%d\n", dev->szdev_id, dev->smoke_detector_alarm, dev->temperature_alarm, dev->press_alarm, dev->spurt_flag); strcat(buf, buf_temp); return 0; } int yf2825_get_tbmqtt_data(int idx, char *buf) { struct yf2825_t *dev = &yf2825[idx]; sprintf(buf, "'fe_smoke':%d, 'fe_temperature':%d, 'fe_press':%d, 'fe_spurt':%d, \ 'fe_pack1':%d, 'fe_pack2':%d, 'fe_pack3':%d, 'fe_pack4':%d, 'fe_pack5':%d, 'fe_pack6':%d, 'fe_state':'%s'", dev->smoke_detector_alarm, dev->temperature_alarm, dev->press_alarm, dev->spurt_flag, dev->pack1_on, dev->pack2_on, dev->pack3_on, dev->pack4_on, dev->pack5_on, dev->pack6_on, dev->sm.szState); return 0; } int yf2825_get_cloud_data(int ctn_idx, int idx, char *buf) { struct yf2825_t *dev = &yf2825[idx]; sprintf(buf, "{\"device_id\":\"%s\", \"type\":5, \"smoke\":%d, \"temp\":%d, \"press\":%d, \"spurt\":%d, \"state\":\"%d\", \ \"pack1\":%d,\"pack2\":%d,\"pack3\":%d,\"pack4\":%d,\"pack5\":%d,\"pack6\":%d}", dev->szdev_id, dev->smoke_detector_alarm, dev->temperature_alarm, dev->press_alarm, dev->spurt_flag, dev->sm.state, dev->pack1_on, dev->pack2_on, dev->pack3_on, dev->pack4_on, dev->pack5_on, dev->pack6_on); return 0; } int yf2825_get_init_data(int ctn_idx, int idx, char *buf) { sprintf(buf, "{\"device_id\":\"%s\", \"type\":5, \"idx\":%d, \"pid\":\"%s\", \"config\":{ \ \"chan_idx\":%d, \"adr\":%d }}", yf2825[idx].szdev_id, idx, ctn_get_dev_id(), yf2825[idx].comm.chanidx, yf2825[idx].comm.adr); return 0; } int yf2825_get_modbus_data(int idx, unsigned short* data) { struct yf2825_t *dev = &yf2825[idx]; data[0] = (unsigned short)DEVM_YF2825; 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->press_alarm; data[8] = (unsigned short)dev->temperature_alarm; data[9] = (unsigned short)dev->smoke_detector_alarm; data[10] = (unsigned short)dev->spurt_flag; data[11] = (unsigned short)dev->pack1_on; data[12] = (unsigned short)dev->pack2_on; data[13] = (unsigned short)dev->pack3_on; data[14] = (unsigned short)dev->pack4_on; data[15] = (unsigned short)dev->pack5_on; data[16] = (unsigned short)dev->pack6_on; return 0; }