from device_conf import class_comm_device_config
import comm_remap as remap
import menu_utils as uitls
from enum import Enum
from datetime import datetime
import time
import menu_utils as utils
import json
import re
from enum import Enum
from comm_modbus_device import class_modbus_comm_device

now_time = datetime.now()
current_time = now_time.strftime("(%y/%m/%d)")
VERSION_DATE = current_time

class StrInfo(Enum):
    STR_MAX_VALUE       = 0,                                      #"最大值"
    STR_MIN_VALUE       = 1,                                      #"最小值"
    STR_SET_VALUE       = 2,                                      #"设定值"
    STR_VALUE_EXCEED    = 3,                                   #"设定值越界"
    STR_ZERO_PHASE      = 4,                                     #"有功相位"
    STR_INPUT_PASSWORD  = 5,                                 #"请输入系统密码
    STR_SYSTEM_PASSWORD = 6,                                #"系统密码"
    STR_PASSWORD_ERR    = 7,                                   #"密码错误"
    STR_SHORT_PHASE_MODIFY  =8,                             #"短路倍数\n恢复缺省"
    STR_SWITCH_TYPE_MODIFY  =9,                             #"漏电延时\n恢复缺省"
    STR_SOFT_VERSION        =10,                                   #"软件版本"
    STR_NO_RECORD           =11,                                      #"无记录"
    STR_ENTER_TO_CLEAR      =12,                                 #"确定清除"
    STR_ESC_TO_CANCEL       =13,                                  #"ESC取消"
    STR_CLEAR_FINISH        =14,                                   #"清除成功"
    STR_ESC_TO_EXIT         =15,                                    #"ESC退出
    STR_INPUT_NEW_PASSWORD  =16,                             #"请设置新密码"
    STR_NOT_POSSIBLE_TO_MODIFY =17,                         #"不可修改"
    STR_CAPACITOR_MEASURE      =18,                              #"电容检测
    STR_CAPACITOR_EQUAL         =19,                                #"电容"
    STR_LEEKAGE_MEASURE         =20,                                #"检测中.."
    STR_TEST_ACTIVE             =21,                                    #"测试状态
    STR_POWER_ON=22,                                       #"合闸"
    STR_POWER_OFF=23,                                      #"分闸"
    STR_FAIL=24,                                           #"失效"
    STR_NORMAL=25,                                         #"正常"
    STR_MEASURE_CURRENT=26,                                #"实测电流"
    STR_RATE_CURRENT=27,                                   #"额定电流"
    STR_ANGLE=28,                                          #"角度"
    STR_AVERAGE_CURRENT=29,                                #"平均电流"
    STR_MAX_CURRENT=30,                                    #"最大电流
    STR_LOW_VOLTAGE_RATE=31,                               #"欠压值
    STR_MEASURE_VOLTAGE=32,                                #"实测电压"
    STR_ZERO_CURRENT=33,                                   #"零序电流"
    STR_ZERO_VOLTAGE=34,                                   #"零序电压"
    STR_RECORD=35,                                         #"记录"
    STR_LEEAKGE_LOCK_VALUE=36,                             #"闭锁设定值"
    STR_LEEAKGE_ACTIVE_VALUE=37,                           #"动作设定值"
    STR_MEASURE_RESISTOR=38,                               #"电阻测量值"
    STR_FRONT_CIRCUIT=39,                                  #"前机"
    STR_NEXT_CIRCUIT=40,                                   #"后机"
    STR_RATE_VOLTAGE=41,                                   #"额定电压"
    STR_PARA_MODIFY_ERROR=42,                              #"参数修改失败"
    STR_LEEKAGE_RES_FINISH=43,                             #"闭锁电阻检测完"
    STR_LEEKAGE_RES_MEASURE=44,                            #"闭锁电阻检测中"
    STR_SFJ_MONITOR=45,                                    #"双风机磁力监控"
    STR_CL_MONITOR=46,                                     #"磁力监控系统"
    STR_CL_DB_MONITOR=47,                                  #"双回路磁力监控"
    STR_YB_MONITOR=48,                                     #"移变低压侧"
    STR_KUI_DIAN_MONITOR=49,                               #"馈电开关监控"
    STR_TYPE=50,                                           #"类型:"
    STR_TOP_SWITCH=51,                                     #"总开关"
    STR_BOTTOM_SWITCH=52,                                  #"分开关"
    STR_COS_V=53,                                          #"COS(ΦaΦbΦc)"
    STR_POWER_VI=54,                                       #"有功功率"
    STR_POWER_VI_T=55,                                     #"电量"
    STR_FRONT_POWER_ON=56,                                 #"前机合"
    STR_NEXT_POWER_ON=57,                                  #"后机合"
    STR_FRONT_POWER_OFF=58,                                #"前机分"
    STR_NEXT_POWER_OFF=59,                                 #"后机分"
    STR_FRONT_RESISTOR=60,                                 #"前机电阻:"
    STR_NEXT_RESISTOR=61,                                  #"后机电阻:"
    STR_WORK_MODE=62,                                      #"模式:"
    STR_LEEKAGE_RES=63,                                    #"漏电电阻"
    STR_REVERSE_VOLTAGE=64,                                #"反电动势\n注意安全"
    STR_LIGHT=65,                                          #"照明"
    STR_SIGNAL=66,                                         #"信号"
    STR_SWITCH_STATUS=67,                                  #"开关状态"
    STR_POWER_ON_COUNTS=68,                                #"合闸次数"
    STR_SETTING=69,                                        #"设定值"
    STR_POW_U=70,                                          #"无功功率"
    STR_TEST_FAIL=71,                                      #"测试不正确"
    STR_LEAKAGE_TEST_FAIL=72,                              #"漏电测试不正确"
    STR_DETECT_FAIL_WHEN_POWER_ON=73,                      #"合闸时不能检测"
    STR_LINK=74,                                           #"联机系统"
    STR_SINGLE=75,                                         #"单机系统"
    STR_STATUS=76,                                         #"状态"
    STR_MAIN_FIRST=77,                                     #"主机优先"
    STR_MAIN_BAK_SAME=78,                                  #"主辅对等"
    STR_MAIN_BAK_SWITCH=79,                                #"主辅轮动"
    STR_SWITCH_DISABLE=80,                                 #"局扇模式"
    STR_MAIN=81,                                           #"主机"
    STR_BACKUP=82,                                         #"备机"
    STR_SINGLE_CIRCUIT_FRONT=83,                           #"单前机"
    STR_SINGLE_CIRCUIT_NEXT=84,                            #"单后机"
    STR_DOUBLE_CIRCUIT=85,                                 #"双回路"
    STR_WATER_PUMP_ON=86,                                  #"水泵已开阀"
    STR_WATER_PUMP_OFF=87,                                 #"水泵已关阀"
    STR_SWITCH_FAIL=88,                                    #"粘连"
    STR_SWITCH_THROUGH_2=89,                               #"击穿"
    STR_GB_MONITOR=90,                                     #"高压真空配电装置"
    STR_GB_PROTECT_SYSTEM=91,                              #"综合保护监控系统"
    STR_YBGY_MONITOR=92,                                   #"移变高压侧"
    STR_ERROR=93,                                          #"故障"
    STR_TEMPERATURE=94,                                    #"温度"
    STR_NEXT_CURRENT=95,                                   #"后机电流"
    STR_FRONT_CURRENT=96,                                  #"前机电流"
    STR_FJ_WS_LOCK=97,                                     #"风机处瓦斯闭锁"
    STR_GZM_WS_LOCK=98,                                    #"工作面瓦斯闭锁"
    STR_CURRENT=99,                                        #"电流"
    STR_VOLTAGE=100,                                        #"电压"
    STR_MONITOR_RES=101,                                    #"监视电阻"
    STR_SWITCH_THROUGH_SIMPLE=102,                          #"击穿"
    STR_LINK_FAIL=103,                                      #"联机失败"
    STR_RES_OFF=104,                                        #"漏电临界值"
    STR_ANOTHER_REPAIR=105,                                 #"另台维修请勿停机"
    STR_WS_INTENSITY=106,                                   #"瓦斯浓度"
    STR_HIGH_VOLT=107,                                      #"高压"
    STR_AUTO=108,                                           #"自动"
    STR_MANUAL=109,                                         #"手动"
    STR_FREQUENCY=110,                                      #"频率"
    STR_FWD_ON=111,                                         #正向启动
    STR_RVS_ON=112,                                         #反向启动
    STR_TEMPERATURE_UNIT=113,                               #温度单位
    STR_POWER_LOSS=114, 
    STR_CALIBRATE_SUCCESS=115,                                #标定成功
    STR_ISOLATE_RES=116,                                    #绝缘电阻
    STR_WATER_MONITOR=117,                                  #水位控制排水监控系统
    STR_SWTICH_PROTECTOR=118,                               #ZDB-200电动机保护器
    STR_INDEPENDENT=119,                                    #独立模式
    STR_DEPENDENT=120,                                      #联动模式
    STR_OUT_POSITION=121,
    STR_IN_POSITION=122,
    STR_QUIT_IN_POSITION=123,
    STR_RUN_DATA=124,
    STR_WATER_MODE=125,
    STR_INV_MODE=126,
    STR_ISOLATE_RESISTOR_LOW=127,
    STR_SWITCH=128,
    STR_DIDAO=129,
    STR_REMOTE=130,
    STR_LOCAL=131,
    STR_ONE_KEY_LOCK=132,
    STR_COMM_FAIL=133,

