-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path__init__.py
153 lines (121 loc) · 5.17 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
from eventlet import Queue
from modules import cbpi, app, ActorBase
from modules.core.hardware import SensorActive
import json
import os, re, threading, time
from modules.core.props import Property
q = Queue()
def on_connect(client, userdata, flags, rc):
print("MQTT Connected" + str(rc))
class MQTTThread (threading.Thread):
def __init__(self,server,port,username,password,tls):
threading.Thread.__init__(self)
self.server = server
self.port = port
self.username = username
self.password = password
self.tls = tls
client = None
def run(self):
self.client = mqtt.Client()
self.client.on_connect = on_connect
if self.username != "username" and self.password != "password":
self.client.username_pw_set(self.username, self.password)
if self.tls.lower() == 'true':
self.client.tls_set_context(context=None)
self.client.connect(str(self.server), int(self.port), 60)
self.client.loop_forever()
@cbpi.actor
class MQTTActor(ActorBase):
topic = Property.Text("Topic", configurable=True, default_value="", description="MQTT TOPIC")
def on(self, power=100):
self.api.cache["mqtt"].client.publish(self.topic, payload=json.dumps({"state": "on"}), qos=2, retain=True)
def off(self):
self.api.cache["mqtt"].client.publish(self.topic, payload=json.dumps({"state": "off"}), qos=2, retain=True)
@cbpi.actor
class ESPEasyMQTT(ActorBase):
topic = Property.Text("Topic", configurable=True, default_value="", description="MQTT TOPIC")
def on(self, power=100):
self.api.cache["mqtt"].client.publish(self.topic, payload=1, qos=2, retain=True)
def off(self):
self.api.cache["mqtt"].client.publish(self.topic, payload=0, qos=2, retain=True)
@cbpi.sensor
class MQTT_SENSOR(SensorActive):
a_topic = Property.Text("Topic", configurable=True, default_value="", description="MQTT TOPIC")
b_payload = Property.Text("Payload Dictioanry", configurable=True, default_value="", description="Where to find msg in patload, leave blank for raw payload")
c_unit = Property.Text("Unit", configurable=True, default_value="", description="Units to display")
last_value = None
def init(self):
self.topic = self.a_topic
if self.b_payload == "":
self.payload_text = None
else:
self.payload_text = self.b_payload.split('.')
self.unit = self.c_unit[0:3]
SensorActive.init(self)
def on_message(client, userdata, msg):
try:
print "payload " + msg.payload
json_data = json.loads(msg.payload)
#print json_data
val = json_data
if self.payload_text is not None:
for key in self.payload_text:
val = val.get(key, None)
#print val
if isinstance(val, (int, float, basestring)):
q.put({"id": on_message.sensorid, "value": val})
except Exception as e:
print e
on_message.sensorid = self.id
self.api.cache["mqtt"].client.subscribe(self.topic)
self.api.cache["mqtt"].client.message_callback_add(self.topic, on_message)
def get_value(self):
return {"value": self.last_value, "unit": self.unit}
def get_unit(self):
return self.unit
def stop(self):
self.api.cache["mqtt"].client.unsubscribe(self.topic)
SensorActive.stop(self)
def execute(self):
'''
Active sensor has to handle his own loop
:return:
'''
self.sleep(5)
@cbpi.initalizer(order=0)
def initMQTT(app):
server = app.get_config_parameter("MQTT_SERVER",None)
if server is None:
server = "localhost"
cbpi.add_config_parameter("MQTT_SERVER", "localhost", "text", "MQTT Server")
port = app.get_config_parameter("MQTT_PORT", None)
if port is None:
port = "1883"
cbpi.add_config_parameter("MQTT_PORT", "1883", "text", "MQTT Sever Port")
username = app.get_config_parameter("MQTT_USERNAME", None)
if username is None:
username = "username"
cbpi.add_config_parameter("MQTT_USERNAME", "username", "text", "MQTT username")
password = app.get_config_parameter("MQTT_PASSWORD", None)
if password is None:
password = "password"
cbpi.add_config_parameter("MQTT_PASSWORD", "password", "text", "MQTT password")
tls = app.get_config_parameter("MQTT_TLS", None)
if tls is None:
tls = "false"
cbpi.add_config_parameter("MQTT_TLS", "false", "text", "MQTT TLS")
app.cache["mqtt"] = MQTTThread(server,port,username, password, tls)
app.cache["mqtt"].daemon = True
app.cache["mqtt"].start()
def mqtt_reader(api):
while True:
try:
m = q.get(timeout=0.1)
api.cache.get("sensors")[m.get("id")].instance.last_value = m.get("value")
api.receive_sensor_value(m.get("id"), m.get("value"))
except:
pass
cbpi.socketio.start_background_task(target=mqtt_reader, api=app)