123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- #include "abb_b23_4.h"
- #include "plt.h"
- int abb_b23_4_nbr;
- struct abb_b23_4_t abb_b23_4[ABB_B23_4_NBR_MAX + 1];
- static int abb_b23_4_dbcb_0(void *para, int ncolumn, char **columnvalue, char *columnname[])
- {
- int i;
- struct dbcbparam_t *pcbparam = (struct dbcbparam_t *)para;
- struct abb_b23_4_t *dev = NULL;
- pcbparam->nrow++;
- log_dbg("%s, ++,row:%d, col:%d", __func__, pcbparam->nrow, ncolumn);
- dev = &abb_b23_4[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]);
- }
- }
- pcbparam->ret = 0;
- log_dbg("%s, --,ret:%d", __func__, pcbparam->ret);
- return 0;
- }
- void* abb_b23_4_thrd_main(void *param)
- {
- struct abb_b23_4_t *dev = NULL;
- int idx;
- log_dbg("%s, ++", __func__);
- for (idx = 1; idx <= abb_b23_4_nbr; idx++)
- {
- dev = &abb_b23_4[idx];
- dev->PT = 1;
- dev->CT = 1;
- abb_b23_4_sm_init(idx);
- abb_b23_4_comm_init(idx);
- }
- while (1)
- {
- for (idx = 1; idx <= abb_b23_4_nbr; idx++)
- {
- abb_b23_4_comm_dac(idx);
- abb_b23_4_sm(idx);
- usleep(100000); // 100ms
- }
- }
- log_dbg("%s, --, idx:%d", __func__, idx);
- return NULL;
- }
- int abb_b23_4_get_cmd(int idx)
- {
- return abb_b23_4[idx].cmd;
- }
- void abb_b23_4_reset_cmd(int idx)
- {
- abb_b23_4[idx].cmd = CMD_SM_DONE;
- }
- int abb_b23_4_init()
- {
- pthread_t thrd;
- int result = 0;
- int ret = 0;
- int idx = 0;
- struct abb_b23_4_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 abb_b23_4");
- cbparam.nrow = 0;
- result = sqlite3_exec(db, sql, abb_b23_4_dbcb_0, (void *)&cbparam, &errmsg);
- plt_unlock_ctndb();
- if (result != SQLITE_OK)
- {
- log_dbg("%s, result != SQLITE_OK, result:%d", __func__, result);
- ret = -1;
- }
- else if (cbparam.ret != 0)
- {
- log_dbg("%s, cbparam.ret != 0, cbparam.ret:%d", __func__, cbparam.ret);
- ret = -2;
- }
- else
- {
- abb_b23_4_nbr = cbparam.nrow;
- if (pthread_create(&thrd, NULL, abb_b23_4_thrd_main, NULL) != 0)
- {
- log_dbg("%s, create abb_b23_4 thrd main fail", __func__);
- ret = -1;
- }
- }
- log_dbg("%s--, ret:%d", __func__, ret);
- return ret;
- }
- int abb_b23_4_get_state(int idx)
- {
- return abb_b23_4[idx].sm.state;
- }
- char *abb_b23_4_get_state_string(int idx)
- {
- return abb_b23_4[idx].sm.szState;
- }
- int abb_b23_4_get_com_ap(int idx)
- {
- return abb_b23_4[idx].com_ap;
- }
- double abb_b23_4_get_com_ae(int idx)
- {
- return abb_b23_4[idx].com_active_e;
- }
- double abb_b23_4_get_pos_ae(int idx)
- {
- return abb_b23_4[idx].pos_active_e;
- }
- double abb_b23_4_get_neg_ae(int idx)
- {
- return abb_b23_4[idx].neg_active_e;
- }
- double abb_b23_4_get_ua(int idx)
- {
- return abb_b23_4[idx].ua;
- }
- double abb_b23_4_get_ub(int idx)
- {
- return abb_b23_4[idx].ub;
- }
- double abb_b23_4_get_uc(int idx)
- {
- return abb_b23_4[idx].uc;
- }
- double abb_b23_4_get_ia(int idx)
- {
- return abb_b23_4[idx].ia;
- }
- double abb_b23_4_get_ib(int idx)
- {
- return abb_b23_4[idx].ib;
- }
- double abb_b23_4_get_ic(int idx)
- {
- return abb_b23_4[idx].ic;
- }
- double abb_b23_4_get_pwrfct(int idx)
- {
- return abb_b23_4[idx].pwr_factor;
- }
- double abb_b23_4_get_freq(int idx)
- {
- return abb_b23_4[idx].freq;
- }
- char *abb_b23_4_get_info_str(int idx)
- {
- return abb_b23_4[idx].szinfo;
- }
- int abb_b23_4_chk_state_all(int stat)
- {
- int idx;
- for (idx = 1; idx <= abb_b23_4_nbr; idx++)
- {
- if (abb_b23_4_get_state(idx) != stat)
- {
- return -1;
- }
- }
- return 0;
- }
- int abb_b23_4_send_sm_cmd(int idx, int val)
- {
- int ret = 0;
- struct abb_b23_4_t *dev = &abb_b23_4[idx];
- dev->cmd = val;
- leave:
- log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, val, ret);
- return ret;
- }
- int abb_b23_4_send_sm_cmd_all(int cmd)
- {
- int ret = 0;
- int idx;
- for (idx = 1; idx <= abb_b23_4_nbr; idx++)
- {
- abb_b23_4_send_sm_cmd(idx, cmd);
- }
- log_dbg("%s, idx:%d, cmd:%d, ret:%d", __func__, idx, cmd, ret);
- return ret;
- }
- int abb_b23_4_get_comm_st(int idx)
- {
- struct abb_b23_4_t *dev = &abb_b23_4[idx];
- struct comm_t *comm = &dev->comm;
- return comm_get_state(comm);
- }
- void abb_b23_4_set_dac_param_en(int idx, int val)
- {
- struct abb_b23_4_t *dev = &abb_b23_4[idx];
- struct comm_t *comm = &dev->comm;
- comm_set_dac_param_en(comm, val);
- }
- int abb_b23_4_get_sm_step(int idx)
- {
- struct statemachine_t *sm = &abb_b23_4[idx].sm;
- return sm_get_step(sm);
- }
- char *abb_b23_4_get_sm_err_str(int idx)
- {
- return abb_b23_4[idx].sm.szerr;
- }
- int abb_b23_4_get_chan_idx(int idx)
- {
- struct comm_t *comm = &abb_b23_4[idx].comm;
- return comm_get_chan_idx(comm);
- }
- int abb_b23_4_get_adr(int idx)
- {
- struct comm_t *comm = &abb_b23_4[idx].comm;
- return comm_get_adr(comm);
- }
- int abb_b23_4_get_tick(int idx)
- {
- return abb_b23_4[idx].sm.tick;
- }
- char *abb_b23_4_get_comm_state_str(int idx)
- {
- struct comm_t *comm = &abb_b23_4[idx].comm;
- return comm_get_state_str(comm);
- }
- int abb_b23_4_get_PT(int idx)
- {
- return abb_b23_4[idx].PT;
- }
- int abb_b23_4_get_CT(int idx)
- {
- return abb_b23_4[idx].CT;
- }
- int abb_b23_4_get_nbr()
- {
- return abb_b23_4_nbr;
- }
- int abb_b23_4_get_tool_data(int idx, char *buf)
- {
- struct abb_b23_4_t *dev = &abb_b23_4[idx];
- struct statemachine_t *sm = &dev->sm;
- struct comm_t *comm = &dev->comm;
- char temp_buf[1024 * 8];
- if (idx < 1 || idx > abb_b23_4_nbr || buf == NULL)
- return -1;
- sm_get_summary(sm, temp_buf, sizeof(temp_buf));
- sprintf(buf, " [%d] %s ", idx, temp_buf);
- comm_get_summary(comm, temp_buf, sizeof(temp_buf));
- strcat(buf, temp_buf);
- sprintf(temp_buf, " com_ap:" L_GREEN "%.1f" NONE " com_ae:%.1f pos_ae:%.1f neg_ae:%.1f ua:%.1f ub:%.1f uc:%.1f ia:%.1f ib:%.1f ic:%.1f freq:%.2f pwr_fct:%.2f \n",
- dev->com_ap, dev->com_active_e, dev->pos_active_e, dev->neg_active_e,
- dev->ua, dev->ub, dev->uc, dev->ia, dev->ib, dev->ic, dev->freq,
- dev->pwr_factor);
- strcat(buf, temp_buf);
- return 0;
- }
- int abb_b23_4_get_tbmqtt_data(int idx, char *buf)
- {
- struct abb_b23_4_t *dev = &abb_b23_4[idx];
- struct statemachine_t *sm = &dev->sm;
- struct comm_t *comm = &dev->comm;
- char temp_buf[1024 * 8];
- sprintf(buf, "'abb_m%d_state':'%s','abb_m%d_com_ae':%.1f,'abb_m%d_pos_ae':%.1f,'abb_m%d_neg_ae':%.1f,'abb_m%d_com_ap':%d,\
- 'abb_m%d_ua':%.1f,'abb_m%d_ub':%.1f,'abb_m%d_uc':%.1f,\
- 'abb_m%d_ia':%.1f,'abb_m%d_ib':%.1f,'abb_m%d_ic':%.1f,\
- 'abb_m%d_pwr_fct':%.2f",
- idx, abb_b23_4_get_state_string( idx ),
- idx, abb_b23_4_get_com_ae( idx ),
- idx, abb_b23_4_get_pos_ae( idx ),
- idx, abb_b23_4_get_neg_ae( idx ),
- idx, abb_b23_4_get_com_ap( idx ),
- idx, abb_b23_4_get_ua( idx ),
- idx, abb_b23_4_get_ub( idx ),
- idx, abb_b23_4_get_uc( idx ),
- idx, abb_b23_4_get_ia( idx ),
- idx, abb_b23_4_get_ib( idx ),
- idx, abb_b23_4_get_ic( idx ),
- idx, abb_b23_4_get_pwrfct( idx ));
- }
- int abb_b23_4_get_cloud_data(int ctn_idx, int idx, char *buf)
- {
- sprintf(buf, "'id':'meter_%d', 'com_ae':%.1f,\
- 'pos_ae':%.1f,\
- 'neg_ae':%.1f,\
- 'com_ap':%d,\
- 'ua':%.1f,\
- 'ub':%.1f,\
- 'uc':%.1f,\
- 'ia':%.1f,\
- 'ib':%.1f,\
- 'ic':%.1f,\
- 'gf':%.1f,\
- 'pf':%.1f,\
- 'state':%d",
- idx, abb_b23_4_get_com_ae(idx),
- abb_b23_4_get_pos_ae(idx),
- abb_b23_4_get_neg_ae(idx),
- abb_b23_4_get_com_ap(idx),
- abb_b23_4_get_ua(idx),
- abb_b23_4_get_ub(idx),
- abb_b23_4_get_uc(idx),
- abb_b23_4_get_ia(idx),
- abb_b23_4_get_ib(idx),
- abb_b23_4_get_ic(idx),
- abb_b23_4_get_freq(idx),
- abb_b23_4_get_pwrfct(idx),
- abb_b23_4_get_state(idx));
- }
- int abb_b23_4_get_bkds_data(int idx, char *buf)
- {
- sprintf(buf, "'id':'meter_%d', \
- \"info\":\"%s\",\"model\":\"%s\",\"state\":\"%s\",\"stp\":%d,\"err\":\"%s\",\
- \"chan_idx\":%d,\"mbadr\":%d,\"tick\":%d,\"comm_state\":\"%s\",\"pt\":%d,\
- \"ct\":%d,\"com_ae\":%.1f,\"pos_ae\":%.1f,\"neg_ae\":%.1f,\"com_ap\":%d,\
- \"ua\":%.1f,\"ub\":%.1f,\"uc\":%.1f,\"ia\":%.1f,\"ib\":%.1f,\
- \"ic\":%.1f,\"freq\":%.1f,\"pwr_fct\":%.1f\
- ",
- idx,
- abb_b23_4_get_info_str(idx), "abb_b23_4", abb_b23_4_get_state_string(idx), abb_b23_4_get_sm_step(idx), abb_b23_4_get_sm_err_str(idx),
- abb_b23_4_get_chan_idx(idx), abb_b23_4_get_adr(idx), abb_b23_4_get_tick(idx), abb_b23_4_get_comm_state_str(idx), abb_b23_4_get_PT(idx),
- abb_b23_4_get_CT(idx), abb_b23_4_get_com_ae(idx), abb_b23_4_get_pos_ae(idx), abb_b23_4_get_neg_ae(idx), abb_b23_4_get_com_ap(idx),
- abb_b23_4_get_ua(idx), abb_b23_4_get_ub(idx), abb_b23_4_get_uc(idx), abb_b23_4_get_ia(idx), abb_b23_4_get_ib(idx),
- abb_b23_4_get_ic(idx), abb_b23_4_get_freq(idx), abb_b23_4_get_pwrfct(idx));
- }
|