From d3aae7368c44116be5967f0f8a9d2a34175a4a7e Mon Sep 17 00:00:00 2001 From: sessionm21 Date: Sat, 16 May 2020 02:25:10 +0100 Subject: [PATCH] Add sending to collar --- main.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index a2a5b8f..e196375 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ import base64 import message_pb2 import sqlite3 +import time import configparser config = configparser.ConfigParser() config.read("config.ini") @@ -16,14 +17,14 @@ def store_collar_data(collarname, res): print("Coord {x: " + str(res.loc.x) + ", y: " + str(res.loc.y) + "}") db = sqlite3.connect('data.sqlite') - entries = [(1, res.loc.x, res.loc.y)] + entries = [(1, res.loc.y, res.loc.x)] db.executemany( "INSERT INTO data_point (collar_id, longitude, latitude, datetime) VALUES (?,?,?,datetime('now'))", entries) db.commit() if(res.oob == 1): - entries = [(1, res.loc.x, res.loc.y)] + entries = [(1, res.loc.y, res.loc.x)] db.executemany( "INSERT INTO stimulus_activation (collar_id, longitude, latitude, datetime) VALUES (?,?,?,datetime('now'))", entries) @@ -31,15 +32,63 @@ def store_collar_data(collarname, res): db.close() +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 # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): + global connected + connected = True 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") - client.publish(appid+"/devices/+/down", "hello world") + client.publish(app_id+"/devices/+/down", "hello world") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): @@ -56,6 +105,45 @@ def on_message(client, userdata, msg): # store_collar_data(payload['dev_id'], res.loc.x, res.loc.y) store_collar_data(payload['dev_id'], res) + +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())) + client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message @@ -67,4 +155,11 @@ client.connect("us-west.thethings.network", 1883, 60) # handles reconnecting. # Other loop*() functions are available that give a threaded interface and a # manual interface. -client.loop_forever() +client.loop_start() +while not connected: + time.sleep(0.1) + +print("Starting boundary checking work.") +while True: + find_new_boundaries() + time.sleep(10)