#!/usr/bin/python from flask import Flask, render_template, jsonify, request #from flask_bootstrap import Bootstrap import datetime from flask.wrappers import Request import redis import socket from werkzeug.utils import redirect #create a socket object s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #connect to the server on local computer s.connect(("8.8.8.8", 80)) #assegno a variabile indirizzo ip per usi futuri ipv4 = s.getsockname()[0] s.close() #volendo stampo a video IPv4 #print("Indirizzo Ipv4 assegnato: ",s.getsockname()[0]) #configurazione per lavorare su server redis locale REDIS_PORT = 6379 REDIS_HOST = '127.0.0.1' redSrv0 = redis.Redis( host=REDIS_HOST, port=REDIS_PORT, db=0) flaskApp = Flask(__name__) @flaskApp.route("/") #funzione main: passa al template html i primi dati che dovranno popolarlo. legge direttamente da redis. edit: passa anche indirizzo ip del server def main(): channelsData = { 'title' : 'Home', 'timeSrv' : redSrv0.get('RTDATA:CH:SRVTIME').decode('utf-8'), 'timeLog' : redSrv0.get('RTDATA:CH:RECTIME').decode('utf-8'), 'Ch0' : redSrv0.get('RTDATA:CH:0').decode('utf-8'), 'Ch1' : redSrv0.get('RTDATA:CH:1').decode('utf-8'), 'Ch2' : redSrv0.get('RTDATA:CH:2').decode('utf-8'), 'Ch3' : redSrv0.get('RTDATA:CH:3').decode('utf-8'), 'Ch4' : redSrv0.get('RTDATA:CH:4').decode('utf-8'), 'Ch5' : redSrv0.get('RTDATA:CH:5').decode('utf-8'), 'Ch6' : redSrv0.get('RTDATA:CH:6').decode('utf-8'), 'Ch7' : redSrv0.get('RTDATA:CH:7').decode('utf-8') } #assegno il template html di riferimento return render_template('home.html', **channelsData) @flaskApp.route("/logger") #funzione logger, route a pagina /logger def logger(): channelsData = { 'title' : 'logger', 'timeSrv' : redSrv0.get('RTDATA:CH:SRVTIME').decode('utf-8'), 'timeLog' : redSrv0.get('RTDATA:CH:RECTIME').decode('utf-8'), 'Ch0' : redSrv0.get('RTDATA:CH:0').decode('utf-8'), 'Ch1' : redSrv0.get('RTDATA:CH:1').decode('utf-8'), 'Ch2' : redSrv0.get('RTDATA:CH:2').decode('utf-8'), 'Ch3' : redSrv0.get('RTDATA:CH:3').decode('utf-8'), 'Ch4' : redSrv0.get('RTDATA:CH:4').decode('utf-8'), 'Ch5' : redSrv0.get('RTDATA:CH:5').decode('utf-8'), 'Ch6' : redSrv0.get('RTDATA:CH:6').decode('utf-8'), 'Ch7' : redSrv0.get('RTDATA:CH:7').decode('utf-8') } #assegno il template html di riferimento return render_template('logger.html', **channelsData) @flaskApp.route("/setup") #funzione setup, route a pagina /setup def setup(): channelsData = { 'title' : 'setup', 'timeSrv' : redSrv0.get('RTDATA:CH:SRVTIME').decode('utf-8'), 'timeLog' : redSrv0.get('RTDATA:CH:RECTIME').decode('utf-8'), 'Ch0' : redSrv0.get('RTDATA:CH:0').decode('utf-8'), 'Ch1' : redSrv0.get('RTDATA:CH:1').decode('utf-8'), 'Ch2' : redSrv0.get('RTDATA:CH:2').decode('utf-8'), 'Ch3' : redSrv0.get('RTDATA:CH:3').decode('utf-8'), 'Ch4' : redSrv0.get('RTDATA:CH:4').decode('utf-8'), 'Ch5' : redSrv0.get('RTDATA:CH:5').decode('utf-8'), 'Ch6' : redSrv0.get('RTDATA:CH:6').decode('utf-8'), 'Ch7' : redSrv0.get('RTDATA:CH:7').decode('utf-8') } #assegno il template html di riferimento return render_template('setup.html', **channelsData) @flaskApp.route("/about") #funzione setup, route a pagina /setup def about(): channelsData = { 'title' : 'about', 'timeSrv' : redSrv0.get('RTDATA:CH:SRVTIME').decode('utf-8') } #assegno il template html di riferimento return render_template('about.html', **channelsData) # definisco una NUOVA route a cui rispondere su chiamta in metodo GET x restituire dati aggiornati @flaskApp.route("/api/v1/channels/all", methods=['GET']) def api_channels_all(): #funzione api_channels_all: legge direttamente da redis, ritorna jsonify channelsData = { 'title' : 'Flythis', 'timeSrv' : redSrv0.get('RTDATA:CH:SRVTIME').decode('utf-8'), 'timeLog' : redSrv0.get('RTDATA:CH:RECTIME').decode('utf-8'), 'Ch0' : redSrv0.get('RTDATA:CH:0').decode('utf-8'), 'Ch1' : redSrv0.get('RTDATA:CH:1').decode('utf-8'), 'Ch2' : redSrv0.get('RTDATA:CH:2').decode('utf-8'), 'Ch3' : redSrv0.get('RTDATA:CH:3').decode('utf-8'), 'Ch4' : redSrv0.get('RTDATA:CH:4').decode('utf-8'), 'Ch5' : redSrv0.get('RTDATA:CH:5').decode('utf-8'), 'Ch6' : redSrv0.get('RTDATA:CH:6').decode('utf-8'), 'Ch7' : redSrv0.get('RTDATA:CH:7').decode('utf-8') } # restituisce in formato json i dati letti da redis return jsonify(channelsData) # Route di comando x LOG: start e stop @flaskApp.route("/api/v1/log/start", methods=['PUT']) def start_log(): #funzione start_log: scrive su redis LOG:STATUS -> 1 redSrv0.set('RTDATA:LOG:STATUS', 1) return "OK" @flaskApp.route("/api/v1/log/stop", methods=['PUT']) def stop_log(): #funzione stop_log: scrive su redis LOG:STATUS -> 0 redSrv0.set('RTDATA:LOG:STATUS', 0) return "OK" @flaskApp.route("/api/v1/sessions", methods=['POST']) def sessionCreate(): #funzione creazione sessione: scrive su redis SESSION:NAME prendendolo dall'input in pagina html con nome NewSessionName sessionName = request.form['NewSessionName'] redSrv0.set('RTDATA:SESSION:NAME', sessionName) # rimando in HOME return redirect("/") #dichiaro host ipv4 (ottenuto sopra utilizzando il modulo socket) if __name__ == "__main__": flaskApp.run(host=ipv4, port=80, debug=True)