Browse Source

latest changes in tools

master
root 5 years ago
parent
commit
f241e7cf04
  1. 20
      tools/can2coil.py
  2. 12
      tools/coil2can.py
  3. 4
      tools/light2can.py
  4. 23
      tools/shutter2can.py

20
tools/can2coil.py

@ -70,28 +70,24 @@ def on_connect(mcp_mqtt, userdata, flags, rc):
def main(): def main():
verbosity = 2
logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
can.set_logging_level(logging_level_name)
can_filters = [] can_filters = []
config = {"can_filters": can_filters, "single_handle": True} config = {"can_filters": can_filters, "single_handle": True}
config["interface"] = "socketcan" config["interface"] = "socketcan_native"
config["bitrate"] = 125000 config["bitrate"] = 125000
bus = Bus("can1", **config) canbus = can.Bus("can1", **config)
# canBuffer= canbus.BufferedReader()
print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info)) print('Connected to {}: {}'.format(canbus.__class__.__name__, canbus.channel_info))
print('Can Logger (Started on {})\n'.format(datetime.now())) print('Can Logger (Started on {})\n'.format(datetime.now()))
mcp_mqtt = mqtt.Client() mcp_mqtt = mqtt.Client()
mcp_mqtt.on_connect = on_connect mcp_mqtt.on_connect = on_connect
mcp_mqtt.user_data_set(bus) mcp_mqtt.user_data_set(canbus)
mcp_mqtt.connect("mcp", 1883, 60) mcp_mqtt.connect("mcp", 1883, 60)
try: try:
while True: while True:
msg = bus.recv(1) msg = canbus.recv(1)
# msg = canBuffer.get_message()
if msg is not None: if msg is not None:
de=decon(msg.arbitration_id) de=decon(msg.arbitration_id)
m= { "prio": hex(de[0]), "type": hex(de[1]), "dst": hex(de[2]), "src": hex(de[3]), "cmd": hex(de[4]), "action": hex(msg.data[0]) } m= { "prio": hex(de[0]), "type": hex(de[1]), "dst": hex(de[2]), "src": hex(de[3]), "cmd": hex(de[4]), "action": hex(msg.data[0]) }
@ -104,7 +100,7 @@ def main():
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
bus.shutdown() canbus.shutdown()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

12
tools/coil2can.py

