Passed
Push — master ( 91ee3f...f145d6 )
by Matt
01:40
created

Vdim.__is_vdim_channel()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
"""
2
 *  PyDMXControl: A Python 3 module to control DMX using OpenDMX or uDMX.
3
 *                Featuring fixture profiles, built-in effects and a web control panel.
4
 *  <https://github.com/MattIPv4/PyDMXControl/>
5
 *  Copyright (C) 2022 Matt Cowley (MattIPv4) ([email protected])
6
"""
7
8
from datetime import datetime
9
from typing import Union, Tuple, List
10
11
from ._Fixture import Fixture
12
13
14
class Vdim(Fixture):
15
16
    def __init__(self, *args, **kwargs):
17
        super().__init__(*args, **kwargs)
18
19
        self.__vdims = []
20
        self.__vdim = 255
21
        self.__vdimUpdated = datetime.utcnow()
22
23
    def _register_channel(self, name: str, *, parked: Union[bool, int] = False, vdim: bool = False) -> int:
24
        super_call = super()._register_channel(name, parked=parked)
25
26
        # Register vdim if applicable
27
        if vdim:
28
            self.__vdims.append(super_call)
29
30
        return super_call
31
32
    def __is_vdim_channel(self, channel: Union[str, int]) -> bool:
33
        return str(channel).lower().strip() in ["dimmer", "vdim", "dim", "d"] \
34
               or str(channel) == str(self.next_channel - 1)
35
36
    def get_channel_id(self, channel: Union[str, int]) -> int:
37
        # Look for vdim channel
38
        if self.__is_vdim_channel(channel):
39
            return self.next_channel - 1
40
41
        # Get normal channel
42
        return super().get_channel_id(channel)
43
44
    def get_channel_value(self, channel: Union[str, int], apply_vdim: bool = True) -> Tuple[int, datetime]:
45
        # Look for vdim channel
46
        if self.__is_vdim_channel(channel):
47
            return self.__vdim, self.__vdimUpdated
48
49
        # Get normal channel
50
        super_call = super().get_channel_value(channel)
51
        new_val = super_call[0]
52
        new_time = super_call[1]
53
54
        # Apply vdim if applicable
55
        if apply_vdim and self.get_channel_id(channel) in self.__vdims:
56
            new_val = int(new_val * (self.__vdim / 255))
57
            if self.__vdimUpdated > new_time:
58
                new_time = self.__vdimUpdated
59
60
        return new_val, new_time
61
62
    def set_channel(self, channel: Union[str, int], value: int) -> Fixture:
63
        # Allow setting of vdim
64
        if self.__is_vdim_channel(channel):
65
            return self.set_vdim(value)
66
67
        # Set normal channel
68
        return super().set_channel(channel, value)
69
70
    def set_vdim(self, value: int) -> Fixture:
71
        # Update the vdim value
72
        if not self._valid_channel_value(value, 'vdim'):
73
            return self
74
        self.__vdim = value
75
        self.__vdimUpdated = datetime.utcnow()
76
        return self
77
78
    def get_color(self) -> Union[None, List[int]]:
79
        if not self.has_channel('r') or not self.has_channel('g') or not self.has_channel('b'):
80
            return None
81
82
        color = [
83
            self.get_channel_value('r', False)[0],
84
            self.get_channel_value('g', False)[0],
85
            self.get_channel_value('b', False)[0],
86
        ]
87
88
        if self.has_channel('w'):
89
            color.append(self.get_channel_value('w', False)[0])
90
91
            if self.has_channel('a'):
92
                color.append(self.get_channel_value('a', False)[0])
93
94
        return color
95