#include "plt.h" #include "dtsd1352.h" int dtsd1352_nbr; struct dtsd1352_t dtsd1352[DTSD1352_NBR_MAX + 1]; static int dtsd1352_dbcb_0(void *para,int ncolumn,char ** columnvalue,char *columnname[]) { int i; struct dbcbparam_t* pcbparam = (struct dbcbparam_t*)para; struct dtsd1352_t* dev = NULL; pcbparam->nrow++; log_dbg("%s, ++,row:%d, col:%d",__func__,pcbparam->nrow,ncolumn); dev = &dtsd1352[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* dtsd1352_thrd_main(void *param) { struct dtsd1352_t* dev = NULL; int idx; log_dbg("%s, ++",__func__); for( idx = 1; idx <= dtsd1352_nbr; idx++){ dev = &dtsd1352[idx]; dev->PT = 1; dev->CT = 1; dtsd1352_sm_init(idx); dtsd1352_comm_init(idx); } while(1){ for( idx = 1; idx <= dtsd1352_nbr; idx++ ){ dtsd1352_comm_dac(idx); dtsd1352_sm( idx ); usleep(300000); // 300ms } } log_dbg("%s, --, idx:%d",__func__,idx); return NULL; } int dtsd1352_get_cmd( int idx ) { return dtsd1352[idx].cmd; } void dtsd1352_reset_cmd(int idx) { dtsd1352[idx].cmd = CMD_SM_DONE; } int dtsd1352_init() { pthread_t thrd; int result = 0; int ret = 0; int idx = 0; struct dtsd1352_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 dtsd1352"); cbparam.nrow = 0; result = sqlite3_exec(db,sql, dtsd1352_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{ dtsd1352_nbr = cbparam.nrow ; if(pthread_create(&thrd,NULL, dtsd1352_thrd_main, NULL)!=0){ log_dbg( "%s, create dtsd1352 thrd main fail", __func__); ret = -1; } } log_dbg("%s--, ret:%d",__func__,ret); return ret; } int dtsd1352_get_state( int idx) { return dtsd1352[idx].sm.state; } char* dtsd1352_get_state_string( int idx) { return dtsd1352[idx].sm.szState; } int dtsd1352_get_com_ap( int idx ) { return dtsd1352[idx].com_active_p; } double dtsd1352_get_com_ae( int idx ) { return dtsd1352[idx].com_active_e; } double dtsd1352_get_pos_ae( int idx ) { return dtsd1352[idx].pos_active_e; } double dtsd1352_get_neg_ae( int idx ) { return dtsd1352[idx].neg_active_e; } double dtsd1352_get_ua( int idx ) { return dtsd1352[idx].ua; } double dtsd1352_get_ub( int idx ) { return dtsd1352[idx].ub; } double dtsd1352_get_uc( int idx ) { return dtsd1352[idx].uc; } double dtsd1352_get_ia( int idx ) { return dtsd1352[idx].ia; } double dtsd1352_get_ib( int idx ) { return dtsd1352[idx].ib; } double dtsd1352_get_ic( int idx ) { return dtsd1352[idx].ic; } double dtsd1352_get_pwrfct( int idx ) { return dtsd1352[idx].pwr_factor; } double dtsd1352_get_THDUa( int idx ) { return dtsd1352[idx].THDUa; } double dtsd1352_get_THDUb( int idx ) { return dtsd1352[idx].THDUb; } double dtsd1352_get_THDUc( int idx ) { return dtsd1352[idx].THDUc; } double dtsd1352_get_THDIa( int idx ) { return dtsd1352[idx].THDIa; } double dtsd1352_get_THDIb( int idx ) { return dtsd1352[idx].THDIb; } double dtsd1352_get_THDIc( int idx ) { return dtsd1352[idx].THDIc; } double dtsd1352_get_freq( int idx ) { return dtsd1352[idx].freq; } double dtsd1352_get_total_fund_ap( int idx ) { return dtsd1352[idx].total_fund_ap; } double dtsd1352_get_total_harm_ap( int idx ) { return dtsd1352[idx].total_harm_ap; } double dtsd1352_get_total_fund_rp( int idx ) { return dtsd1352[idx].total_fund_rp; } double dtsd1352_get_total_harm_rp( int idx ) { return dtsd1352[idx].total_harm_rp; } char* dtsd1352_get_info_str(int idx) { return dtsd1352[idx].szinfo; } int dtsd1352_chk_state_all( int stat) { int idx; for( idx = 1; idx <= dtsd1352_nbr; idx++){ if( dtsd1352_get_state(idx) != stat){ return -1; } } return 0; } int dtsd1352_send_sm_cmd( int idx, int val ) { int ret = 0; struct dtsd1352_t* dev = &dtsd1352[idx]; dev->cmd = val; leave: log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, val, ret); return ret; } int dtsd1352_send_sm_cmd_all( int cmd ) { int ret = 0; int idx; for( idx = 1; idx <= dtsd1352_nbr; idx++ ){ dtsd1352_send_sm_cmd(idx, cmd); } log_dbg("%s, idx:%d, cmd:%d, ret:%d",__func__, idx, cmd, ret); return ret; } int dtsd1352_get_comm_st(int idx) { struct dtsd1352_t* dev = &dtsd1352[idx]; struct comm_t* comm = &dev->comm; return comm_get_state(comm); } void dtsd1352_set_dac_param_en(int idx, int val) { struct dtsd1352_t* dev = &dtsd1352[idx]; struct comm_t* comm = &dev->comm; comm_set_dac_param_en(comm, val); } int dtsd1352_get_sm_step(int idx) { struct statemachine_t *sm = &dtsd1352[idx].sm; return sm_get_step(sm); } char* dtsd1352_get_sm_err_str(int idx) { return dtsd1352[idx].sm.szerr; } int dtsd1352_get_chan_idx(int idx) { struct comm_t* comm = &dtsd1352[idx].comm; return comm_get_chan_idx(comm); } int dtsd1352_get_adr(int idx) { struct comm_t* comm = &dtsd1352[idx].comm; return comm_get_adr(comm); } int dtsd1352_get_tick(int idx) { return dtsd1352[idx].sm.tick; } char* dtsd1352_get_comm_state_str(int idx) { struct comm_t* comm = &dtsd1352[idx].comm; return comm_get_state_str(comm); } int dtsd1352_get_PT(int idx) { return dtsd1352[idx].PT; } int dtsd1352_get_CT(int idx) { return dtsd1352[idx].CT; } int dtsd1352_get_nbr() { return dtsd1352_nbr; } int dtsd1352_get_tool_data(int idx,char* buf) { struct dtsd1352_t* dev = &dtsd1352[idx]; struct statemachine_t* sm = &dev->sm; struct comm_t* comm = &dev->comm; char temp_buf[1024 * 8]; if(idx < 1 || idx > dtsd1352_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," pt:%d ct:%d 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 \ THDUa:%.2f THDUb:%.2f THDUc:%.2f THDIa:%.2f THDIb:%.2f THDIc:%.2f \ tot_fund_ap:%.3f tot_fund_rp:%.3f tot_harm_ap:%.3f tot_harm_rp:%.3f \n", dev->PT,dev->CT, 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, dev->pwr_factor, dev->THDUa, dev->THDUb, dev->THDUc, dev->THDIa,dev->THDIb, dev->THDIc, dev->total_fund_ap, dev->total_fund_rp, dev->total_harm_ap, dev->total_harm_rp); strcat(buf,temp_buf); return 0; } int dtsd1352_get_tbmqtt_data(int idx,char* buf) { struct dtsd1352_t* dev = &dtsd1352[idx]; struct statemachine_t* sm = &dev->sm; struct comm_t* comm = &dev->comm; char temp_buf[1024 * 8]; sprintf(buf, "'id':'meter_%d', 'dt_state':'%s','dt_com_ae':%.1f,'dt_pos_ae':%.1f,'dt_neg_ae':%.1f,'dt_com_ap':%d,\ 'dt_ua':%.1f,'dt_ub':%.1f,'dt_uc':%.1f,\ 'dt_ia':%.1f,'dt_ib':%.1f,'dt_ic':%.1f,\ 'dt_pwr_fct':%.2f,\ 'dt_THDUa':%.2f,'dt_THDUb':%.2f,'dt_THDUc':%.2f,'dt_THDIa':%.2f,'dt_THDIb':%.2f,'dt_THDIc':%.2f,\ 'dt_tot_fund_ap':%.3f,'dt_tot_fund_rp':%.3f,'dt_tot_harm_ap':%.3f,'dt_tot_harm_rp':%.3f", idx, dtsd1352_get_state_string( idx ), dtsd1352_get_com_ae( idx ), dtsd1352_get_pos_ae( idx ), dtsd1352_get_neg_ae( idx ), dtsd1352_get_com_ap( idx ), dtsd1352_get_ua( idx ), dtsd1352_get_ub( idx ), dtsd1352_get_uc( idx ), dtsd1352_get_ia( idx ), dtsd1352_get_ib( idx ), dtsd1352_get_ic( idx ), dtsd1352_get_pwrfct( idx ), dtsd1352_get_THDUa( idx ), dtsd1352_get_THDUb( idx ), dtsd1352_get_THDUc( idx ), dtsd1352_get_THDIa( idx ), dtsd1352_get_THDIb( idx ), dtsd1352_get_THDIc( idx ), dtsd1352_get_total_fund_ap( idx ), dtsd1352_get_total_fund_rp( idx ), dtsd1352_get_total_harm_ap( idx ), dtsd1352_get_total_harm_rp( idx )); } int dtsd1352_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, dtsd1352_get_com_ae( idx ), dtsd1352_get_pos_ae( idx ), dtsd1352_get_neg_ae( idx ), dtsd1352_get_com_ap( idx ), dtsd1352_get_ua( idx ), dtsd1352_get_ub( idx ), dtsd1352_get_uc( idx ), dtsd1352_get_ia( idx ), dtsd1352_get_ib( idx ), dtsd1352_get_ic( idx ), dtsd1352_get_freq( idx ), dtsd1352_get_pwrfct( idx ), dtsd1352_get_state( idx )); } int dtsd1352_get_bkds_data(int idx,char* buf) { sprintf(buf,"\ \"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,\"thd_ua\":%.1f,\"thd_ub\":%f,\"thd_uc\":%f,\ \"thd_ia\":%f,\"thd_ib\":%f,\"thd_ic\":%f,\"tot_fund_ap\":%f,\"tot_fund_rp\":%f,\ \"tot_harm_ap\":%f,\"tot_harm_rp\":%f\ ", dtsd1352_get_info_str( idx ),"dtsd1352",dtsd1352_get_state_string( idx ),dtsd1352_get_sm_step( idx ),dtsd1352_get_sm_err_str( idx ), dtsd1352_get_chan_idx( idx ),dtsd1352_get_adr( idx ),dtsd1352_get_tick( idx ),dtsd1352_get_comm_state_str( idx ),dtsd1352_get_PT( idx ), dtsd1352_get_CT( idx ),dtsd1352_get_com_ae( idx ),dtsd1352_get_pos_ae( idx ),dtsd1352_get_neg_ae( idx ),dtsd1352_get_com_ap( idx ), dtsd1352_get_ua( idx ),dtsd1352_get_ub( idx ),dtsd1352_get_uc( idx ),dtsd1352_get_ia( idx ),dtsd1352_get_ib( idx ), dtsd1352_get_ic( idx ),dtsd1352_get_freq( idx ),dtsd1352_get_pwrfct( idx ),dtsd1352_get_THDUa( idx ),dtsd1352_get_THDUb( idx ),dtsd1352_get_THDUc( idx ), dtsd1352_get_THDIa( idx ),dtsd1352_get_THDIb( idx ),dtsd1352_get_THDIc( idx ),dtsd1352_get_total_fund_ap( idx ),dtsd1352_get_total_fund_rp( idx ), dtsd1352_get_total_harm_ap( idx ),dtsd1352_get_total_harm_rp( idx )); }