@ -28,8 +28,10 @@ coil_map = {
'SCH': [ 0x01, 0x08 ], 'SCH': [ 0x01, 0x08 ],
'B1': [ 0x01, 0x01 ], 'B1': [ 0x01, 0x01 ],
'B2': [ 0x01, 0x02 ], 'B2': [ 0x01, 0x02 ],
'ROL_EZ_DOWN': [0x03, 0x05, -1], 'ROL_EZ_G_DOWN': [0x03, 0x05, -1],
'ROL_EZ_UP': [0x03,0x04, -1], 'ROL_EZ_G_UP': [0x03,0x04, -1],
'ROL_EZ_N_DOWN': [0x03, 0x06, -2],
'ROL_EZ_N_UP': [0x03,0x08, -2],
'EG1': [ 0x02, 0x09 ], 'EG1': [ 0x02, 0x09 ],
'EG2': [ 0x03, 0x09 ], 'EG2': [ 0x03, 0x09 ],
'OG': [ 0x01, 0x09 ], 'OG': [ 0x01, 0x09 ],
@ -82,6 +84,7 @@ def on_message(mcp_mqtt, userdata, msg):
coil=sub coil=sub
print("substring: "+coil) print("substring: "+coil)
addr = coil_map[coil] addr = coil_map[coil]
print("Addr:", addr)
if msg_cmd == 2: if msg_cmd == 2:
msg_data.append(value) msg_data.append(value)
print("msg_cmd",msg_cmd, len(addr)) print("msg_cmd",msg_cmd, len(addr))
@ -90,6 +93,7 @@ def on_message(mcp_mqtt, userdata, msg):
for key, value in coil_map.items(): for key, value in coil_map.items():
print("checking: ", key) print("checking: ", key)
if len(value) is 3: if len(value) is 3:
print("value_2 is", value[2], "Addr: ", addr[2])
if value[2] is addr[2]: if value[2] is addr[2]:
msg_id=con(msg_dst=value[0],msg_cmd=0) msg_id=con(msg_dst=value[0],msg_cmd=0)
msg_data=[value[1]] msg_data=[value[1]]
@ -135,10 +139,10 @@ def main():
mcp_mqtt.on_message = on_message mcp_mqtt.on_message = on_message
mcp_mqtt.user_data_set(bus) mcp_mqtt.user_data_set(bus)
mcp_mqtt.connect("mcp", 1883, 60) mcp_mqtt.connect("mcp", 1883, 60)
# mcp_mqtt.loop_start()
try: try:
while True: while True:
mcp_mqtt.loop_start() mcp_mqtt.loop()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:

4
tools/light2can.py

@ -134,7 +134,7 @@ def main():
can_filters = [] can_filters = []
config = {"can_filters": can_filters, "single_handle": True} config = {"can_filters": can_filters, "single_handle": True}
config["interface"] = "socketcan" config["interface"] = "socketcan_native"
config["bitrate"] = 125000 config["bitrate"] = 125000
bus = Bus("can1", **config) bus = Bus("can1", **config)
@ -146,10 +146,10 @@ def main():
mcp_mqtt.on_message = on_message mcp_mqtt.on_message = on_message
mcp_mqtt.user_data_set(bus) mcp_mqtt.user_data_set(bus)
mcp_mqtt.connect("mcp", 1883, 60) mcp_mqtt.connect("mcp", 1883, 60)
mcp_mqtt.loop_start()
try: try:
while True: while True:
mcp_mqtt.loop_start()
msg = bus.recv(1) msg = bus.recv(1)
if msg is not None: if msg is not None:
de=decon(msg.arbitration_id) de=decon(msg.arbitration_id)

23
tools/roll2can.py → tools/shutter2can.py

@ -12,22 +12,25 @@ from can import Bus, BusState, Logger
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
sub_topic = "roll" sub_topic = "shutter"
aliases = { aliases = {
'Esszimmer': 'EZ', 'Esszimmer_Garten': 'EZ_G',
'Esszimmer_Nachbar': 'EZ_N',
'Wohnzimmer1': 'WZ1', 'Wohnzimmer1': 'WZ1',
'Wohnzimmer2': 'WZ2', 'Wohnzimmer2': 'WZ2',
'Wohnzimmer3': 'WZ3', 'Wohnzimmer3': 'WZ3',
'Wohnzimmer4': 'WZ4', 'Wohnzimmer4': 'WZ4',
} }
event_map = { event_map = {
'stop': 0,
'off': 0, 'off': 0,
'up': 1, 'up': 1,
'down': 2 'down': 2
} }
coil_map = { coil_map = {
'EZ': [ 0x03, 0x04 , 0x03, 0x05 ], # up , down
'EZ_G': [ 0x03, 0x04 , 0x03, 0x05 ],
'EZ_N': [ 0x03, 0x06 , 0x03, 0x08 ],
'WZ1': [ 0x00, 0x01 , 0x00, 0x01 ], 'WZ1': [ 0x00, 0x01 , 0x00, 0x01 ],
'WZ2': [ 0x00, 0x01 , 0x00, 0x01 ], 'WZ2': [ 0x00, 0x01 , 0x00, 0x01 ],
'WZ3': [ 0x00, 0x01 , 0x00, 0x01 ], 'WZ3': [ 0x00, 0x01 , 0x00, 0x01 ],
@ -75,11 +78,11 @@ def on_message(mcp_mqtt, userdata, msg):
data=m_decode data=m_decode
sub=msg.topic[len(sub_topic)+1:] sub=msg.topic[len(sub_topic)+1:]
event=event_map[data] event=event_map[data]
coils={
'up': [ coil_map[sub][0], coil_map[sub][1] ],
'down': [ coil_map[sub][2], coil_map[sub][3] ]
}
if event is not None: # it's 0,1,2 if event is not None: # it's 0,1,2
coils={
'up': [ coil_map[sub][0], coil_map[sub][1] ],
'down': [ coil_map[sub][2], coil_map[sub][3] ]
}
for key, value in coils.items(): for key, value in coils.items():
msg_id=con(msg_dst=value[0],msg_cmd=0) msg_id=con(msg_dst=value[0],msg_cmd=0)
msg_data=[value[1]] msg_data=[value[1]]
@ -94,8 +97,8 @@ def on_message(mcp_mqtt, userdata, msg):
logging.error("Error sending can message {%s}: %s" % (m, e)) logging.error("Error sending can message {%s}: %s" % (m, e))
print("data sent to CAN",m) print("data sent to CAN",m)
if event is not 0: if event is not 0:
print("turning on", coil) addr = coils[data]
addr = coils[event-1] print("turning on: ",addr[0],addr[1])
msg_id=con(msg_dst=addr[0],msg_cmd=1) msg_id=con(msg_dst=addr[0],msg_cmd=1)
msg_data=[addr[1]] msg_data=[addr[1]]
print(msg_data) print(msg_data)
Loading…
Cancel
Save