#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: DetectionContext.py
 @DateTime: 2025/5/21 9:15
 @SoftWare: PyCharm
 @Author  ：xsh
"""
import sys
from PyQt5                                  import Qt
from AppInterfaces.ModelInterface           import ContextModelInterface
from AppCore.Levels                         import *
from AppUtils.utils                         import *
from AppCore.Requests                       import *
from AppContexts.DetectionRegisterContext   import DetectionRegisterContext
from AppContexts.ModBusRegister             import ModBusRegister

class DetectionContext(ContextModelInterface):
    __slots__ = (
        'Level'                         ,
        'OilPumpRegister'               ,
        'OilSpeedRegister'              ,
        'OilStatusRegister'             ,
        'DoorStatusRegister'            ,
        'BuzzerRegister'                ,
        'ChamberStatusRegister'         ,
    )

    __types__ = (
        int             ,
        ModBusRegister  ,
        ModBusRegister  ,
        ModBusRegister  ,
        ModBusRegister  ,
        ModBusRegister  ,
        ModBusRegister  ,

    )
    Level                 : int
    OilPumpRegister       : ModBusRegister
    BuzzerRegister        : ModBusRegister
    OilStatusRegister     : ModBusRegister
    DoorStatusRegister    : ModBusRegister
    ChamberStatusRegister : ModBusRegister
    OilSpeedRegister      : ModBusRegister
    def __init__(self, **args):
        super(DetectionContext, self).__init__()
        self.initialization(self.__class__.__name__)
        self.initParameter(**args)


    @classmethod
    def instance(cls, level: int = 7) -> 'DetectionContext':
        result: cls = cls.__new__(cls)
        result.__init__()
        result.setLevel(level)
        return result


    def initValue(self):
        super(DetectionContext, self).initValue()
        self.initDetection()

    def initDetection(self):
        self.Level                         = AxisLevel.Detection
        self.OilPumpRegister               = ModBusRegister.instance(Type=ModBusLevel.Bool      , Addr=70  , Data = False        , Information="油泵开关　　　")
        self.BuzzerRegister                = ModBusRegister.instance(Type=ModBusLevel.Bool      , Addr=71  , Data = False        , Information="蜂鸣器开关　　")
        self.OilStatusRegister             = ModBusRegister.instance(Type=ModBusLevel.Bool      , Addr=13  , Data = False        , Information="油路检测　　　")
        self.DoorStatusRegister            = ModBusRegister.instance(Type=ModBusLevel.Bool      , Addr=312 , Data = False        , Information="开门检测开关　")
        self.ChamberStatusRegister         = ModBusRegister.instance(Type=ModBusLevel.Bool      , Addr=310  , Data = False        , Information="片仓状态　　　")
        self.OilSpeedRegister              = ModBusRegister.instance(Type=ModBusLevel.UInt16    , Addr=3000, Data = int(200)     , Information="油泵速度　　　")
        self.flushInfo()


    def initParameter(self, **args):
        super(DetectionContext, self).initParameter(**args)
        for index, _property in enumerate(self.__slots__):
            dst_type = self.__types__[index]
            if dst_type == ModBusRegister and isinstance(getattr(self, _property, None), ModBusRegister):
                getattr(self, _property).deserialize(args.get(_property, {}))
            else:
                setattr(self, _property, AppConvert.ObjectConvert(args.get(_property, getattr(self, _property, None)), dst_type))
        self.Level = AxisLevel.Detection

    def flushInfo(self):
        if self.Level == AxisLevel.AxisX:
            self.Information = "Ｘ轴地址"
        elif self.Level == AxisLevel.AxisY:
            self.Information = "Ｙ轴地址"
        elif self.Level == AxisLevel.AxisH:
            self.Information = "快扫Ｈ轴地址"
        elif self.Level == AxisLevel.AxisZ:
            self.Information = "快扫Ｚ轴地址"
        elif self.Level == AxisLevel.ScanX:
            self.Information = "快扫Ｘ轴地址"
        elif self.Level == AxisLevel.ScanY:
            self.Information = "快扫Ｙ轴地址"
        elif self.Level == AxisLevel.SlowScanX:
            self.Information = "慢扫Ｘ轴地址"
        elif self.Level == AxisLevel.SlowScanY:
            self.Information = "慢扫Ｙ轴地址"
        elif self.Level == AxisLevel.DropOilX:
            self.Information = "滴油Ｘ轴地址"
        elif self.Level == AxisLevel.DropOilY:
            self.Information = "滴油Ｙ轴地址"
        elif self.Level == AxisLevel.AxisZ:
            self.Information = "片仓Ｚ轴地址"
        elif self.Level == AxisLevel.Detection:
            self.Information = "传感器地址"
        else:
            self.Information = "Ｘ轴地址"


    def asRegisterContext(self, context: DetectionRegisterContext):
        if not isinstance(context, DetectionRegisterContext):
            return
        self.Level              = context.Level
        self.OilPumpRegister               .Addr   = context.OilPumpRegister                        #
        self.BuzzerRegister                .Addr   = context.BuzzerRegister                         #
        self.OilStatusRegister             .Addr   = context.OilStatusRegister                      #
        self.DoorStatusRegister            .Addr   = context.DoorStatusRegister                     #
        self.ChamberStatusRegister         .Addr   = context.ChamberStatusRegister                  #
        self.OilSpeedRegister              .Addr   = context.OilSpeedRegister                       #


    def toRegisterContext(self, context: DetectionRegisterContext):
        if not isinstance(context, DetectionRegisterContext):
            return
        context.Level                   = self.Level                    #
        context.OilPumpRegister                    = self.OilPumpRegister                   .Addr #
        context.BuzzerRegister                     = self.BuzzerRegister                    .Addr #
        context.OilStatusRegister                  = self.OilStatusRegister                 .Addr #
        context.DoorStatusRegister                 = self.DoorStatusRegister                .Addr #
        context.ChamberStatusRegister              = self.ChamberStatusRegister             .Addr #
        context.OilSpeedRegister                   = self.OilSpeedRegister                  .Addr #