Passed
Pull Request — master (#19)
by Matt
01:42
created

uDMXController.__connect()   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
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
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
                self.udmx.send_multi_value(1, data)
50
                success = True
51
            except Exception as e:
52
                retry_count += 1
53
                if retry_count > 5:
54
55
                    # Attempt to reconnect and then send data max 2 times
56
                    success_reconn = False
57
                    retry_count_reconn = 0
58
                    while not success_reconn:
59
                        try:
60
                            self.__connect()
61
                            self.udmx.send_multi_value(1, data)
62
                            success_reconn = True
63
                        except Exception:
64
                            retry_count_reconn += 1
65
                            if retry_count_reconn > 2:
66
                                raise e
67