123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- #include "plt.h"
- int dlt645_nbr;
- struct dlt645_t dlt645[DLT645_NBR_MAX + 1];
- static int dlt645_dbcb_0(void *para,int ncolumn,char ** columnvalue,char *columnname[])
- {
- int i, idx;
- struct dbcbparam_t* pcbparam = (struct dbcbparam_t*)para;
- struct dlt645_t* dev;
- pcbparam->nrow++;
- log_dbg("%s, ++,row:%d, col:%d",__func__,pcbparam->nrow,ncolumn);
- idx = pcbparam->nrow;
- dev = &dlt645[idx];
- dev->low = -900000;
- dev->high = 900000;
- for( i = 0; i < ncolumn; i++){
- if( strcmp("chan_idx",columnname[i]) == 0){
- dev->comm.chanidx = atoi(columnvalue[i]);
- log_dbg("%s, idx:%d, chan_idx:%d, chan_idx:%s",__func__, idx, dev->comm.chanidx, columnvalue[i]);
- }else if( strcmp("info",columnname[i]) == 0){
- strcpy(dev->szinfo, columnvalue[i]);
- }else if( strcmp("address",columnname[i]) == 0){
- strcpy(dev->szaddr, columnvalue[i]);
- log_dbg("%s, idx:%d, addr:%s, address:%s",__func__, idx, dev->szaddr, columnvalue[i]);
- }else if( strcmp("pratio",columnname[i]) == 0){
- dev->pratio = atof(columnvalue[i]);
- }else if( strcmp("vratio",columnname[i]) == 0){
- dev->vratio = atof(columnvalue[i]);
- }else if( strcmp("cratio",columnname[i]) == 0){
- dev->cratio = atof(columnvalue[i]);
- }
- else if (strcmp("limit_low", columnname[i]) == 0)
- {
- dev->low = atoi(columnvalue[i]);
- }
- else if (strcmp("limit_high", columnname[i]) == 0)
- {
- dev->high = atoi(columnvalue[i]);
- }
- }
- pcbparam->ret = 0;
- log_dbg("%s, --,ret:%d",__func__,pcbparam->ret);
- return 0;
- }
- void dlt645_set_state( int idx, int state, int err)
- {
- sm_set_state(&dlt645[idx].sm, state, err );
- }
- int dlt645_get_com_ap( int idx )
- {
- return dlt645[idx].com_active_p;
- }
- double dlt645_get_com_ae( int idx )
- {
- return dlt645[idx].com_active_e;
- }
- double dlt645_get_pos_ae( int idx )
- {
- return dlt645[idx].pos_active_e;
- }
- double dlt645_get_neg_ae( int idx )
- {
- return dlt645[idx].neg_active_e;
- }
- double dlt645_get_ua( int idx )
- {
- return dlt645[idx].ua;
- }
- double dlt645_get_ub( int idx )
- {
- return dlt645[idx].ub;
- }
- double dlt645_get_uc( int idx )
- {
- return dlt645[idx].uc;
- }
- double dlt645_get_ia( int idx )
- {
- return dlt645[idx].ia;
- }
- double dlt645_get_ib( int idx )
- {
- return dlt645[idx].ib;
- }
- double dlt645_get_ic( int idx )
- {
- return dlt645[idx].ic;
- }
- double dlt645_get_freq( int idx )
- {
- return dlt645[idx].freq;
- }
- int dlt645_get_state( int idx)
- {
- return dlt645[idx].sm.state;
- }
- int dlt645_chk_low_hi(int idx)
- {
- if(dlt645[idx].com_active_p < dlt645[idx].low)
- {
- return -1;
- }
- if(dlt645[idx].com_active_p > dlt645[idx].high)
- {
- return 1;
- }
- return 0;
- }
- char* dlt645_get_state_string( int idx)
- {
- return dlt645[idx].sm.szState;
- }
- char* dlt645_get_info_str(int idx)
- {
- return dlt645[idx].szinfo;
- }
- int dlt645_chk_state_all( int stat)
- {
- int idx;
- for( idx = 1; idx <= dlt645_nbr; idx++){
- if( dlt645_get_state(idx) != stat){
- return -1;
- }
- }
- return 0;
- }
- int dlt645_send_sm_cmd_all( int cmd )
- {
- int ret = 0;
- int idx;
- for( idx = 1; idx <= dlt645_nbr; idx++ ){
- dlt645_send_sm_cmd(idx, cmd);
- }
- log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, cmd, ret);
- return ret;
- }
- int dlt645_get_comm_st(int idx)
- {
- struct dlt645_t* dev = &dlt645[idx];
- struct comm_t* comm = &dev->comm;
- return comm_get_state(comm);
- }
- int dlt645_get_cmd( int idx )
- {
- return dlt645[idx].cmd;
- }
- void dlt645_reset_cmd(int idx)
- {
- dlt645[idx].cmd = CMD_SM_DONE;
- }
- static void* dlt645_thrd_main(void *param)
- {
- int idx;
- log_dbg("%s, ++",__func__);
-
- for( idx = 1; idx <= dlt645_nbr; idx++){
- dlt645_comm_init(idx);
- dlt645_sm_init(idx);
- }
- while(1){
- for( idx = 1; idx <= dlt645_nbr; idx++ ){
- dlt645_comm_dac(idx);
- dlt645_sm( idx );
- usleep(100000); // 100ms
- }
- }
- log_dbg("%s, --, idx:%d",__func__,idx);
- }
- int dlt645_init()
- {
- pthread_t thrd;
- int result = 0;
- int ret = 0;
- int idx = 0;
- struct dlt645_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 dlt645");
- cbparam.nrow = 0;
- result = sqlite3_exec(db,sql, dlt645_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{
- dlt645_nbr = cbparam.nrow ;
- if(pthread_create(&thrd,NULL, dlt645_thrd_main, NULL)!=0){
- ret = -3;
- log_dbg("%s, create dlt645 thrd main fail",__func__);
- }
- }
- log_dbg("%s--, ret:%d",__func__,ret);
- return ret;
- }
- int dlt645_send_sm_cmd( int idx, int val )
- {
- int ret = 0;
- struct dlt645_t* dev = &dlt645[idx];
- dev->cmd = val;
- leave:
- log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, val, ret);
- return ret;
- }
- void dlt645_set_dac_param_en(int idx, int val)
- {
- struct dlt645_t* dev = &dlt645[idx];
- struct comm_t* comm = &dev->comm;
-
- comm_set_dac_param_en(comm, val);
- }
- int dlt645_get_sm_step(int idx)
- {
- struct statemachine_t *sm = &dlt645[idx].sm;
- return sm_get_step(sm);
- }
- char* dlt645_get_sm_err_str(int idx)
- {
-
- return dlt645[idx].sm.szerr;
- }
- int dlt645_get_chan_idx(int idx)
- {
- struct comm_t* comm = &dlt645[idx].comm;
- return comm_get_chan_idx(comm);
- }
- int dlt645_get_adr(int idx)
- {
- struct comm_t* comm = &dlt645[idx].comm;
- return comm_get_adr(comm);
- }
- int dlt645_get_tick(int idx)
- {
- return dlt645[idx].sm.tick;
- }
- char* dlt645_get_comm_state_str(int idx)
- {
- struct comm_t* comm = &dlt645[idx].comm;
- return comm_get_state_str(comm);
- }
- // int dlt645_get_PT(int idx)
- // {
- // return dlt645[idx].PT;
- // }
- // int dlt645_get_CT(int idx)
- // {
- // return dlt645[idx].CT;
- // }
- double dlt645_get_pratio(int idx)
- {
- return dlt645[idx].pratio;
- }
- double dlt645_get_vratio(int idx)
- {
- return dlt645[idx].vratio;
- }
- double dlt645_get_cratio(int idx)
- {
- return dlt645[idx].cratio;
- }
- int dlt645_get_nbr()
- {
- return dlt645_nbr;
- }
- int dlt645_get_tool_data(int idx,char* buf)
- {
- struct dlt645_t* dev = &dlt645[idx];
- struct statemachine_t* sm = &dev->sm;
- struct comm_t* comm = &dev->comm;
- char temp_buf[1024*8];
- if(idx < 1 || idx > dlt645_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,"pratio:%.1f vratio:%.1f cratio:%.1f 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\n",
- dev->pratio,dev->vratio, dev->cratio, dev->com_active_p, 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);
- strcat(buf,temp_buf);
- return 0;
- }
- int dlt645_get_tbmqtt_data(int idx,char* buf)
- {
- struct dlt645_t* dev = &dlt645[idx];
- struct statemachine_t* sm = &dev->sm;
- struct comm_t* comm = &dev->comm;
- sprintf(buf, "'device_id':'meter_%d', 'dlt_state':'%s','dlt_com_ae':%.1f,'dlt_pos_ae':%.1f,'dlt_neg_ae':%.1f,'dlt_com_ap':%d,\
- 'dlt_ua':%.1f,'dlt_ub':%.1f,'dlt_uc':%.1f,\
- 'dlt_ia':%.1f,'dlt_ib':%.1f,'dlt_ic':%.1f\
- ",
- idx, dlt645_get_state_string( idx ),
- dlt645_get_com_ae( idx ),
- dlt645_get_pos_ae( idx ),
- dlt645_get_neg_ae( idx ),
- dlt645_get_com_ap( idx ),
- dlt645_get_ua( idx ),
- dlt645_get_ub( idx ),
- dlt645_get_uc( idx ),
- dlt645_get_ia( idx ),
- dlt645_get_ib( idx ),
- dlt645_get_ic( idx ));
- return 0;
- }
- int dlt645_get_bkds_data(int idx,char* buf)
- {
- sprintf(buf,"\
- \"info\":\"%s\",\"model\":\"%s\",\"state\":\"%s\",\"stp\":%d,\"err\":\"%s\",\
- \"chan_idx\":%d,\"adr\":%d,\"tick\":%d,\"comm_state\":\"%s\",\"pratio\":%f,\
- \"vratio\":%f,\"cratio\":%f,\"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\
- ",
- dlt645_get_info_str( idx ),"dlt645",dlt645_get_state_string( idx ),dlt645_get_sm_step( idx ),dlt645_get_sm_err_str( idx ),
- dlt645_get_chan_idx( idx ),dlt645_get_adr( idx ),dlt645_get_tick( idx ),dlt645_get_comm_state_str( idx ),dlt645_get_pratio( idx ),
- dlt645_get_vratio( idx ),dlt645_get_cratio( idx ),dlt645_get_com_ae( idx ),dlt645_get_pos_ae( idx ),dlt645_get_neg_ae( idx ),
- dlt645_get_com_ap( idx ),dlt645_get_ua( idx ),dlt645_get_ub( idx ),dlt645_get_uc( idx ),dlt645_get_ia( idx ),
- dlt645_get_ib( idx ),dlt645_get_ic( idx ),dlt645_get_freq( idx ),1.0);
- return 0;
- }
|