Passed
Push — web-timed ( 80b983...b83f75 )
by Matt
01:13
created

uDMXController._send_data()   A

Complexity

Conditions 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
rs 9.2333
c 0
b 0
f 0
cc 5
nop 1
1
"""
2
 *  PyDMXControl: A Python 3 module to control DMX via Python. Featuring fixture profiles and working with uDMX.
3
 *  <https://github.com/MattIPv4/PyDMXControl/>
4
 *  Copyright (C) 2018 Matt Cowley (MattIPv4) ([email protected])
5
"""
6
7
from pyudmx import pyudmx
8
9
from ._transmittingController import transmittingController
10
11
12
class uDMXController(transmittingController):
13
14
    def __init__(self, *args, **kwargs):
15
        self.udmx = None
16
        self.__connect()
17
18
        super().__init__(*args, **kwargs)
19
20
    def close(self):
21
        # uDMX
22
        self.udmx.close()
23
        print("CLOSE: uDMX closed")
24
25
        # Parent
26
        super().close()
27
28
    def __connect(self):
29
        # Try to close if exists
30
        if self.udmx is not None:
31
            try:
32
                self.udmx.close()
33
            except Exception:
34
                pass
35
        # Get new device
36
        self.udmx = pyudmx.uDMXDevice()
37
        self.udmx.open()
38
39
    def _send_data(self):
40
        # Get the data
41
        data = self.get_frame()
42
43
        # Attempt to send data max 5 times, then 2 more with reconnect to device
44
        # Thanks to Dave Hocker (pyudmx author) for giving me this solution to the random usb errors
45
        success = False
46
        retry_count = 0
47
        while not success:
48
            try:
49
                if retry_count > 5:
50
                    self.__connect()
51
                self.udmx.send_multi_value(1, data)
52
                success = True
53
            except Exception as e:
54
                retry_count += 1
55
                if retry_count > 7:
56
                    raise e
57