|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
from flask import render_template, Blueprint, request, flash, redirect, url_for
|
|
|
|
|
from flask import render_template, Blueprint, request, flash, redirect, url_for, jsonify
|
|
|
|
|
import minibase.theme as theme
|
|
|
|
|
from minibase.blueprints.sensor.models import nbiotDevice
|
|
|
|
|
import minibase.blueprints.sensor.utils as sensorUtils
|
|
|
|
@ -6,36 +6,52 @@ from minibase.blueprints.sensor.forms import updateNbioDeviceUpdateForm, updateN
|
|
|
|
|
import minibase.blueprints.database.utils as dbUtils
|
|
|
|
|
import minibase.blueprints.company.utils as companyUtils
|
|
|
|
|
import minibase.blueprints.user.utils as userUtils
|
|
|
|
|
import minibase.blueprints.main.utils as mainUtils
|
|
|
|
|
from minibase.app import db
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# TODO:
|
|
|
|
|
|
|
|
|
|
sensor = Blueprint('sensor', __name__, template_folder='templates')
|
|
|
|
|
|
|
|
|
|
DATA_FILE = 'data.json'
|
|
|
|
|
|
|
|
|
|
sensor.route('/callback', methods=['POST'])
|
|
|
|
|
@sensor.route('/callback', methods=['POST'])
|
|
|
|
|
def callback():
|
|
|
|
|
data = request.json
|
|
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
|
return jsonify({"error": "Invalid data"}), 400
|
|
|
|
|
|
|
|
|
|
# Read existing data
|
|
|
|
|
if os.path.exists(DATA_FILE):
|
|
|
|
|
with open(DATA_FILE, 'r') as f:
|
|
|
|
|
stored_data = json.load(f)
|
|
|
|
|
else:
|
|
|
|
|
stored_data = []
|
|
|
|
|
|
|
|
|
|
# Append new data
|
|
|
|
|
stored_data.append(data)
|
|
|
|
|
|
|
|
|
|
# Write data back to file
|
|
|
|
|
with open(DATA_FILE, 'w') as f:
|
|
|
|
|
json.dump(stored_data, f, indent=4)
|
|
|
|
|
|
|
|
|
|
return 'Callback received', 200
|
|
|
|
|
return ({"error": "Invalid data"}), 400
|
|
|
|
|
|
|
|
|
|
# Access each element from the data
|
|
|
|
|
customer_id = data[0].get("customerId")
|
|
|
|
|
rcv_time = data[0].get("rcvTime")
|
|
|
|
|
src_imsi = data[0].get("srcImsi")
|
|
|
|
|
src_ip = data[0].get("srcIP")
|
|
|
|
|
src_port = data[0].get("srcPort")
|
|
|
|
|
payload = data[0].get("payload")
|
|
|
|
|
|
|
|
|
|
# Print the elements to the console (for debugging)
|
|
|
|
|
#print(f"Customer ID: {customer_id}")
|
|
|
|
|
#print(f"Receive Time: {rcv_time}")
|
|
|
|
|
#print(f"Source IMSI: {src_imsi}")
|
|
|
|
|
#print(f"Source IP: {src_ip}")
|
|
|
|
|
#print(f"Source Port: {src_port}")
|
|
|
|
|
#print(f"Payload: {payload}")
|
|
|
|
|
|
|
|
|
|
response_data = {
|
|
|
|
|
"customerId": customer_id,
|
|
|
|
|
"rcvTime": rcv_time,
|
|
|
|
|
"srcImsi": src_imsi,
|
|
|
|
|
"srcIP": src_ip,
|
|
|
|
|
"srcPort": src_port,
|
|
|
|
|
"payload": payload
|
|
|
|
|
}
|
|
|
|
|
sensorUtils.decode_payload(payload)
|
|
|
|
|
|
|
|
|
|
return jsonify(response_data), 200
|
|
|
|
|
#return 'data recieved', 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@sensor.route('/data', methods=['GET'])
|
|
|
|
@ -61,14 +77,17 @@ def edit(deviceId):
|
|
|
|
|
if deviceId:
|
|
|
|
|
device = nbiotDevice.query.get_or_404(deviceId)
|
|
|
|
|
form = updateNbioDeviceUpdateForm(current_device_id=device.id)
|
|
|
|
|
form.user_id.choices = [(row.id, row.username) for row in userUtils.queryUserNamesWithDefault(device.user_id)]
|
|
|
|
|
form.owner_id.choices = [(row.id, row.username) for row in userUtils.queryUserNamesWithDefault(device.user_id)]
|
|
|
|
|
form.manufacturer_id.choices = [(row.id, row.name) for row in companyUtils.queryNamesWithDefault(device.manufacturer_id)]
|
|
|
|
|
form.company_id.choices = [(row.id, row.name) for row in companyUtils.queryNamesWithDefault(device.company_id)]
|
|
|
|
|
form.user_id.choices = [(row.id, row.username) for row in userUtils.queryUserNamesWithDefault(device.device_user_id)]
|
|
|
|
|
form.owner_id.choices = [(row.id, row.username) for row in userUtils.queryUserNamesWithDefault(device.device_owner_id)]
|
|
|
|
|
form.manufacturer_id.choices = [(row.id, row.name) for row in companyUtils.queryNamesWithDefault(device.company_manufacturer_id)]
|
|
|
|
|
form.company_id.choices = [(row.id, row.name) for row in companyUtils.queryNamesWithDefault(device.company_owner_id)]
|
|
|
|
|
form.status_id.choices = [(row.id, row.name) for row in sensorUtils.queryStatusNamesWithDefault(device.status_id)]
|
|
|
|
|
form.type_id.choices = [(row.id, row.name) for row in sensorUtils.queryTypeNamesWithDefault(device.type_id)]
|
|
|
|
|
form.area_id.choices = [(row.id, row.name) for row in sensorUtils.queryAreaNamesWithDefault(device.area_id)]
|
|
|
|
|
if form.validate_on_submit():
|
|
|
|
|
if form.picture.data:
|
|
|
|
|
picture_file = mainUtils.save_picture(form.picture.data)
|
|
|
|
|
device.image_file = picture_file
|
|
|
|
|
device.name=form.name.data
|
|
|
|
|
device.serial_no = form.serial_no.data
|
|
|
|
|
device.device_id=form.device_id.data
|
|
|
|
@ -79,13 +98,13 @@ def edit(deviceId):
|
|
|
|
|
device.registration_date=form.registration_date.data
|
|
|
|
|
device.activation_date=form.activation_date.data
|
|
|
|
|
device.deactivation_date=form.deactivation_date.data
|
|
|
|
|
device.owner_id=form.owner_id.data
|
|
|
|
|
device.user_id=form.user_id.data
|
|
|
|
|
device.device_owner_id=form.owner_id.data
|
|
|
|
|
device.device_user_id=form.user_id.data
|
|
|
|
|
device.status_id=form.status_id.data
|
|
|
|
|
device.type_id=form.type_id.data
|
|
|
|
|
device.area_id=form.area_id.data
|
|
|
|
|
device.manufacturer_id = form.manufacturer_id.data
|
|
|
|
|
device.company_id = form.company_id.data
|
|
|
|
|
device.company_manufacturer_id = form.manufacturer_id.data
|
|
|
|
|
device.company_owner_id = form.company_id.data
|
|
|
|
|
db.session.commit()
|
|
|
|
|
flash('Device has been successfully updated', 'success')
|
|
|
|
|
return redirect(url_for('sensor.edit', deviceId=deviceId))
|
|
|
|
@ -100,9 +119,17 @@ def edit(deviceId):
|
|
|
|
|
form.registration_date.data = device.registration_date
|
|
|
|
|
form.activation_date.data = device.activation_date
|
|
|
|
|
form.deactivation_date.data = device.deactivation_date
|
|
|
|
|
|
|
|
|
|
form.user_id.data = device.device_user.username
|
|
|
|
|
form.owner_id.data = device.device_owner.username
|
|
|
|
|
form.manufacturer_id.data = device.company_manufacturer.name
|
|
|
|
|
form.company_id.data = device.company_owner.name
|
|
|
|
|
form.status_id.data = device.status.name
|
|
|
|
|
form.type_id.data = device.type.name
|
|
|
|
|
form.area_id.data = device.area.name
|
|
|
|
|
image_file = url_for('static', filename='pics/' + sensorUtils.queryImageById(deviceId))
|
|
|
|
|
return render_template('sensor/account.html',
|
|
|
|
|
theme=theme,
|
|
|
|
|
image_file=image_file,
|
|
|
|
|
form=form)
|
|
|
|
|
else:
|
|
|
|
|
flash('You need to select a Device id', 'alarm')
|
|
|
|
|