class Alarm(Enum):
    ERR_NONE = 0,
    ERR_CREEPAGE_LOCK = 1,                              #漏电报警
    ERR_VOLTAGE_LOW = 2,                                #欠压报警
    ERR_OVER_CURRENT = 3,                               #过流保护
    ERR_SHORT_CURRENT = 4,                              #短路保护
    ERR_CREEPAGE_SELECT = 5,                            #选漏
    ERR_UNBANLANCE_CURRENT = 6,                         #三相不平衡
    ERR_FLASH_FAIL = 7,                                 #FLASH无法写入
    ERR_CREEPAGE_ACTIVE = 8,                            #漏电动作
    ERR_BREAK_PHASE = 9,                                #断相保护动作
    ERR_VOLTAGE_BREAK_PHASE = 10,                       #电压断相        
    ERR_GAS_LOCK = 11,                                  #瓦斯闭锁
    ERR_WIND_MOTOR_LOCK = 12,                           #风电闭锁
    ERR_VOLTAGE_OVER = 13,                              #过压报警
    ERR_SHORT_PHASE = 14,                               #相敏短路
    ERR_WATCH_DOG = 15,                                 #看门狗保房
    ERR_SWITCH_FAIL = 16,                               #开关粘轿
    ERR_SWITCH_THROUGH = 17,                            #开关击?
    ERR_SWITCH_ON_FAIL = 18,                            #开关合闸失?
    ERR_COMM_FAIL = 19,                                 #通讯故障
    ERR_CREEPAGE_SELECT2 = 20,                          #选择性报?
    ERR_TEMP_IO_HIGH = 21,                              #IO口温度过驿
    ERR_SHORT_CURRENT_SIGNAL = 22,                      #信号电流短路
    ERR_REMOTE_OFF = 23,                                #远方分励
    ERR_REPAIR = 24,                                    #维修状?
    ERR_TEMP_ALARM = 25,                                #温度报警
    ERR_QUICK_CREEPAGE_ACT = 26,                        #快速漏电动仿
    ERR_GB_LEEK_SHORT = 27,                             #高爆绝缘短路
    ERR_GB_LEEK_BREAK = 28,                             #高爆绝缘断路
    ERR_YB_LOW_VOLTAGE_FAIL = 29,                       #移变低压侧失?
    ERR_YB_LOW_VOLTAGE_UNREADY = 30,                    #移变低压侧未准备?
    ERR_WS_FREQ_FAIL = 31,                              #瓦斯浓度超标
    ERR_HIGH_LOW_LOCK = 32,                             #高低压联钿
    ERR_MOTOR_ROTATE_LOCK = 33,                         #电机堵转
    ERR_FIX_TIME_OVER_CURRENT = 34,                     #定时限过泿
    ERR_FIX_TIME_SHORT_CURRENT = 35,                    #定时限短?
    ERR_PHASE_ERROR = 36,                               #相序故障
    ERR_UNDER_LOAD = 37,                                #欠载保护
    ERR_PHASE_UNMATCH = 38,                             #互感器反?
    ERR_EXTERNAL_ALARM = 39,                            #外部故障
    ERR_SENSOR_LOSS = 40,                               #水位传感器未?
    ERR_ROTOR_SLOW = 41,                                 
    ERR_ROTOR_LOCK = 42, 
    ERR_POWER_LOSS = 43,                                 #系统断电
    ERR_RSV1 = 44,
    ERR_RSV2 = 45,
    ERR_RSV3 = 46,
    ERR_RSV4 = 47,
    ERR_RSV5 = 48,
    ERR_RSV6 = 49,
    ERR_RSV7 = 50,
    ERR_USER_ALARM1 = 51,                                #50,用户自定义故阿
    ERR_USER_ALARM2 = 52,                                #51,用户自定义故阿
    ERR_USER_ALARM3 = 53,                                #52,用户自定义故阿
    ERR_USER_ALARM4 = 54,                                #53,用户自定义故阿
    ERR_GB_LIGHT_THREAD_SON_LOSS = 55,
    ERR_GB_LIGHT_THREAD_BROTHER_LOSS = 56,
    ERR_GB_LIGHT_MODULE_LOSS = 57,
    ERR_GB_FORCE_LEEK_OFF = 58,
    ERR_GB_FORCE_SHORT_OFF = 59,
    ERR_GB_CREEPAGE_SELECT = 60,                        #选择性报警
    ERR_SWITCH_OUT_POSITION = 61,        
    ERR_DZM_CREEPAGE_ACTIVE = 62,
    ERR_OIL_PRESSURE_SENSOR_FAIL = 63,
    ERR_FREQ_SENSOR_EXCEED = 64,
    ERR_FREQ_SENSOR_FAIL = 65,
    ERR_ANALOG_SENSOR_EXCEED = 66,
    ERR_ANALOG_SENSOR_FAIL = 67,
    ERR_VOLTAGE_LOSS = 68,
    ERR_SPEED_LOSS = 69,
    ERR_GB_ZERO_VOLT_OVER = 70,                         #零序过压
    ERR_PT_BREAK = 71,
    ERR_CT_BREAK = 72,
    ERR_ZERO_OVER_VOLTAGE = 73,
    ERR_ZERO_OVER_CURRENT = 74,
    ERR_GAS_WARNING = 75,     
    ERR_TEMP_WARNING = 76,
    ERR_COMM_LOSS = 77,
    ERR_CAR_FAIL = 78,
    ERR_WATER_LOW = 79,
    ERR_SWITCH_DIDAO_IN_POSITION = 80,
    ERR_SWITCH_DIDAO_MID_POSITION = 81,
    ERR_GB_CREEPAGE_SELECT_5 = 82,                     #5次谐波选漏
    ERR_FLUX_LOCK = 83,
    ERR_GLOBAL_OIL_TEMP_LOCK = 84,
    ERR_GLOBAL_WATER_PRESSURE_LOCK = 85,
    ERR_GLOBAL_WATER_LEVEL_LOCK = 86,
    ERR_GLOBAL_CURRENT_SENSOR_LOSS = 87,
    ERR_GLOBAL_GAS_PERCENTAGE_LOCK = 88,
    ERR_GLOBAL_GAS_SENSOR_LOSS = 89,
    ERR_MEASURE_OVER_CURRENT = 90,                    #测量回路过流保护
    ERR_CREAPGE_BREAK = 91,
    ERR_REMOTE_CREAPGE_TEST_FAIL = 92,
    ERR_REMOTE_CREAPGE_TEST_SUCCESS = 93,



class Alias(Enum):
    alias_alarm = 0,
    alias_str = 1,
    alias_company = 2,
    alias_bool = 3,
    alias_voltage = 4,
    alias_parity = 5,
    alias_baud = 6,
    alias_input = 7,
    alias_onoff = 8,
    alias_test = 9,
    alias_can_baud = 10,
    alias_measure_v = 11,
    alias_switch_type = 12,
    alias_work_mode = 13,
    alias_us_phase = 14,
    alias_sensor = 15,
    alias_table = 16,
    alias_remote = 17,
    alias_current = 18,
    alias_eth = 19,
    alias_lcd = 20,
    alias_dev = 21,
    alias_fyj = 22,
    alias_alarm_type = 23,
    alias_car_position = 24,
    
class Menu(Enum):
    menu_current = 0,
    menu_test = 1,
    
