| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105 | /* * 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"#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;/**  * @brief  Общая структура настроек  */extern SETTINGS_t sSettings;#define UPS_PDU_MAX_LEN 80float 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;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",    "T\r",    "TL\r",    "T",    "Q\r",    "S",    "R",    "C\r",    "CT\r",    "I\r",    "F\r",    "Q2\r",    "QGS\r",    "QBV\r",    "QMD\r",    "QVFW\r",    "QVFW2\r",    "QID\r",    "QPI\r",    "QS\r",    "M\r",    "PDA\r",    "QMOD\r",    "SON\r",};extern bool flUpdateLog;void init_UPS_value(void){    UPS.Freq_in = 0;    UPS.VAC_in = 0;    UPS.VAC_out = 0;    UPS.Temp = 0;    UPS.Load = 0;    UPS.SOC = 0;    UPS.work_time = 0;    UPS.akb_work_time = 0;    UPS.Status = 0;    UPS.Mode = 0;    UPS.Alarm = 0;    UPS.Test_Status = 0;    UPS.cnt_err_ups = 0;    UPS.Flag_Present = false;    UPS.Present = false;    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);} 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;    }    DBG printf("UPS raw data: %s\r\n", ups_pdu.data);    DBG 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;    DBG printf("ups_status_response: %s\r\n", data);    if (data[0] != '(') {        return;    }    DBG printf("ups_status_parser_start\r\n");    UPS.Present = true;    UPS.Flag_Present = true;    UPS.cnt_err_ups = 0;#ifdef UPS_BUZZER_OFF        if (flag_buzzer_on) {            data[45] = '0';        }#endif    if (flUpdateLog) {        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);    }    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);    }    if (sSettings.UPS_Setting.type_ups == ups_kestar || sSettings.UPS_Setting.type_ups == ups_offline) {        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));        }    }    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 = 0; i < len; i ++) {            stat |= (value[i] - 0x30) << (7 - i);        }        UPS.Status = stat;    }}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 = true;    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_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 = true;    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);    strncpy(UPS.serial, data, 8);    data += 8;    strncpy(UPS.vertion, data, 2);    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 = true;    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 = true;    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 = 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 = true;    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) {            strcpy(UPS.model, "RTMP-II");        } else {            len = strlen(value);            strncpy(UPS.model, value, len);        }        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 = true;    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 = true;    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 = true;    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){    DBG printf("ups_protocol_id_response: %s\r\n", data);    UPS.Present = true;    UPS.Flag_Present = true;    UPS.cnt_err_ups = 0;    if (data[0] != '(') {        if (strncmp(data, "NAK", 3) == 0 || strncmp(data, " \r", 2) == 0) {            sSettings.UPS_Setting.type_ups = ups_kestar;        }        else if(strncmp(data, "QPI", 3) == 0) {            sSettings.UPS_Setting.type_ups = ups_offline;        }        return;    }    data++;    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 = true;    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){    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 = true;    UPS.Flag_Present = true;    UPS.cnt_err_ups = 0;    data++;    DBG printf("UPS ups_mode_parser_start: %s\r\n", data);#ifdef UPS_MODE_MONITOR    if (UPS.Mode != data[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    UPS.Mode = data[0]; }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;        default:            break;    }}int ups_metac_service_pdu(cmdMegaTecEnums_t command){    uint8_t UPS_Status_prev;    if (UPS.Present) {        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;            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;            }        }        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)) {                return 1;            }        } else if (command == ups_cancel_shut_down) {            if ((((UPS.Status >> 1) & 0x01) != ((UPS_Status_prev >> 1) & 0x01))             && !((UPS.Status >> 1) & 0x01)) {                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;            }        }        megatec_send = true;    }    return -1;}void kstar_mode(void){    uint8_t mode = UPS.Mode;    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';    }#ifdef UPS_MODE_MONITOR    if (UPS.Mode != mode) {        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[3] = { ups_status_req, ups_remain_time_reg, ups_info};    uint8_t voltronic_req[7] = {ups_status_req, ups_akb_info, ups_model_req, ups_version_req, ups_version2_req, ups_serial_req, ups_mode_req};//    uint8_t num_req = 0;    uint8_t *req;    ups_megatec_rx_pdu();    for (;;) {        if (UPS.Present == true) {            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.SOC = 0;                    UPS.work_time = 0;                    UPS.Status = 0;                    UPS.Mode = 0;                    UPS.Test_Status = 0;                    UPS.Alarm = 0;                    UPS.Present = false;                    //memset(UPS.model, 0, 11);                    memset(UPS.vertion, 0, sizeof(UPS.vertion));                    init_ups_rbuf();                }            }        }        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;        }        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;                send_MegaTec_cmd(req[i]);                if (ups_megatec_rx_pdu()) {                    ups_megatec_process_pdu(req[i]);                }                megatec_send = true;            }        }        if(sSettings.UPS_Setting.type_ups == ups_kestar             || sSettings.UPS_Setting.type_ups == ups_offline) {                kstar_mode();        }    }}void akb_time_work_task(void *params){    static uint32_t tick_prev = 0;    for (;;) {        if (UPS.Present && ((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;        }        vTaskDelay(1000);    }}void UPSRestoreCallback(TimerHandle_t pxTimer){    if (UPS.Present) {        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_remote_turn_on);        if (ups_megatec_rx_pdu()) {            ups_megatec_process_pdu(ups_remote_turn_on);        }        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) {        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 ups_megatec_init(void){    init_UPS_value();    UPS.Present = true;    xTaskCreate(request_task, ( char * ) "request_task", configMINIMAL_STACK_SIZE * 2, 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);}
 |