No Description

roll2can.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python3
  2. from __future__ import absolute_import, print_function
  3. import sys
  4. import argparse
  5. import socket
  6. from datetime import datetime
  7. import can
  8. from can import Bus, BusState, Logger
  9. import paho.mqtt.client as mqtt
  10. sub_topic = "roll"
  11. aliases = {
  12. 'Esszimmer': 'EZ',
  13. 'Wohnzimmer1': 'WZ1',
  14. 'Wohnzimmer2': 'WZ2',
  15. 'Wohnzimmer3': 'WZ3',
  16. 'Wohnzimmer4': 'WZ4',
  17. }
  18. coil_map = {
  19. 'EZ': [ 0x03, 0x04 , 0x03, 0x05 ],
  20. 'WZ1': [ 0x00, 0x01 , 0x00, 0x01 ],
  21. 'WZ2': [ 0x00, 0x01 , 0x00, 0x01 ],
  22. 'WZ3': [ 0x00, 0x01 , 0x00, 0x01 ],
  23. 'WZ4': [ 0x00, 0x01 , 0x00, 0x01 ],
  24. }
  25. cmd_map = {
  26. 'off': 0,
  27. 'on': 1,
  28. 'value': 2,
  29. 'toggle': 3
  30. }
  31. def decon(msg_id):
  32. msg_prio=msg_id>>26
  33. msg_type=(msg_id>>24) & 0x03
  34. msg_dst=(msg_id>>16) & 0xFF
  35. msg_src=(msg_id>>8) & 0xFF
  36. msg_cmd=msg_id & 0xFF
  37. print("msg_id: ", hex(msg_id), "prio: ", hex(msg_prio),"type: ", hex(msg_type)," dst: ", hex(msg_dst)," src: ", hex(msg_src)," cmd: ",hex(msg_cmd))
  38. return [msg_prio, msg_type, msg_dst, msg_src, msg_cmd]
  39. def con(msg_dst,msg_cmd,msg_prio=3,msg_type=0,msg_src=11, ):
  40. msg_id=(msg_prio<<26) + \
  41. ((msg_type&0x03)<<24) +\
  42. ((msg_dst&0xFF)<<16) +\
  43. ((msg_src&0xFF)<<8) +\
  44. (msg_cmd&0xFF)
  45. print("Constructed ID: "+hex(msg_id))
  46. return msg_id
  47. def on_connect(mcp_mqtt, userdata, flags, rc):
  48. print("Connected with result code "+str(rc))
  49. # Subscribing in on_connect() means that if we lose the connection and
  50. # reconnect then subscriptions will be renewed.
  51. mcp_mqtt.subscribe(sub_topic+"/+")
  52. print("Subscribed to:"+sub_topic+"/+")
  53. def on_message(mcp_mqtt, userdata, msg):
  54. local_bus=userdata
  55. print("data Received topic: ", msg.topic)
  56. m_decode=str(msg.payload.decode("utf-8","ignore"))
  57. print("data Received",m_decode)
  58. msg_cmd=2
  59. try:
  60. value=int(m_decode)
  61. except ValueError:
  62. msg_cmd=cmd_map[m_decode]
  63. sub=msg.topic[len(sub_topic)+1:]
  64. roll=aliases.get(sub,None)
  65. if roll is None:
  66. roll=sub
  67. print("substring: "+roll)
  68. addr = light_map[light]
  69. msg_id=con(msg_dst=addr[0],msg_cmd=msg_cmd)
  70. msg_data=[addr[1]]
  71. print(msg_data)
  72. if msg_cmd == 2:
  73. msg_data.append(value)
  74. print(msg_data)
  75. m = can.Message(arbitration_id=msg_id,
  76. data=msg_data,
  77. extended_id=True)
  78. print("going to sent to CAN",m)
  79. try:
  80. local_bus.send(m)
  81. except BaseException as e:
  82. logging.error("Error sending can message {%s}: %s" % (m, e))
  83. print("data sent to CAN",m)
  84. def main():
  85. verbosity = 2
  86. logging_level_name = ['critical', 'error', 'warning', 'info', 'debug', 'subdebug'][min(5, verbosity)]
  87. can.set_logging_level(logging_level_name)
  88. can_filters = []
  89. config = {"can_filters": can_filters, "single_handle": True}
  90. config["interface"] = "socketcan"
  91. config["bitrate"] = 125000
  92. bus = Bus("can1", **config)
  93. print('Connected to {}: {}'.format(bus.__class__.__name__, bus.channel_info))
  94. print('Can Logger (Started on {})\n'.format(datetime.now()))
  95. mcp_mqtt = mqtt.Client()
  96. mcp_mqtt.on_connect = on_connect
  97. mcp_mqtt.on_message = on_message
  98. mcp_mqtt.user_data_set(bus)
  99. mcp_mqtt.connect("mcp", 1883, 60)
  100. try:
  101. while True:
  102. mcp_mqtt.loop_start()
  103. except KeyboardInterrupt:
  104. pass
  105. finally:
  106. bus.shutdown()
  107. if __name__ == "__main__":
  108. main()