alias_table = [
    {Alias.alias_alarm: {Alarm.ERR_NONE: "无故障",
                     Alarm.ERR_CREEPAGE_LOCK: "照明闭锁",
                     Alarm.ERR_VOLTAGE_LOW:   "欠压",
                     Alarm.ERR_OVER_CURRENT : "照明过载",                               #过流保护
                     Alarm.ERR_SHORT_CURRENT : "短路保护",                              #短路保护
                     Alarm.ERR_CREEPAGE_SELECT : "选漏",                            #选漏
                     Alarm.ERR_UNBANLANCE_CURRENT : "三相不平衡",                         #三相不平衡报?
                     Alarm.ERR_FLASH_FAIL : "FLASH无法写入",                                 #FLASH无法写入
                     Alarm.ERR_CREEPAGE_ACTIVE : "漏电动作",                            #漏电动作
                     Alarm.ERR_BREAK_PHASE : "照明电流断相",                                #断相保护动作
                     Alarm.ERR_VOLTAGE_BREAK_PHASE : "电压断相",                       #电压断相        
                     Alarm.ERR_GAS_LOCK : "瓦斯闭锁",                                  #瓦斯闭锁
                     Alarm.ERR_WIND_MOTOR_LOCK : "风电闭锁",                           #风电闭锁
                     Alarm.ERR_VOLTAGE_OVER : "过压",                              #过压报警
                     Alarm.ERR_SHORT_PHASE : "相敏短路",                               #相敏短路
                     Alarm.ERR_WATCH_DOG : "看门狗重启",                                 #看门狗保房
                     Alarm.ERR_SWITCH_FAIL : "真空管粘连",                               #真空管粘连
                     Alarm.ERR_SWITCH_THROUGH : "真空管击穿",                            #真空管击穿
                     Alarm.ERR_SWITCH_ON_FAIL : "合闸失败",                            #合闸失败
                     Alarm.ERR_COMM_FAIL : "通讯失败",                                 #通讯失败
                     Alarm.ERR_CREEPAGE_SELECT2 : "选  漏",                          #选  漏
                     Alarm.ERR_TEMP_IO_HIGH : "温度过高",                              #温度过高
                     Alarm.ERR_SHORT_CURRENT_SIGNAL : "信号电流短路",                      #信号电流短路
                     Alarm.ERR_REMOTE_OFF : "远方分励",                                #远方分励
                     Alarm.ERR_REPAIR : "维修状态",                                    #维修状态
                     Alarm.ERR_TEMP_ALARM : "温度报警",                                #温度报警
                     Alarm.ERR_QUICK_CREEPAGE_ACT : "漏电  动作",                       #快速漏电  动作
                     Alarm.ERR_GB_LEEK_SHORT : "绝缘短路",                             #绝缘短路
                     Alarm.ERR_GB_LEEK_BREAK : "绝缘断路",                             #绝缘断路
                     Alarm.ERR_YB_LOW_VOLTAGE_FAIL : "低压侧故障",                       #低压侧故障
                     Alarm.ERR_YB_LOW_VOLTAGE_UNREADY : "低压侧未准备好",                    #低压侧未准备好
                     Alarm.ERR_WS_FREQ_FAIL : "瓦斯浓度超标",                              #瓦斯浓度超标
                     Alarm.ERR_HIGH_LOW_LOCK : "高低压联锁",                             #高低压联锁
                     Alarm.ERR_MOTOR_ROTATE_LOCK : "电机堵转",                         #电机堵转
                     Alarm.ERR_FIX_TIME_OVER_CURRENT : "定时限过流",                     #定时限过流
                     Alarm.ERR_FIX_TIME_SHORT_CURRENT : "定时限短路",                    #定时限短路?
                     Alarm.ERR_PHASE_ERROR : "相序故障",                               #相序故障
                     Alarm.ERR_UNDER_LOAD : "欠载保护",                                #欠载保护
                     Alarm.ERR_EXTERNAL_ALARM : "外部故障",                            #外部故障
                     Alarm.ERR_POWER_LOSS : "系统断电",                                 #系统断电
                     Alarm.ERR_USER_ALARM1 : "触头温度超标",                            #用户自定义故阿
                     Alarm.ERR_USER_ALARM2 : "触头温度超标",                            #用户自定义故阿
                     Alarm.ERR_GB_LIGHT_THREAD_SON_LOSS : "光纤子节点丢失",
                     Alarm.ERR_GB_LIGHT_THREAD_BROTHER_LOSS : "光纤兄弟节点丢失",
                     Alarm.ERR_GB_LIGHT_MODULE_LOSS : "无光纤模块",
                     Alarm.ERR_GB_FORCE_LEEK_OFF : "光纤漏电越级跳",
                     Alarm.ERR_GB_FORCE_SHORT_OFF : "光纤短路越级跳",
                     Alarm.ERR_GB_CREEPAGE_SELECT : "漏电",                            #选择性报警
                     Alarm.ERR_SWITCH_OUT_POSITION : "开关未推到位",        
                     Alarm.ERR_DZM_CREEPAGE_ACTIVE : "漏电动作",
                     Alarm.ERR_OIL_PRESSURE_SENSOR_FAIL : "油压传感器未接入",
                     Alarm.ERR_FREQ_SENSOR_EXCEED : "频率传感器超限",
                     Alarm.ERR_FREQ_SENSOR_FAIL : "频率传感器未接入",
                     Alarm.ERR_ANALOG_SENSOR_EXCEED : "模拟量传感器超限",
                     Alarm.ERR_ANALOG_SENSOR_FAIL : "模拟量传感器未接入",
                     Alarm.ERR_VOLTAGE_LOSS : "失压故障",
                     Alarm.ERR_SPEED_LOSS : "失速故障",
                     Alarm.ERR_GB_ZERO_VOLT_OVER : "零序过压",
                     Alarm.ERR_PT_BREAK : "PT断线",
                     Alarm.ERR_CT_BREAK : "CT断线",
                     Alarm.ERR_ZERO_OVER_VOLTAGE : "零序过压",
                     Alarm.ERR_ZERO_OVER_CURRENT : "零序过流",
                     Alarm.ERR_GAS_WARNING : "轻瓦斯告警",     
                     Alarm.ERR_TEMP_WARNING : "温度异常",
                     Alarm.ERR_COMM_LOSS : "通讯中断",
                     Alarm.ERR_CAR_FAIL : "手车控制器故障",
                     Alarm.ERR_WATER_LOW : "循环泵无水故障",
                     Alarm.ERR_SWITCH_DIDAO_IN_POSITION: "地刀合闸保护中",
                     Alarm.ERR_SWITCH_DIDAO_MID_POSITION: "地刀未到位",
                     Alarm.ERR_GB_CREEPAGE_SELECT_5: "5次谐波选漏",
                     Alarm.ERR_FLUX_LOCK: "流量闭锁",
                     Alarm.ERR_GLOBAL_OIL_TEMP_LOCK: "油温闭锁",
                     Alarm.ERR_GLOBAL_WATER_PRESSURE_LOCK: "水压闭锁",  
                     Alarm.ERR_GLOBAL_WATER_LEVEL_LOCK: "液位闭锁",
                     Alarm.ERR_GLOBAL_CURRENT_SENSOR_LOSS: "互感器未接入",
                     Alarm.ERR_GLOBAL_GAS_PERCENTAGE_LOCK: "瓦斯浓度闭锁",
                     Alarm.ERR_GLOBAL_GAS_SENSOR_LOSS: "瓦斯断线",
                     Alarm.ERR_MEASURE_OVER_CURRENT: "测量回路过流",
                     Alarm.ERR_CREAPGE_BREAK: "接地开路",
                     Alarm.ERR_REMOTE_CREAPGE_TEST_FAIL: "漏电测试失败",
                     Alarm.ERR_REMOTE_CREAPGE_TEST_SUCCESS: "漏电测试成功",
                     }
    },
    
    {"alias_str" : { StrInfo.STR_MAX_VALUE :"最大值",
                    StrInfo.STR_MIN_VALUE :"最小值",
                    StrInfo.STR_SET_VALUE :"设定值",
                    StrInfo.STR_VALUE_EXCEED  :"设定值越界",
                    StrInfo.STR_ZERO_PHASE :"有功相位",
                    StrInfo.STR_INPUT_PASSWORD :"请输入系统密码",
                    StrInfo.STR_SYSTEM_PASSWORD :"系统密码",
                    StrInfo.STR_PASSWORD_ERR :"密码错误",
                    StrInfo.STR_SHORT_PHASE_MODIFY :"短路倍数\n恢复缺省",
                    StrInfo.STR_SWITCH_TYPE_MODIFY :"漏电延时\n恢复缺省",
                    StrInfo.STR_SOFT_VERSION:"软件版本",
                    StrInfo.STR_NO_RECORD  :"无记录",
                    StrInfo.STR_ENTER_TO_CLEAR:"确定清除",
                    StrInfo.STR_ESC_TO_CANCEL:"ESC取消",
                    StrInfo.STR_CLEAR_FINISH :"清除成功",
                    StrInfo.STR_ESC_TO_EXIT :"ESC退出",
                    StrInfo.STR_INPUT_NEW_PASSWORD :"请设置新密码",
                    StrInfo.STR_NOT_POSSIBLE_TO_MODIFY :"不可修改",
                    StrInfo.STR_CAPACITOR_MEASURE :"电容检测",
                    StrInfo.STR_CAPACITOR_EQUAL :"电容",
                    StrInfo.STR_LEEKAGE_MEASURE :"检测中..",
                    StrInfo.STR_TEST_ACTIVE :"测试状态",
                    StrInfo.STR_POWER_ON :"合闸",
                    StrInfo.STR_POWER_OFF :"分闸",
                    StrInfo.STR_FAIL :"失效",
                    StrInfo.STR_NORMAL  :"正常",
                    StrInfo.STR_MEASURE_CURRENT :"实测电流",
                    StrInfo.STR_RATE_CURRENT :"额定电流",
                    StrInfo.STR_ANGLE :"角度",
                    StrInfo.STR_AVERAGE_CURRENT :"平均电流",
                    StrInfo.STR_MAX_CURRENT :"最大电流",
                    StrInfo.STR_LOW_VOLTAGE_RATE :"欠压值",
                    StrInfo.STR_MEASURE_VOLTAGE :"实测电压",
                    StrInfo.STR_ZERO_CURRENT :"零序电流",
                    StrInfo.STR_ZERO_VOLTAGE :"零序电压",
                    StrInfo.STR_RECORD  :"记录",
                    StrInfo.STR_LEEAKGE_LOCK_VALUE  :"闭锁设定值",
                    StrInfo.STR_LEEAKGE_ACTIVE_VALUE  :"动作设定值",
                    StrInfo.STR_MEASURE_RESISTOR :"电阻测量值",
                    StrInfo.STR_FRONT_CIRCUIT  :"前机",
                    StrInfo.STR_NEXT_CIRCUIT :"后机",
                    StrInfo.STR_RATE_VOLTAGE :"额定电压",
                    StrInfo.STR_PARA_MODIFY_ERROR :"参数修改失败",
                    StrInfo.STR_LEEKAGE_RES_FINISH :"闭锁电阻检测完",
                    StrInfo.STR_LEEKAGE_RES_MEASURE :"闭锁电阻检测中",
                    StrInfo.STR_SFJ_MONITOR :"双风机磁力监控",
                    StrInfo.STR_CL_MONITOR :"磁力监控系统",
                    StrInfo.STR_CL_DB_MONITOR :"双回路磁力监控",
                    StrInfo.STR_YB_MONITOR :"移变低压侧",
                    StrInfo.STR_KUI_DIAN_MONITOR :"馈电开关监控",
                    StrInfo.STR_TYPE :"类型:",
                    StrInfo.STR_TOP_SWITCH :"总开关",
                    StrInfo.STR_BOTTOM_SWITCH :"分开关",
                    StrInfo.STR_COS_V  :"COS(ΦaΦbΦc)",
                    StrInfo.STR_POWER_VI :"有功功率",
                    StrInfo.STR_POWER_VI_T  :"电量",
                    StrInfo.STR_FRONT_POWER_ON :"前机合",
                    StrInfo.STR_NEXT_POWER_ON :"后机合",
                    StrInfo.STR_FRONT_POWER_OFF :"前机分",
                    StrInfo.STR_NEXT_POWER_OFF :"后机分",
                    StrInfo.STR_FRONT_RESISTOR :"前机电阻:",
                    StrInfo.STR_NEXT_RESISTOR :"后机电阻:",
                    StrInfo.STR_WORK_MODE :"模式:",
                    StrInfo.STR_LEEKAGE_RES :"漏电电阻",
                    StrInfo.STR_REVERSE_VOLTAGE :"反电动势\n注意安全",
                    StrInfo.STR_LIGHT :"照明",
                    StrInfo.STR_SIGNAL :"信号",
                    StrInfo.STR_SWITCH_STATUS :"开关状态",
                    StrInfo.STR_POWER_ON_COUNTS :"合闸次数",
                    StrInfo.STR_SETTING :"设定值",
                    StrInfo.STR_POW_U :"无功功率",
                    StrInfo.STR_TEST_FAIL :"测试不正确",
                    StrInfo.STR_LEAKAGE_TEST_FAIL :"漏电测试不正确",
                    StrInfo.STR_DETECT_FAIL_WHEN_POWER_ON :"合闸时不能检测",
                    StrInfo.STR_LINK :"联机系统",
                    StrInfo.STR_SINGLE :"单机系统",
                    StrInfo.STR_STATUS :"状态",
                    StrInfo.STR_MAIN_FIRST :"主机优先",
                    StrInfo.STR_MAIN_BAK_SAME :"主辅对等",
                    StrInfo.STR_MAIN_BAK_SWITCH :"主辅轮动",
                    StrInfo.STR_SWITCH_DISABLE :"局扇模式",
                    StrInfo.STR_MAIN :"主机",
                    StrInfo.STR_BACKUP :"备机",
                    StrInfo.STR_SINGLE_CIRCUIT_FRONT :"单前机",
                    StrInfo.STR_SINGLE_CIRCUIT_NEXT :"单后机",
                    StrInfo.STR_DOUBLE_CIRCUIT :"双回路",
                    StrInfo.STR_WATER_PUMP_ON :"水泵已开阀",
                    StrInfo.STR_WATER_PUMP_OFF :"水泵已关阀",
                    StrInfo.STR_SWITCH_FAIL :"粘连",
                    StrInfo.STR_SWITCH_THROUGH_2 :"击穿",
                    StrInfo.STR_GB_MONITOR :"高压真空配电装置",
                    StrInfo.STR_GB_PROTECT_SYSTEM :"综合保护监控系统",
                    StrInfo.STR_YBGY_MONITOR :"移变高压侧",
                    StrInfo.STR_ERROR :"故障",
                    StrInfo.STR_TEMPERATURE:"温度",
                    StrInfo.STR_NEXT_CURRENT :"后机电流",
                    StrInfo.STR_FRONT_CURRENT :"前机电流",
                    StrInfo.STR_FJ_WS_LOCK :"风机处瓦斯闭锁",
                    StrInfo.STR_GZM_WS_LOCK :"工作面瓦斯闭锁",
                    StrInfo.STR_CURRENT :"电流",
                    StrInfo.STR_VOLTAGE:"电压",
                    StrInfo.STR_MONITOR_RES :"监视电阻",
                    StrInfo.STR_SWITCH_THROUGH_SIMPLE :"击穿",
                    StrInfo.STR_LINK_FAIL :"联机失败",
                    StrInfo.STR_RES_OFF :"漏电临界值",
                    StrInfo.STR_ANOTHER_REPAIR :"另台维修请勿停机",
                    StrInfo.STR_WS_INTENSITY :"瓦斯浓度",
                    StrInfo.STR_HIGH_VOLT :"高压",
                    StrInfo.STR_AUTO :"自动",
                    StrInfo.STR_MANUAL :"手动",
                    StrInfo.STR_FREQUENCY :"频率",
                    StrInfo.STR_FWD_ON  :"正向启动",
                    StrInfo.STR_RVS_ON  :"反向启动",
                    StrInfo.STR_TEMPERATURE_UNIT : "℃",
                    StrInfo.STR_POWER_LOSS :"系统断电",
                    StrInfo.STR_CALIBRATE_SUCCESS :"标定成功",
                    StrInfo.STR_ISOLATE_RES :"绝缘电阻",
                    StrInfo.STR_WATER_MONITOR  :"水位控制排水监控系统",
                    StrInfo.STR_SWTICH_PROTECTOR  :"ZDB-200电动机保护器",
                    StrInfo.STR_INDEPENDENT  :"独立模式",
                    StrInfo.STR_DEPENDENT :"联动模式",    
                    StrInfo.STR_OUT_POSITION : "未到位",
                    StrInfo.STR_IN_POSITION : "进到位",
                    StrInfo.STR_QUIT_IN_POSITION : "退到位",
                    StrInfo.STR_RUN_DATA : "运行数据",
                    StrInfo.STR_WATER_MODE : "水位",
                    StrInfo.STR_INV_MODE : "可逆",
                    StrInfo.STR_ISOLATE_RESISTOR_LOW : "绝缘电阻降低",
                    StrInfo.STR_SWITCH : "开关",
                    StrInfo.STR_DIDAO : "地刀",
                    StrInfo.STR_REMOTE : "程控",
                    StrInfo.STR_LOCAL : "就地",
                    StrInfo.STR_ONE_KEY_LOCK : "闭锁中",
                    StrInfo.STR_COMM_FAIL : "通讯失败",
                   }},
    
    {"alias_company": {0: "浙江志展智能科技",
                       1: "华荣科技",
                      }
    },
    {"alias_sensor" :    {0: "10 A", 1: "30 A", 2: "50 A", 3: "75 A", 4: "80 A", 5: "100 A",
                         6: "120 A", 7: "150 A", 8: "200 A", 9: "300 A", 10: "315 A", 11: "400 A",
                         12: "500 A", 13: "600 A", 14: "630 A", 15: "800 A", 16: "1000 A", 17: "1250 A",
                         18: "1300 A", 19: "1600 A", 20: "160 A", 21: "250 A", 22: "750 A", 23: "1200 A",
                         24: "1500 A", 25: "2000 A", 26: "2500 A" ,27: "4000 A", 28:"5 A"}
    },
    {"alias_switch_type" :  {0: "总开关", 1: "分开关"}},
    {"alias_measure_v":  {0: "正电压", 1: "负电压"}}, 
    {"alias_bool": {0:"关闭", 1:"打开"}},
    {"alias_voltage": {0:"380V", 1:"660V", 2:"1140V", 3:"3300V", 4:"100V", 5:"127V", 6:"220V", 7:"440V"}},
    {"alias_parity": {0: "无校验", 1: "奇校验", 2: "偶校验"}},
    {"alias_baud": {0: "1200", 1: "2400", 2: "4800", 3: "9600", 4:"19200", 5:"38400", 6:"51200", 7:"115200"}},
    {"alias_input": {0: "常开", 1: "常闭"}},
    {"alias_onoff": {0: "分闸", 1: "合闸"}},
    {"alias_test":  {0: "模拟测试", 1: "继电器测试"}},
    {"alias_can_baud":  {0: "1000K", 1: "800K", 2: "500K", 3: "250K", 4: "125K", 5: "100K", 6: "50K", 7: "20K", 8: "10K", 9: "AUTO"}},
    {Alias.alias_measure_v:  {0: "正电压", 1: "负电压"}},
    {Alias.alias_switch_type :  {0: "总开关", 1: "分开关"}},
    {"alias_work_mode":    {0: "水位模式", 1: "磁力模式", 2: "正反转模式", 3: "联控模式"}},
    {"alias_us_phase":    {0: "Uab_ABC", 1: "Uab_ACB", 2: "Ubc_ABC", 3: "Ubc_ACB", 4: "Uca_ABC", 5: "Uca_ACB"}}, 
    {Alias.alias_sensor :    {0: "10 A", 1: "30 A", 2: "50 A", 3: "75 A", 4: "80 A", 5: "100 A",
                         6: "120 A", 7: "150 A", 8: "200 A", 9: "300 A", 10: "315 A", 11: "400 A",
                         12: "500 A", 13: "600 A", 14: "630 A", 15: "800 A", 16: "1000 A", 17: "1250 A",
                         18: "1300 A", 19: "1600 A", 20: "160 A", 21: "250 A", 22: "750 A", 23: "1200 A",
                         24: "2500 A", 25: "5 A"}},    
    {"alias_table":    {0: "0", 1: "1", 2: "2", 3: "3"}}, 
    {"alias_remote":   {0: "就地", 1: "程控"}},
    {"alias_current":   {0: "三相测量", 1: "两相平均", 2: "两相测量"}},
    
    {Alias.alias_eth : {0: "无", 1: "一代以太网", 2: "二代以太网"}},
    {Alias.alias_lcd : {0: "一体迪文屏", 1: "分体迪文屏", 2: "大彩屏", 3 : "昆仑通态屏"}},
    {Alias.alias_fyj : {0: "无", 1: "FYJ", 2: "FYJ2", 3 : "FYJ4"}}, 
    {Alias.alias_dev : {0: "馈电", 1: "高爆", 2: "移变", 3 : "双风机", 4 : "磁力", 
                        5 : "双磁力", 6 : "照明", 7 : "移变高压侧", 8: "双高爆",
                        9: "组合馈电", 10: "双照明", 11 : "ZZ630"}},
    {"alias_alarm_type":   {0: "试验", 1: "动作"}},
    {"alias_car_position":   {0: "小车未到位", 1 : "小车试验位", 2: "小车工作位", 3 : "位置信号异常"}},  
]

