You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.0 KiB

from flask import render_template, Blueprint, request, flash, redirect, url_for, jsonify
import minibase.theme as theme
from minibase.blueprints.sensor.models import nbiotDevice
from minibase.blueprints.company.models import Companies
import minibase.blueprints.sensor.utils as sensorUtils
from minibase.blueprints.sensor.forms import nbiotDeviceForm
import minibase.blueprints.database.utils as dbUtils
import minibase.blueprints.main.utils as mainUtils
from minibase.app import db
import json
import os
sensor = Blueprint('sensor', __name__, template_folder='templates')
_form = ()
DATA_FILE = 'data.json'
@sensor.route('/callback', methods=['POST'])
def callback():
data = request.json
if not data:
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'])
def get_data():
if os.path.exists(DATA_FILE):
with open(DATA_FILE, 'r') as f:
stored_data = json.load(f)
# TODO:
else:
stored_data = []
return jsonify(stored_data)
@sensor.route('/list', methods=['GET','POST'])
def list():
page=request.args.get('page', 1, type=int)
table=dbUtils.table_printable_paginate(nbiotDevice, page, 20, 'edit/', 'id')
return(render_template('view.html', theme=theme, table=table, title="Devices List"))
@sensor.route("/edit/<int:deviceId>", methods=['GET', 'POST'])
def edit(deviceId):
if deviceId:
device = nbiotDevice.query.get_or_404(deviceId)
form = nbiotDeviceForm()
form.populate_for_updating(device)
_accountInfo = mainUtils.accountInfo(
title=device.name,
description=device.company_owner.name,
short=device.device_owner.username,
status=device.status.name,
image_file=mainUtils.imageFileLink(device.image_file)
)
if form.validate_on_submit():
if form.image_file.data:
picture_file = mainUtils.save_picture(form.image_file.data)
device.image_file = picture_file
mainUtils.fill_model(device, form)
db.session.commit()
flash('Device has been successfully updated', 'success')
return redirect(url_for('sensor.edit', deviceId=deviceId))
elif request.method == 'GET':
mainUtils.populate_form(form, device)
return render_template('account.html',
theme=theme,
accountInfo=_accountInfo,
form=form)
else:
flash('You need to select a Device id', 'alarm')
return redirect(url_for('sensor.list'))
@sensor.route('/add', methods=['GET', 'POST'])
def add():
device = nbiotDevice()
form = nbiotDeviceForm()
print(f"Route : manufacturer_id : { form.manufacturer_id.data }")
form.populate_for_adding(device)
if request.method == 'GET':
form.populate_for_adding(device)
if form.validate_on_submit():
mainUtils.fill_model(device, form)
#dbUtils.dbAddAndCommit(device)
flash('Device has been successfully added', 'success')
return render_template('edit.html',
theme=theme,
form=form)