|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os.path |
| 4 | +from requests import ConnectionError |
| 5 | +import rospy |
| 6 | +from switchbot_ros.switchbot import SwitchBotAPIClient |
| 7 | +from switchbot_ros.switchbot import DeviceError, SwitchBotAPIError |
| 8 | +from switchbot_ros.msg import Meter, PlugMini, Hub2, Bot, StripLight |
| 9 | + |
| 10 | + |
| 11 | +class SwitchBotStatusPublisher: |
| 12 | + """ |
| 13 | + Publissh your switchbot status with ROS and SwitchBot API |
| 14 | + """ |
| 15 | + def __init__(self): |
| 16 | + # SwitchBot configs |
| 17 | + # '~token' can be file path or raw characters |
| 18 | + token = rospy.get_param('~token') |
| 19 | + if os.path.exists(token): |
| 20 | + with open(token) as f: |
| 21 | + self.token = f.read().replace('\n', '') |
| 22 | + else: |
| 23 | + self.token = token |
| 24 | + |
| 25 | + # Switchbot API v1.1 needs secret key |
| 26 | + secret = rospy.get_param('~secret', None) |
| 27 | + if secret is not None and os.path.exists(secret): |
| 28 | + with open(secret, 'r', encoding='utf-8') as f: |
| 29 | + self.secret = f.read().replace('\n', '') |
| 30 | + else: |
| 31 | + self.secret = secret |
| 32 | + |
| 33 | + # Initialize switchbot client |
| 34 | + self.bots = self.get_switchbot_client() |
| 35 | + self.print_apiversion() |
| 36 | + |
| 37 | + # Get parameters for publishing |
| 38 | + self.rate = rospy.get_param('~rate', 0.1) |
| 39 | + rospy.loginfo('Rate: ' + str(self.rate)) |
| 40 | + |
| 41 | + device_name = rospy.get_param('~device_name') |
| 42 | + if device_name: |
| 43 | + self.device_name = device_name |
| 44 | + else: |
| 45 | + rospy.logerr('No Device Name') |
| 46 | + return |
| 47 | + |
| 48 | + self.device_type = None |
| 49 | + self.device_list = sorted( |
| 50 | + self.bots.device_list, |
| 51 | + key=lambda device: str(device.get('deviceName'))) |
| 52 | + for device in self.device_list: |
| 53 | + device_name = str(device.get('deviceName')) |
| 54 | + if self.device_name == device_name: |
| 55 | + self.device_type = str(device.get('deviceType')) |
| 56 | + |
| 57 | + if self.device_type: |
| 58 | + rospy.loginfo('deviceName: ' + self.device_name + ' / deviceType: ' + self.device_type) |
| 59 | + else: |
| 60 | + rospy.logerr('Invalid Device Name: ' + self.device_name) |
| 61 | + return |
| 62 | + |
| 63 | + topic_name = '~' + self.device_name |
| 64 | + topic_name = topic_name.replace('-', '_') |
| 65 | + |
| 66 | + # Publisher Message Class for each device type |
| 67 | + if self.device_type == 'Remote': |
| 68 | + rospy.logerr('Device Type: "' + self.device_type + '" has no status in specifications.') |
| 69 | + return |
| 70 | + else: |
| 71 | + if self.device_type == 'Meter': |
| 72 | + self.msg_class = Meter |
| 73 | + elif self.device_type == 'MeterPlus': |
| 74 | + self.msg_class = Meter |
| 75 | + elif self.device_type == 'WoIOSensor': |
| 76 | + self.msg_class = Meter |
| 77 | + elif self.device_type == 'Hub 2': |
| 78 | + self.msg_class = Hub2 |
| 79 | + elif self.device_type == 'Plug Mini (JP)': |
| 80 | + self.msg_class = PlugMini |
| 81 | + elif self.device_type == 'Plug Mini (US)': |
| 82 | + self.msg_class = PlugMini |
| 83 | + elif self.device_type == 'Bot': |
| 84 | + self.msg_class = Bot |
| 85 | + elif self.device_type == 'Strip Light': |
| 86 | + self.msg_class = StripLight |
| 87 | + else: |
| 88 | + rospy.logerr('No publisher process for "' + self.device_type + '" in switchbot_status_publisher.py') |
| 89 | + return |
| 90 | + |
| 91 | + self.status_pub = rospy.Publisher(topic_name, self.msg_class, queue_size=1, latch=True) |
| 92 | + |
| 93 | + rospy.loginfo('Ready: SwitchBot Status Publisher for ' + self.device_name) |
| 94 | + |
| 95 | + |
| 96 | + def get_switchbot_client(self): |
| 97 | + try: |
| 98 | + client = SwitchBotAPIClient(token=self.token, secret=self.secret) |
| 99 | + rospy.loginfo('Switchbot API Client initialized.') |
| 100 | + return client |
| 101 | + except ConnectionError: # If the machine is not connected to the internet |
| 102 | + rospy.logwarn_once('Failed to connect to the switchbot server. The client would try connecting to it when subscribes the ActionGoal topic.') |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | + def spin(self): |
| 107 | + rate = rospy.Rate(self.rate) |
| 108 | + while not rospy.is_shutdown(): |
| 109 | + rate.sleep() |
| 110 | + if self.bots is None: |
| 111 | + self.bots = self.get_switchbot_client() |
| 112 | + |
| 113 | + if self.device_type == 'Remote': |
| 114 | + return |
| 115 | + else: |
| 116 | + status = self.get_device_status(device_name=self.device_name) |
| 117 | + |
| 118 | + if status: |
| 119 | + time = rospy.get_rostime() |
| 120 | + if self.msg_class == Meter: |
| 121 | + msg = Meter() |
| 122 | + msg.header.stamp = time |
| 123 | + msg.temperature = status['temperature'] |
| 124 | + msg.humidity = status['humidity'] |
| 125 | + msg.battery = status['battery'] |
| 126 | + elif self.msg_class == Hub2: |
| 127 | + msg = Hub2() |
| 128 | + msg.header.stamp = time |
| 129 | + msg.temperature = status['temperature'] |
| 130 | + msg.humidity = status['humidity'] |
| 131 | + msg.light_level = status['lightLevel'] |
| 132 | + elif self.msg_class == PlugMini: |
| 133 | + msg = PlugMini() |
| 134 | + msg.header.stamp = time |
| 135 | + msg.voltage = status['voltage'] |
| 136 | + msg.weight = status['weight'] |
| 137 | + msg.current = status['electricCurrent'] |
| 138 | + msg.minutes_day = status['electricityOfDay'] |
| 139 | + elif self.msg_class == Bot: |
| 140 | + msg = Bot() |
| 141 | + msg.header.stamp = time |
| 142 | + msg.battery = status['battery'] |
| 143 | + if status['power'] == 'on': |
| 144 | + msg.power = True |
| 145 | + else: |
| 146 | + msg.power = False |
| 147 | + msg.device_mode = status['deviceMode'] |
| 148 | + elif self.msg_class == StripLight: |
| 149 | + msg = StripLight() |
| 150 | + msg.header.stamp = time |
| 151 | + if status['power'] == 'on': |
| 152 | + msg.power = True |
| 153 | + else: |
| 154 | + msg.power = False |
| 155 | + msg.brightness = status['brightness'] |
| 156 | + rgb_string = status['color'] |
| 157 | + r, g, b = map(int, rgb_string.split(':')) |
| 158 | + msg.color_r = r |
| 159 | + msg.color_g = g |
| 160 | + msg.color_b = b |
| 161 | + else: |
| 162 | + return |
| 163 | + |
| 164 | + if msg: |
| 165 | + self.status_pub.publish(msg) |
| 166 | + |
| 167 | + |
| 168 | + def get_device_status(self, device_name=None): |
| 169 | + if self.bots is None: |
| 170 | + return |
| 171 | + elif device_name: |
| 172 | + status = self.bots.device_status(device_name=device_name) |
| 173 | + return status |
| 174 | + else: |
| 175 | + return |
| 176 | + |
| 177 | + |
| 178 | + def print_apiversion(self): |
| 179 | + if self.bots is None: |
| 180 | + return |
| 181 | + |
| 182 | + apiversion_str = 'Using SwitchBot API '; |
| 183 | + apiversion_str += self.bots.api_version; |
| 184 | + rospy.loginfo(apiversion_str) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == '__main__': |
| 188 | + try: |
| 189 | + rospy.init_node('switchbot_status_publisher') |
| 190 | + ssp = SwitchBotStatusPublisher() |
| 191 | + ssp.spin() |
| 192 | + except rospy.ROSInterruptException: |
| 193 | + pass |
0 commit comments