MENU_PROPERTY_READ_ONLY = 0
MENU_PROPERTY_USER      = 1
MENU_PROPERTY_ENGINEER  = 2
MENU_PROPERTY_WRITE_ONLY  = 3


def get_alias_value_str(alias_str, alias_value) :
    alias_value_msg = None
    compare_value   = int(alias_value)
    
    for alias_dict in alias_table :                                                         
        for alias_name, value_dict in alias_dict.items():                                    
            if isinstance(alias_name, Enum):
                alias_name = alias_name.name
            
            if alias_name == alias_str :
                for each_value_dict in value_dict.items():
                    if isinstance(each_value_dict[0], Enum) :
                        key_value = each_value_dict[0].value[0]
                    else :
                        key_value = each_value_dict[0]
                    key_string = each_value_dict[1]
                    
                    if int(key_value) == compare_value :
                        alias_value_msg = key_string
                        return alias_value_msg
    return alias_value_msg

def func_adjust_current_menu_item(device : class_modbus_comm_device, menu_item) :
    #获得互感器类型, 根据互感器类型来调整 scale, format, 让电流可以正确显示与设置
    sensor_type = device.comm_get_value_from_mqtt_name("Sensor")
    sensor_current = 400
    
    if sensor_type != None :
        value_str = get_alias_value_str("alias_sensor", sensor_type)
        if value_str != None :
            value_str_no_unit = value_str.replace("A", "")
            sensor_current    = int(value_str_no_unit)

    #80A 以下电流互感器, 显示一位小数点
    if sensor_current <= 80 : 
        new_item    = menu_item
        new_item["scale"] = 0.1
        new_item["format"] = "%1.1f"
        return new_item
    else :
        return menu_item

# def func_caculate_Is(device : class_modbus_comm_device, menu_item, mqtt_dict : dict = None) :
#     format_str = utils.dict_or_object_get_attr(menu_item, "format", "%4.2f")
#     try :
#         Ia = device.comm_get_value_from_mqtt_name("Ia")
#         Ib = device.comm_get_value_from_mqtt_name("Ib")
#         Ic = device.comm_get_value_from_mqtt_name("Ic")
#         Is = (Ia + Ib + Ic) / 3
#     except Exception as e :
#         Is = 0.00
#     Is_str = format_str%(Is)
#     mqtt_dict["Is"] = Is_str

#视频触发
def func_video_trigger(device : class_modbus_comm_device, menu_item, mqtt_dict : dict = None) :
    value = device.comm_get_value_from_menu_item(menu_item)
    if value == None :
        #通讯未建立
        return
    
    value = round(value)
    in_position  = (value >> 10) & 0x1
    out_position = (value >> 9) & 0x1
    now_time     = time.time()
    if hasattr(device, "MqttVideoActive") == False:   
        #动态添加视频触发所需的变量
        device.MqttVideoActive           = False
        device.MqttVideoBaseTime         = now_time
        device.DI_SwitchInPosition   = in_position
        device.DI_SwitchQuitPosition = out_position

    if in_position != device.DI_SwitchInPosition or out_position != device.DI_SwitchQuitPosition:
        #开关进到位与 开关退到位信号, 当这两个状态发生变化时, 触发20秒的视频
        device.MqttVideoActive     = True
        device.MqttVideoBaseTime   = now_time
        mqtt_dict["VideoActive"] = "1"
    if device.MqttVideoActive :
        #视频延时20后主动关闭视频显示
        if now_time - device.MqttVideoBaseTime > 20.0 : 
            mqtt_dict["VideoActive"] = "0"
            device.MqttVideoActive = False

    device.DI_SwitchInPosition   = in_position
    device.DI_SwitchQuitPosition = out_position


COLOR_RED      = "#EE2D2D"                                                                                                              
COLOR_GREEN    = "#83BF6E"      

def func_process_car_message(device : class_modbus_comm_device, menu_item, mqtt_dict : dict = None) :
    car_pos_value = device.comm_get_value_from_menu_item(menu_item)
    if car_pos_value == None or mqtt_dict == None:
        return
    car_msg = get_alias_value_str("alias_car_position", car_pos_value)
    if car_msg != None :
        mqtt_dict["CarPositionMsg"] = car_msg

def func_process_alarm_message(device : class_modbus_comm_device, menu_item, mqtt_dict : dict = None) :
    alarm_id = device.comm_get_value_from_menu_item(menu_item)
    if alarm_id == None or mqtt_dict == None:
        #通讯未建立
        return
    alarm_msg = get_alias_value_str("alias_alarm", alarm_id)
    if alarm_msg != None :
        alarm_msg_dict = {}
        if alarm_id > 0 :
            alarm_msg_dict["value"] = alarm_msg
            alarm_msg_dict["color"] = COLOR_RED
        else :
            alarm_msg_dict["value"] = alarm_msg
            alarm_msg_dict["color"] = COLOR_GREEN

        mqtt_dict["AlarmMessage"] = alarm_msg_dict

mqtt_video_trigger = [
    {
        "name": "视频触发",
        "addr": "8015",
        "call" : func_video_trigger,
    },
]

mqtt_time_update = [
    {
        "name": "Year",
        "addr": "2300",
        "format" : "%04.0f",
        "mqtt" : "Year",
    },
    {
        "name": "Month",
        "addr": "2301",
        "format" : "%02.0f",
        "mqtt" : "Month",
    },
    {
        "name": "Day",
        "addr": "2302",
        "format" : "%02.0f",
        "mqtt" : "Day",
    },
    {
        "name": "Hour",
        "addr": "2303",
        "format" : "%02.0f",
        "mqtt" : "Hour",
    },
    {
        "name": "Minute",
        "addr": "2304",
        "format" : "%02.0f",
        "mqtt" : "Minute",
    },
    {
        "name": "Second",
        "addr": "2305",
        "format" : "%02.0f",
        "mqtt" : "Second",
    },
    {
        "name": "SetupTime",
        "addr": "2305",
        "mqtt" : "SetupTime",
        "action" : "SetupTime",
    },
]

