def read_env(which): try: data = http_get("http://127.0.0.1:9876/sensors/env").json() except Exception: print extract_traceback() text = u"读取传感器数据时出错,可能 pitools 服务未运行" else: if which in ("temp", "temperature"): text = u"温度:{:.2f}摄氏度".format(data["temperature"]) elif which == "pressure": text = u"大气压:{:.3f}千帕".format(data["pressure"] / 1000.0) elif which == "env": text = u"温度:{:.2f}摄氏度,大气压{:.3f}千帕".format(data["temperature"], data["pressure"] / 1000.0) finally: return text
def on_msg(client, config, mqtt_msg): try: msg = json.loads(mqtt_msg.payload) t_msg = telegram.Message.de_json(msg) client.logger.debug(t_msg.message_id) return_msg = {"reply_to": msg} return_msg.update(handle_client_command(t_msg)) client.publish(config.get("return_topic"), json.dumps(return_msg), qos=config.get("qos")) except Exception: client.logger.error(extract_traceback())