#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: AxisRegisterContext.py
 @DateTime: 2025/4/8 19:57
 @SoftWare: PyCharm
 @Author  ：xsh
"""
from PyQt5                              import Qt
from AppInterfaces.ModelInterface       import ContextModelInterface
from AppContexts.ModBusRegister         import ModBusRegister
from AppCore.Levels                     import *
from AppUtils.utils                     import *
from AppCore.Requests                   import *


class AxisRegisterContext(ContextModelInterface):
    Level               : int
    MovePosition        : int
    MoveVelocity        : int
    Acceleration        : int
    Deceleration        : int
    JOPVelocity         : int
    JOPAcceleration     : int
    JOPDeceleration     : int
    CurrentPosition     : int
    StatusRegister      : int
    ReadOnlyRegister    : int
    PowerRegister       : int
    MalfunctionRegister : int
    __slots__ = (
        'Level'                 ,
        'MovePosition'          ,
        'MoveVelocity'          ,
        'Acceleration'          ,
        'Deceleration'          ,
        'JOPVelocity'           ,
        'JOPAcceleration'       ,
        'JOPDeceleration'       ,
        'CurrentPosition'       ,
        'StatusRegister'        ,
        'ReadOnlyRegister'      ,
        'PowerRegister'         ,
        'MalfunctionRegister'   ,
    )
    __types__ = (
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
        int ,
    )
    def __init__(self, Information: str = "", **args):
        super(AxisRegisterContext, self).__init__()
        self.initialization(Information)
        self.initParameter(**args)


    @classmethod
    def instance(cls, **args) -> 'AxisRegisterContext':
        result: cls = cls.__new__(cls)
        result.__init__(Information="", **args)
        return result

    @classmethod
    def Instance(cls, Level            : int = 0   , MovePosition     : int = 1080,
                    MoveVelocity     : int = 1082, Acceleration     : int = 1084, Deceleration     : int = 1086,
                    JOPVelocity      : int = 1088, JOPAcceleration  : int = 1090, JOPDeceleration  : int = 1092,
                    CurrentPosition  : int = 1094, StatusRegister   : int = 4   , ReadOnlyRegister : int = 24  ,
                    PowerRegister    : int = 4, MalfunctionRegister: int = 401) -> "AxisRegisterContext":
        result: cls = cls.__new__(cls)
        args: dict[str, object] = {}
        args['Level'                 ] = Level
        args['MovePosition'          ] = MovePosition
        args['MoveVelocity'          ] = MoveVelocity
        args['Acceleration'          ] = Acceleration
        args['Deceleration'          ] = Deceleration
        args['JOPVelocity'           ] = JOPVelocity
        args['JOPAcceleration'       ] = JOPAcceleration
        args['JOPDeceleration'       ] = JOPDeceleration
        args['CurrentPosition'       ] = CurrentPosition
        args['StatusRegister'        ] = StatusRegister
        args['ReadOnlyRegister'      ] = ReadOnlyRegister
        args['PowerRegister'         ] = PowerRegister
        args['MalfunctionRegister'  ] =MalfunctionRegister

        result.__init__(Information="", **args)
        return result

    def initContext(self, Level            : int = 0   , MovePosition     : int = 1080,
                    MoveVelocity     : int = 1082, Acceleration     : int = 1084, Deceleration     : int = 1086,
                    JOPVelocity      : int = 1088, JOPAcceleration  : int = 1090, JOPDeceleration  : int = 1092,
                    CurrentPosition  : int = 1094, StatusRegister   : int = 4   , ReadOnlyRegister : int = 24  ,
                    PowerRegister    : int = 4, MalfunctionRegister: int = 401):
        self.Level                  = Level
        self.MovePosition           = MovePosition
        self.MoveVelocity           = MoveVelocity
        self.Acceleration           = Acceleration
        self.Deceleration           = Deceleration
        self.JOPVelocity            = JOPVelocity
        self.JOPAcceleration        = JOPAcceleration
        self.JOPDeceleration        = JOPDeceleration
        self.CurrentPosition        = CurrentPosition
        self.StatusRegister         = StatusRegister
        self.ReadOnlyRegister       = ReadOnlyRegister
        self.PowerRegister          = PowerRegister
        self.MalfunctionRegister    = MalfunctionRegister
        self.flushInfo()

    def initValue(self):
        super(AxisRegisterContext, self).initValue()
        self.Level                      = AxisLevel.AxisX
        self.MovePosition               = 1000
        self.MoveVelocity               = 1002
        self.Acceleration               = 1004
        self.Deceleration               = 1006
        self.JOPVelocity                = 1008
        self.JOPAcceleration            = 1010
        self.JOPDeceleration            = 1012
        self.CurrentPosition            = 1014
        self.StatusRegister             = 0
        self.ReadOnlyRegister           = 20
        self.PowerRegister              = 0
        self.MalfunctionRegister        = 401
        self.flushInfo()

    def initAxisX(self):
        self.Level                      = AxisLevel.AxisX
        self.MovePosition               = 1000
        self.MoveVelocity               = 1002
        self.Acceleration               = 1004
        self.Deceleration               = 1006
        self.JOPVelocity                = 1008
        self.JOPAcceleration            = 1010
        self.JOPDeceleration            = 1012
        self.CurrentPosition            = 1014
        self.StatusRegister             = 0
        self.ReadOnlyRegister           = 20
        self.PowerRegister              = 0
        self.MalfunctionRegister        = 401
        self.flushInfo()

    def initAxisY(self):
        self.Level                      = AxisLevel.AxisY
        self.MovePosition               = 1020
        self.MoveVelocity               = 1022
        self.Acceleration               = 1024
        self.Deceleration               = 1026
        self.JOPVelocity                = 1028
        self.JOPAcceleration            = 1030
        self.JOPDeceleration            = 1032
        self.CurrentPosition            = 1034
        self.StatusRegister             = 1
        self.ReadOnlyRegister           = 21
        self.PowerRegister              = 1
        self.MalfunctionRegister        = 403
        self.flushInfo()

    def initAxisZ(self):
        self.Level                      = AxisLevel.AxisZ
        self.MovePosition               = 1040
        self.MoveVelocity               = 1042
        self.Acceleration               = 1044
        self.Deceleration               = 1046
        self.JOPVelocity                = 1048
        self.JOPAcceleration            = 1050
        self.JOPDeceleration            = 1052
        self.CurrentPosition            = 1054
        self.StatusRegister             = 2
        self.ReadOnlyRegister           = 22
        self.PowerRegister              = 2
        self.MalfunctionRegister        = 405
        self.flushInfo()

    def initScanX(self):
        self.Level                      = AxisLevel.ScanX
        self.MovePosition               = 1000
        self.MoveVelocity               = 1002
        self.Acceleration               = 1004
        self.Deceleration               = 1006
        self.JOPVelocity                = 1008
        self.JOPAcceleration            = 1010
        self.JOPDeceleration            = 1012
        self.CurrentPosition            = 1014
        self.StatusRegister             = 0
        self.ReadOnlyRegister           = 20
        self.PowerRegister              = 0
        self.MalfunctionRegister        = 401
        self.flushInfo()

    def initScanY(self):
        self.Level                      = AxisLevel.ScanY
        self.MovePosition               = 1020
        self.MoveVelocity               = 1022
        self.Acceleration               = 1024
        self.Deceleration               = 1026
        self.JOPVelocity                = 1028
        self.JOPAcceleration            = 1030
        self.JOPDeceleration            = 1032
        self.CurrentPosition            = 1034
        self.StatusRegister             = 1
        self.ReadOnlyRegister           = 21
        self.PowerRegister              = 1
        self.MalfunctionRegister        = 403
        self.flushInfo()

    def initSlowX(self):
        self.Level                      = AxisLevel.SlowScanX
        self.MovePosition               = 1000
        self.MoveVelocity               = 1002
        self.Acceleration               = 1004
        self.Deceleration               = 1006
        self.JOPVelocity                = 1008
        self.JOPAcceleration            = 1010
        self.JOPDeceleration            = 1012
        self.CurrentPosition            = 1014
        self.StatusRegister             = 0
        self.ReadOnlyRegister           = 20
        self.PowerRegister              = 0
        self.MalfunctionRegister        = 401
        self.flushInfo()

    def initSlowY(self):
        self.Level                      = AxisLevel.SlowScanY
        self.MovePosition               = 1020
        self.MoveVelocity               = 1022
        self.Acceleration               = 1024
        self.Deceleration               = 1026
        self.JOPVelocity                = 1028
        self.JOPAcceleration            = 1030
        self.JOPDeceleration            = 1032
        self.CurrentPosition            = 1034
        self.StatusRegister             = 1
        self.ReadOnlyRegister           = 21
        self.PowerRegister              = 1
        self.MalfunctionRegister        = 403
        self.flushInfo()

    def initDropX(self):
        self.Level                      = AxisLevel.DropOilX
        self.MovePosition               = 1000
        self.MoveVelocity               = 1002
        self.Acceleration               = 1004
        self.Deceleration               = 1006
        self.JOPVelocity                = 1008
        self.JOPAcceleration            = 1010
        self.JOPDeceleration            = 1012
        self.CurrentPosition            = 1014
        self.StatusRegister             = 0
        self.ReadOnlyRegister           = 20
        self.PowerRegister              = 0
        self.MalfunctionRegister        = 401
        self.flushInfo()

    def initDropY(self):
        self.Level                      = AxisLevel.DropOilY
        self.MovePosition               = 1020
        self.MoveVelocity               = 1022
        self.Acceleration               = 1024
        self.Deceleration               = 1026
        self.JOPVelocity                = 1028
        self.JOPAcceleration            = 1030
        self.JOPDeceleration            = 1032
        self.CurrentPosition            = 1034
        self.StatusRegister             = 1
        self.ReadOnlyRegister           = 21
        self.PowerRegister              = 1
        self.MalfunctionRegister        = 403
        self.flushInfo()

    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 setLevel(self, level: int):
        self.Level = level if isinstance(level, int) and level in AxisLevel.all_axis() else AxisLevel.AxisX
        if self.Level == AxisLevel.AxisX:
            self.initAxisX()
        elif self.Level == AxisLevel.AxisY:
            self.initAxisY()
        elif self.Level == AxisLevel.ScanX:
            self.initScanX()
        elif self.Level == AxisLevel.ScanY:
            self.initScanY()
        elif self.Level == AxisLevel.SlowScanX:
            self.initSlowX()
        elif self.Level == AxisLevel.SlowScanY:
            self.initSlowY()
        elif self.Level == AxisLevel.DropOilX:
            self.initDropX()
        elif self.Level == AxisLevel.DropOilY:
            self.initDropY()
        elif self.Level == AxisLevel.AxisZ:
            self.initAxisZ()
        else:
            self.initAxisX()

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






