Passed
Push — master ( d01dda...94e82a )
by Matt
01:53
created

uDMXController._connect()   A

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nop 1
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 typing import List
9
10
from pyudmx import pyudmx
11
12
from ._TransmittingController import TransmittingController
13
14
15
class uDMXController(TransmittingController):
16
17
    def __init__(self, *args, **kwargs):
18
        # Device information
19
        self.__udmx_vendor_id = kwargs.pop("udmx_vendor_id", 0x16c0)
20
        self.__udmx_product_id = kwargs.pop("udmx_product_id", 0x5dc)
21
        self.__udmx_bus = kwargs.pop("udmx_bus", None)
22
        self.__udmx_address = kwargs.pop("udmx_address", None)
23
24
        # Store the device
25
        self.__udmx = None
26
27
        # Create the parent controller
28
        super().__init__(*args, **kwargs)
29
30
    def _connect(self):
31
        # Try to close if exists
32
        if self.__udmx is not None:
33
            try:
34
                self.__udmx.close()
35
            except Exception:
36
                pass
37
38
        # Get new device
39
        self.__udmx = pyudmx.uDMXDevice()
40
        self.__udmx.open(self.__udmx_vendor_id, self.__udmx_product_id, self.__udmx_bus, self.__udmx_address)
41
42
    def _close(self):
43
        self.__udmx.close()
44
        print("CLOSE: uDMX closed")
45
46
    def _transmit(self, frame: List[int], first: int):
47
        # Attempt to send data max 5 times, then 2 more with reconnect to device
48
        # Thanks to Dave Hocker (pyudmx author) for giving me this solution to the random usb errors
49
        success = False
50
        retry_count = 0
51
        while not success:
52
            try:
53
                if retry_count > 5:
54
                    self._connect()
55
                self.__udmx.send_multi_value(first, frame)
56
                success = True
57
            except Exception as e:
58
                retry_count += 1
59
                if retry_count > 7:
60
                    raise e
61