gateway/main.py

166 lines
5.0 KiB
Python
Raw Normal View History

2020-04-18 23:36:00 -07:00
import paho.mqtt.client as mqtt
import json
import base64
import message_pb2
import sqlite3
2020-05-15 18:25:10 -07:00
import time
2020-05-12 14:11:33 -07:00
import configparser
config = configparser.ConfigParser()
config.read("config.ini")
2020-04-18 23:36:00 -07:00
2020-05-12 14:11:33 -07:00
app_id = config['Default']['AppId']
access_key = config['Default']['AccessKey']
2020-04-18 23:36:00 -07:00
# For now use collar id of 1, should look up collar id based on collarname from db
2020-05-12 22:36:23 -07:00
def store_collar_data(collarname, res):
print("Coord {x: " + str(res.loc.x) + ", y: " + str(res.loc.y) + "}")
2020-04-18 23:36:00 -07:00
db = sqlite3.connect('data.sqlite')
2020-05-12 22:36:23 -07:00
2020-05-15 18:25:10 -07:00
entries = [(1, res.loc.y, res.loc.x)]
2020-05-12 22:36:23 -07:00
db.executemany(
"INSERT INTO data_point (collar_id, longitude, latitude, datetime) VALUES (?,?,?,datetime('now'))",
entries)
2020-04-18 23:36:00 -07:00
db.commit()
2020-05-12 22:36:23 -07:00
if(res.oob == 1):
2020-05-15 18:25:10 -07:00
entries = [(1, res.loc.y, res.loc.x)]
2020-05-12 22:36:23 -07:00
db.executemany(
"INSERT INTO stimulus_activation (collar_id, longitude, latitude, datetime) VALUES (?,?,?,datetime('now'))",
entries)
db.commit()
2020-04-18 23:36:00 -07:00
db.close()
2020-05-15 18:25:10 -07:00
def boundary_get_coord(self, key):
if key == 0: return self.coord0
if key == 1: return self.coord1
if key == 2: return self.coord2
if key == 3: return self.coord3
if key == 4: return self.coord4
if key == 5: return self.coord5
if key == 6: return self.coord6
if key == 7: return self.coord7
if key == 8: return self.coord8
if key == 9: return self.coord9
message_pb2.Coordinates.__getitem__ = boundary_get_coord
def pack_boundary(boundary):
coordinates = message_pb2.Coordinates()
coordinates.isr = boundary[2]
for i in range(boundary[2]):
coordinates[i].x = boundary[4 + 2*i]
coordinates[i].y = boundary[5 + 2*i]
for i in range(boundary[2], 10):
coordinates[i].x = 1.0
coordinates[i].y = 1.0
return coordinates
def push_coordinate(id, packed):
payload = {
'port': 1,
'confirmed': False,
'payload_raw': base64.b64encode(packed.SerializeToString())
}
print("Pushing coordinate", packed)
print("Device ID:", id)
print("Payload:", json.dumps(payload))
client.publish(app_id+"/devices/" + 'fenceless' + "/down", json.dumps(payload))
def find_new_boundaries():
db = sqlite3.connect('data.sqlite')
bounds = db.execute("SELECT * from bounding_box where needs_push=1").fetchall()
for bound in bounds:
print("Found boundary. ID:", bound[0])
push_coordinate(bound[1], pack_boundary(bound))
db.execute("UPDATE bounding_box set needs_push=0 where id=?", (bound[0],))
db.commit()
connected = False
2020-04-18 23:36:00 -07:00
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
2020-05-15 18:25:10 -07:00
global connected
connected = True
2020-04-18 23:36:00 -07:00
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
# client.subscribe("+/devices/+/events/activations")
client.subscribe("+/devices/+/up")
2020-05-15 18:25:10 -07:00
client.publish(app_id+"/devices/+/down", "hello world")
2020-04-18 23:36:00 -07:00
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
# Print entire payload
print(msg.payload)
# Parse payload (JSON)
payload = json.loads(msg.payload)
# Decode payload_raw, which is the protobuf
code = payload['payload_raw']
bcode = base64.b64decode(code)
res = message_pb2.CollarResponse()
res.ParseFromString(bcode)
2020-05-12 22:36:23 -07:00
# store_collar_data(payload['dev_id'], res.loc.x, res.loc.y)
store_collar_data(payload['dev_id'], res)
2020-04-18 23:36:00 -07:00
2020-05-15 18:25:10 -07:00
coordinates = message_pb2.Coordinates()
coordinates.isr = 4
coordinates.coord0.x = 0
coordinates.coord1.x = 1
coordinates.coord2.x = 0
coordinates.coord3.x = 1
coordinates.coord0.y = 1
coordinates.coord1.y = 1
coordinates.coord2.y = 0
coordinates.coord3.y = 0
# coordinates.coord0.x = 44.5934295644297
# coordinates.coord1.x = 44.5934295644297
# coordinates.coord2.x = 44.5934295664297
# coordinates.coord3.x = 44.5934295664297
#
# coordinates.coord0.y = -123.306968698965
# coordinates.coord1.y = -123.306968678965
# coordinates.coord2.y = -123.306968678965
# coordinates.coord3.y = -123.306968698965
coordinates.coord4.x = 1
coordinates.coord5.x = 1
coordinates.coord6.x = 1
coordinates.coord7.x = 1
coordinates.coord8.x = 1
coordinates.coord9.x = 1
coordinates.coord4.y = 2
coordinates.coord5.y = 2
coordinates.coord6.y = 2
coordinates.coord7.y = 2
coordinates.coord8.y = 2
coordinates.coord9.y = 2
# SerializeToString()
print(base64.b64encode(coordinates.SerializeToString()))
2020-04-18 23:36:00 -07:00
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(app_id, access_key)
client.connect("us-west.thethings.network", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
2020-05-15 18:25:10 -07:00
client.loop_start()
while not connected:
time.sleep(0.1)
print("Starting boundary checking work.")
while True:
find_new_boundaries()
time.sleep(10)