def send_state(self, state, time_like=None):
""" Reports the given state to Losant for this device """
LOGGER.debug("Sending state for %s", self._device_id)
if not self._mqtt_client:
return False
if isinstance(time_like, datetime.datetime):
# getting utc tuple, and so use timegm
seconds = calendar.timegm(time_like.utctimetuple())
millis = time_like.microsecond / 1000
time_like = int(seconds * 1000 + millis)
if isinstance(time_like, time.struct_time):
# don't know the timezone, assume it is local and use mktime
time_like = int(time.mktime(time_like) * 1000)
if not time_like:
time_like = int(time.time() * 1000)
payload = json.dumps({"time": time_like, "data": state}, sort_keys=True)
result = self._mqtt_client.publish(self._state_topic(), payload)
return mqtt.MQTT_ERR_SUCCESS == result
# ============================================================
# Private functions
# ============================================================
评论列表
文章目录