diff --git a/main.py b/main.py index 6836bc7..a2a5b8f 100644 --- a/main.py +++ b/main.py @@ -12,12 +12,23 @@ app_id = config['Default']['AppId'] access_key = config['Default']['AccessKey'] # For now use collar id of 1, should look up collar id based on collarname from db -def store_coord(collarname, x, y): - print("Coord {x: " + str(x) + ", y: " + str(y) + "}") +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, x, y)] - db.executemany("INSERT INTO data_point (collar_id, longitude, latitude, datetime) VALUES (?,?,?,datetime('now'))", entries) + + entries = [(1, res.loc.x, res.loc.y)] + 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)] + db.executemany( + "INSERT INTO stimulus_activation (collar_id, longitude, latitude, datetime) VALUES (?,?,?,datetime('now'))", + entries) + db.commit() + db.close() # The callback for when the client receives a CONNACK response from the server. @@ -42,7 +53,8 @@ def on_message(client, userdata, msg): bcode = base64.b64decode(code) res = message_pb2.CollarResponse() res.ParseFromString(bcode) - store_coord(payload['dev_id'], res.loc.x, res.loc.y) + # store_collar_data(payload['dev_id'], res.loc.x, res.loc.y) + store_collar_data(payload['dev_id'], res) client = mqtt.Client() client.on_connect = on_connect diff --git a/tests.py b/tests.py index 0c89831..f7cd649 100644 --- a/tests.py +++ b/tests.py @@ -5,12 +5,13 @@ import message_pb2 import sqlite3 # Test protobuf here -code = bytearray('CgoNAAAAABUAAAAA', "utf-8") +code = bytearray('CgoNAABIQxUAAMhCEAA=', "utf-8") print(code) bcode = base64.b64decode(code) res = message_pb2.CollarResponse() res.ParseFromString(bcode) -assert res.loc.x == 0 -assert res.loc.y == 0 +assert res.loc.x == 200 +assert res.loc.y == 100 +assert res.oob == 0 print("Coord {x: " + str(res.loc.x) + ", y: " + str(res.loc.y) + "}")