Skip to content

Commit 4712d1a

Browse files
author
Nicolas
committed
Raise custom exception in case of connection issue
2 parents a9a05a1 + 40eec14 commit 4712d1a

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

pybecker/becker.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .database import Database
88
from .becker_helper import finalize_code
99
from .becker_helper import generate_code
10+
from .becker_helper import BeckerConnectionError
1011

1112
COMMAND_UP = 0x20
1213
COMMAND_UP2 = 0x21 # move up
@@ -54,7 +55,7 @@ def __init__(self, device_name=DEFAULT_DEVICE_NAME, init_dummy=False):
5455
"""
5556
self.is_serial = "/" in device_name
5657
if self.is_serial and not os.path.exists(device_name):
57-
raise FileExistsError(device_name + " don't exists")
58+
raise BeckerConnectionError(device_name + " is not existing")
5859
self.device = device_name
5960
self.db = Database()
6061

@@ -63,7 +64,10 @@ def __init__(self, device_name=DEFAULT_DEVICE_NAME, init_dummy=False):
6364
if not units and init_dummy:
6465
self.db.init_dummy()
6566

66-
self._connect()
67+
try:
68+
self._connect()
69+
except serial.SerialException:
70+
raise BeckerConnectionError("Error when trying to establish connection using " + device_name)
6771

6872
def _connect(self):
6973
if self.is_serial:

pybecker/becker_helper.py

+13
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,16 @@ def generate_code(channel, unit, cmd_code, with_checksum=True):
4444

4545
def finalize_code(code):
4646
return b"".join([STX, code.encode(), ETX])
47+
48+
49+
class BeckerConnectionError(Error):
50+
"""Exception raised in case of connection error (serial error).
51+
52+
Attributes:
53+
expression -- input expression in which the error occurred
54+
message -- explanation of the error
55+
"""
56+
57+
def __init__(self, expression, message):
58+
self.expression = expression
59+
self.message = message

pybecker/database.py

+19
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ def get_all_units(self):
9999

100100
return result
101101

102+
def get_rowid_from_unit(self, code, create=True):
103+
c = self.conn.cursor()
104+
res = c.execute('SELECT rowid FROM unit WHERE code = ?', (code,))
105+
106+
result = res.fetchone()
107+
rowid = result[0] if result is not None else -1
108+
109+
return rowid
110+
111+
def add_unit(self, unit):
112+
c = self.conn.cursor()
113+
c.execute("INSERT INTO unit VALUES (?, ?, ?, ?)", (unit[0], int(unit[1]), int(unit[2]), 0,))
114+
self.conn.commit()
115+
116+
def remove_unit(self, code):
117+
c = self.conn.cursor()
118+
c.execute("DELETE FROM unit WHERE code = ?", (code,))
119+
self.conn.commit()
120+
102121
def set_unit(self, unit, test=False):
103122
c = self.conn.cursor()
104123
last_run = int(time.time())

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="pybecker",
8-
version="1.0.3",
8+
version="1.0.4",
99
author="Nicolas Berthel",
1010
author_email="[email protected]",
1111
install_requires=['pyserial>=3.4'],

0 commit comments

Comments
 (0)