mqtt_pack_param = [
    #电流参数
    {
        "name": "额定电流",
        "unit": "A",
        "addr": "8200",
        "scale" : 1.0,
        "format": "%04.0f",
        "action": "rate_current",
        "mqtt" : "RateCurrent",
        "adjust" : func_adjust_current_menu_item,
    },
    {
        "name": "短路倍数",
        "unit": "Ie",
        "scale" : 0.01,
        "format": "%05.2f",
        "addr": "8201",
        "min" : 1.00, 
        "max" : 10.00,
        "mqtt" : "ShortLevel",
    },
    {
        "name": "短路延时",
        "unit": "ms",
        "scale" : 1.0,
        "format": "%03.0f",
        "addr": "8198",
        "min" : 0.0, 
        "max" : 300.0,
        "mqtt" : "ShortDelay",
    },
    {
        "name": "信号Ie",
        "unit": "A",
        "addr": "8600",
        "scale" : 0.01,
        "format": "%05.2f",
        "mqtt" : "CurrentIs",
    },
    {
        "name": "信号短路倍数",
        "unit": "Ie",
        "scale" : 0.01,
        "format": "%05.2f",
        "addr": "8601",
        "mqtt" : "ShortIs",
    },
    {
        "name": "Is 校正",
        "unit": "%",
        "scale" : 0.1,
        "format": "%04.2f",
        "addr": "8602",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectIs",
    },
    {
        "name": "互感器类型",
        "alias" :"alias_sensor",
        "addr": "8207",
        "mqtt" : "Sensor",
    },
    {
        "name": "B相电流",
        "alias": "alias_current",
        "addr": "8244",
        "mqtt" : "CurrentB" 
    },
    {
        "name": "不平衡延时",
        "unit": "s",
        "addr": "8197", 
        "scale" : 0.1,
        "format": "%04.1f",
        "mqtt" : "UnbalanceDelay",
    },
    {
        "name": "不平衡范围",
        "unit": "%",
        "addr": "8213", 
        "scale" : 1.0,
        "format": "%03.0f",
        "min" : 10,
        "max" : 99,
        "mqtt" : "UnbalanceLevel",
    },
    {
        "name": "定时限过流",
        "unit": "Ie",
        "addr": "8239", 
        "scale" : 0.01,
        "format": "%04.2f",
        "mqtt" : "TimeOvercurrent",
    },
    {
        "name": "定时限过流延时",
        "unit": "ms",
        "addr": "8240", 
        "scale" : 1,
        "format": "%05.0f",
        "mqtt" : "TimeOvercurrentDelay",
    },
    {
        "name": "Ia 校正",
        "unit": "%",
        "scale" : 0.1,
        "format": "%05.1f",
        "addr": "8217",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectIa",
    },
    {
        "name": "Ib 校正",
        "unit": "%",
        "scale" : 0.1,
        "format": "%05.1f",
        "addr": "8218",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectIb",
    },
    {
        "name": "Ic 校正",
        "unit": "%",
        "scale" : 0.1,
        "format": "%05.1f",
        "addr": "8219",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectIc",
    },
    #电压参数
    {
        "name": "额定电压",
        "addr": "8211",
        "alias": "alias_voltage",
        "mqtt" : "RateVoltage",
    },
    {
        "name": "欠压延时",
        "unit": "s",
        "addr": "8177", 
        "scale" : 0.01,
        "format": "%05.2f",
        "min" : 0.1,
        "max" : 60.0,
        "mqtt" : "DelayLowVoltage",
    },
    {
        "name": "欠压比值",
        "unit": "%",
        "addr": "8202", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 10.0, 
        "max" : 100.0,
        "mqtt" : "PercentLowVoltage",
    },
    {
        "name": "过压延时",
        "unit": "s",
        "addr": "8176", 
        "scale" : 0.01,
        "format": "%05.2f",
        "min" : 0.1,
        "max" : 60.0,
        "mqtt" : "DelayHighVoltage",
    },
    {
        "name": "过压比值",
        "unit": "%",
        "addr": "8224", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 100.0, 
        "max" : 200.0,
        "mqtt" : "PercentHighVoltage",
    },
    {
        "name": "Ua校正",
        "unit": "%",
        "addr": "8220", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectUa",
    },
    {
        "name": "Ub校正",
        "unit": "%",
        "addr": "8221", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectUb",
    },
    {
        "name": "Uc校正",
        "unit": "%",
        "addr": "8222", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectUc",
    },
    {
        "name": "Us校正",
        "unit": "%",
        "addr": "8214", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        "mqtt" : "CorrectUs",
    },


    #漏电参数
    {
        "name": "闭锁电阻",
        "addr": "8204",
        "unit": "K",
        "scale" : 0.1,
        "format" : "%05.1f",
        "mqtt" : "BlockResistor"
    },
    {
        "name": "串联电阻",
        "addr": "8236",
        "unit": "K",
        "scale" : 0.1,
        "format" : "%05.1f",
        "mqtt" : "SeriesResistor"
    },
    {
        "name": "漏电延时",
        "addr": "8205",
        "unit": "ms",
        "scale" : 1.0,
        "format" : "%03.0f",
        "mqtt" : "LeakageDelay",
    },
    #保护参数投入
    {
        "name": "短路保护",
        "alias": "alias_bool",
        "addr": "8192.1", 
        "mqtt" : "ProtectShort",
    },
    {
        "name": "过载保护",
        "alias": "alias_bool",
        "addr": "8192.3", 
        "mqtt" : "ProtectOverLoad",
    },
    {
        "name": "电流断相保护",
        "alias": "alias_bool",
        "addr": "8192.4", 
        "mqtt" : "ProtectCurrentPhaseOff",
    },
    {
        "name": "不平衡保护",
        "alias": "alias_bool",
        "addr": "8192.2", 
        "mqtt" : "ProtectImbalance",
    },
    {
        "name": "漏电闭锁保护",
        "alias": "alias_bool",
        "addr": "8192.10", 
        "mqtt" : "ProtectLeakage"
    },
     {
        "name": "定时限过载保护",
        "alias": "alias_bool",
        "addr": "8192.11", 
        "mqtt" : "ProtectTimeLimitOverload"
    },
     {
        "name": "定时限短路保护",
        "alias": "alias_bool",
        "addr": "8192.12", 
        "mqtt" : "ProtectTimeLimitShortCircuit",
    },
    {
        "name": "信号短路保护",
        "alias": "alias_bool",
        "addr": "8175.15", 
        "mqtt" : "ProtectIsShort",
    },
    {
        "name": "欠压保护",
        "alias": "alias_bool",
        "addr": "8174.1", 
        "mqtt" : "ProtectUnderVoltage",
    },
    {
        "name": "过压保护",
        "alias": "alias_bool",
        "addr": "8174.0", 
        "mqtt" : "ProtectOverVoltage",
    },
    {
        "name": "电压断相保护",
        "alias": "alias_bool",
        "addr": "8174.2", 
        "mqtt" : "ProtectVoltagePhaseOff",
    },
    {
        "name": "漏电动作保护",
        "alias": "alias_bool",
        "addr": "8174.6", 
        "mqtt" : "ProtectLeakageAction",
    },
    {
        "name": "温度报警保护",
        "alias": "alias_bool",
        "addr": "8174.8", 
        "mqtt" : "ProtectTemperatureAlarm",
    },
    #系统设置
    {
        "name": "瓦斯触点",
        "addr": "8190",
        "alias" : "alias_input", 
        "mqtt" : "Gas",
    },
    {
        "name": "瓦斯延时",
        "addr": "8191",
        "scale" : 0.01,
        "format": "%06.2f",
        "min" : 0.00, 
        "max" : 60.00, 
        "mqtt" : "GasDelay",
    },
    {
        "name": "瓦斯浓度",
        "addr": "8237",
        "scale" : 0.01,
        "format": "%05.2f",
        "mqtt" : "GasConcentration",
    },
    {
        "name": "风电触点",
        "addr": "8248",
        "alias" : "alias_input", 
        "mqtt" : "Fan",
    },
    {
        "name": "风电延时",
        "addr": "8234",
        "scale" : 0.01,
        "format": "%06.2f",
        "min" : 0.00, 
        "max" : 60.00, 
        "mqtt" : "FanDelay",
    },
    {
        "name": "远近控模式",
        "addr": "8558",
        "alias" : "alias_remote", 
        "mqtt" : "FarAndNearMode",
    },
    {
        "name": "维修模式",
        "addr": "8565",
        "alias" : "alias_bool",
        "mqtt" : "MaintenanceMode",
    },
    {
        "name": "小车报警延时",
        "addr": "8576",
        "scale" : 0.1,
        "format": "%05.1f", 
        "mqtt" : "CarAlarmDelay",
    },
    {
        "name": "温度设定",
        "addr": "8423",
        "scale" : 1,
        "format": "%03.0f",
        "mqtt" : "Temperature",
    },
    #通讯设置
    {
        "name": "485地址",
        "addr": "8212", 
        "format": "%03.0f",
        "mqtt" : "485Address", 
    },
    {
        "name": "485波特率",
        "alias": "alias_baud",
        "addr": "8188",
        "mqtt" : "485Baud", 
    },
    {
        "name": "485校验",
        "alias": "alias_parity",
        "addr": "8194",
        "mqtt" : "485Parity",
    },
    {
        "name": "CAN地址",
        "format": "%03.0f",
        "addr": "8440",
        "mqtt" : "CANAddress",
    },
    {
        "name": "CAN波特率",
        "alias": "alias_can_baud",
        "addr": "8439",
        "mqtt" : "CANBaud",
    },
]

