Commit 213810ca authored by Pinon Gregory's avatar Pinon Gregory 🦉
Browse files

Ajout du database feeder

parent 17c12d52
import serial
import time
import socket
import os
import sys
import json
from pyignite import Client
import geojson
from shapely.geometry import shape, Point, Polygon
listePieces =[]
try:
with open("bat_info_v2.oemc") as f:
plan = json.load(f)
print("GeoJSON found and opened")
except Exception:
print("Unable to open any GeoJSON")
exit(1)
for piece in plan["rooms"]["features"]:
infoUn = str(list(piece['properties'].values())[0])
infoDeux = str(list(piece['properties'].values())[1])
listePieces.append((infoUn+" "+infoDeux,Polygon(piece['geometry']['coordinates'])))
print(listePieces)
try:
serialDev = "/dev/serial/by-id/"+os.listdir("/dev/serial/by-id")[0]
print(serialDev)
except Exception:
type, value, traceback = sys.exc_info()
print('Error opening %s: %s' % (value.filename, value.strerror))
print("Switching to GPIO")
serialDev = "/dev/serial0"
try:
dbAdress = os.environ['DB_ADRESS']
except Exception:
dbAdress = "127.0.0.1"
try:
password = os.environ['DB_PASS']
except Exception:
print("Please set password for the db in DB_PASS env variable")
exit(1)
print("Current settings : "+dbAdress+" "+serialDev+" "+password)
client = Client(username='ignite', password=password, use_ssl=False)
client.connect(dbAdress,10800)
cachePositions = client.get_or_create_cache('positions')
cachePositions.clear()
print("DB Connected, connecting to DWM")
hostname = socket.gethostname()
try:
DWM = serial.Serial(port=serialDev, baudrate=115200)
except Exception:
type, value, traceback = sys.exc_info()
print('Error opening %s: %s' % (value.filename, value.strerror))
print("No DWM detected ! Try to specify it with env. var :)")
exit(1)
print("Connecting to " + DWM.name)
#DWM.write("reset\r".encode())
time.sleep(3)
DWM.write("\r\r".encode())
shellModeChar = DWM.read().decode()
if(shellModeChar =="@"):
while(shellModeChar =="@"):
print("Trying to get into shell mode...")
DWM.write("\r\r".encode())
time.sleep(1)
shellModeChar = DWM.read().decode()
time.sleep(2)
DWM.write("les\r".encode())
try:
print("Beginning data parsing...")
while True:
data = DWM.readline()
if(data):
localisation = data.decode()
stripped = localisation.strip()
#print(localisation)
splited = stripped.split(")")
try:
goodLine = splited[1]
tagNo = goodLine[1:5]
positions = goodLine[goodLine.find("[")+1:goodLine.find("]")]
listPos = positions.split(",")
stringPos = "{\"baliseID\":\""+tagNo+"\",\"x\":"+listPos[0]+",\"y\":"+listPos[1]+"}"
#print(stringPos)
try:
pStr =""
for elem in listePieces:
point = Point(float(listPos[0]),float(listPos[1]))
if elem[1].contains(point):
pStr = elem[0]
except Exception as e:
print(e)
if "nan" not in (listPos[0],listPos[1]):
print(stringPos)
cachePositions.put(tagNo,stringPos)
except Exception as e:
pass
#DWM.write("\r".encode())
DWM.close()
except KeyboardInterrupt:
#DWM.write("les\r".encode())
DWM.write("reset\r".encode())
print("Stop")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment