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.

149 lines
5.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.legal_entity.name,
short=device.website,
status=device.type.name,
image_file=mainUtils.imageFileLink(device.image_file)
)
if form.validate_on_submit():
if form.picture.data:
picture_file = mainUtils.save_picture(form.picture.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)
@sensor.route('/search/<field_name>', methods=['GET'])
def search_field(field_name):
query = request.args.get('modalQueryArg', '')
# Assuming field_name corresponds to a model field, you would add conditional logic here to query the correct model.
if field_name == 'manufacturer_id':
results = Companies.query.filter(Companies.name.ilike(f'%{query}%')).all()
# Add more conditions for other fields as needed
return render_template('sensor/manufacturer_search_results.html', results=results, field_name=field_name)
@sensor.route('/select/<field_name>/<int:result_id>', methods=['GET'])
def select_field(field_name, result_id):
# Return the selected option
# This is a simplified example, you'll need to adapt this to your specific model fields and logic.
if field_name == 'manufacturer_id':
selected = Companies.query.get(result_id)
return render_template("sensor/manufacturer_options.html", selected=selected)