mqtt_pack_measure = [
    {
        "name": "SwitchStatus",
        "addr": "8162.0",
        "alias": Alias.alias_onoff,
        "remap_addr": remap.COMM_REMAP_MEASURE_SWITCH_STATUS,
        "measure" : True,
        "mqtt" : "SwitchStatus",
    },
    {
        "name": "AlarmId",
        "addr": "8166",
        "alias": Alias.alias_alarm,
        "remap_addr": remap.COMM_REMAP_MEASURE_SWITCH_ALARM,
        "measure" : True,
        "mqtt" : "AlarmId",
    },
    {
        "name":  "AlarmMessage",
        "addr":  "8166",
        "alias":  Alias.alias_alarm,
        "call" :  func_process_alarm_message,
    },
    {
        "name":  "CarPositionMsg",
        "addr":  "8015.10#2",
        "alias":  Alias.alias_car_position,
        "call" :  func_process_car_message,
    },
    {
        "name": "Ia",
        "unit": "A",
        "addr": "8156",
        "format": "%05.1f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_IA,
        "measure" : True,
        "mqtt" : "Ia",
        "adjust" : func_adjust_current_menu_item,
    },
    {
        "name": "Ib",
        "unit": "A",
        "addr": "8157",
        "format": "%05.1f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_IB,
        "measure" : True,
        "mqtt" : "Ib",
        "adjust" : func_adjust_current_menu_item,
    },
    {
        "name": "Ic",
        "unit": "A",
        "addr": "8158",
        "format": "%05.1f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_IC,
        "measure" : True,
        "mqtt" : "Ic",
        "adjust" : func_adjust_current_menu_item,
    },
    # {
    #     "name": "Is",
    #     "unit": "A",
    #     "addr": "8142",
    #     "format": "%05.2f",
    #     "measure" : True,
    #     "mqtt" : "Is",
    #     "adjust" : func_adjust_current_menu_item,
    # },
    {
        "name": "Voltage",
        "unit": "V",
        "addr": "8153",
        "format": "%05.0f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_UAB,
        "measure" : True,
        "mqtt" : "Voltage",
    },
    {
        "name": "Current",
        "unit": "A",
        "addr": "8156",
        "scale":0.1,
        "format": "%05.1f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_IA,
        "measure" : True,
        # "adjust" : func_adjust_current_menu_item,
        "mqtt" : "Current",
    },
    {
        "name": "Power",
        "unit": "kw",
        "addr": "8167",
        "format": "%07.0f",
        "measure" : True,
        "mqtt" : "Power",
    },
    {
        "name": "Electricity",
        "unit": "kwh",
        "addr": "8160#2",
        "format": "%07.0f",
        "measure" : True,
        "mqtt" : "Electricity",
    },
    {
        "name": "Uab",
        "unit": "V",
        "addr": "8153",
        "format": "%05.0f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_UAB,
        "measure" : True,
        "mqtt" : "Uab",
    },
    {
        "name": "Ubc",
        "unit": "V",
        "addr": "8154",
        "format": "%05.0f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_UBC,
        "measure" : True,
        "mqtt" : "Ubc",
    },
    {
        "name": "Uca",
        "unit": "V",
        "addr": "8155",
        "format": "%05.0f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_UCA,
        "measure" : True,
        "mqtt" : "Uca",
    },
    # {
    #     "name": "Uo",
    #     "unit": "V",
    #     "addr": "8152",
    #     "format": "%05.1f",
    #     "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_UO,
    #     "measure" : True,
    #     "mqtt" : "Uo",
    # }, 
    # {
    #     "name": "Io",
    #     "unit": "mA",
    #     "addr": "8151",
    #     "format": "%05.1f",
    #     "scale" : 1.0,
    #     "remap_addr" : remap.COMM_REMAP_MEASURE_FLOAT_IO,
    #     "measure" : True,
    #     "mqtt" : "Io",
    # }, 
    {
        "name": "Ro",
        "unit": "K",
        "addr": "8150",
        "scale": 0.1,
        "format": "%05.1f",
        "remap_addr": remap.COMM_REMAP_MEASURE_FLOAT_RO,
        "measure" : True,
        "mqtt" : "Ro",
    },
    {
        "name": "合闸次数",
        "addr": "8169", 
        "format": "%06.0f",
        "property": MENU_PROPERTY_READ_ONLY,
        "mqtt" : "OnCount",
    },

    # {
    #     "name": "Is",
    #     "addr": "8171",
    #     "scale": 0.01,
    #     "format": "%04.2f",
    #     "measure" : True,
    #     "call" : func_caculate_Is,
    # },
]
mqtt_pack_globalmeasure = [
    # {
    #     "name": "A相上触头",
    #     "addr": "8056#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "A_up",
    # },
    
    # {
    #     "name": "B相上触头",
    #     "addr": "8057#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "B_up",
    # },
    # {
    #     "name": "C相上触头",
    #     "addr": "8058#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "glboalmeasure" : True,
    #     "mqtt" : "C_up",
    # },
    # {
    #     "name": "A相下触头1",
    #     "addr": "8059#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "A1_down",
    # },
    # {
    #     "name": "B相下触头1",
    #     "addr": "8060#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "B1_down",
    # },
    # {
    #     "name": "C相下触头1",
    #     "addr": "8061#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "C1_down",
    # },
    # {
    #     "name": "A相下触头2",
    #     "addr": "8051#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "A2_down",
    # },
    
    # {
    #     "name": "B相下触头2",
    #     "addr": "8052#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "B2_down",
    # },
    # {
    #     "name": "C相下触头2",
    #     "addr": "8053#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "C2_down",
    # },
    # {
    #     "name": "A相下触头3",
    #     "addr": "8045#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "A3_down",
    # },
    # {
    #     "name": "B相下触头3",
    #     "addr": "8046#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "B3_down",
    # },
    # {
    #     "name": "C相下触头3",
    #     "addr": "8047#s",
    #     "scale": 0.1,
    #     "format": "%05.1f",
    #     "globalmeasure" : True,
    #     "mqtt" : "C3_down",
    # },
]

mqtt_pack_alarm = [
    {
        "name": "AlarmCode",
        "addr": "8121",
        "alias": "alias_alarm",
        "mqtt" : "AlarmCode",
    },
    {
        "name": "AlarmTest",
        "addr": "8138",
        "alias": "alias_alarm_type",
        "format" : "%1.0f",
        "mqtt" : "AlarmTest",
    },
    {
        "name": "Ua",
        "addr": "8122",
        "format" : "%1.0f",
        "mqtt" : "AlarmUa",
    },
    {
        "name": "Ub",
        "addr": "8123",
        "format" : "%1.0f",
        "mqtt" : "AlarmUb",
    },
    {
        "name": "Uc",
        "addr": "8124",
        "format" : "%1.0f",
        "mqtt" : "AlarmUc",
    },
    {
        "name": "Ia",
        "addr": "8125",
        "format" : "%1.0f",
        "mqtt": "AlarmIa",
        "adjust" : func_adjust_current_menu_item,
    },
    {
        "name": "Ib",
        "addr": "8126",
        "format" : "%1.0f",
        "mqtt": "AlarmIb",
        "adjust" : func_adjust_current_menu_item,
    },
    {
        "name": "Ic",
        "addr": "8127",
        "format" : "%1.0f",
        "mqtt": "AlarmIc",
        "adjust" : func_adjust_current_menu_item,
    },
    {
        "name": "Ro",
        "addr": "8128",
        "format" : "%5.1f",
        "scale" : 0.1,
        "mqtt": "AlarmRo",
    },
    {
        "name": "Is",
        "addr": "8142",
        "format": "%05.2f",
        "mqtt" : "AlarmIs",
        "adjust" : func_adjust_current_menu_item,
    },
    # {
    #     "name": "Io",
    #     "addr": "8129",
    #     "format" : "%1.1f",
    #     "scale" : 0.1,
    #     "mqtt": "AlarmIo",
    # },
    # {
    #     "name": "Uo",
    #     "addr": "8130",
    #     "format" : "%5.0f",
    #     "scale" : 1.0,
    #     "mqtt": "AlarmUo",
    # },
    {
        "name": "Year",
        "addr": "8131",
        "format" : "%4.0f",
        "mqtt": "AlarmYear",
    },
    {
        "name": "Month",
        "addr": "8132",
        "format" : "%2.0f",
        "mqtt": "AlarmMonth",
    },
    {
        "name": "Day",
        "addr": "8133",
        "format" : "%2.0f",
        "mqtt": "AlarmDay",
    },
    {
        "name": "Hour",
        "addr": "8134",
        "format" : "%2.0f",
        "mqtt": "AlarmHour",
    },
    {
        "name": "Minute",
        "addr": "8135",
        "format" : "%2.0f",
        "mqtt": "AlarmMinute",
    },
    {
        "name": "Second",
        "addr": "8136",
        "format" : "%2.0f",
        "mqtt": "AlarmSecond",
    },
]

menu_data = [
    {
        "name": "数据页面",
        "sub_menu": "menu_curve_physical",
    },
]

#曲线菜单
menu_curve_physical = [
    {
        "name": "物理曲线",
        "property" : MENU_PROPERTY_READ_ONLY,
        "action" : "Idle",
    },
]

#曲线菜单
menu_curve_adc = [
    {
        "name": "ADC曲线",
        "property" : MENU_PROPERTY_READ_ONLY,
        "action" : "Idle",
    },
]

#谐波菜单
menu_curve_harmonic = [
    {
        "name": "谐波曲线",
        "property" : MENU_PROPERTY_READ_ONLY,
        "action" : "Idle",
    },
]

modbus_comm_table = [
    {"name":"alarm",       "reg_addr":8120,   "reg_count":25,  "cycle": 0,       "mqtt_pack": mqtt_pack_alarm},
    {"name":"measure",     "reg_addr":8000, "reg_count":50, "cycle": 500,        "mqtt_pack": mqtt_video_trigger},
    {"name":"globalmeasure","reg_addr":8050, "reg_count":20, "cycle": 500,       "mqtt_pack": mqtt_pack_globalmeasure},
    {"name":"measure",     "reg_addr":8139, "reg_count":35, "cycle": 200,        "mqtt_pack": mqtt_pack_measure},
    {"name":"comm_cmd",    "reg_addr":100618, "reg_count":32, "cycle": 100},       
    #{"name":"key_group",   "reg_addr":1000,  "reg_count":18, "cycle": 20},
    # {"name":"para_group",  "reg_addr":2000, "reg_count":32, "cycle": 0},
    {"name":"time_group",  "reg_addr":2300, "reg_count":6,  "cycle": 1000,       "mqtt_pack": mqtt_time_update},
    {"name":"param/info",  "reg_addr":8174, "reg_count":96, "cycle": 3000,       "mqtt_pack" : mqtt_pack_param},
    {"name":"param/info",  "reg_addr":8180, "reg_count":60, "cycle": 3000},
    {"name":"param/info",  "reg_addr":8490, "reg_count":10, "cycle": 3000},
    {"name":"param/info",  "reg_addr":8520, "reg_count":20, "cycle": 3000},
    {"name":"param/info",  "reg_addr":8550, "reg_count":30, "cycle": 3000},
    {"name":"param/info",  "reg_addr":8435, "reg_count":10, "cycle": 3000},
    {"name":"param/info",  "reg_addr":8420, "reg_count":10, "cycle": 3000},
    {"name":"param/info",  "reg_addr":2000, "reg_count":32, "cycle": 3000},
    {"name":"param/info",  "reg_addr":8595, "reg_count":10, "cycle": 3000},

]

def func_comm_table_trigger_check(object_comm_table, comm_table_item) :
    if not hasattr(comm_table_item, "name"):
        return False
    return False

