#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: ModBusRequest.py
 @DateTime: 2025/4/8 20:11
 @SoftWare: PyCharm
 @Author  ：xsh
"""
import datetime
import sys
from PyQt5                              import Qt
from AppInterfaces.ModelInterface       import RequestModelInterface
from AppCore.Levels                     import *
from AppUtils.utils                     import *
from AppCore.Requests                   import *


class ModBusRequest(RequestModelInterface):
    Type           : int    #
    Addr           : int    #
    Length         : int    #
    Data           : object #
    Buffer         : object #
    __slots__ = ('Type'   ,'Addr'   ,'Length' ,'Data'   ,'Buffer' )


    def __init__(self, Type: int = 0, Addr: int = 0, Length: int = 0, Data: (bool, int, float) = 0, Buffer: list[(bool, int, float)] = []):
        super(ModBusRequest, self).__init__()
        self.initialization("PLC请求")
        self.initParameter(**{
            'Type'        : Type  ,
            'Addr'        : Addr  ,
            'Length'      : Length,
            'Data'        : Data  ,
            'Buffer'      : Buffer,})

    # def __del__(self):
    #     self.deinitialize()

    @classmethod
    def instance(cls, Type: int = 0, Addr: int = 0, Length: int = 0,  Data: (bool, int, float) = 0, Buffer: list[(bool, int, float)] = []) -> 'ModBusRequest':
        result = cls.__new__(cls)
        result.__init__(Type, Addr, Length, Data, Buffer)
        return result

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

    def initParameter(self, **args):
        super(ModBusRequest, self).initParameter(**args)
        self.ResultNumber   = AppConvert.ObjectConvert(args.get('ResultNumber', self.ResultNumber   ), int)
        self.Type           = AppConvert.ObjectConvert(args.get('Type'        , self.Type           ), int)
        self.Addr           = AppConvert.ObjectConvert(args.get('Addr'        , self.Addr           ), int)
        self.Length         = AppConvert.ObjectConvert(args.get('Length'      , self.Length         ), int)
        self.Data           = args.get('Data'                                 , self.Data           )
        self.Buffer         = args.get('Buffer'                               , self.Buffer         )
        self.initProperty()



    def initValue(self):
        super(ModBusRequest, self).initValue()
        self.ResultNumber   = 0
        self.Type           = None
        self.Addr           = None
        self.Length         = None
        self.Data           = None
        self.Buffer         = None


    def initProperty(self):
        super(ModBusRequest, self).initProperty()
        if self.Type == ModBusLevel.Bool:
            self.Length     = 1
        elif self.Type == ModBusLevel.Int16:
            self.Length     = 1
        elif self.Type == ModBusLevel.UInt16:
            self.Length     = 1
        elif self.Type == ModBusLevel.Int32:
            self.Length     = 2
        elif self.Type == ModBusLevel.UInt32:
            self.Length     = 2
        elif self.Type == ModBusLevel.Float:
            self.Length     = 2
        else:
            self.Length     = 1

