#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: ModBusRegister.py
 @DateTime: 2025/4/8 19:57
 @SoftWare: PyCharm
 @Author  ：xsh
"""
from PyQt5                              import Qt
from AppInterfaces.ModelInterface       import ContextModelInterface
from AppCore.Levels                     import *
from AppUtils.utils                     import *
from AppCore.Requests                   import *
class ModBusRegister(ContextModelInterface):
    __slots__ = ('Type', 'Addr', 'Data', 'Information')
    __types__ = (int, int, int, str)
    Type        : int
    Addr        : int
    Data        : (int, float)
    Information : str
    Buffer      : list
    def __init__(self, Type: int = 0, Addr: int = 0, Data: (bool, int, float) = 0, Information: str = "ModBus寄存器"):
        super(ModBusRegister, self).__init__()
        self.initialization("ModBus寄存器")
        self.initParameter(**{
            'Type'          : Type          ,
            'Addr'          : Addr          ,
            'Data'          : Data          ,
            'Information'   : Information   ,

        })

    @classmethod
    def instance(cls,Type: int, Addr: int, Data: (bool, int, float), Information: str = "ModBus寄存器") -> 'ModBusRegister':
        result: ModBusRegister = cls.__new__(cls)
        result.__init__(Type    ,Addr    ,Data    ,Information  )
        return result

    @classmethod
    def Instance(cls, **args) -> 'ModBusRegister':
        result: ModBusRegister = cls.__new__(cls)
        result.__init__()
        result.initParameter(**args)
        return result

    def asRequest(self, request: ModBusRequest):
        if not isinstance(request, ModBusRequest):
            return
        request.Type    = self.Type
        request.Addr    = self.Addr
        request.Data    = self.Data
        request.Buffer  = [self.Data]
        request.set_property("Information", self.Information)

    def toRequest(self, request: ModBusRequest):
        if not isinstance(request, ModBusRequest):
            return
        self.Type = request.Type
        self.Addr = request.Addr
        self.Data = request.Data
        if self.Data is None:
            if isinstance(request.Buffer, list) and len(request.Buffer) > 0:
                self.Data = request.Buffer[0]

    def initParameter(self, **args):
        super(ModBusRegister, self).initParameter(**args)
        if not isinstance(args, dict) or len(args) <= 0:
            return
        for index, _property in enumerate(self.__slots__):
            if _property == 'Data':
                setattr(self, _property, AppConvert.ObjectConvert(args.get(_property, getattr(self, _property, None)),
                                                                  type(self.Data)))
            else:
                setattr(self, _property, AppConvert.ObjectConvert(args.get(_property, getattr(self, _property, None)),
                                                                  self.__types__[index]))

    def initRegister(self, Type: int, Addr: int, Data: (bool, int, float), Information: str = "ModBus寄存器"):
        if ModBusLevel.exists(Type):
            self.Type   = Type
            self.Addr   = Addr
            self.Data   = Data
            self.Information = Information

    def initValue(self):
        super(ModBusRegister, self).initValue()
        self.Type   = 0
        self.Addr   = 0
        self.Data   = 0


