No Description

sunevents2can.py 3.9KB

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