123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881 |
- #include "plt.h"
- struct tb_t tb;
- /* in seconds */
- static long long tb_get_unixts()
- {
- return (long long)time(NULL);
- }
- /* in ms */
- static long long tb_get_ts()
- {
- return (tb_get_unixts() + tbmqtt_get_tz() * (long long)3600) * (long long)1000;
- // return tb_get_unixts()*1000;
- }
- static int tb_send_rpc_response(char *request_topic, char *response_payload)
- {
- char *p = NULL;
- char szreqid[256];
- char *pszreqid = szreqid;
- int reqid = 0;
- tbmqtt_ringbuffer_element_t e;
- p = strrchr(request_topic, '/');
- if (p != NULL)
- {
- p++;
- while (*p != 0)
- {
- *pszreqid++ = *p++;
- }
- *pszreqid = 0;
- reqid = atoi(szreqid);
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- strcpy(e.szpayload, response_payload);
- sprintf(e.sztopic, "v1/devices/me/rpc/response/%s", szreqid);
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- return 0;
- }
- static void tb_upload_param()
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[128];
- char itm_buf[2048];
- int i;
- /* 5 seconds period */
- if (dev->param_en > 0)
- {
- dev->param_en = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- /* sw version */
- sprintf(itm_buf, "'sys_sw_version':'%s', 'norm_cap':%d,'norm_pow':%d",
- plt_get_version_string(), ctn_get_norm_cap(), ctn_get_norm_pow());
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf); /* thingsboard */
- // sprintf(e.sztopic,"ems/data/rtEss/%s", sta_get_sn());
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- static void tb_upload_sys()
- {
- struct tb_t *c = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[128];
- char itm_buf[2048];
- int i;
- /* 5 seconds period */
- if (c->sys_intv[0] > 0)
- {
- if (c->sys_timer[0]++ >= c->sys_intv[0])
- {
- c->sys_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- // sys state and err
- itm_buf[0] = 0;
- sprintf(itm_buf, "'state':'%s','err':'%s','ap':%d,'soc':%.1f,'chgable':%d,'dhgable':%d,\
- 'con_com_ap':%d,'con_com_ae':%.1f,'con_pos_ae':%.1f,'con_neg_ae':%.1f,\
- 'load_com_ap':%d,'load_com_ae':%.1f,'load_pos_ae':%.1f,'load_neg_ae':%.1f,'ems_state':'%s'",
- ctn_get_state_str(), ctn_get_err_str(), ctn_get_ap(), ctn_get_soc(), ctn_get_chgable(), ctn_get_dhgable(),
- ems_get_transf_con_com_ap(), ems_get_transf_con_com_ae(), ems_get_transf_con_pos_ae(), ems_get_transf_con_neg_ae(),
- ems_get_transf_load_com_ap(), ems_get_transf_load_com_ae(), ems_get_transf_load_pos_ae(), ems_get_transf_load_neg_ae(), ems_get_state_str());
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- /* 300 seconds period */
- if (c->sys_intv[1] > 0)
- {
- if (c->sys_timer[1]++ >= c->sys_intv[1])
- {
- c->sys_timer[1] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- itm_buf[0] = 0;
- /* cpu mem disk */
- mac_get_tbmqtt_data(buf);
- strcat(itm_buf, buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- static void tb_upload_pcs()
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[128];
- char itm_buf[8192];
- int i;
- if (pcs_get_comm_st() != COMMST_NORMAL)
- {
- return;
- }
- // 5 seconds period
- if (dev->pcs_intv[0] > 0)
- {
- if (dev->pcs_timer[0]++ >= dev->pcs_intv[0])
- {
- dev->pcs_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- // pcs_get_tbmqtt_main_data(itm_buf);
- pcs_get_tbmqtt_data_interval_30s(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- /* 60 seconds period */
- if (dev->pcs_intv[1] > 0)
- {
- if (dev->pcs_timer[1]++ >= dev->pcs_intv[1])
- {
- dev->pcs_timer[1] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- // pcs_get_tbmqtt_secondy_data(itm_buf);
- pcs_get_tbmqtt_data_interval_60s(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- static void tb_upload_pack()
- {
- struct tb_t *c = &tb;
- tbmqtt_ringbuffer_element_t e;
- char itm_buf[10240];
- int i, j;
- int model = 0;
- model = pack_get_model();
- if (pack_get_comm_st() != COMMST_NORMAL)
- {
- return;
- }
- /* 5 seconds */
- if (c->pack_intv[0] > 0)
- {
- if (c->pack_timer[0]++ >= c->pack_intv[0])
- {
- c->pack_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- // pack_get_tbmqtt_main_data(itm_buf);
- pack_get_tbmqtt_data_inteval_30s(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- // fflush(stdout);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- // 60 seconds sub sys
- if (c->pack_intv[1] > 0)
- {
- if (c->pack_timer[1]++ >= c->pack_intv[1])
- {
- c->pack_timer[1] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- /* bms sub voltage and current */
- memset(itm_buf, 0, sizeof(itm_buf));
- // pack_get_tbmqtt_submain_data(itm_buf);
- pack_get_tbmqtt_data_inteval_60s(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- // fflush(stdout);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- /* cell voltage*/
- if (c->pack_intv[2] > 0)
- {
- if (c->pack_timer[2]++ >= c->pack_intv[2])
- {
- c->pack_timer[2] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- /* module cell v */
- itm_buf[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- // pack_get_tbmqtt_cellv_data(c->cellv_sub_idx,itm_buf);
- pack_get_tbmqtt_data_inteval_90s(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- // fflush(stdout);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- c->cellv_sub_idx++;
- if (model == DEVM_CATL280A)
- {
- if (c->cellv_sub_idx > BMU_NBR)
- {
- c->cellv_sub_idx = 1;
- }
- }
- else if (model == DEVM_KL)
- {
- if (c->cellv_sub_idx > KL_CLUSTER_NBR)
- {
- c->cellv_sub_idx = 1;
- }
- }
- }
- }
- /* cell temp*/
- if (c->pack_intv[3] > 0)
- {
- if (c->pack_timer[3]++ >= c->pack_intv[3])
- {
- c->pack_timer[3] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- /* module cell t */
- memset(itm_buf, 0, sizeof(itm_buf));
- // pack_get_tbmqtt_cellt_data(c->cellt_sub_idx,itm_buf);
- pack_get_tbmqtt_data_inteval_300s(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- // fflush(stdout);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- c->cellt_sub_idx++;
- if (model == DEVM_CATL280A)
- {
- if (c->cellt_sub_idx > BMU_NBR)
- {
- c->cellt_sub_idx = 1;
- }
- }
- else if (model == DEVM_KL)
- {
- if (c->cellt_sub_idx > KL_CLUSTER_NBR)
- {
- c->cellt_sub_idx = 1;
- }
- }
- }
- }
- }
- static void tb_upload_ac(void)
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf_temp[8000];
- /* 60 seconds */
- if (dev->ac_intv[0] > 0)
- {
- if (dev->ac_timer[0]++ >= dev->ac_intv[0])
- {
- dev->ac_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(buf_temp, 0, sizeof(buf_temp));
- ac_get_tbmqtt_data(buf_temp);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), buf_temp);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- static void tb_upload_meter(void)
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[8192];
- char itm_buf[8192];
- int i;
- /* 60 seconds */
- if (dev->meter_intv[0] > 0)
- {
- if (dev->meter_timer[0]++ >= dev->meter_intv[0])
- {
- dev->meter_timer[0] = 0;
- // dtsd1352
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- meter_get_tbmqtt_data(itm_buf);
- if (strlen(itm_buf) > 0)
- {
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- }
- static void tb_upload_env(void)
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[8192];
- char itm_buf[8192];
- /* 60 seconds */
- if (dev->env_intv[0] > 0)
- {
- if (dev->env_timer[0]++ >= dev->env_intv[0])
- {
- dev->env_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- /* env */
- memset(itm_buf, 0, sizeof(itm_buf));
- env_get_tbmqtt_data(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- static void tb_upload_dehumi(void)
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[8192];
- char itm_buf[8192];
- /* 60 seconds */
- if (dev->dehumi_intv[0] > 0)
- {
- if (dev->dehumi_timer[0]++ >= dev->dehumi_intv[0])
- {
- dev->dehumi_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- /* env */
- memset(itm_buf, 0, sizeof(itm_buf));
- dehumi_get_tbmqtt_data(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- static void tb_upload_fe(void)
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[8192];
- char itm_buf[8192];
- /* 60 seconds */
- if (dev->fe_intv[0] > 0)
- {
- if (dev->fe_timer[0]++ >= dev->fe_intv[0])
- {
- dev->fe_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- /* env */
- memset(itm_buf, 0, sizeof(itm_buf));
- fe_get_tbmqtt_data(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- static void tb_upload_plc(void)
- {
- struct tb_t *dev = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[8192];
- char itm_buf[8192];
- int i;
- /* 60 seconds */
- if (dev->plc_intv[0] > 0)
- {
- if (dev->plc_timer[0]++ >= dev->plc_intv[0])
- {
- dev->plc_timer[0] = 0;
- if (plc_get_comm_st() == COMMST_NORMAL)
- {
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- plc_get_tbmqtt_data(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- }
- static void tb_upload_ups(void)
- {
- struct tb_t *c = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[8192];
- char itm_buf[8192];
- int i;
- if (ups_get_comm_state() != COMMST_NORMAL)
- {
- return;
- }
- /* 60 seconds */
- if (c->ups_intv[0] > 0)
- {
- if (c->ups_timer[0]++ >= c->ups_intv[0])
- {
- c->ups_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- ups_get_tbmqtt_data(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- static void tb_upload_ctn_cal(void)
- {
- struct tb_t *c = &tb;
- tbmqtt_ringbuffer_element_t e;
- char buf[8192];
- char itm_buf[8192];
- int i;
- if (ctn_cal_get_state() != SMST_RUN)
- {
- return;
- }
- /* 60 seconds */
- if (c->ctn_cal_intv[0] > 0)
- {
- if (c->ctn_cal_timer[0]++ >= c->ctn_cal_intv[0])
- {
- c->ctn_cal_timer[0] = 0;
- e.cmd = CMD_MQTT_SENDKV;
- e.sztopic[0] = 0;
- e.szpayload[0] = 0;
- memset(itm_buf, 0, sizeof(itm_buf));
- ctn_cal_get_tb_data(itm_buf);
- sprintf(e.szpayload, "{'ts':%lld,'values':{%s}}", tb_get_ts(), itm_buf);
- // printf("%s,%s\n",__func__,e.szpayload);
- strcpy(e.sztopic, "v1/devices/me/telemetry");
- tbmqtt_lock_txbuf();
- tbmqtt_queue_txbuf(e);
- tbmqtt_unlock_txbuf();
- }
- }
- }
- int tb_is_upload_enable()
- {
- struct tb_t *dev = &tb;
- return dev->upload_enable;
- }
- int tb_enable_upload()
- {
- struct tb_t *dev = &tb;
- dev->upload_enable = 1;
- return 0;
- }
- int tb_disable_upload()
- {
- struct tb_t *dev = &tb;
- dev->upload_enable = 0;
- return 0;
- }
- int tb_init(void)
- {
- struct tb_t *dev = &tb;
- int ret = 0;
- dev->upload_enable = 1;
- dev->param_en = 0;
- dev->sys_intv[0] = 5;
- dev->sys_intv[1] = 300;
- dev->pcs_intv[0] = 5;
- dev->pcs_intv[1] = 60;
- dev->cellv_sub_idx = 1;
- dev->cellt_sub_idx = 1;
- dev->pack_intv[0] = 5;
- dev->pack_intv[1] = 60;
- dev->pack_intv[2] = 90; /* cell v disabled */
- // tb_set_cellv_lowspeed();
- dev->pack_intv[3] = 300; /* cell t disabled 30*10=300s=5min for all 10 cabinet */
- dev->ac_intv[0] = 60;
- dev->meter_intv[0] = 60;
- dev->env_intv[0] = 60;
- dev->plc_intv[0] = 60;
- dev->ups_intv[0] = 60;
- dev->dehumi_intv[0] = 60;
- dev->fe_intv[0] = 60;
- dev->ctn_cal_intv[0] = 60;
- if (tbmqtt_init() != 0)
- {
- log_dbg("%s, tbmqtt init fail", __func__);
- }
- log_dbg("%s, ret:%d", __func__, ret);
- return ret;
- }
- int tb_set_cellv_highspeed()
- {
- struct tb_t *dev = &tb;
- dev->pack_intv[2] = 6; /* cell v */
- /* 6 = high: 6*10=60s=1min for all 10 cabinet */
- return 0;
- }
- int tb_set_cellv_lowspeed()
- {
- struct tb_t *dev = &tb;
- dev->pack_intv[2] = 60; /* cell v */
- /* 60 = low: 60*10=600s=10min for all 10 cabinet */
- }
- static int tmpaps_received = 0;
- static int tmpaps;
- static void tb_proc_recv()
- {
- cJSON *root = NULL;
- cJSON *item = NULL;
- int aps = 0;
- int rc;
- tbmqtt_ringbuffer_element_t e;
- tbmqtt_lock_rxbuf();
- rc = tbmqtt_dequeue_rxbuf(&e);
- tbmqtt_unlock_rxbuf();
- if (rc == 1)
- {
- root = cJSON_Parse(e.szpayload);
- if (!root)
- {
- log_dbg("%s, cJSON_Parse null", __func__);
- }
- else
- {
- item = cJSON_GetObjectItem(root, "method");
- if (!item)
- {
- log_dbg("%s, cJSON_GetObjectItem method fail", __func__);
- }
- else
- {
- log_dbg("%s, get method:%s", __func__, item->valuestring);
- if (strcmp(item->valuestring, "get_aps") == 0)
- {
- sprintf(e.szpayload, "%d", ess_get_aps());
- tb_send_rpc_response(e.sztopic, e.szpayload);
- }
- else if (strcmp(item->valuestring, "stdby") == 0)
- {
- ess_send_sm_cmd(CMD_SM_STDBY);
- }
- else if (strcmp(item->valuestring, "stop") == 0)
- {
- ess_send_sm_cmd(CMD_SM_STOP);
- }
- else if (strcmp(item->valuestring, "ready") == 0)
- {
- ess_send_sm_cmd(CMD_SM_READY);
- }
- else if (strcmp(item->valuestring, "offgrid") == 0)
- {
- ess_send_sm_cmd(CMD_SM_OFFGRID);
- }
- else if (strcmp(item->valuestring, "active_aps") == 0)
- {
- if (tmpaps_received == 1)
- {
- ess_set_aps(tmpaps);
- tmpaps_received = 0;
- }
- }
- else if (strcmp(item->valuestring, "set_aps") == 0)
- {
- item = cJSON_GetObjectItem(root, "params");
- if (!item)
- {
- log_dbg("%s, cJSON_GetObjectItem params fail", __func__);
- }
- else
- {
- // ess_set_aps(item->valueint);
- log_dbg("%s, aps:%d", __func__, item->valueint);
- tmpaps = item->valueint;
- tmpaps_received = 1;
- }
- }
- else if (strcmp(item->valuestring, "bms_ctrl_pwrup") == 0)
- {
- plc_bms_ctrl_pwrup();
- }
- else if (strcmp(item->valuestring, "pack_stop") == 0)
- {
- pack_send_sm_cmd(CMD_SM_STOP);
- }
- else if (strcmp(item->valuestring, "pack_ready") == 0)
- {
- pack_send_sm_cmd(CMD_SM_READY);
- }
- else if (strcmp(item->valuestring, "pcs_stop") == 0)
- {
- pcs_send_sm_cmd(CMD_SM_STOP);
- }
- else if (strcmp(item->valuestring, "pcs_ready") == 0)
- {
- pcs_send_sm_cmd(CMD_SM_READY);
- }
- else if (strcmp(item->valuestring, "bms_ctrl_pwrup_without_shielding_signal"))
- {
- plc_bms_ctrl_pwrup_without_shielding_signal();
- }
- else
- {
- log_dbg("%s, unknown cmd : %s", __func__, item->valuestring);
- }
- }
- cJSON_Delete(root);
- }
- }
- }
- void tb_exe(void)
- {
- tb_proc_recv();
- if (tb_is_upload_enable())
- {
- tb_upload_param();
- tb_upload_sys();
- tb_upload_pcs();
- tb_upload_pack();
- tb_upload_ac();
- tb_upload_meter();
- tb_upload_env();
- tb_upload_dehumi();
- tb_upload_fe();
- // tb_upload_plc();
- // tb_upload_ups();
- // tb_upload_ctn_cal();
- }
- }
- int tb_set_param_en(int val)
- {
- struct tb_t *dev = &tb;
- dev->param_en = val;
- }
- char *tb_get_state_str(void)
- {
- return tbmqtt_get_state_str();
- }
- int tb_get_stp(void)
- {
- return tbmqtt_get_stp();
- }
- char *tb_get_err_str(void)
- {
- return tbmqtt_get_err_str();
- }
- int tb_get_tick(void)
- {
- return tbmqtt_get_tick();
- }
- double tb_get_timing_ave(void)
- {
- return tbmqtt_get_timing_ave();
- }
- double tb_get_timing_cur(void)
- {
- return tbmqtt_get_timing_cur();
- }
- double tb_get_timing_max(void)
- {
- return tbmqtt_get_timing_max();
- }
- int tb_get_enable(void)
- {
- return tbmqtt_get_enable();
- }
- char *tb_get_servip_str(void)
- {
- return tbmqtt_get_servip_str();
- }
- int tb_get_servport(void)
- {
- return tbmqtt_get_servport();
- }
- char *tb_get_client_id(void)
- {
- return tbmqtt_get_client_id();
- }
- double tb_get_txbuf_usage(void)
- {
- return tbmqtt_get_txbuf_usage();
- }
- char *tb_get_access_token(void)
- {
- return tbmqtt_get_access_token();
- }
- int tb_get_tz(void)
- {
- return tbmqtt_get_tz();
- }
- int tb_get_tool_data(char *buf)
- {
- struct tb_t *t = &tb;
- char buf_temp[1024];
- sprintf(buf, "" REVERSE " TB " NONE " \n");
- tbmqtt_get_tool_data(buf_temp);
- strcat(buf, buf_temp);
- sprintf(buf_temp, " upload_en:%d param_en:%d\n", t->upload_enable, t->param_en);
- strcat(buf, buf_temp);
- return 0;
- }
|