Skip to content

Commit c656e6b

Browse files
author
Nicolas
committed
Integrate latest centronic-py improvements
1 parent e1e4eb1 commit c656e6b

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

pybecker/becker.py

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import logging
22
import os
3-
import time
4-
import socket
3+
import re
54
import serial
5+
import socket
6+
import time
67

7-
from .database import Database
88
from .becker_helper import finalize_code
99
from .becker_helper import generate_code
1010
from .becker_helper import BeckerConnectionError
11+
from .database import Database
1112

1213
COMMAND_UP = 0x20
1314
COMMAND_UP2 = 0x21 # move up
@@ -102,6 +103,9 @@ async def run_codes(self, channel, unit, cmd, test):
102103
_LOGGER.error("The unit %s is not configured" % (unit[0]))
103104
return
104105

106+
# move up/down dependent on given time
107+
mt = re.match(r"(DOWN|UP):(\d+)", cmd)
108+
105109
codes = []
106110
if cmd == "UP":
107111
codes.append(generate_code(channel, unit, COMMAND_UP))
@@ -114,12 +118,8 @@ async def run_codes(self, channel, unit, cmd, test):
114118
elif cmd == "DOWN2":
115119
codes.append(generate_code(channel, unit, COMMAND_DOWN5))
116120
elif cmd == "TRAIN":
117-
codes.append(generate_code(channel, unit, COMMAND_PAIR))
118-
unit[1] += 1
119121
codes.append(generate_code(channel, unit, COMMAND_PAIR2))
120122
unit[1] += 1
121-
codes.append(generate_code(channel, unit, COMMAND_PAIR))
122-
unit[1] += 1
123123
codes.append(generate_code(channel, unit, COMMAND_PAIR2))
124124
# set unit as configured
125125
unit[2] = 1
@@ -134,24 +134,38 @@ async def run_codes(self, channel, unit, cmd, test):
134134
unit[1] += 1
135135
codes.append(generate_code(channel, unit, COMMAND_CLEARPOS4))
136136
elif cmd == "REMOVE":
137-
codes.append(generate_code(channel, unit, COMMAND_PAIR))
138-
unit[1] += 1
139137
codes.append(generate_code(channel, unit, COMMAND_PAIR2))
140138
unit[1] += 1
141-
codes.append(generate_code(channel, unit, COMMAND_PAIR))
142-
unit[1] += 1
143139
codes.append(generate_code(channel, unit, COMMAND_PAIR2))
144140
unit[1] += 1
145141
codes.append(generate_code(channel, unit, COMMAND_PAIR3))
146142
unit[1] += 1
147143
codes.append(generate_code(channel, unit, COMMAND_PAIR4))
144+
unit[2] = 0
148145

149-
unit[1] += 1
146+
if mt:
147+
_LOGGER.INFO("Moving %s for %s seconds..." % (mt.group(1), mt.group(2)))
148+
# move down/up for a specific time
149+
if mt.group(1) == "UP":
150+
code = generate_code(channel, unit, COMMAND_UP)
151+
elif mt.group(1) == "DOWN":
152+
code = generate_code(channel, unit, COMMAND_DOWN)
150153

151-
# append the release button code
152-
codes.append(generate_code(channel, unit, 0))
154+
unit[1] += 1
155+
await self.write([code])
156+
157+
time.sleep(int(mt.group(2)))
153158

154-
unit[1] += 1
159+
# stop moving
160+
code = generate_code(channel, unit, COMMAND_HALT)
161+
unit[1] += 1
162+
await self.write([code])
163+
else:
164+
unit[1] += 1
165+
166+
# append the release button code
167+
#codes.append(generate_code(channel, unit, 0))
168+
#unit[1] += 1
155169

156170
await self.write(codes)
157171
self.db.set_unit(unit, test)

pybecker/becker_helper.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,15 @@ def generate_code(channel, unit, cmd_code, with_checksum=True):
3737
unit_id = unit[0] # contains the unit code in hex (5 chars)
3838
unit_inc = unit[1] # contains the next increment (required to convert into hex4)
3939

40-
code = CODE_PREFIX + hex4(unit_inc) + CODE_SUFFIX + unit_id + CODE_21 + CODE_REMOTE + hex2(channel) + '00' \
41-
+ hex2(cmd_code)
40+
if channel == 0:
41+
# channel 0 may be used for wall mounted sender (primary used as master sender)
42+
code = CODE_PREFIX + hex4(unit_inc) + CODE_SUFFIX + unit_id + CODE_21 + "00" + hex2(channel) + '00' + hex2(
43+
cmd_code)
44+
else:
45+
code = CODE_PREFIX + hex4(unit_inc) + CODE_SUFFIX + unit_id + CODE_21 + CODE_REMOTE + hex2(channel) + '00' \
46+
+ hex2(cmd_code)
4247
return checksum(code) if with_checksum else code
4348

44-
4549
def finalize_code(code):
4650
return b"".join([STX, code.encode(), ETX])
4751

pybecker/database.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import sqlite3
55
from random import randrange
6+
from .becker_helper import hex4
67

78
NUMBER_FILE = "centronic-stick.num"
89

@@ -74,12 +75,14 @@ def output(self):
7475
c = self.conn.cursor()
7576
res = c.execute('SELECT * FROM unit')
7677
_LOGGER.info('%-10s%-10s%-12s%-15s' % ('code', 'increment', 'configured', 'last run'))
78+
_LOGGER.info('%-10s%-18s%-12s%-15s' % ('code', 'increment (hex)', 'configured', 'last run'))
7779
for line in res.fetchall():
7880
last_run = '(unknown)'
7981

8082
if line[3] > 0:
8183
last_run = time.strftime('%Y-%m-%d %H:%M', time.localtime(line[3]))
8284
_LOGGER.info('%-10s%-10s%-12s%-15s' % (line[0], line[1], line[2], last_run))
85+
_LOGGER.info('%-10s%-6s%-12s%-12s%-15s' % (line[0], line[1], "(0x" + hex4(line[1]) + ")", line[2], last_run))
8386

8487
def get_unit(self, rowid):
8588
c = self.conn.cursor()
@@ -122,8 +125,17 @@ def set_unit(self, unit, test=False):
122125
c = self.conn.cursor()
123126
last_run = int(time.time())
124127

125-
c.execute('UPDATE unit SET increment = ?, configured = ?, executed = ? WHERE code = ?',
126-
(unit[1], unit[2], last_run, unit[0],))
128+
#c.execute('UPDATE unit SET increment = ?, configured = ?, executed = ? WHERE code = ?',
129+
# (unit[1], unit[2], last_run, unit[0],))
130+
131+
if len(unit[0]) < 5:
132+
# assume the index is given (and not the exact unit)
133+
c.execute('UPDATE unit SET increment = ?, configured = ?, executed = ? '
134+
'WHERE code = (SELECT code FROM unit LIMIT 1 OFFSET ?)',
135+
(unit[1], unit[2], last_run, int(unit[0]) - 1,))
136+
else:
137+
c.execute('UPDATE unit SET increment = ?, configured = ?, executed = ? WHERE code = ?',
138+
(unit[1], unit[2], last_run, unit[0],))
127139

128140
if test:
129141
self.conn.rollback()

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.4",
8+
version="1.0.5",
99
author="Nicolas Berthel",
1010
author_email="[email protected]",
1111
install_requires=['pyserial>=3.4'],

0 commit comments

Comments
 (0)