Browse Source

latest changes in tools

master
root 4 years ago
parent
commit
f241e7cf04
  1. 20
      tools/can2coil.py
  2. 14
      tools/coil2can.py
  3. 6
      tools/light2can.py
  4. 27
      tools/shutter2can.py

20
tools/can2coil.py

@ -70,28 +70,24 @@ def on_connect(mcp_mqtt, userdata, flags, rc):
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 = []
config = {"can_filters": can_filters, "single_handle": True}
config["interface"] = "socketcan"
config["interface"] = "socketcan_native"
config["bitrate"] = 125000
bus = Bus("can1", **config)
print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info))
canbus = can.Bus("can1", **config)
# canBuffer= canbus.BufferedReader()
print('Connected to {}: {}'.format(canbus.__class__.__name__, canbus.channel_info))
print('Can Logger (Started on {})\n'.format(datetime.now()))
mcp_mqtt = mqtt.Client()
mcp_mqtt.on_connect = on_connect
mcp_mqtt.user_data_set(bus)
mcp_mqtt.user_data_set(canbus)
mcp_mqtt.connect("mcp", 1883, 60)
try:
while True:
msg = bus.recv(1)
msg = canbus.recv(1)
# msg = canBuffer.get_message()
if msg is not None:
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]) }
@ -104,7 +100,7 @@ def main():
except KeyboardInterrupt:
pass
finally:
bus.shutdown()
canbus.shutdown()
if __name__ == "__main__":
main()

14
tools/coil2can.py

@ -28,8 +28,10 @@ coil_map = {
'SCH': [ 0x01, 0x08 ],
'B1': [ 0x01, 0x01 ],
'B2': [ 0x01, 0x02 ],
'ROL_EZ_DOWN': [0x03, 0x05, -1],
'ROL_EZ_UP': [0x03,0x04, -1],
'ROL_EZ_G_DOWN': [0x03, 0x05, -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 ],
'EG2': [ 0x03, 0x09 ],
'OG': [ 0x01, 0x09 ],
@ -82,6 +84,7 @@ def on_message(mcp_mqtt, userdata, msg):
coil=sub
print("substring: "+coil)
addr = coil_map[coil]
print("Addr:", addr)
if msg_cmd == 2:
msg_data.append(value)
print("msg_cmd",msg_cmd, len(addr))
@ -89,7 +92,8 @@ def on_message(mcp_mqtt, userdata, msg):
print("hello 001")
for key, value in coil_map.items():
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]:
msg_id=con(msg_dst=value[0],msg_cmd=0)
msg_data=[value[1]]
@ -135,10 +139,10 @@ def main():
mcp_mqtt.on_message = on_message
mcp_mqtt.user_data_set(bus)
mcp_mqtt.connect("mcp", 1883, 60)
# mcp_mqtt.loop_start()
try:
while True:
mcp_mqtt.loop_start()
mcp_mqtt.loop()
except KeyboardInterrupt:
pass
finally:

6
tools/light2can.py

@ -123,7 +123,7 @@ def on_message(mcp_mqtt, userdata, msg):
if light == 'ALL':
print("ALL detected")
for key in light_map:
mcp_mqtt.publish("light/"+key+"/state", msg_cmd, retain=1)
mcp_mqtt.publish("light/"+key+"/state", msg_cmd, retain=1)
def main():
@ -134,7 +134,7 @@ def main():
can_filters = []
config = {"can_filters": can_filters, "single_handle": True}
config["interface"] = "socketcan"
config["interface"] = "socketcan_native"
config["bitrate"] = 125000
bus = Bus("can1", **config)
@ -146,10 +146,10 @@ def main():
mcp_mqtt.on_message = on_message
mcp_mqtt.user_data_set(bus)
mcp_mqtt.connect("mcp", 1883, 60)
mcp_mqtt.loop_start()
try:
while True:
mcp_mqtt.loop_start()
msg = bus.recv(1)
if msg is not None:
de=decon(msg.arbitration_id)

27
tools/roll2can.py → tools/shutter2can.py

@ -12,22 +12,25 @@ from can import Bus, BusState, Logger
import paho.mqtt.client as mqtt
sub_topic = "roll"
sub_topic = "shutter"
aliases = {
'Esszimmer': 'EZ',
'Esszimmer_Garten': 'EZ_G',
'Esszimmer_Nachbar': 'EZ_N',
'Wohnzimmer1': 'WZ1',
'Wohnzimmer2': 'WZ2',
'Wohnzimmer3': 'WZ3',
'Wohnzimmer4': 'WZ4',
}
event_map = {
'stop': 0,
'off': 0,
'up': 1,
'down': 2
}
coil_map = {
'EZ': [ 0x03, 0x04 , 0x03, 0x05 ],
coil_map = {
# up , down
'EZ_G': [ 0x03, 0x04 , 0x03, 0x05 ],
'EZ_N': [ 0x03, 0x06 , 0x03, 0x08 ],
'WZ1': [ 0x00, 0x01 , 0x00, 0x01 ],
'WZ2': [ 0x00, 0x01 , 0x00, 0x01 ],
'WZ3': [ 0x00, 0x01 , 0x00, 0x01 ],
@ -75,11 +78,11 @@ def on_message(mcp_mqtt, userdata, msg):
data=m_decode
sub=msg.topic[len(sub_topic)+1:]
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
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():
msg_id=con(msg_dst=value[0],msg_cmd=0)
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))
print("data sent to CAN",m)
if event is not 0:
print("turning on", coil)
addr = coils[event-1]
addr = coils[data]
print("turning on: ",addr[0],addr[1])
msg_id=con(msg_dst=addr[0],msg_cmd=1)
msg_data=[addr[1]]
print(msg_data)
@ -103,7 +106,7 @@ def on_message(mcp_mqtt, userdata, msg):
data=msg_data,
extended_id=True)
print("going to sent to CAN",m)
try:
local_bus.send(m)
except BaseException as e:
Loading…
Cancel
Save