lorawan/python/nodered-receiver.py

41 lines
1 KiB
Python
Raw Normal View History

2016-11-15 10:32:51 +00:00
#!/usr/bin/env python3
# Nico Schottelius <nico.schottelius -at- ungleich.ch>
# 2016-11-15
# GPLv3+
import urllib
import psycopg2
import websocket
from http.server import BaseHTTPRequestHandler, HTTPServer
import re
import json
import lorautil
import logging
2016-11-15 15:39:12 +00:00
logging.basicConfig(format='%(levelname)s: %(message)s')
2016-11-15 10:32:51 +00:00
class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers['Content-Length'])
post_data = self.rfile.read(length).decode('utf-8')
2016-11-15 15:39:12 +00:00
try:
if json.loads(post_data)['action'] == "connected":
self.send_24h_gps()
2016-11-15 10:32:51 +00:00
2016-11-15 15:39:12 +00:00
except Exception as e:
log.error("Got unknown request: {} {}".format(post_data, e))
2016-11-15 10:32:51 +00:00
2016-11-15 15:39:12 +00:00
def send_24h_gps(self):
db = lorautil.DB.gps_query()
for record in db:
lorautil.nodered_send("gps-plain", record)
2016-11-15 10:32:51 +00:00
if __name__ == '__main__':
2016-11-15 15:39:12 +00:00
log = logging.getLogger(__name__)
2016-11-15 10:32:51 +00:00
server_address = ('0.0.0.0', 1900)
httpd = HTTPServer(server_address, RequestHandler)
httpd.serve_forever()