-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfarmserve_websockets.py
executable file
·113 lines (91 loc) · 3.53 KB
/
farmserve_websockets.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
#!/usr/bin/env python3
#import pymongo
from simple_websocket_server import WebSocketServer, WebSocket
import threading
from threading import Event
import time
import datetime
import pytz
import json
from influxdb import InfluxDBClient
ws_clients = []
class THWebSocket(WebSocket):
def __init__(self, server, sock, address):
WebSocket.__init__(self, server, sock, address)
self.db_client = InfluxDBClient(host='68.183.44.212', port=8086)
self.db_client.create_database('farm_iot_sensor_data')
self.db_client.switch_database('farm_iot_sensor_data')
self.is_listener = False
def get_db_entry_json(self, node, temperature, humidity, soil_moisture):
db_entry_data = [{
"measurement" : "temperature_humidity",
"tags": {
"node_id" : node
},
"fields" : {
"Temperature" : temperature,
"Humidity" : humidity,
"Soil Moisture" : soil_moisture
}
}]
return db_entry_data
def handle(self):
SAST = pytz.timezone('Africa/Johannesburg')
ct = datetime.datetime.now(SAST)
print(ct, self.data)
#client.send_message(self.data)
self.send_message(self.data)
print(self.data)
if "_id" in self.data and "temperature" in self.data:
datajson = json.loads(self.data)
self.node_id = datajson['node_id']
t = datajson['temperature']
h = datajson['humidity']
m = datajson['soil_moisture']
self.db_client.write_points(self.get_db_entry_json(self.node_id, t, h, m))
if "_id" in self.data and "cam_ready" in self.data:
datajson = json.loads(self.data)
self.node_id = datajson['node_id']
cam_ready = datajson['cam_ready']
ip = datajson['ip']
print("Cam Ready:", cam_ready, "IP:", ip)
if cam_ready == 1:
for client in ws_clients: #Forward data to pic listener
if client.is_listener:
client.send_message(self.data)
#client.send_message("Cam ready for %s IP: %s" % (self.node_id, ip))
if "_id" in self.data and "pic_listener" in self.data:
print("Pic Listener registered")
self.is_listener = True
#for client in ws_th_clients: #Forward data web Trainers
# if client != self:
# client.send_message(self.data)
#for client in ws_th_clients: #Forward data to web clients
# if client.node_id is None:
# print("A Web client is connected, sending:")
# client.send_message(self.data)
def connected(self):
print(self.address, 'Client Connected')
ws_clients.append(self)
def handle_close(self):
print(self.address, 'Client Closed')
ws_clients.remove(self)
class THWebSocketThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stopped = Event()
def run(self):
server = WebSocketServer('68.183.44.212', 12012, THWebSocket)
#server = WebSocketServer('localhost', 12012, THWebSocket)
print("Serving ws @ 68.183.44.212:12012")
#print("Serving ws @ localhost:12012")
server.serve_forever()
def main():
THThread = THWebSocketThread()
THThread.daemon = True
THThread.start()
print("Wating for clients to connect...")
while(1):
time.sleep(2)
if __name__ == "__main__":
main()