#include "plt.h" #include "rs311.h" #include "rs311_comm.h" #include "rs311_sm.h" int rs311_nbr; struct rs311_t rs311[RS311_NBR_MAX + 1]; int rs311_send_sm_cmd(int idx, int cmd) { int ret = 0; struct rs311_t *dev = &rs311[idx]; dev->cmd = cmd; log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, cmd, ret); return ret; } int rs311_send_sm_cmd_all(int cmd) { int ret = 0; int idx; struct rs311_t *dev = NULL; for (idx = 1; idx <= rs311_nbr; idx++) { rs311_send_sm_cmd(idx, cmd); } return ret; } int rs311_chk_state_all(int stat) { int idx; for (idx = 1; idx <= rs311_nbr; idx++) { if (rs311_get_state(idx) != stat) { return -1; } } return 0; } int rs311_get_cmd(int idx) { return rs311[idx].cmd; } void rs311_reset_cmd(int idx) { rs311[idx].cmd = CMD_SM_DONE; } int rs311_get_state(int idx) { return rs311[idx].sm.state; } static int rs311_aux(int idx) { static int cnt = 0; if (cnt < rs311_nbr && mqtt_get_state() == SMST_READY) { cnt++; if (idx == rs311_nbr) { cloud.fe_init = 0; } char buf[4096] = {0}; rs311_get_init_data(ctn_get_idx_in_ess(), idx, buf); cloud_send_init(buf); } } static void *rs311_thrd_main(void *param) { struct rs311_t *dev = NULL; int idx; log_dbg("%s, ++", __func__); for (idx = 1; idx <= rs311_nbr; idx++) { rs311_sm_init(idx); rs311_comm_init(idx); } while (1) { for (idx = 1; idx <= rs311_nbr; idx++) { rs311_comm_dac(idx); rs311_sm(idx); rs311_aux(idx); // usleep(100000); /* 100ms */ sleep(1); } } log_dbg("%s, --, idx:%d", __func__, idx); } static int rs311_dbcb(void *para, int ncolumn, char **columnvalue, char *columnname[]) { int i; struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para; struct rs311_t *pdev; int idx = 0; pcbparam->nrow++; log_dbg("%s, ++, row:%d, col:%d", __func__, pcbparam->nrow, ncolumn); pdev = &rs311[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 rs311_init(void) { pthread_t thrd; int result = 0; int ret = 0; int idx = 0; struct rs311_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 rs311"); cbparam.nrow = 0; result = sqlite3_exec(db, sql, rs311_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 { rs311_nbr = cbparam.nrow; cbparam.nrow = 0; db = plt_get_devdb(); plt_lock_devdb(); sprintf(sql, "select * from rs311"); result = sqlite3_exec(db, sql, rs311_dbcb, (void *)&cbparam, &errmsg); plt_unlock_devdb(); for (int i = 1; i <= rs311_nbr; ++i) { if (strcmp(rs311[i].szdev_id, "") == 0) { if (get_snow_id(rs311[i].szdev_id) != 0) { ret = -4; } else { char sql[1024]; char *errmsg = NULL; sprintf(sql, "update rs311 set devid=\"%s\" where idx=%d", rs311[i].szdev_id, rs311[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__, rs311[i].szdev_id); } } } } if (pthread_create(&thrd, NULL, rs311_thrd_main, NULL) != 0) { ret = -1; log_dbg("%s, create rs311 thrd main fail", __func__); } } leave: log_dbg("%s--, ret:%d", __func__, ret); return ret; } int rs311_get_addr(int idx) { return rs311[idx].comm.adr; } int rs311_get_comm_st(int idx) { struct rs311_t *dev = &rs311[idx]; struct comm_t *comm = &dev->comm; return comm_get_state(comm); } int rs311_get_chan_idx(int idx) { struct rs311_t *dev = &rs311[idx]; struct comm_t *comm = &dev->comm; return comm_get_chan_idx(comm); } char *rs311_get_comm_st_str(int idx) { struct rs311_t *dev = &rs311[idx]; struct comm_t *comm = &dev->comm; return comm_get_state_str(comm); } int rs311_get_tick(int idx) { return rs311[idx].sm.tick; } int rs311_get_nbr() { return rs311_nbr; } int rs311_get_tool_data(int idx, char *buf) { if (idx < 1 || idx > rs311_nbr || buf == NULL) return -1; struct rs311_t *dev = &rs311[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 spurt_flag:%d\n", dev->szdev_id, dev->spurt_flag); strcat(buf, buf_temp); return 0; } int rs311_get_tbmqtt_data(int idx, char *buf) { struct rs311_t *dev = &rs311[idx]; sprintf(buf, "'fe_spurt':%d, 'fe_state':'%s'", dev->spurt_flag, dev->sm.szState); return 0; } int rs311_get_cloud_data(int ctn_idx, int idx, char *buf) { struct rs311_t *dev = &rs311[idx]; sprintf(buf, "{\"device_id\":\"%s\", \"type\":5, \"spurt\":%d, \"state\":\"%d\"}", dev->szdev_id, dev->spurt_flag, dev->sm.state); return 0; } int rs311_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 }}", rs311[idx].szdev_id, idx, ctn_get_dev_id(), rs311[idx].comm.chanidx, rs311[idx].comm.adr); return 0; } int rs311_get_modbus_data(int idx, unsigned short* data) { struct rs311_t *dev = &rs311[idx]; data[0] = (unsigned short)DEVM_RS311; data[1] = (unsigned short)1; data[2] = (unsigned short)0; data[3] = (unsigned short)0; data[4] = (unsigned short)0; data[5] = (unsigned short)0; data[6] = (unsigned short)0; data[7] = (unsigned short)0; data[8] = (unsigned short)0; data[9] = (unsigned short)0; data[10] = (unsigned short)dev->spurt_flag; data[11] = (unsigned short)0; data[12] = (unsigned short)0; data[13] = (unsigned short)0; data[14] = (unsigned short)0; data[15] = (unsigned short)0; data[16] = (unsigned short)0; return 0; }