| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668 |
- /*
- * megatec.c
- *
- * Created on: 22.05.2017
- * Author: balbekova
- */
- #include "FreeRTOS.h"
- #include "task.h"
- #include "fr_timers.h"
- #include "usart.h"
- #include "megatec.h"
- #include "ups_monitor.h"
- #include "log.h"
- #include "snmp_api.h"
- #include "syslog.h"
- #include "settings_api.h"
- #include "parameters.h"
- #include "hal.h"
- #include "rtc.h"
- #include "crc_calc.h"
- #ifdef PRINTF_STDLIB
- #include <stdio.h>
- #endif
- #ifdef PRINTF_CUSTOM
- #include "tinystdio.h"
- #endif
- #include <string.h>
- #include <math.h>
- #define DBG if (0)
- TimerHandle_t UPSRestoreTimer;
- TimerHandle_t AutoTestTimer;
- /**
- * @brief Общая структура настроек
- */
- extern SETTINGS_t sSettings;
- #define UPS_PDU_MAX_LEN 80
- #define UPS_PDU_MIN_LEN 5
- float TimeParamFloat = 0;
- uint16_t TimeParam = 0;
- uint16_t TimeParam2 = 0;
- bool megatec_send = true;
- UPS_value_t UPS;
- int test_time = 0;
- static bool flag_buzzer_on = false;
- static float Ubat_sum = 0;
- static float Pload_sum = 0;
- uint32_t Period_testing = 0;
- static bool sync_data = false;
- static bool QFLAG_L = false;
- enum {
- NORMAL = 0x00,
- VER_ERROR = 0x01,
- CHKSUM_ERROR = 0x02,
- LCHKSUM_ERROR = 0x03,
- CID2_INVALID = 0x04,
- CMD_FMT_ERROR = 0x05,
- INVALID_DATA = 0x06,
- };
- static struct {
- uint8_t data[UPS_PDU_MAX_LEN];
- uint16_t len;
- } ups_pdu;
- const char *MegaTecCMD[] = {
- "Q1\r", // ups_status_req [1] - OK
- "T\r", // ups_test_10sec
- "TL\r", // ups_test_low_bat
- "T", // ups_test_time
- "Q\r", // ups_beep
- "S", // ups_shutdown
- "R", // ups_shutdown_restore
- "C\r", // ups_cancel_shut_down
- "CT\r", // ups_cancel_test
- "I\r", // ups_info [3] - OK
- "F\r", // ups_rating_info
- "Q2\r", // ups_remain_time_reg [2] - ERR
- "QGS\r", // ups_general_status_req
- "QBV\r", // ups_akb_info
- "QMD\r", // ups_model_req
- "QVFW\r", // ups_version_req
- "QVFW2\r", // ups_version2_req
- "QID\r", // ups_serial_req
- "QPI\r", // ups_protocol_id_req
- "QS\r", // ups_offline_status_req
- "M\r", // ups_passw_req
- "PDA\r", // ups_buzzer_cntrl
- "QMOD\r", // ups_mode_req
- "SON\r", // ups_remote_turn_on
- "QWS\r", // ups_warning_status
- "QBYV\r", // ups_bypass_volt_range
- "PHV264\r", // ups_bypass_hvolt_264_set
- "QFLAG\r", // ups_qflag_status
- "PEL\r", // ups_cmd_enable_flag_l
- "BT\r", // ups_kstar_status [4] - ERR
- "QTPR\r", // ups_temperature
- "QFS\r", // ups_fault_status
- "SOFF\r", // ups_remote_turn_off
- };
- extern bool flUpdateLog;
- static bool flag_WPHVR2K0L = false;
- void AutoTestCallback(void);
- void init_UPS_value(void)
- {
- UPS.Freq_in = 0;
- UPS.VAC_in = 0;
- UPS.VAC_out = 0;
- UPS.Temp = 0;
- UPS.data_check_load = 0;
- UPS.data_check_load_flag = false;
- UPS.Load = 0;
- UPS.Load_test_akb = 0;
- UPS.SOC = 0;
- UPS.SOC_prev = 0;
- UPS.work_time = 0;
- UPS.akb_work_time = 0;
- UPS.Status = 0;
- UPS.Mode = 0;
- UPS.Alarm = 0;
- UPS.temp_stm32 = 0;
- UPS.warn_status = 0;
- UPS.Test_Status = 0;
- UPS.fault_type = 0;
- UPS.cnt_err_ups = 0;
- UPS.Flag_Present = false;
- UPS.Present = UPS_WAIT_CONNECT;
- memset(UPS.model, 0, sizeof(UPS.model));
- memset(UPS.vertion, 0, sizeof(UPS.vertion));
- memset(UPS.serial, 0, sizeof(UPS.serial));
- }
- void init_var_for_testing(void)
- {
- Ubat_sum = 0;
- Pload_sum = 0;
- Period_testing = 0;
- }
- float voltage_bat_average(void)
- {
- return (Ubat_sum / Period_testing);
- }
- float power_load_average(void)
- {
- return (Pload_sum / Period_testing);
- }
- bool ups_additional_prtcl_rx_pdu(void)
- {
- int c = 0;
- uint8_t cnt_answer = 0;
- ups_pdu.len = 0;
- /* Check empty responce (UART has no data) */
- if ((c = ups_getchar(1000)) < 0) {
- DBG printf("no data\r\n");
- return false;
- } else {
- ups_pdu.len = 0;
- ups_pdu.data[ups_pdu.len++] = c;
- }
- while ((ups_pdu.len < UPS_PDU_MAX_LEN) && ((c = ups_getchar(10)) >= 0)) {
- ups_pdu.data[ups_pdu.len++] = c;
- }
- DBG printf("rx %d bytes from 0x%X\r\n", ups_pdu.len, ups_pdu.data[0]);
- if (ups_pdu.len < UPS_PDU_MIN_LEN ||
- ups_pdu.data[0] != 0x01) {
- return false;
- }
- #if 1
- uint16_t crc = (uint16_t)(ups_pdu.data[ups_pdu.len - 2] | ((uint16_t)(ups_pdu.data[ups_pdu.len - 1]) << 8));
- if (crc != calc_crc16(ups_pdu.data, ups_pdu.len - 2)) {
- DBG {
- printf("bad crc:");
- for (uint16_t i = 0; i < ups_pdu.len; i++) {
- printf(" 0x%X", ups_pdu.data[i]);
- }
- printf("\r\n");
- }
- return false;
- }
- #endif
- return true;
- }
-
- void send_MegaTec_cmd(cmdMegaTecEnums_t command)
- {
- DBG printf("MegaTecCMD: %s\r\n", MegaTecCMD[command]);
- if (command == ups_test_time) {
- uint8_t req[10];
- memset(req, 0, 10);
- if (TimeParam < 10) {
- sprintf(req, "%s0%d\r", MegaTecCMD[command], TimeParam);
- } else {
- sprintf(req, "%s%d\r", MegaTecCMD[command], TimeParam);
- }
- ups_send_block(req, strlen(req));
- } else if (command == ups_shutdown) {
- uint8_t req[10];
- memset(req, 0, 10);
- if (TimeParamFloat >= 1 && TimeParamFloat < 10) {
- sprintf(req, "%s0%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
- } else if (TimeParamFloat < 1) {
- sprintf(req, "%s.%d\r", MegaTecCMD[command], (uint16_t)(10 * TimeParamFloat));
- } else {
- sprintf(req, "%s%d\r", MegaTecCMD[command], (uint16_t)TimeParamFloat);
- }
- ups_send_block(req, strlen(req));
- } else if (command == ups_shutdown_restore) {
- uint8_t req[10];
- memset(req, 0, 10);
- if (TimeParamFloat >= 1 && TimeParamFloat < 10) {
- sprintf(req, "%s0%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)TimeParamFloat, MegaTecCMD[command], TimeParam2);
- } else if (TimeParamFloat < 1) {
- sprintf(req, "%s.%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)(10 * TimeParamFloat), MegaTecCMD[command], TimeParam2);
- } else {
- sprintf(req, "%s%d%s000%d\r", MegaTecCMD[command - 1], (uint16_t)TimeParamFloat, MegaTecCMD[command], TimeParam2);
- }
- ups_send_block(req, strlen(req));
- } else {
- // TODO ����������� ���������
- //ups_send_block(MegaTecCMD[command], strlen(MegaTecCMD[command]));
- ups_send_block((void *)MegaTecCMD[command], strlen(MegaTecCMD[command]));
- }
- }
- bool ups_megatec_rx_pdu(void)
- {
- int c = 0;
- uint8_t cnt_answer = 0;
- ups_pdu.len = 0;
- while (c >= 0) {
- c = ups_getchar(300);//portMAX_DELAY200
- if (c >= 0) {
- ups_pdu.len = 0;
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- ups_pdu.data[ups_pdu.len++] = c;
- while ((ups_pdu.len < UPS_PDU_MAX_LEN) && (c != 0x0d)) {
- c = ups_getchar(100);//portMAX_DELAY200
- if (c < 0) {
- ups_pdu.len = 0;
- return false;
- }
- ups_pdu.data[ups_pdu.len++] = c;
- }
- DBG printf("UPS raw data: %s\r\n", ups_pdu.data);
- cnt_answer ++;
- } else {
- if (ups_pdu.len == 0) {
- return false;
- }
- }
- }
- if (cnt_answer > 1) {
- DBG printf("MegaTecCMD: false\r\n");
- return false;
- }
- // printf("UPS raw data: %s\r\n", ups_pdu.data);
- // printf("UPS raw data len: %d\r\n", ups_pdu.len);
- return true;
- }
- int8_t get_ups_param(char *buf, char *param, char *val)
- {
- char *endValue;
- int8_t param_len;
- memset(val, 0, 20);
- endValue = strpbrk(buf, param);
- if (endValue != NULL) {
- param_len = endValue - buf;
- if (param_len < 20) {
- strncpy(val, buf, param_len);
- } else {
- param_len = 0;
- }
- } else {
- param_len = 0;
- }
- DBG printf("UPS parameter: %s\r\n", val);
- return param_len;
- }
- void ups_status_response(char *data)
- {
- uint8_t i;
- char value[20];
- float tmp;
- uint8_t len = 0;
- // printf("ups_status_response: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_status_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- #ifdef UPS_BUZZER_OFF
- if (flag_buzzer_on) {
- data[45] = '0';
- }
- #endif
- if (flUpdateLog) {
- if(sSettings.UPS_Setting.type_ups == ups_voltronic) {
- char temperature_str[5];
- sprintf(temperature_str, "%3.1f", UPS.Temp);
- for(uint8_t k = 0; k < 4; k++){
- data[33 + k] = temperature_str[k];
- }
-
- }
- flUpdateLog = false;
- log_add(data);
- }
- data++;
- DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- tmp = atof(value);
- if (tmp > 20) {
- UPS.VAC_in = tmp;
- } else {
- UPS.VAC_in = 0;
- }
- }
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.VAC_out = atof(value);
- }
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.Load = atoi(value);
- }
- sync_data = true;
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.Freq_in = atof(value);
- }
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.Vcell_curr = atof(value);
- }
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- tmp = atof(value);
- if (tmp == 0 && UPS.temp_stm32 < 0) {
- UPS.Temp = UPS.temp_stm32;
- } else {
- UPS.Temp = tmp;
- }
- }
- len = get_ups_param(data, "\r", value);
- data += (len + 1);
- if (len > 0) {
- uint8_t stat = 0;
- for (i = 0; i < len; i ++) {
- stat |= (value[i] - 0x30) << (7 - i);
- }
- UPS.Status = stat; // теперь параметр как обычное число
- }
- // if (sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline) {
- // Bat Capacity
- if(UPS.Status & 0x04) {
- UPS.SOC_prev = UPS.SOC;
- }
- #ifdef HARDWARE_BT6721
- if(UPS.Mode == 'B') {
- UPS.SOC = round(100 * (UPS.Vcell_curr / AKB_NUM_CELL - 1.96) /
- (2.13 - 1.96));
- } else
- #endif
- {
- if (UPS.Vcell_curr < 7) {
- UPS.SOC = round(100 * (UPS.Vcell_curr - sSettings.UPS_Setting.Ucellmin) / (sSettings.UPS_Setting.Ucellmax -
- sSettings.UPS_Setting.Ucellmin));
- } else {
- UPS.SOC = round(100 * (UPS.Vcell_curr / AKB_NUM_CELL - sSettings.UPS_Setting.Ucellmin) /
- (sSettings.UPS_Setting.Ucellmax - sSettings.UPS_Setting.Ucellmin));
- }
- }
- // }
- if ((UPS.Status >> 2) & 0x01 || ((UPS.Status >> 7) & 0x01)) {
- UPS.Load_test_akb = UPS.Load;
- TM_RTC_t data;
- TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
- UPS.data_check_load_flag = true;
- UPS.data_check_load = data.unix;
- }
- }
- void ups_general_status_response(char *data)
- {
- uint8_t i;
- char value[20];
- float tmp;
- uint8_t len = 0;
- DBG printf("ups_status_response: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_status_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- DBG printf("UPS ups_status_parser_startr: %s\r\n", data);
-
- data = data + 72;
- uint8_t stat = 0;
- UPS.Test_Status = 0;
- for (i = 0; i < 2; i ++) {
- UPS.Test_Status |= (data[i] - 0x30) << (1 - i);
- }
- /*
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- tmp = atof(value);
- if (tmp > 20) {
- UPS.VAC_in = tmp;
- } else {
- UPS.VAC_in = 0;
- }
- }
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.Freq_in = atof(value);
- }
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.VAC_out = atof(value);
- }
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.Load = atoi(value);
- }
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.Temp = atof(value);
- }
- len = get_ups_param(data, "\r", value);
- data += (len + 1);
- if (len > 0) {
- uint8_t stat = 0;
- for (i = 2; i < (len - 2); i ++) {
- stat |= (value[i] - 0x30) << (7 - i);
- }
- UPS.Status = stat;
- }
- value[len - 2] = 0;
- if (flUpdateLog) {
- flUpdateLog = false;
- memset(data, 0, UPS_PDU_MAX_LEN);
- sprintf(data, "(%0.1f n/a %0.1f %d %0.1f %0.2f %0.1f %s", UPS.VAC_in, UPS.VAC_out, UPS.Load, UPS.Freq_in, UPS.Vcell_curr, UPS.Temp, &value[2]);
- log_add(data);
- }
- */
- }
- void ups_specific_response_sn(uint8_t *data)
- {
- uint8_t len = data[4] - 3;
- if(len >= sizeof(UPS.serial)) {
- len = sizeof(UPS.serial) - 1;
- }
- strncpy(UPS.serial, &data[8], len);
- }
- void ups_info_response(char *data)
- {
- uint8_t i = 0, j = 0;
- char value[20];
- DBG printf("ups_info_response: %s\r\n", data);
- if (data[0] != '#') {
- return;
- }
- DBG printf("ups_info_response_startr: %s\r\n", data);
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- data += 16; //пропускаем поле название компании
- memset(value, 0, sizeof(value));
- for (uint8_t i = 0; i < KSTAR_MODEL_LENGTH; i ++) {
- if (data[i] != ' ') {
- value[j] = data[i];
- j ++;
- }
- }
- if (j != 0) {
- if (UPS.model[0] == 0) {
- strncpy(UPS.model, value, strlen(value));
- SNMP_SetObjDescr();
- } else {
- strncpy(UPS.model, data, strlen(value));
- }
- } else {
- if (UPS.model[0] == 0) {
- strcpy(UPS.model, "RTMP-II");
- SNMP_SetObjDescr();
- } else {
- strcpy(UPS.model, "RTMP-II");
- }
- }
- data += (KSTAR_MODEL_LENGTH + 1);
- if (UPS.serial[0] == 0 && data[0] != ' '){
- strncpy(UPS.serial, data, 8);
- data += 8;
- } else {
- while (*data == ' '){
- data += 1;
- }
- }
-
- memset(value, 0, sizeof(value));
- j = 0;
- while (*data != '\r'){
- value[j] = *data;
- j++;
- data += 1;
- }
- if( (sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline) && strlen(UPS.serial) == 0){
- uint8_t specific_req_sn[7] = {0x01, 0x06, 0x95, 0x00, 0x0f, 0x49, 0x60};
- ups_send_block(specific_req_sn, 7);
- if (ups_additional_prtcl_rx_pdu()) {
- ups_specific_response_sn(ups_pdu.data);
- }
- }
- strncpy(UPS.vertion, value, (j+1));
- SNMP_SetObjDescr();
- }
- void ups_remain_time_response(char *data)
- {
- char value[20];
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_remain_time_response: %s\r\n", data);
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- if (strlen(data) > 5) {
- return;
- }
- memset(value, 0, 10);
- strcpy(value, data);
- //if((UPS.Status >> 7) & 0x01)
- UPS.work_time = atoi(value);
- //else
- // UPS.work_time = 0;
- }
- void ups_akb_info_response(char *data)
- {
- char value[20];
- uint8_t len = 0;
- DBG printf("ups_akb_info_response: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_akb_info_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if(len > 0) {
- UPS.Vakb_curr = atof(value);
- }
- #ifdef TEST_ALARM_AKB_MONITOR
- if((UPS.Status >> 2) & 0x01) {
- Ubat_sum += UPS.Vakb_curr;
- Pload_sum += UPS.Load;
- Period_testing ++;
- }
- #endif
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- //TODO
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- /* if (len > 0) {
- UPS.SOC_prev = UPS.SOC;
- UPS.SOC = atoi(value);
- }*/
- len = get_ups_param(data, "\r", value);
- data += (len + 1);
- if (len > 0) {
- UPS.work_time = atoi(value);
- }
- }
- void ups_model_response(char *data)
- {
- uint8_t j = 0;
- char value[20];
- uint8_t len = 0;
- DBG printf("ups_akb_info_response: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_akb_info_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
- memset(value, 0, 20);
- if (UPS.model[0] == 0) {
- for (uint8_t i = 0; i < VOLTRONIC_MODEL_LENGTH; i ++) {
- if (data[i] != '#') {
- value[j] = data[i];
- j ++;
- }
- }
- if(strncmp(value, "WPHV", 4) == 0 || strncmp(value, "G2", 2) == 0 || value[0] == 0) {
- #if HARDWARE_BT6711_V1 || HARDWARE_BT6721
- if(UPS.serial[0] == 0) {
- return;
- }
- if(strncmp(value, "WPHVR2K0L", 9) == 0) {
- strcpy(UPS.model, "RTMP-II v1rih");
- flag_WPHVR2K0L = true;
- } else if(strncmp(UPS.serial, "83122103100051", 14) == 0) {
- strcpy(UPS.model, "RTMP-II v0hi v1");
- //flag_WPHVR2K0L = true;
- }
- else
- #endif
- #if HARDWARE_BT6709_MTS || HARDWARE_BT6710
- if(UPS.serial[0] == 0) {
- return;
- }
- if(strncmp(value, "WPHVR3K0", 8) == 0) {
- strcpy(UPS.model, "RTMP-II v3r");
- } else if(strncmp(value, "WPHVR2K0", 8) == 0) {
- strcpy(UPS.model, "RTMP-II v2r");
- } else if(strncmp(value, "WPHVR1K0", 8) == 0) {
- strcpy(UPS.model, "RTMP-II r");
- } else
- #endif
- {
- strcpy(UPS.model, "RTMP-II");
- }
- } else {
- #ifdef HARDWARE_BT6709
- if(UPS.serial[0] == 0) {
- return;
- }
- if(strncmp(UPS.serial, "83122104100", 11) == 0) {
- char str[15];
- strncpy(str, &UPS.serial[11], 3);
- int32_t tmp = atoi(str);
- if (tmp >= 281 && tmp <= 770) {
- strcpy(UPS.model, "RTMP-II hi");
- } else {
- len = strlen(value);
- strncpy(UPS.model, value, len);
- }
- } else
- #endif
- #if HARDWARE_BT6709_MTS || HARDWARE_BT6710
- if(strncmp(value, "RTMP-II v", 9) == 0 || strncmp(value, "RTMP-IIv", 8) == 0) {
- strcpy(UPS.model, "RTMP-II r");
- } else
- #endif
- {
- len = strlen(value);
- strncpy(UPS.model, value, len);
- }
- }
- #if HARDWARE_BT6711_V1 || HARDWARE_BT6721
- if(UPS.serial[0] == 0) {
- memset(UPS.model, 0, sizeof(UPS.model));
- return;
- }
- if(strncmp(UPS.serial, "83122103100051", 14) == 0) {
- strcpy(UPS.model, "RTMP-II v0hi v1");
- //flag_WPHVR2K0L = true;
- } else if (strncmp(UPS.serial, "83122204100", 11) == 0) {
- char str[15];
- strncpy(str, &UPS.serial[11], 3);
- int32_t tmp = atoi(str);
- if (tmp >= 139 && tmp <= 938) {
- strcpy(UPS.model, "RTMP-II v0hi v1");
- }
- }
- #endif
- SNMP_SetObjDescr();
- data += (VOLTRONIC_MODEL_LENGTH + 1);
- memset(value, 0, sizeof(value));
- j = 0;
- for (uint8_t i = 0; i < VOLTRONIC_OUTPUT_RATED_LENGTH; i ++) {
- if (data[i] != '#') {
- value[j] = data[i];
- j ++;
- }
- }
- float temp_value = atof(value);
- if (sSettings.UPS_Setting.ups_power != temp_value) {
- sSettings.UPS_Setting.ups_power = temp_value;
- SETTINGS_Save();
- }
- } else {
- return;
- }
- }
- void ups_version_response(char *data)
- {
- char value[20];
- uint8_t len = 0;
- DBG printf("ups_akb_info_response: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_akb_info_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- if (UPS.vertion[0] != 0) {
- if(strncmp(data, UPS.vertion, strlen(UPS.vertion)) == 0){
- return;
- }
- }
- memset(UPS.vertion, 0, sizeof(UPS.vertion));
- len = get_ups_param(data, ":", value);
- data += (len + 1);
- DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
- len = get_ups_param(data, "\r", value);
- if (len > VOLTRONIC_VERSION_LENGTH) {
- len = VOLTRONIC_VERSION_LENGTH;
- }
- strncpy(UPS.vertion, value, len);
- }
- void ups_version_part2_response(char *data)
- {
- char value[20];
- uint8_t len = 0;
- DBG printf("ups_akb_info_response: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_akb_info_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- if (strncmp(ups_pdu.data, "(NAK", 4) == 0) {
- return;
- }
- data++;
- if (UPS.vertion[VOLTRONIC_VERSION_LENGTH] != 0) {
- return;
- }
- DBG printf("UPS ups_akb_info_parser_start: %s\r\n", data);
- len = get_ups_param(data, ":", value);
- data += (len + 1);
- len = get_ups_param(data, "\r", value);
- if (len > VOLTRONIC_VERSION_LENGTH) {
- len = VOLTRONIC_VERSION_LENGTH;
- }
- strncat(UPS.vertion, "/", 1);
- strncat(UPS.vertion, value, len);
- }
- void ups_serial_response(char *data)
- {
- char value[20];
- uint8_t len = 0;
- DBG printf("ups_serialresponse: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_serial_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- DBG printf("UPS ups_serial_parser_start: %s\r\n", data);
- len = get_ups_param(data, "\r", value);
- strncpy(UPS.serial, value, VOLTRONIC_SN_LENGTH);
- }
- void ups_protocol_id_response(char *data)
- {
- //printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
- DBG printf("ups_protocol_id_response: %s\r\n", data);
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- if (data[0] != '(') {
- if (strncmp(data, "NAK", 3) == 0 || strncmp(data, " \r", 2) == 0) {
- printf("-------------------------------------------");
- printf("UPS protocol kestar!!!!!!!");
- printf("-------------------------------------------");
- sSettings.UPS_Setting.type_ups = ups_kestar;
- }
- else if(strncmp(data, "QPI", 3) == 0) {
- printf("-------------------------------------------");
- printf("UPS protocol ups_offline!!!!!!!");
- printf("-------------------------------------------");
- sSettings.UPS_Setting.type_ups = ups_offline;
- }
- return;
- }
- data++;
- printf("-------------------------------------------");
- printf("UPS protocol coltronic!!!!!!!");
- printf("-------------------------------------------");
- sSettings.UPS_Setting.type_ups = ups_voltronic;
- DBG printf("UPS ups_protocol_id_parser_start: %s\r\n", data);
- }
- void ups_buzzer_cntrl_response(char *data)
- {
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- if (strlen(data) != 0) {
- if (strncmp(data, "(ACK", 4) == 0) {
- flag_buzzer_on = true;
- }
- }
- DBG printf("UPS ups_buzzer_cntrl_parser_start: %s\r\n", data);
- }
- void ups_mode_response(char *data)
- {
- static char fault_status[50] = {0};
- char value[50];
- uint8_t len = 0;
- DBG printf("ups_serialresponse: %s\r\n", data);
- if (data[0] != '(') {
- return;
- }
- DBG printf("ups_serial_parser_start\r\n");
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- DBG printf("UPS ups_mode_parser_start: %s\r\n", data);
- #if 1
- // -----------------------------------------------------------------------
- // Тестовый
- // Если значение режима изменилось или режим "Fault"
- if ((UPS.Mode != data[0]) || (data[0] == 'F')) {
- UPS.Mode = data[0];
- #ifdef UPS_MODE_MONITOR
- memset(value, 0, sizeof(value));
- GetModeStr(value, &len);
- // В режиме "Fault" запрашиваем статус
- if (data[0] == 'F') {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- send_MegaTec_cmd(ups_fault_status);
- if (ups_megatec_rx_pdu()) {
- if (ups_pdu.data[0] != '(' || strncmp(ups_pdu.data, "(OK", 3) == 0) {
- return;
- }
- UPS.fault_type = (ups_pdu.data[1] - 0x30) << 4 | (ups_pdu.data[2] - 0x30);
- strncat(value, &ups_pdu.data[1], 6);
- strncat(value, &ups_pdu.data[14], 4);
- strncat(value, &ups_pdu.data[25], 8);
- strncat(value, &ups_pdu.data[35], 4);
- strncat(value, &ups_pdu.data[41], 4);
- strncat(value, &ups_pdu.data[47], 4);
- strncat(value, &ups_pdu.data[53], 4);
- strncat(value, &ups_pdu.data[59], 8);
- if ((strncmp(value, fault_status, sizeof(value)) != 0))
- {
- SNMP_SendUserTrap(UPS_MODE);
- syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
- log_event_data(LOG_UPS_MODE, value);
- }
- memcmp(fault_status, value, sizeof(value));
- return;
- }
- }
- SNMP_SendUserTrap(UPS_MODE);
- syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
- log_event_data(LOG_UPS_MODE, value);
- #endif
- }
- // -----------------------------------------------------------------------
- #endif
- #if 0 // Пока закроем
- if (UPS.Mode != data[0]) {
- UPS.Mode = data[0];
- #ifdef UPS_MODE_MONITOR
- memset(value, 0, sizeof(value));
- GetModeStr(value, &len);
- if(UPS.Mode == 'F') {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- send_MegaTec_cmd(ups_fault_status);
- if (ups_megatec_rx_pdu()) {
- if (ups_pdu.data[0] != '(' || strncmp(ups_pdu.data, "(OK", 3) == 0) {
- return;
- }
- UPS.fault_type = (ups_pdu.data[1] - 0x30) << 4 | (ups_pdu.data[2] - 0x30);
- strncat(value, &ups_pdu.data[1], 6);
- strncat(value, &ups_pdu.data[14], 4);
- strncat(value, &ups_pdu.data[25], 8);
- strncat(value, &ups_pdu.data[35], 4);
- strncat(value, &ups_pdu.data[41], 4);
- strncat(value, &ups_pdu.data[47], 4);
- strncat(value, &ups_pdu.data[53], 4);
- strncat(value, &ups_pdu.data[59], 8);
- }
- }
- SNMP_SendUserTrap(UPS_MODE);
- syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
- log_event_data(LOG_UPS_MODE, value);
- #endif
- }
- #endif
- }
- #define BIT_OFFSET(c, offset) ((c - 0x30) << offset)
- void ups_warn_status_response(char *data)
- {
- if (data[0] != '(') {
- return;
- }
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- sync_data = false;
- UPS.warn_status = BIT_OFFSET(data[0], 0) + BIT_OFFSET(data[2], 1) + BIT_OFFSET(data[5], 2) + BIT_OFFSET(data[6], 3)
- + BIT_OFFSET(data[7], 4) + BIT_OFFSET(data[8], 5) + BIT_OFFSET(data[12], 6) + BIT_OFFSET(data[13], 7) + BIT_OFFSET(data[10], 8);
- }
- void ups_get_ack(char *data)
- {
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- if (strlen(data) != 0) {
- if (strncmp(data, "(ACK", 4) == 0) {
- return;
- }
- }
- DBG printf("UPS ups_buzzer_cntrl_parser_start: %s\r\n", data);
- }
- void ups_qflag_status_get(char *data)
- {
- char *ptr1, *ptr2;
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- if (strlen(data) != 0) {
- ptr1 = strchr(data, 'l');
- ptr2 = strchr(data, 'D');
- if (ptr1 != NULL && ptr2 != NULL) {
- if (ptr2 < ptr1) {
- QFLAG_L = true;
- }
- }
- }
- }
- void ups_kstar_status_response(char *data)
- {
- char value[20];
- uint8_t len;
- if (data[0] != '#') {
- return;
- }
- DBG printf("ups_kstar_status_response: %s\r\n", data);
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data++;
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- UPS.work_time = atoi(value);
- }
- /* len = get_ups_param(data, "\r", value);
- data += (len + 1);
- if (len > 0) {
- UPS.warn_status = value[3] - 0x30;
- }
- */
- }
- void ups_temperature_response(char *data)
- {
- char value[20];
- uint8_t len = 0;
- float temp = 0;
- if (data[0] != '(') {
- return;
- }
- UPS.Present = UPS_CONNECTED;
- UPS.Flag_Present = true;
- UPS.cnt_err_ups = 0;
- data += 13;
- // Вернул получение значения UPS.Temp в команду "ups_status_response"
- #if 0
- len = get_ups_param(data, " ", value);
- data += (len + 1);
- if (len > 0) {
- temp = atof(value);
- if (temp == 0 && UPS.temp_stm32 < 0) {
- UPS.Temp = UPS.temp_stm32;
- } else {
- UPS.Temp = temp;
- }
- }
- #endif
- }
- void ups_megatec_process_pdu(cmdMegaTecEnums_t command)
- {
- switch (command) {
- case ups_status_req:
- case ups_offline_status_req:
- ups_status_response(ups_pdu.data);
- break;
- case ups_info:
- ups_info_response(ups_pdu.data);
- break;
- case ups_rating_info:
- break;
- case ups_remain_time_reg:
- ups_remain_time_response(ups_pdu.data);
- break;
- case ups_general_status_req:
- ups_general_status_response(ups_pdu.data);
- break;
- case ups_akb_info:
- ups_akb_info_response(ups_pdu.data);
- break;
- case ups_model_req:
- ups_model_response(ups_pdu.data);
- break;
- case ups_version_req:
- ups_version_response(ups_pdu.data);
- break;
- case ups_version2_req:
- ups_version_part2_response(ups_pdu.data);
- break;
- case ups_serial_req:
- ups_serial_response(ups_pdu.data);
- break;
- case ups_protocol_id_req:
- ups_protocol_id_response(ups_pdu.data);
- break;
- case ups_buzzer_cntrl:
- ups_buzzer_cntrl_response(ups_pdu.data);
- break;
- case ups_mode_req:
- ups_mode_response(ups_pdu.data);
- break;
- case ups_warning_status:
- ups_warn_status_response(ups_pdu.data);
- break;
- case ups_bypass_hvolt_264_set:
- case ups_cmd_enable_flag_l:
- ups_get_ack(ups_pdu.data);
- break;
- case ups_qflag_status:
- ups_qflag_status_get(ups_pdu.data);
- break;
- case ups_kstar_status:
- ups_kstar_status_response(ups_pdu.data);
- break;
- case ups_temperature:
- ups_temperature_response(ups_pdu.data);
- break;
- default:
- break;
- }
- }
- int ups_metac_service_pdu(cmdMegaTecEnums_t command)
- {
- uint8_t UPS_Status_prev;
- if (UPS.Present == UPS_CONNECTED) {
- while (!megatec_send) {
- vTaskDelay(50);
- }
- UPS_Status_prev = UPS.Status;
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- if (command == ups_shutdown) {//sSettings.UPS_Setting.type_ups == ups_offline &&
- TimeParam2 = 1;
- xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.1)*60*configTICK_RATE_HZ, 0);
- xTimerStart(UPSRestoreTimer, 0);
- send_MegaTec_cmd(ups_shutdown_restore);
- } else {
- if((UPS.Status >> 2) & 0x01) {
- if (command == ups_cancel_test) {
- test_time = 100;
- }
- } else {
- if (command == ups_test_low_bat) {
- test_time = 100;
- } else {
- test_time = (int)TimeParam;
- }
- }
- send_MegaTec_cmd(command);
- }
- if (ups_megatec_rx_pdu()) {
- megatec_send = true;
- if (strncmp(ups_pdu.data, "ACK", 3) == 0 || strncmp(ups_pdu.data, "(ACK", 4) == 0) {
- if (command == ups_shutdown) {
- xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
- xTimerStart(UPSRestoreTimer, 0);
- } else if(command == ups_cancel_shut_down) {
- xTimerStop(UPSRestoreTimer, 0);
- } else if (command == ups_test_time) {
- init_var_for_testing();
- }
- return 1;
- } else if (strncmp(ups_pdu.data, "NAK", 3) == 0 || strncmp(ups_pdu.data, "(NAK", 4) == 0) {
- return 0;
- }
- }
- vTaskDelay(1500);
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- UPS.Flag_Present = false;
- send_MegaTec_cmd(ups_status_req);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(ups_status_req);
- }
- megatec_send = true;
- if (command == ups_shutdown) {
- if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
- && ((UPS.Status >> 1) & 0x01)) {
- xTimerChangePeriod(UPSRestoreTimer, (TimeParamFloat + 0.3)*60*configTICK_RATE_HZ, 0);
- xTimerStart(UPSRestoreTimer, 0);
- return 1;
- }
- } else if (command == ups_cancel_shut_down) {
- if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
- && !((UPS.Status >> 1) & 0x01)) {
- xTimerStop(UPSRestoreTimer, 0);
- return 1;
- }
- } else if (command == ups_cancel_test) {
- if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
- && !((UPS.Status >> 2) & 0x01)) {
- return 1;
- }
- } else {
- if ((((UPS.Status >> 2) & 0x01) != ((UPS_Status_prev >> 2) & 0x01))
- && ((UPS.Status >> 2) & 0x01)) {
- init_var_for_testing();
- return 1;
- }
- if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))
- && ((UPS.Status >> 1) & 0x01)) {
- return 1;
- }
- }
- megatec_send = true;
- }
- #if HARDWARE_BT6721
- if (command == ups_shutdown) {
- return 1;
- }
- #endif
- return -1;
- }
- void kstar_mode(void)
- {
- uint8_t mode = UPS.Mode;
- static uint32_t fault_mode_cnt = 0;
- if((UPS.Status >> 4) & 0x01) {
- UPS.Mode = 'F';
- } else if(!((UPS.Status >> 4) & 0x01) && UPS.VAC_out == 0) {
- UPS.Mode = 'D';
- } else if(((UPS.Status >> 5) & 0x01) && UPS.VAC_out > 120 && UPS.VAC_in != 0) {
- UPS.Mode = 'Y';
- } else if(((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
- UPS.Mode = 'B';
- } else if(!((UPS.Status >> 7) & 0x01) && UPS.VAC_out > 190) {
- UPS.Mode = 'L';
- } else if((UPS.Status >> 2) & 0x01) {
- UPS.Mode = 'T';
- }
- // Добавилл отправку команды "C", т.к. ИБП
- // самостоятельно не выходит из состояния Fault.
- if (UPS.Mode == 'F') {
- fault_mode_cnt++;
- }
- else {
- fault_mode_cnt = 0;
- }
- if (fault_mode_cnt == UPS_CANSEL_DELAY) {
- send_MegaTec_cmd(ups_cancel_shut_down);
- fault_mode_cnt = 0;
- }
- if (UPS.Mode != mode) {
- #ifdef UPS_MODE_MONITOR
- char value[20];
- uint8_t len = 0;
- memset(value, 0, sizeof(value));
- GetModeStr(value, &len);
- SNMP_SendUserTrap(UPS_MODE);
- syslog(SYSLOG_INFORMATIONAL, "Режим ИБП: %s", value);
- log_event_data(LOG_UPS_MODE, value);
- #endif
- }
- }
- // TODO ����������� ���������
- //void request_task(void)
- void request_task(void *params)
- {
- uint8_t kestar_req[4] = { ups_status_req, ups_remain_time_reg, ups_info, ups_kstar_status};
- uint8_t voltronic_req[9] = {ups_temperature, ups_status_req, ups_akb_info, ups_model_req, ups_mode_req, ups_version_req, ups_version2_req, ups_serial_req, ups_warning_status};//
- uint8_t num_req = 0;
- uint8_t *req;
- bool flag_init_ups = false;
- ups_megatec_rx_pdu();
- for (;;) {
- if (UPS.Present != UPS_FAIL_CONNECT) {
- if (UPS.Flag_Present == false) {
- if (UPS.cnt_err_ups != 2) {
- UPS.cnt_err_ups++;
- } else {
- UPS.Freq_in = 0;
- UPS.VAC_in = 0;
- UPS.VAC_out = 0;
- UPS.Temp = 0;
- UPS.Load = 0;
- UPS.Load_test_akb = 0;
- UPS.SOC = 0;
- UPS.SOC_prev = 0;
- UPS.work_time = 0;
- UPS.Status = 0;
- UPS.Mode = 0;
- UPS.Test_Status = 0;
- UPS.Alarm = 0;
- UPS.warn_status = 0;
- UPS.fault_type = 0;
- UPS.Present = UPS_FAIL_CONNECT;
- //memset(UPS.model, 0, 11);
- memset(UPS.vertion, 0, sizeof(UPS.vertion));
- init_ups_rbuf();
- flag_init_ups = false;
- }
- }
- }
- //printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");
- //printf("megatec_send: %u\r\n", megatec_send);
- if (megatec_send) {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- UPS.Flag_Present = false;
- send_MegaTec_cmd(ups_protocol_id_req);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(ups_protocol_id_req);
- }
- megatec_send = true;
- }
- switch (sSettings.UPS_Setting.type_ups) {
- case ups_kestar:
- case ups_offline:
- num_req = sizeof(kestar_req);
- req = kestar_req;
- break;
- case ups_voltronic:
- num_req = sizeof(voltronic_req);
- req = voltronic_req;
- #ifdef UPS_BUZZER_OFF
- if (!flag_buzzer_on) {
- if (megatec_send) {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- UPS.Flag_Present = false;
- send_MegaTec_cmd(ups_buzzer_cntrl);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(ups_buzzer_cntrl);
- }
- megatec_send = true;
- }
- }
- #endif
- break;
- default:
- num_req = sizeof(kestar_req);
- req = kestar_req;
- break;
- }
- if(!flag_init_ups) {
- flag_init_ups = true;
- if(sSettings.UPS_Setting.type_ups == ups_voltronic) {
- if (megatec_send) {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- UPS.Flag_Present = false;
- send_MegaTec_cmd(ups_bypass_hvolt_264_set);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(ups_bypass_hvolt_264_set);
- }
- megatec_send = true;
- }
- if (megatec_send) {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- UPS.Flag_Present = false;
- send_MegaTec_cmd(ups_qflag_status);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(ups_qflag_status);
- }
- megatec_send = true;
- }
- if (QFLAG_L) {
- if (megatec_send) {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- UPS.Flag_Present = false;
- send_MegaTec_cmd(ups_cmd_enable_flag_l);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(ups_cmd_enable_flag_l);
- }
- megatec_send = true;
- }
- }
- } else if( sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline){
- uint8_t specific_req_sn[7] = {0x01, 0x06, 0x95, 0x00, 0x0f, 0x49, 0x60};
- vTaskDelay(1000);
- ups_send_block(specific_req_sn, 7);
- if (ups_additional_prtcl_rx_pdu()) {
- ups_specific_response_sn(ups_pdu.data);
- }
- }
- #ifdef CHECK_LOAD_ENABLE
- xTimerStart(AutoTestTimer, 0);
- xTimerChangePeriod(AutoTestTimer, 60*configTICK_RATE_HZ, 0);
- #endif
- }
-
- vTaskDelay(1000);
- for (uint8_t i = 0; i < num_req; i++) {
- if (megatec_send) {
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- UPS.Flag_Present = false;
- // printf("~~~~~~~~~~ set UPS.Flag_Present false\r\n");
- send_MegaTec_cmd(req[i]);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(req[i]);
- }
- megatec_send = true;
- // printf("~~~~~~~~~~~~~~~ Current UPS.Flag_Present: %u\r\n", (uint8_t)UPS.Flag_Present);
- }
- }
-
- // printf("UPS.Flag_Present: %u\r\n", (uint8_t)UPS.Flag_Present);
- // printf("UPS_Setting.type_ups: %u\r\n", sSettings.UPS_Setting.type_ups);
- if(UPS.Present == UPS_CONNECTED && (sSettings.UPS_Setting.type_ups == ups_kestar
- || sSettings.UPS_Setting.type_ups == ups_offline)) {
- kstar_mode();
- }
- }
- }
- bool get_sync_data(void)
- {
- return sync_data;
- }
- void akb_time_work_task(void *params)
- {
- static uint32_t tick_prev = 0;
- static uint32_t cnt_sec = 0;
- for (;;) {
- if (UPS.Present == UPS_CONNECTED && ((UPS.Status >> 7) & 0x01)) {
- if (tick_prev == 0) {
- tick_prev = xTaskGetTickCount();
- }
- UPS.akb_work_time += (xTaskGetTickCount() - tick_prev) / 1000;
- tick_prev = xTaskGetTickCount();
- } else {
- tick_prev = 0;
- UPS.akb_work_time = 0;
- }
- if(flag_WPHVR2K0L) {
- cnt_sec ++;
- if (cnt_sec == 3600) {
- cnt_sec = 0;
- int res = ups_metac_service_pdu(ups_test_10sec);
- }
- }
- vTaskDelay(1000);
- }
- }
- void UPSRestoreCallback(TimerHandle_t pxTimer)
- {
- if (UPS.Present == UPS_CONNECTED) {
- while (!megatec_send) {
- vTaskDelay(50);
- }
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- //UPS.Flag_Present = false;
- #ifdef HARDWARE_BT6721
- send_MegaTec_cmd(ups_cancel_shut_down);
- #else
- send_MegaTec_cmd(ups_remote_turn_on);
- #endif
- if (ups_megatec_rx_pdu()) {
- #ifndef HARDWARE_BT6721
- ups_megatec_process_pdu(ups_remote_turn_on);
- #endif
- }
- megatec_send = true;
- }
- }
- void UPScmd(cmdMegaTecEnums_t cmd)
- {
- if (UPS.Present == UPS_CONNECTED) {
- while (!megatec_send) {
- vTaskDelay(50);
- }
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- //UPS.Flag_Present = false;
- send_MegaTec_cmd(cmd);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(cmd);
- }
- megatec_send = true;
- }
- }
- void UPSReadTestStatus(void)
- {
- if(sSettings.UPS_Setting.type_ups == ups_kestar
- || sSettings.UPS_Setting.type_ups == ups_offline) {
- return;
- }
- if (UPS.Present == UPS_CONNECTED) {
- while (!megatec_send) {
- vTaskDelay(50);
- }
- memset(ups_pdu.data, 0, UPS_PDU_MAX_LEN);
- megatec_send = false;
- //UPS.Flag_Present = false;
- send_MegaTec_cmd(ups_general_status_req);
- if (ups_megatec_rx_pdu()) {
- ups_megatec_process_pdu(ups_general_status_req);
- }
- megatec_send = true;
- }
- }
- void AutoTestCallback(void)
- {
- static bool first_start = false;
- if (!first_start) {
- xTimerChangePeriod(AutoTestTimer, configTICK_RATE_HZ*PERIOD_AUTO_TEST, 0);
- }
- if(!((UPS.Status >> 2) & 0x01)) {
- // printf("Test command!!!!!!!!!!!!!!!!");
- int res = ups_metac_service_pdu(ups_test_10sec);
- set_act_source(AUTO_ACT);
- }
- first_start = true;
- }
- void ups_megatec_init(void)
- {
- init_UPS_value();
- xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 3, NULL, tskIDLE_PRIORITY, NULL);
- xTaskCreate(akb_time_work_task, ( char * ) "akb_time_work_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY,
- NULL);
- UPSRestoreTimer = xTimerCreate("UPSRestoreTmr", configTICK_RATE_HZ*30, pdFALSE, ( void * ) 0, UPSRestoreCallback);
- #ifdef CHECK_LOAD_ENABLE
- AutoTestTimer = xTimerCreate("AutoTestTmr", configTICK_RATE_HZ*PERIOD_AUTO_TEST, pdTRUE, ( void * ) 0, AutoTestCallback);
- #endif
- }
|