views.py
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from django.http import HttpResponse
from django.shortcuts import render
from common.models import AgentCmd
# Create your views here.
def index(request):
if request.user.is_authenticated:
# return the initial view (the dashboard's one)
return render(request, 'dashboard/index.html', {'USER_LEVEL': request.user.get_priority(), 'base_template' : "base.html"})
return render(request, 'dashboard/index.html', {'USER_LEVEL': 0, 'base_template' : "base_unlogged.html"})
def weather_config(request):
""" PM 20180926 prototype without database
http://127.0.0.1:8000/dashboard/weather/config
Moved to monitoring
"""
try:
# Import PLC status sensor parser
from monitoring.plc_checker import PlcChecker
# Parse PLC status in colibri-new-fixed-2.json
#colibri_json = open("../simulators/plc/colibri-new-fixed-2.json")
#colibri_struct = json.load(colibri_json)
#plc_status = colibri_struct["statuses"][1]["entities"][0]
plc_checker = PlcChecker()
_struct = {"origin":plc_checker.origin, "sensors_table":plc_checker.sensors_table, "monitoring_names":list(plc_checker.monitoring_names.keys())}
# Return template with sensors list
return render(request, 'dashboard/config_weather.html', {'weather_config' : _struct, 'base_template' : "base.html"})
except Config.DoesNotExist:
return render(request, 'dashboard/config_weather.html', {'weather_info' : None})
def weather_config_update(request):
""" PM 20190716 ajax
http://127.0.0.1:8000/dashboard/weather/config/update
Get data...
"""
try:
# print(request.GET["data"])
cmd_name, cmd_args = request.GET["data"].split(" ", 1)
# ['nom', 'monitoring_name actuel', 'id(json path) du capteur', 'nouveau monitoring_name']
# <class 'list'>: ['monitor_name_switch', 'None', 'Came:/S/N_A5EM:/Power_input', 'Rain_boolean']
if "Error_code" in cmd_args:
something = 5/0
AgentCmd.send_command('Dashboard', 'AgentM', cmd_name, cmd_args)
#TODO: Pour l'instant, on ne recupere pas encore ce retour
return HttpResponse('{"update":"OK", "cmd_name":"' + cmd_name + '"}', content_type="application/json")
except:
return HttpResponse('{"update":"ERROR", "cmd_name":"' + cmd_name + '"}', content_type="application/json", status="500")