Files
AnaLogger/server/FlythisServer.py
T
Marco Locatelli ce2e4c523e initial commit
2021-03-09 15:22:10 +01:00

72 lines
2.5 KiB
Python

#!/usr/bin/python
from flask import Flask, render_template, jsonify
#from flask_bootstrap import Bootstrap
import datetime
import redis
import socket
#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))
#volendo stampo a video IPv4
#print("Indirizzo Ipv4 assegnato: ",s.getsockname()[0])
#assegno a variabile indirizzo ip per usi futuri
ipv4 = s.getsockname()[0]
s.close()
#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 dati che dovranno popolarlo. legge direttamente da redis
def main():
channelsData = {
'title' : 'Flythis',
'timeNow' : datetime.datetime.now(),
'timeLog' : redSrv0.get('RTDATA:CH:TIME').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('FlythisTrap.html', **channelsData)
# definisco una NUOVA route a cui rispondere su chiamta in metodo GET
@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',
'timeNow' : datetime.datetime.now(),
'timeLog' : redSrv0.get('RTDATA:CH:TIME').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)
#dichiaro host ipv4 (ottenuto sopra utilizzando il modulo socket)
if __name__ == "__main__":
flaskApp.run(host=ipv4, port=80, debug=True)