123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- #include "fa.h"
- #include "plt.h"
- struct fa_t fa[FA_NBR_MAX + 1];
- static int fa_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[])
- {
- struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para;
- struct fa_t *dev = &fa[1];
- pcbparam->nrow++;
- log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn);
- for (int i = 0; i < ncolumn; i++)
- {
- if (strcmp("model", columnname[i]) == 0)
- {
- strcpy(dev->szmodel, columnvalue[i]);
- dev->model = plt_devm_str2nbr(dev->szmodel);
- }
- }
- dev->model_nbr = 1;
- pcbparam->ret = 0;
- log_dbg("%s, --,ret:%d", __func__, pcbparam->ret);
- return 0;
- }
- int fa_get_model()
- {
- return fa[1].model;
- }
- int fa_get_init_data(int ctn_idx, char *buf)
- {
- char temp[1024];
- int model = fa[1].model;
- if (model == DEVM_CW1310)
- {
- int r = cw1310_get_init_data(ctn_idx, 1, buf);
- strcat(buf, temp);
- return r;
- }
- else
- {
- return NULL;
- }
- }
- int fa_init()
- {
- int result;
- char *errmsg = NULL;
- sqlite3 *db = NULL;
- char sql[1024];
- struct dbcbparam_t cbparam;
- int ret = 0;
- int model = 0;
- log_dbg("%s, ++", __func__);
- plt_lock_ctndb();
- db = plt_get_ctndb();
- sprintf(sql, "select * from fa");
- cbparam.nrow = 0;
- result = sqlite3_exec(db, sql, fa_dbcb_0, (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 if (cbparam.nrow != 1)
- {
- log_dbg("%s, cbparam.nrow != 1 : %d", __func__, cbparam.nrow);
- ret = -3;
- }
- else
- {
- model = fa_get_model();
- if (model == DEVM_CW1310)
- {
- cw1310_init();
- }
- else
- {
- log_dbg("%s, unknown model:%d", __func__, model);
- ret = -4;
- }
- }
- log_dbg("%s, --, ret:%d", __func__, ret);
- return ret;
- }
- // 0 : ok, all device in stat
- int fa_chk_state_all(int stat)
- {
- int model = fa[1].model;
- if (model == DEVM_CW1310)
- {
- return cw1310_chk_state_all(stat);
- }
- else
- {
- return -1;
- }
- }
- int fa_send_sm_cmd_all(int cmd)
- {
- int model = fa[1].model;
- if (model == DEVM_CW1310)
- {
- return cw1310_send_sm_cmd_all(cmd);
- }
- else
- {
- return -1;
- }
- }
- int fa_send_sm_cmd(int idx, int cmd)
- {
- int model = fa[1].model;
- if (model == DEVM_CW1310)
- {
- return cw1310_send_sm_cmd(idx, cmd);
- }
- else
- {
- return -1;
- }
- }
- int fa_get_warning_level(int idx)
- {
- int model = fa[1].model;
- if (model == DEVM_CW1310)
- {
- return cw1310_get_warning(idx);
- }
- else
- {
- return 0;
- }
- }
- int fa_get_tool_data(char *buf)
- {
- int model = fa[1].model;
- int i = 0;
- char temp_buf[2048];
- if (model == DEVM_CW1310)
- {
- sprintf(buf, "" REVERSE " FA " NONE " model:%s info:%s\n", fa[1].szmodel, fa[1].szinfo);
- for (i = 1; i <= cw1310_get_nbr(); i++)
- {
- memset(temp_buf, 0, sizeof(temp_buf));
- cw1310_get_tool_data(i, temp_buf);
- strcat(buf, temp_buf);
- }
- return 0;
- }
- else
- {
- return -1;
- }
- }
- int fa_get_tbmqtt_data(char *buf)
- {
- for (int i = 1; i <= fa[1].model_nbr; i++)
- {
- if (fa[1].model == DEVM_CW1310)
- {
- for (int idx = 1; idx <= cw1310_get_nbr(); idx++)
- {
- if (cw1310_get_comm_st(idx) != COMMST_NORMAL)
- {
- if (i == fa[1].model_nbr && idx == cw1310_get_nbr() && strlen(buf) > 1)
- {
- buf[strlen(buf) - 1] = 0;
- }
- continue;
- }
- char temp_buf[2048];
- memset(temp_buf, 0, sizeof(temp_buf));
- cw1310_get_tbmqtt_data(idx, temp_buf);
- strcat(buf, temp_buf);
- if (i != fa[1].model_nbr || idx != cw1310_get_nbr())
- {
- strcat(buf, ",");
- }
- }
- }
- }
- return 0;
- }
- int fa_get_cloud_data(int ctn_idx, char *buf)
- {
- for (int i = 1; i <= fa[1].model_nbr; i++)
- {
- if (fa[1].model == DEVM_CW1310)
- {
- for (int idx = 1; idx <= cw1310_get_nbr(); idx++)
- {
- if (cw1310_get_comm_st(idx) != COMMST_NORMAL)
- {
- if (i == fa[1].model_nbr && idx == cw1310_get_nbr() && strlen(buf) > 1)
- {
- buf[strlen(buf) - 1] = 0;
- }
- continue;
- }
- char temp_buf[2048];
- memset(temp_buf, 0, sizeof(temp_buf));
- cw1310_get_cloud_data(ctn_idx, idx, temp_buf);
- strcat(buf, temp_buf);
- if (i != fa[1].model_nbr || idx != cw1310_get_nbr())
- {
- strcat(buf, ",");
- }
- }
- }
- }
- return 0;
- }
- int fa_get_modbus_data(int idx, unsigned short* data)
- {
- int model = fa[1].model;
- if (model == DEVM_CW1310)
- {
- cw1310_get_modbus_data(idx,data);
- }
- else
- {
- data[0] = (unsigned short)model;
- data[1] = (unsigned short)0;
- }
- return 0;
- }
|