menu_current = [                                             #菜单表[menu_current],存储菜单项属性字典<>
    {
        "name": "额定电流",
        "unit": "A",
        "addr": "8200",
        "action": "rate_current",
        "adjust" : func_adjust_current_menu_item,
    },
    {
        "name": "短路倍数",
        "unit": "Ie",
        "scale" : 0.01,
        "format": "%05.2f",
        "addr": "8201",
        "min" : 1.0, 
        "max" : 10.0,
    },
    {
        "name": "短路延时",
        "unit": "ms",
        "scale" : 1.0,
        "format": "%03.0f",
        "addr": "8198",
        "min" : 0.0, 
        "max" : 200.0,
        #"mqtt" : "ShortDelay",
    },
    {
        "name": "互感器类型",
        "alias" : Alias.alias_sensor,
        "addr": "8207",
    },
     # {
    #     "name": "反时限表",
    #     "alias" :"alias_table",
    #     "addr": "8178",
    # },
     {
        "name": "不平衡值",
        "unit": "%",
        "addr": "8213", 
        "scale" : 1.0,
        "format": "%03.0f",
        "min" : 10.0,
        "max" : 100.0,
        #"mqtt" : "UnbalanceLevel",
    },
     {
        "name": "不平衡延时",
        "unit": "s",
        "addr": "8197", 
        "scale" : 0.1,
        "format": "%04.1f",
        #"mqtt" : "UnbalanceDelay",
    },
    {
        "name": "B相电流",
        "alias": "alias_current",
        "addr": "8244", 
    },
    {
        "name": "Ia 校正",
        "unit": "%",
        "scale" : 0.1,
        "format": "%05.1f",
        "addr": "8217",
        "min" : 60.0,
        "max" : 130.0,
        #"mqtt" : "CorrectIa",
    },
    {
        "name": "Ib 校正",
        "unit": "%",
        "scale" : 0.1,
        "format": "%05.1f",
        "addr": "8218",
        "min" : 60.0,
        "max" : 130.0,
        #"mqtt" : "CorrectIb",
    },
    {
        "name": "Ic 校正",
        "unit": "%",
        "scale" : 0.1,
        "format": "%05.1f",
        "addr": "8219",
        "min" : 60.0,
        "max" : 130.0,
        #"mqtt" : "CorrectIc",
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_voltage = [
    {
        "name": "额定电压",
        "addr": "8211",
        "alias": "alias_voltage",
        "default": 0,
    },
    {
        "name": "欠压比值",
        "unit": "%",
        "addr": "8202", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0, 
        "max" : 100.0,
        #"mqtt" : "PercentLowVoltage",
    },
    {
        "name": "欠压延时",
        "unit": "s",
        "addr": "8177", 
        "scale" : 0.01,
        "format": "%05.2f",
        "min" : 0.1,
        "max" : 60.0,
        #"mqtt" : "DelayLowVoltage",
    },
    {
        "name": "过压比值",
        "unit": "%",
        "addr": "8224", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 100.0, 
        "max" : 150.0,
        #"mqtt" : "PercentHighVoltage",
    },
    {
        "name": "过压延时",
        "unit": "s",
        "addr": "8176", 
        "scale" : 0.01,
        "format": "%05.2f",
        "min" : 0.1,
        "max" : 60.0,
        #"mqtt" : "DelayHighVoltage",
    },
    {
        "name": "Ua校正",
        "unit": "%",
        "addr": "8220", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        #"mqtt" : "CorrectUa",
    },
    {
        "name": "Ub校正",
        "unit": "%",
        "addr": "8221", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        #"mqtt" : "CorrectUb",
    },
    {
        "name": "Uc校正",
        "unit": "%",
        "addr": "8222", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        #"mqtt" : "CorrectUc",
    },
    {
        "name": "Us校正",
        "unit": "%",
        "addr": "8214", 
        "scale" : 0.1,
        "format": "%05.1f",
        "min" : 60.0,
        "max" : 130.0,
        #"mqtt" : "CorrectUs",
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_alarm_display = [
    {
        "name": "当前故障",
        "action" : "Idle",
    },
]

menu_alarm_curve = [
    {
        "name": "录波曲线",
        "action" : "Idle",
    },
]

menu_alarm_history = [
    {
        "name": "故障查询",
        "action" : "Idle",
    },
]

menu_alarm = [
    {
        "name": "故障查询",
        "sub_menu": "menu_alarm_history",
    },
    {
        "name": "故障清除",
        "action": "alarm_clear",
    },
    {
        "name": "合闸清零",
        "action": "clear_oncount",
    },
    {
        "name": "合闸次数",
        "addr": "8169", 
        "format": "%05.0f",
        "property": MENU_PROPERTY_READ_ONLY,
        #"mqtt" : "OnCount",
    },
    {
        "name": "电量清除",
        "action": "clear_kwh",
    },
    {
        "name": "累计时间清除",
        "action": "clear_time",
    },
    {
        "name": "累计合闸时间",
        "addr": "8016#2", 
        "unit": "Min",
        "scale": 0.0166666666666667,
        "format": "%07.0f",
        "property": MENU_PROPERTY_READ_ONLY,
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_test = [
    {
        "name": "短路试验",
        "action": "test",
        "addr" : "100628",
        "mqtt" : "TestShort",
    },
    {
        "name": "漏电试验",
        "action": "test",
        "addr" : "100629",
        "mqtt" : "TestIsolation",
    },
    {
        "name": "过压试验",
        "action": "test",
        "addr" : "100641",
        "mqtt" : "TestOverVoltage",
    },
    {
        "name": "电流断相试验",
        "action": "test",
        "addr" : "100642",
        "mqtt" : "TestBreak",
    },

    {
        "name": "复归",
        "action": "test",
        "addr" : "100649",
        "mqtt" : "TestRecover",
    },

    {
        "name": "漏电试验配置",
        "alias" : "alias_test",
        "addr": "8265", 
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_protect_set = [
    {
        "name": "短路保护",
        "alias": Alias.alias_bool,
        "addr": "8192.1", 
        #"mqtt" : "ProtectShort",
    },
    {
        "name": "不平衡保护",
        "alias": "alias_bool",
        "addr": "8192.2", 
        #"mqtt" : "ProtectUnbalance",
    },
    {
        "name": "反时限过流",
        "alias": "alias_bool",
        "addr": "8192.3", 
        #"mqtt" : "ProtectOverCurrent",
    },
    {
        "name": "过压保护",
        "alias": "alias_bool",
        "addr": "8174.0", 
        #"mqtt" : "ProtectHighVotage",
    },
    {
        "name": "欠压保护",
        "alias": "alias_bool",
        "addr": "8174.1", 
        #"mqtt" : "ProtectLowVotage",
    },
     # {
    #     "name": "电压断相",
    #     "alias": "alias_bool",
    #     "addr": "8174.2", 
    # },
     # {
    #     "name": "相序保护",
    #     "alias": "alias_bool",
    #     "addr": "8174.3", 
    # },
     {
        "name": "漏电闭锁",
        "alias": "alias_bool",
        "addr": "8192.10", 
        #"mqtt" : "ProtectIsolationLock",
    },
     {
        "name": "漏电动作",
        "alias": "alias_bool",
        "addr": "8174.6", 
        #"mqtt" : "ProtectIsolationAction",
    },
    # {
    #     "name": "选漏保护",
    #     "alias": "alias_bool",
    #     "addr": "8174.7", 
    # },
    {
        "name": "风电闭锁",
        "alias": "alias_bool",
        "addr": "8175.0", 
        #"mqtt" : "ProtectWindMotor",
    },
    {
        "name": "瓦斯闭锁",
        "alias": "alias_bool",
        "addr": "8174.13", 
        #"mqtt" : "ProtectGasLock",
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_comm_set = [
    {
        "name": "485地址",
        "addr": "8212", 
        "format": "%03.0f", 
    },
    {
        "name": "485波特率",
        "alias": "alias_baud",
        "addr": "8188", 
    },
    {
        "name": "485校验",
        "alias": "alias_parity",
        "addr": "8194", 
    },
    # {
    #     "name": "CAN地址",
    # },
    # {
    #     "name": "CAN波特率",
    #     "alias": "alias_can_baud",  
    # },
    {
        "name": "IP设置",
        "addr": "8271#2", 
        "action": "ip",
    },
    {
        "name": "IP网关",
        "addr": "8273#2", 
        "action": "ip",
    },
    {
        "name": "IP掩码",
        "addr": "8275#2",
        "action": "ip",
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_system = [
    {
        "name": "工作模式",
        "addr": "8231", 
        "alias": "alias_work_mode", 
        "property": MENU_PROPERTY_ENGINEER,
        "action" : "Visible_CL",          #磁力模式显示菜单
    },
    {
        "name": "开关类型",
        "alias": "alias_switch_type",
        "addr": "8210", 
        "action" : "Visible_KD",          #馈电模式显示菜单
    },
    {
        "name": "控制模式",
        "alias": "alias_remote",
        "addr": "8262", 
    },
    {
        "name": "瓦斯触点",
        "alias": "alias_input",
        "addr": "8190", 
    },
    {
        "name": "瓦斯延时",
        "unit": "S",
        "addr": "8191", 
        "scale": 0.01,
        "format": "%05.2f", 
    },
    {
        "name": "风电触点",
        "alias": "alias_input",
        "addr": "8248", 
    },
    {
        "name": "风电延时",
        "unit": "S",
        "addr": "8234", 
        "scale": 0.01,
        "format": "%05.2f", 
    },
    {
        "name": "高水位延时",
        "addr": "8267", 
        "unit": "S",
        "scale": 0.1,
        "format": "%04.1f", 
        "action" : "Visible_CL_WATER",   #水位磁力模式显示
    },  
    {
        "name": "正反转延时",
        "addr": "8266", 
        "unit": "ms",
        "scale": 1.0,
        "format": "%04.0f", 
        "action" : "Visible_CL_INV",    #正反转磁力模式显示
    },  
    {
        "name": "维修模式",
        "addr": "8260", 
        "alias": "alias_bool",
    }, 
       # {
    #     "name": "温度设定",
    #     "unit": "℃",
    #     "addr": "2030", 
    #     "format": "%+03.0f", 
    # },
    {
        "name": "系统日期",
        "addr": "2300#3", 
        "action": "date",
    },  
    {
        "name": "当前时间",
        "addr": "2303#3", 
        "action": "time",
    },
    {
        "name": "软件版本",
        "addr": "8180", 
        "unit" : VERSION_DATE,
        "scale": 0.01,
        "format": "%.2f", 
        "property": MENU_PROPERTY_READ_ONLY,
    }, 
    {
        "name": "系统密码",
        "addr": "8181", 
        "format": "%04.0f", 
        "action": "password",
    }, 
    {
        "name": "Us相位",
        "alias": "alias_us_phase",
        "addr": "8269", 
    }, 
    {
        "name": "重启",
        "action": "restart",
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_switch_cmd = [
    {
        "name":   "通讯合闸",
        "action": "write_bit",
        "addr" :  "100618",
        "mqtt" :  "SwitchOn",
    },
    {
        "name":   "通讯分闸",
        "action": "write_bit",
        "addr" :  "100619",
        "mqtt" :  "SwitchOff",
    },
    {
        "name":   "通讯复位",
        "action": "write_bit",
        "addr" :  "100620",
        "mqtt" :  "SwitchRst",
    },
    {
        "name":   "小车进控制",
        "action": "write_bit",
        "addr" :  "100636",
        "mqtt" :  "CarForward",
    },
    {
        "name":   "小车退控制",
        "action": "write_bit",
        "addr" :  "100637",
        "mqtt" :  "CarRetreat",
    },

]

memu_setting = [
    {
        "name": "电流设置",
        "sub_menu": Menu.menu_current,
    },
    {
        "name": "电压设置",
        "sub_menu": "menu_voltage"
    },
    {
        "name": "漏电设置",
        "sub_menu": "menu_leak"
    },
    {
        "name": "系统设置",
        "sub_menu": "menu_system"
    },
    {
        "name": "保护设置",
        "sub_menu": "menu_protect_set"
    },
    {
        "name": "通讯设置",
        "sub_menu": "menu_comm_set"
    },
    {
        "name": "保护试验",
        "sub_menu": Menu.menu_test   #"menu_test"
    },
    {
        "name": "故障查询",
        "sub_menu": "menu_alarm"
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

menu_leak = [
    {
        "name": "漏电延时",
        "addr": "8205",
        "unit": "ms",
        "scale" : 1.0,
        "format" : "%03.0f",
        "action" : "Visible_KD_GB",   #馈电, 高爆模式所需菜单
    },
    {
        "name": "零序电流",
        "addr": "2007",
        "unit": "mA",
        "scale" : 0.1,
        "format" : "%05.1f",
        #"mqtt" : "KD.Io",
        "action" : "Visible_KD",      #馈电模式所需菜单
    },
    {
        "name": "零序电压",
        "addr": "2008",
        "unit": "V",
        "scale" : 1.0,
        "format" : "%04.0f",
        #"mqtt" : "KD.Uo",
        "action" : "Visible_KD",      #馈电模式菜单有效
    },
    {
        "name": "零序电流",
        "addr": "2007",
        "unit": "A",
        "scale" : 0.01,
        "format" : "%05.2f",
        "mqtt" : "GB.Io",
        "action" : "Visible_GB",      #高爆模式菜单有效
    },
    {
        "name": "零序电压",
        "addr": "2008",
        "unit": "V",
        "scale" : 1.0,
        "format" : "%05.2f",
        "mqtt" : "GB.Uo",
        "action" : "Visible_GB",      #高爆模式菜单有效
    },
    {
        "name": "Uo校正值",
        "addr": "8187",
        "unit": "%",
        "scale" : 0.1,
        "format" : "%05.1f",
        "action" : "Visible_KD_GB",   #馈电与高爆模式菜单有效
    },
    {
        "name": "Io校正值",
        "addr": "8196",
        "unit": "%",
        "scale" : 0.1,
        "format" : "%05.1f",
        "action" : "Visible_KD_GB",   #馈电与高爆模式菜单有效
    },
    {
        "name": "闭锁电阻",
        "addr": "8204",
        "unit": "K",
        "scale" : 0.1,
        "format" : "%05.1f",
        "action" : "Hide_GB",         #高爆模式下隐藏菜单
    },
    {
        "name": "串联电阻",
        "addr": "8236",
        "unit": "K",
        "scale" : 0.1,
        "format" : "%05.1f",
    },
    {
        "name": "漏电投入延时",
        "addr": "8252",
        "unit": "S",
        "scale" : 0.1,
        "format" : "%04.1f",
        "action" : "Hide_GB",         #高爆模式下隐藏菜单
        "default": 3.0
    },
    {
        "name": "检测电压",
        "addr": "8253",
        "alias": Alias.alias_measure_v,
    },
    {
        "name": "退出",
        "action": "exit",
    },
]

calibrate = [
    {
        "name": "999校正",
        "action": "calibrate999",
        "mqtt" : "Calibrate999",
    },
    {
        "name": "0欧校正",
        "action": "calibrate0",
        "mqtt" : "Calibrate0",
    }, 
    {
        "name": "以太网模块",
        "alias": Alias.alias_eth,
        "addr" : "8280",
    }, 
    {
        "name": "LCD模块",
        "alias": Alias.alias_lcd,
        "addr" : "8281",
    }, 
    {
        "name": "防越级模块",
        "alias": Alias.alias_fyj,
        "addr" : "8282",
    }, 
    {
        "name": "设备型号",
        "alias": Alias.alias_dev,
        "addr" : "8270",
    }, 
    {
        "name": "退出",
        "action": "exit",
    },
]

startup = [
    {
        "name": "退出",
    },
]

menu_main = [
    {
        "name": "设置",
        "sub_menu": "memu_setting",
    },
    {
        "name": "数据",
        "sub_menu": "menu_data",
    },
    {
        "name": "报警",
        "sub_menu": "menu_alarm_display",
    },
    {
        "name": "标定",
        "sub_menu": "calibrate",
        "property": MENU_PROPERTY_ENGINEER,
    },
]

mqtt_topic_subscribe = None

MENU_STARTUP_PAGE = 150
MENU_DISPLAY_PAGE = 2
MENU_ALARM_PAGE   = 3
MENU_DATA_PAGE    = 120
MENU_CL_MAIN_PAGE = 90
MENU_KD_MAIN_PAGE          = 20
MENU_KD_MAIN_PAGE_LANGUAGE = 21
MENU_CURVE_PAGE            = 10    
MENU_ADC_CURVE_PAGE        = 18
MENU_HARNOMIC_CURVE_PAGE   = 19

menu_caption = [
   {"name" : "startup",           "menu" : startup,         "caption": "启动页面", "page" : MENU_STARTUP_PAGE,  "max_items" : 1}, 
   {"name" : "menu_main",           "menu" : menu_main,         "caption": "主菜单",   "page" : MENU_KD_MAIN_PAGE,  "max_items" : 4},
   {"name" : "menu_leak",           "menu" : menu_leak,         "caption": "漏电设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "memu_setting",        "menu" : memu_setting,      "caption": "参数设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "menu_current",        "menu" : menu_current,      "caption": "电流设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "menu_voltage",        "menu" : menu_voltage,      "caption": "电压设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "menu_system",         "menu" : menu_system,       "caption": "系统设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "menu_protect_set",    "menu" : menu_protect_set,  "caption": "保护设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "menu_comm_set",       "menu" : menu_comm_set,     "caption": "通讯设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "menu_alarm",          "menu" : menu_alarm,        "caption": "故障查询", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "menu_alarm_display",  "menu" : menu_alarm_display,  "caption": "当前故障", "page" : MENU_ALARM_PAGE,      "prev": menu_alarm_curve,   "next": menu_alarm_curve, "max_items" : 1},
   {"name" : "menu_alarm_curve",    "menu" : menu_alarm_curve,    "caption": "曲线故障", "page" : MENU_ADC_CURVE_PAGE,  "prev": menu_alarm_display, "next": menu_alarm_display, "max_items" : 1},

   {"name" : "menu_alarm_history",  "menu" : menu_alarm_history,  "caption": "故障查询", "page" : MENU_ALARM_PAGE,  "max_items" : 1},
   {"name" : "menu_test",           "menu" : menu_test,         "caption": "保护试验", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "mqtt_pack_alarm",     "menu" : mqtt_pack_alarm,   "caption": "报警数据", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "mqtt_pack_measure",   "menu" : mqtt_pack_measure, "caption": "测量数据", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8}, 
   {"name" : "menu_data",           "menu" : menu_data,         "caption": "数据页面", "page" : MENU_DATA_PAGE,     "max_items" : 8}, 
   {"name" : "menu_curve_physical", "menu" : menu_curve_physical, "caption": "物理曲线", "page" : MENU_CURVE_PAGE,  "prev": menu_curve_harmonic, "next": menu_curve_adc,   "max_items" : 8}, 
   {"name" : "menu_curve_adc",      "menu" : menu_curve_adc,    "caption": "ADC曲线",      "page" : MENU_ADC_CURVE_PAGE,  "prev": menu_curve_physical, "next": menu_curve_harmonic,   "max_items" : 8}, 
   {"name" : "menu_curve_harmonic",     "menu" : menu_curve_harmonic,   "caption": "谐波曲线",  "page" : MENU_HARNOMIC_CURVE_PAGE,  "prev": menu_curve_adc, "next": menu_curve_physical,   "max_items" : 8}, 
   {"name" : "calibrate",           "menu" : calibrate,         "caption": "标定页面", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8}, 
   {"name" : "menu_switch_cmd",     "menu" : menu_switch_cmd,   "caption": "通讯操作", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8}, 
   {"name" : "mqtt_pack_param",     "menu" : mqtt_pack_param,   "caption": "参数刷新", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8}, 
   {"name" : "mqtt_video_trigger",  "menu" : mqtt_video_trigger,   "caption": "视频触发", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8}, 
   {"name" : "mqtt_pack_globalmeasure",  "menu" : mqtt_pack_globalmeasure,   "caption": "测温数据", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},
   {"name" : "mqtt_time_update",  "menu" : mqtt_time_update,   "caption": "时钟设置", "page" : MENU_DISPLAY_PAGE,  "max_items" : 8},    
]


# mqtt  故障查询函数
def func_alarm_query(device : class_modbus_comm_device, alarm_index = 0) :
    result     = device.protocol.modbus_write_regs(device.comm_addr, 8120, 1, alarm_index)
    time.sleep(0.05)
    alarm_regs = device.protocol.modbus_read_regs(device.comm_addr, 8121, 20)
    if alarm_regs != None :
        device.modbus_regs_flush(8121, 20, alarm_regs)
        
        msg_alarm_dict = device.comm_item_trigger_mqtt_pack(mqtt_pack_alarm)
        
        err_code = utils.dict_or_object_get_attr(msg_alarm_dict, "AlarmCode", 0)
        err_code = int(err_code)
        test_code = utils.dict_or_object_get_attr(msg_alarm_dict, "AlarmTest", 0)
        test_code = int(test_code)
        
        
        test_value_str = get_alias_value_str("alias_alarm_type", test_code)
        if test_value_str == None: 
            test_value_str = " "
        err_code_str = get_alias_value_str("alias_alarm", err_code)
        if err_code_str == None: 
            err_code_str = " "
        msg_alarm_dict["AlarmCodeMsg"] = err_code_str
        msg_alarm_dict["AlarmTestMsg"] = test_value_str
        msg_alarm_dict["index"] = alarm_index
        msg : str = json.dumps(msg_alarm_dict, ensure_ascii=False)

        if device.mqtt_thread :
            device.mqtt_thread.publish("response/alarm/" + device.unique_name, msg) 

# mqtt  action处理函数
def func_action_process(device : class_modbus_comm_device, menu_item = None, value_str : str = None) :
    action_str = utils.dict_or_object_get_attr(menu_item, "action", " ")
    mqtt_name  = utils.dict_or_object_get_attr(menu_item, "mqtt", " ")
    result = None
    
    if action_str == "SetupTime" :   #设置保护器时间
        numbers = re.findall(r'\d+', value_str)
        if len(numbers) == 6 :
            year  = int(numbers[0])
            month = int(numbers[1])
            day   = int(numbers[2])
            hour  = int(numbers[3])
            minute = int(numbers[4])
            second = int(numbers[5])
            device.protocol.modbus_write_regs(device.comm_addr, 2300, 6, (year, month, day, hour, minute, second))
        pass
    if action_str == "calibrate999" :
        result = device.protocol.modbus_write_regs(device.comm_addr, 8255, 0x2007)
    if action_str == "calibrate0" :
        result = device.protocol.modbus_write_regs(device.comm_addr, 8255, 0x2006)
    if action_str == "write_bit" or action_str == "test":
        comm_str = utils.dict_or_object_get_attr(menu_item, "addr", " ")
        reg_addr, bit, reg_count = utils.comm_str_unpack(comm_str)
        if reg_count == 1:
            command_value = [1]
            result = device.write_bit_register(reg_addr, command_value)
    
    if device.mqtt_thread and result != None:
        device.mqtt_thread.publish("response/action/" + device.unique_name, '{"name" : "%s", "result" : %d}'%(mqtt_name, result))  


class class_menu_comm_device_config(class_comm_device_config):
    def __init__(self):
        pass

    #@override get_alias_table
    def get_alias_table(self) :
        return alias_table

    #@override get_comm_table
    def get_comm_table(self) :
        return modbus_comm_table, func_comm_table_trigger_check

    #@override get_menu_top
    def get_menu_top(self) :
        return menu_main
    
    def get_menu_caption_info(self) :
        return menu_caption

    #@override get_mqtt_table
    def get_mqtt_table(self) :
        return mqtt_topic_subscribe
    
    def is_gb2312(self) :
        return False
    
#comm_device_config的名字不要更改, 创建通讯配置类
comm_device_config = class_menu_comm_device_config()
