Blame view

src/core/pyros_django/dashboard/views.py 58.5 KB
eefbbbd2   Etienne Pallier   Model splitting g...
1
# General imports
3ec16e74   Alexis Koralewski   Add flexible form...
2
3
from ast import arguments
import re
da13e230   Alexis Koralewski   update UI of webs...
4
import sys
eefbbbd2   Etienne Pallier   Model splitting g...
5
6
7
8
9
10
11
import datetime
from datetime import timezone
import utils.Logger as l
import json
from random import randint
import time,os
from collections import OrderedDict
da13e230   Alexis Koralewski   update UI of webs...
12

eefbbbd2   Etienne Pallier   Model splitting g...
13
# Django imports
da13e230   Alexis Koralewski   update UI of webs...
14
15
from django.http import HttpResponse
from django.shortcuts import render, redirect
da13e230   Alexis Koralewski   update UI of webs...
16
from django.contrib.auth.decorators import login_required
9bd7ac9e   Alexis Koralewski   Adding timeout co...
17
from django.core.paginator import Paginator
da13e230   Alexis Koralewski   update UI of webs...
18
from django.core import serializers
da13e230   Alexis Koralewski   update UI of webs...
19
20
from django.forms import modelformset_factory
from django.http import HttpResponseRedirect
da13e230   Alexis Koralewski   update UI of webs...
21
22
23
24
25
from django.views.generic.edit import UpdateView
from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator
from django.urls import reverse_lazy, reverse
from django.http import Http404
b9068dc6   Alexis Koralewski   Adding WeatherWat...
26
from django.db.models.query import QuerySet
eefbbbd2   Etienne Pallier   Model splitting g...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.conf import settings as pyros_settings
from django.contrib import messages
from django.http import JsonResponse
from django.utils.http import urlencode

# Project imports
from common.models import Log, WeatherWatch, SiteWatch, Config, WeatherWatchHistory, Majordome, AgentSurvey, AgentCmd
#from user_manager.models import PyrosUser, UserLevel
from user_manager.models import ScientificProgram, SP_Period_Guest
#from scientific_program.models import ScientificProgram, SP_Period_Guest
from devices.models import PlcDeviceStatus, TelescopeCommand #, Telescope

from dashboard.forms import ConfigForm, MajordomeForm #, UserForm
from dashboard.decorator import level_required
da13e230   Alexis Koralewski   update UI of webs...
41
42
43
44
45
from devices.Telescope import TelescopeController
from devices.TelescopeRemoteControlDefault import TelescopeRemoteControlDefault
from devices.CameraVISRemoteControlDefault import CameraVISRemoteControlDefault
from devices.CameraNIRRemoteControlDefault import CameraNIRRemoteControlDefault
from devices import PLC
a2dbbde1   Alexis Koralewski   Adding new config...
46
from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig
eefbbbd2   Etienne Pallier   Model splitting g...
47

da13e230   Alexis Koralewski   update UI of webs...
48
sys.path.append("../../..")
42bf33a2   Alexis Koralewski   renaming configpy...
49
from config.pyros.config_pyros import ConfigPyros
da13e230   Alexis Koralewski   update UI of webs...
50
#import utils.celme as celme
252a8b01   Alexis Koralewski   Changing few guit...
51
import vendor.guitastro.src.guitastro as guitastro
da13e230   Alexis Koralewski   update UI of webs...
52

da13e230   Alexis Koralewski   update UI of webs...
53
54
55
56
57
58
59

SUN_ELEV_DAY_THRESHOLD = -10
MAX_LOGS_LINES = 100

log = l.setupLogger("dashboard", "dashboard")

def index(request):
a2dbbde1   Alexis Koralewski   Adding new config...
60
    config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"],os.environ["unit_name"])
b7becde4   Alexis Koralewski   Updating UI (foot...
61
    observatory_name = config.get_obs_name()
a6603b26   Alexis Koralewski   Django and Python...
62
    unit_name = config.unit_name
4290c2cf   Alexis Koralewski   adding unit choic...
63
    request.session["obsname"] = observatory_name+" "+unit_name
a2dbbde1   Alexis Koralewski   Adding new config...
64
65
66
    request.session["pyros_config"] = pyros_settings.CONFIG_PYROS
    logo = pyros_settings.CONFIG_PYROS.get("general").get("logo")
    request.session["logo"] = "media/"+logo
02d94ed3   Alexis Koralewski   Reworking UI of w...
67
    message = ""
da13e230   Alexis Koralewski   update UI of webs...
68
    if request.user.is_authenticated:
02d94ed3   Alexis Koralewski   Reworking UI of w...
69
70
71
72
73
74
75
        if SP_Period_Guest.objects.filter(email=request.user.email):
            message = "You are requested to join the following proposals, click on the following link(s) to accept those invitations :<br><ul>"
            domain = pyros_settings.DEFAULT_DOMAIN
            for sp_period_guest in SP_Period_Guest.objects.filter(email=request.user.email):
                url = f"http://{domain}{reverse('sp_register',args=(sp_period_guest.SP_Period.scientific_program.id,sp_period_guest.SP_Period.period.id))}"
                message+=f"<li><a href='{url}'>{url}</a></li>"
            message+="</ul>"
da13e230   Alexis Koralewski   update UI of webs...
76
        # return the initial view (the dashboard's one)
02d94ed3   Alexis Koralewski   Reworking UI of w...
77
78
        return render(request, 'dashboard/index.html', {'USER_LEVEL': request.user.get_priority(), 'base_template' : "base.html","message":message})                              
    return render(request, 'dashboard/index.html', {"USER_LEVEL" : "Visitor", 'base_template' : "base.html"})                              
da13e230   Alexis Koralewski   update UI of webs...
79

39bd6df6   Alexis Koralewski   Add menu page for...
80
81
def observation_index(request):
    if request.user.is_authenticated:
077d5a23   Alexis Koralewski   adding science th...
82
        CAN_VIEW_SCIENCE_THEMES = request.session.get("role") in ("Admin","Unit-PI","Unit-board")
39bd6df6   Alexis Koralewski   Add menu page for...
83
        # return the initial view (the dashboard's one)
077d5a23   Alexis Koralewski   adding science th...
84
85
86
87
        return render(request, 'dashboard/observation_index.html',{
            "base_template":"base.html",
            "CAN_VIEW_SCIENCE_THEMES": CAN_VIEW_SCIENCE_THEMES
        })                              
02d94ed3   Alexis Koralewski   Reworking UI of w...
88
    return render(request, 'dashboard/observation_index.html', {'USER_LEVEL': "Visitor", 'base_template' : "base.html"})        
da13e230   Alexis Koralewski   update UI of webs...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    
def retrieve_env(request):
    try:
        #weather_status = WeatherWatch.objects.latest('updated')
        weather_status = get_latest_from_db_or_empty(WeatherWatch)
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty()
        plc_timeout = Config.objects.get(pk=1).plc_timeout_seconds
        timeout = (datetime.datetime.now() - plc_device_status.created).total_seconds()
        timeout_affichage = datetime.datetime.now() - datetime.timedelta(seconds=timeout)
        sunelev = get_sunelev()
        t = datetime.datetime.now() + datetime.timedelta(hours=-7)
        isDay = False
        if sunelev >= SUN_ELEV_DAY_THRESHOLD:
            isDay = True
        return render(request, 'dashboard/observation_status_env.html', locals())
    except WeatherWatch.DoesNotExist:
            raise Http404("No WeatherWatch matches the given query.")


'''
4290c2cf   Alexis Koralewski   adding unit choic...
110
        Function that call guitastro code to determine sun elevation, maybe it should be moved somewhere else ?
da13e230   Alexis Koralewski   update UI of webs...
111
112
113
'''

def get_sunelev():
4290c2cf   Alexis Koralewski   adding unit choic...
114
    date = guitastro.dates.Date("now")
9ac4dcf4   Alexis Koralewski   Fixing guiastro s...
115
    site = guitastro.siteobs.Siteobs("MPC 244.5367  0.85792  +0.51292") # coords of san pedro martir site
da13e230   Alexis Koralewski   update UI of webs...
116
117
118
    skyobj= {'planet':'Sun'}
    outputs = ['name','ra','dec','elev']

4290c2cf   Alexis Koralewski   adding unit choic...
119
    target = guitastro.Target()
da13e230   Alexis Koralewski   update UI of webs...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    target.define(skyobj)
    output0s = ['ra','dec']
    results = target.ephem(date,site,output0s)
    ra = results['RA']
    dec = results['DEC']
    skyobj= {'RA':ra , 'DEC':dec }
    target.define(skyobj)
    results = target.ephem(date,site,outputs)
    elev = round(results['ELEV'],2)
    return elev

def retrieve_env_navbar(request):

    '''
            Function which get the last status of the plc in th db, the config info, the sunelev, put it in a json
            and send it to the client.
            This function is called every REFRESH_ICONS_FREQUENCE_MILLISECONDS in base.html

    '''
    try:
        weather = get_weather_data()
        return HttpResponse(json.dumps(weather), content_type="application/json")
    except WeatherWatch.DoesNotExist:
        raise Http404("No WeatherWatch matches the given query.")

    '''
    ##if request.is_ajax():
    try:
        #weather_status = WeatherWatch.objects.latest('updated')
        weather_status = WeatherWatch.objects.latest('updated') if WeatherWatch.objects.all().exists() else WeatherWatch()
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created') if PlcDeviceStatus.objects.all().exists() else PlcDeviceStatus()
        if PlcDeviceStatus.objects.all().exists():
            plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        else:
            plc_device_status = PlcDeviceStatus()
            #plc_device_status.created='2018-07-26 12:48:49.949228'
            plc_device_status.created=datetime.datetime(2018, 6, 21, 9, 53, 30, 599462)
        plc_mode = plc_device_status.plc_mode
        is_safe = plc_device_status.is_safe
        weather = serializers.serialize('json', [weather_status])
        weather = json.loads(weather)
        ack = Config.objects.get(id=1).ack
        plc_timeout = Config.objects.get(pk=1).plc_timeout_seconds
        #if not PlcDeviceStatus.objects.all().exists() plc_device_status.created='2018-07-26 12:48:49.949228'
        #timeout = (datetime.datetime.now() - LAST_PLC_ITEM_TIME).total_seconds()
        timeout = (datetime.datetime.now() - plc_device_status.created).total_seconds()
        weather[0]['max_sunelev'] = SUN_ELEV_DAY_THRESHOLD
        #weather[0]['sunelev'] = 10
        weather[0]['sunelev'] = get_sunelev()
        weather[0]["plc_mode"] = plc_mode
        weather[0]["is_safe"] = is_safe
        weather[0]["ACK"] = ack
        weather[0]["plc_timeout"] = timeout
        weather[0]["max_plc_timeout"] = plc_timeout
        weather[0]["pyros_mode"] = Config.objects.get(id=1).pyros_state
        if SiteWatch.objects.all().exists():
            sitewatch = SiteWatch.objects.latest('updated')
        else:
            sitewatch = SiteWatch()
            #sitewatch.global_status='KO'
        weather[0]["sitewatch_global_status"] = sitewatch.global_status
        #weather[0]["sitewatch_global_status"] = SiteWatch.objects.latest('updated').global_status
        #weather[0]["sitewatch_global_status"] = SiteWatch.objects.latest('updated').global_status if SiteWatch.objects.all().exists() else SiteWatch()
        return HttpResponse(json.dumps(weather), content_type="application/json")
    except WeatherWatch.DoesNotExist:
        raise Http404("No WeatherWatch matches the given query.")
    '''

@login_required
def settings(request):
    '''
        View called to see the settings (for the software, observatory, users...) page
    '''
cc15cb36   Alexis Koralewski   improving user ac...
194
    CAN_VIEW_PERIOD = request.session.get("role") in ("Admin", "Unit-PI", "Observer", "Unit-board")
c1a58d54   Alexis Koralewski   Adding views to v...
195
196
    CAN_CHANGE_SOFT_MODE = request.session.get("role") in ("Admin", "Unit-PI", "Operator")
    CAN_VIEW_AGENTS_STATE = request.session.get("role") in ("Admin", "Unit-PI", "Operator")
da13e230   Alexis Koralewski   update UI of webs...
197
198
    return(render(request, "dashboard/settings.html", locals()))

8e1b0fa9   Alexis Koralewski   Add login require...
199
200
def get_last_all_cmds(agent_name):
    last_agent_all_cmds = None
51b9c5d0   Alexis Koralewski   fix get_specific_...
201
202
203
    last_do_stop_cmd = None
    if "AgentSST" not in agent_name:
        try:
8e1b0fa9   Alexis Koralewski   Add login require...
204
            last_agent_all_cmds = AgentCmd.objects.filter(full_name="get_all_cmds",recipient=agent_name,state__contains="CMD_EXECUTED").latest("s_deposit_time")
51b9c5d0   Alexis Koralewski   fix get_specific_...
205
            last_do_stop_cmd =  AgentCmd.objects.filter(full_name__contains="do_stop",recipient=agent_name).latest("s_deposit_time")
8e1b0fa9   Alexis Koralewski   Add login require...
206
207
208
            if last_agent_all_cmds.s_deposit_time <= last_do_stop_cmd.s_deposit_time:
                last_agent_all_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_all_cmds")
                while not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_exec_error():
51b9c5d0   Alexis Koralewski   fix get_specific_...
209
                    time.sleep(0.5)
8e1b0fa9   Alexis Koralewski   Add login require...
210
            return AgentCmd.objects.get(id=last_agent_all_cmds.id) 
51b9c5d0   Alexis Koralewski   fix get_specific_...
211
        except AgentCmd.DoesNotExist:
8e1b0fa9   Alexis Koralewski   Add login require...
212
213
214
215
            if last_do_stop_cmd == None and last_agent_all_cmds != None:
                return last_agent_all_cmds
            last_agent_all_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_all_cmds")
            while not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_exec_error():
51b9c5d0   Alexis Koralewski   fix get_specific_...
216
                time.sleep(0.5)
8e1b0fa9   Alexis Koralewski   Add login require...
217
            return AgentCmd.objects.get(id=last_agent_all_cmds.id)
51b9c5d0   Alexis Koralewski   fix get_specific_...
218
219
    else:
        # AgentSST doesn't have do_stop cmd... (for the moment)
fe97c24c   Alexis Koralewski   Add timer to get ...
220
221
        datetime_now = datetime.datetime.now(tz=timezone.utc)
        time_delta = datetime_now - datetime.timedelta(minutes=30)
8a356bfe   Alexis Koralewski   Fix get_all_cmds ...
222
223
224
225
        last_agent_all_cmds = AgentCmd.objects.filter(full_name="get_all_cmds",recipient=agent_name,state__contains="CMD_EXECUTED")
        if last_agent_all_cmds.exits() :
            last_agent_all_cmds.latest("s_deposit_time")
            if time_delta <= last_agent_all_cmds.s_deposit_time:
fe97c24c   Alexis Koralewski   Add timer to get ...
226

8a356bfe   Alexis Koralewski   Fix get_all_cmds ...
227
228
229
230
231
232
233
234
235
236
237
238
239
                last_agent_all_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_all_cmds")
                max_wait_time = 3
                current_wait_time = 0
                while not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_exec_error():
                    time.sleep(0.5)
                    current_wait_time+=0.5
                    if max_wait_time <= current_wait_time:
                        break 
                return AgentCmd.objects.get(id=last_agent_all_cmds.id)
            else:
                return AgentCmd.objects.get(id=last_agent_all_cmds.id) 
        else:
            # There is no get_all_cmds for this agent in database, it's the first time we send this command, we have to wait until the command is executed.
fe97c24c   Alexis Koralewski   Add timer to get ...
240
            last_agent_all_cmds = AgentCmd.send_cmd_from_to("System",agent_name,"get_all_cmds")
fe97c24c   Alexis Koralewski   Add timer to get ...
241
            while not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_executed() and not AgentCmd.objects.get(id=last_agent_all_cmds.id).is_exec_error():
8a356bfe   Alexis Koralewski   Fix get_all_cmds ...
242
                    time.sleep(0.5)
8e1b0fa9   Alexis Koralewski   Add login require...
243
@login_required
c1a58d54   Alexis Koralewski   Adding views to v...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def agent_detail(request, agent_name):
    agent_log_path = os.path.join(os.environ.get("PROJECT_ROOT_PATH"),f"logs/{agent_name}/",f"{agent_name}.log")
    try:
        log = open(agent_log_path,"r")
        log_content = log.readlines()
        
        if len(log_content) > 30:
            log_last_30_lines = log_content[-30:]
            
        else:
            log_last_30_lines = log_content
        log.close()
    except FileNotFoundError:
        error_message = f"Cannot find agent logs. (Agent log path tried : {agent_log_path})"
6fa0909e   Alexis Koralewski   Add button to man...
258
    specific_cmd_with_args = None
8e1b0fa9   Alexis Koralewski   Add login require...
259
    cmd_with_choices = []
68880592   Alexis Koralewski   Add tooltip on ag...
260
    cmds_description = {}
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
261
    #agent_general_commands = AgentCmd._AGENT_GENERAL_COMMANDS.copy()
65c091a2   Alexis Koralewski   Update variable n...
262
    if not AgentSurvey.objects.get(name=agent_name).is_stopping():
8e1b0fa9   Alexis Koralewski   Add login require...
263
        agent_specific_cmds = get_last_all_cmds(agent_name)
6fa0909e   Alexis Koralewski   Add button to man...
264
        wait_time = 0
a3ea1d09   Alexis Koralewski   fix agent detail ...
265
        max_wait_time = 20
b2f0eaa8   Alexis Koralewski   add new message w...
266
        unimplemented_command = None
a3ea1d09   Alexis Koralewski   fix agent detail ...
267
        #while not AgentCmd.objects.get(id=agent_specific_cmds.id).is_executed() and wait_time <= max_wait_time:
a310b5f1   Alexis Koralewski   Improving get_spe...
268
269
270
271
272
        # while not AgentCmd.objects.get(id=agent_specific_cmds.id).is_executed() and not AgentCmd.objects.get(id=agent_specific_cmds.id).is_exec_error():
        #     time.sleep(0.5)
        #     wait_time += 0.5
        #cmd = AgentCmd.objects.get(id=agent_specific_cmds.id)
        cmd = agent_specific_cmds
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
273
        
a3ea1d09   Alexis Koralewski   fix agent detail ...
274
        if cmd.is_exec_error():
65c091a2   Alexis Koralewski   Update variable n...
275
            unimplemented_command = cmd.result
d182f6c7   Alexis Koralewski   Fixing display of...
276
        else:
65c091a2   Alexis Koralewski   Update variable n...
277
            agent_specific_cmd_to_list = cmd.result.split(";")
6fa0909e   Alexis Koralewski   Add button to man...
278
279
            specific_cmd_with_args = {}
            for specific_cmd in agent_specific_cmd_to_list:
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
280
281
                if "get_all_cmds" in specific_cmd or "get_specific_cmds" in specific_cmd:
                    continue
6fa0909e   Alexis Koralewski   Add button to man...
282
                if "(" in specific_cmd:
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
283
                    splitted_cmd = specific_cmd.split("/")
e2712b10   Alexis Koralewski   new usage of deli...
284
                    specific_cmd = splitted_cmd.pop(0)
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
285
                    if len(splitted_cmd) > 0:
e2712b10   Alexis Koralewski   new usage of deli...
286
                        description = splitted_cmd.pop(0)
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
287
288
                    else:
                        description = ""
6fa0909e   Alexis Koralewski   Add button to man...
289
290
                    cmd = specific_cmd.split("(")[0]
                    specific_cmd_with_args[cmd] = []
68880592   Alexis Koralewski   Add tooltip on ag...
291
                    cmds_description[cmd] = description
6fa0909e   Alexis Koralewski   Add button to man...
292
293
294
                    # get text between paretheses
                    args = re.findall(pattern="\(([^)]+)\)",string=specific_cmd)
                    for arg in args:
8e1b0fa9   Alexis Koralewski   Add login require...
295
296
297
298
299
300
301
302
303
304
305
                        if "typing.Literal" in arg:
                            arg_name = arg.split(":")[0]
                            values = re.findall(pattern="\[(.*?)\]",string=arg) 
                            trim_values = []
                            for value in values:
                                if "'" in value:
                                    value = value.replace("'","")
                                    trim_values.append(value)
                                specific_cmd_with_args[cmd].append([arg_name,trim_values])
                            cmd_with_choices.append(cmd)
                        else:
e2712b10   Alexis Koralewski   new usage of deli...
306
                            arguments = arg.split("|")
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
307
                            for index ,arg in enumerate(arguments):
e2712b10   Alexis Koralewski   new usage of deli...
308
309
310
311
312
313
314
315
316
317
                                if ("typing.Tuple" in arg or "typing.List" in arg):
                                    arg_name = arg.split(":")[0]
                                    arg_type = arg.split(":")[1]
                                    trim_values = []
                                    values_type = re.findall(pattern="\[(.*?)\]",string=arg_type)
                                    if values_type:
                                        # best way to tell user the type allowed in list or tuple is to put the raw content in one field
                                            specific_cmd_with_args[cmd].append([arg_name+" "+str(values_type),"str"])
                                        #for arg in values_type:
                                        #    specific_cmd_with_args[cmd].append([arg_name+" "+arg_type,arg])
2d3e16c7   Alexis Koralewski   Fixing cmd parsin...
318
319
                                else:
                                    specific_cmd_with_args[cmd].append(arg.split(":"))
6fa0909e   Alexis Koralewski   Add button to man...
320
            if request.GET.get("cmd"):
91a2fab5   Alexis Koralewski   ui changes on age...
321
322
                if request.GET.get("cmd") != "all":
                    if request.GET.get("cmd") in list(specific_cmd_with_args.keys()):
68880592   Alexis Koralewski   Add tooltip on ag...
323
                        return JsonResponse({"specific_cmd_with_args":specific_cmd_with_args.get(request.GET.get("cmd")),"cmd_with_choices":cmd_with_choices,"cmds_description":cmds_description},safe=False)
91a2fab5   Alexis Koralewski   ui changes on age...
324
325
                    else:
                        return JsonResponse(None,safe=False)
6fa0909e   Alexis Koralewski   Add button to man...
326
                else:
68880592   Alexis Koralewski   Add tooltip on ag...
327
                    return JsonResponse({"agent_general_commands":agent_general_commands,"specific_cmd_with_args":specific_cmd_with_args,"unimplemented_command":unimplemented_command,"cmd_with_choices":cmd_with_choices,"cmds_description":cmds_description},safe=False)
4ac65086   Alexis Koralewski   fix form send cmd
328
329
        if request.GET.get("cmd"):
            return JsonResponse(None,safe=False)
6f83043c   Alexis Koralewski   Add websocket for...
330
331
332
333
334
335
336
337
    # commands_sent_by_agent = AgentCmd.get_commands_sent_by_agent(agent_name)
    # commands_recivied_by_agent = AgentCmd.get_commands_sent_to_agent(agent_name)
    # agent_cmds = commands_sent_by_agent | commands_recivied_by_agent
    # agent_cmds = agent_cmds.exclude(full_name="get_specific_cmds")
    # agent_cmds = agent_cmds.exclude(full_name="get_all_cmds")
    # agent_cmds = agent_cmds.order_by("-s_deposit_time")
    # paginator = Paginator(agent_cmds, pyros_settings.NB_ELEMENT_PER_PAGE)
    # page_number = request.GET.get("page",1)
6fa0909e   Alexis Koralewski   Add button to man...
338
339
    config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"],os.environ["unit_name"])
    managed_agents = None
65c091a2   Alexis Koralewski   Update variable n...
340
    agents_status = None
0228f249   Alexis Koralewski   Add link to agent...
341
    managed_by = None
6fa0909e   Alexis Koralewski   Add button to man...
342
343
344
345
    if "AgentSST" in agent_name:
        # get computer of agentSST
        # get agents of this computer
        # mettre des boutons d'actions pour arrêter ou démarrer ou redémarrer l'agent 
65c091a2   Alexis Koralewski   Update variable n...
346
347
        agent_computer = config.get_agent_information(config.unit_name,agent_name).get("computer")
        computer_hostname = config.get_computers().get(agent_computer).get("computer_config").get("hostname")
6fa0909e   Alexis Koralewski   Add button to man...
348
349
        agents = config.get_agents_per_computer(config.unit_name).get(computer_hostname)
        managed_agents = agents
65c091a2   Alexis Koralewski   Update variable n...
350
        agents_status = {}
6fa0909e   Alexis Koralewski   Add button to man...
351
        for agent in managed_agents:
69be5e68   Alexis Koralewski   Change typo in ag...
352
353
354
            if "AgentSST" in agent:
                managed_agents.remove(agent)
                continue
6fa0909e   Alexis Koralewski   Add button to man...
355
            try:
65c091a2   Alexis Koralewski   Update variable n...
356
                agents_status[agent] = AgentSurvey.objects.get(name=agent).status
6fa0909e   Alexis Koralewski   Add button to man...
357
            except AgentSurvey.DoesNotExist:
65c091a2   Alexis Koralewski   Update variable n...
358
                agents_status[agent] = "None"
0228f249   Alexis Koralewski   Add link to agent...
359
360
    else:
        managed_by = None
65c091a2   Alexis Koralewski   Update variable n...
361
362
        agent_computer = config.get_agent_information(config.unit_name,agent_name).get("computer")
        computer_hostname = config.get_computers().get(agent_computer).get("computer_config").get("hostname")
0228f249   Alexis Koralewski   Add link to agent...
363
364
365
366
367
        agents = config.get_agents_per_computer(config.unit_name).get(computer_hostname)
        for agent in agents:
            if "AgentSST" in agent:
                managed_by = agent
                break
3c735dec   Alexis Koralewski   hide commands whe...
368
369
    obj, created = Majordome.objects.get_or_create(id=1)
    CAN_SEND_COMMAND = obj.soft_mode == Majordome.MANUAL_MODE
6f83043c   Alexis Koralewski   Add websocket for...
370
371
372
373
374
375
    # try:
    #     commands = paginator.page(page_number)
    # except PageNotAnInteger:
    #     commands = paginator.page(1)
    # except EmptyPage:
    #     commands = paginator.page(paginator.num_pages)
c1a58d54   Alexis Koralewski   Adding views to v...
376
377
    return render(request, "dashboard/agent_detail.html", locals())

8e1b0fa9   Alexis Koralewski   Add login require...
378
@login_required
6fa0909e   Alexis Koralewski   Add button to man...
379
380
381
382
383
def agent_action(request):
    if request.POST:
        action = request.POST.get("action")
        agentsst = request.POST.get("agentsst")
        recipient = request.POST.get("recipient")
91a2fab5   Alexis Koralewski   ui changes on age...
384
        args = request.POST.get("args")
6fa0909e   Alexis Koralewski   Add button to man...
385
386
387
        if action == "start":
            new_cmd = AgentCmd.send_cmd_from_to(request.user,agentsst,"do_start_agent",recipient)
        elif action == "restart":
91a2fab5   Alexis Koralewski   ui changes on age...
388
389
390
391
392
            if args:
                if args == "soft":
                    new_cmd = AgentCmd.send_cmd_from_to(request.user,agentsst,"do_restart_agent",recipient+ " soft")
                else:
                    new_cmd = AgentCmd.send_cmd_from_to(request.user,agentsst,"do_restart_agent",recipient+ " hard")
6fa0909e   Alexis Koralewski   Add button to man...
393
        elif action == "stop":
12ede5d0   Alexis Koralewski   Add new field in ...
394
            new_cmd = AgentCmd.send_cmd_from_to(request.user,agentsst,"do_stop_agent",recipient)
6fa0909e   Alexis Koralewski   Add button to man...
395
396
397
398
399
        if new_cmd != None:
            messages.add_message(request, messages.INFO, f"Command sent !")
        else:
            messages.add_message(request, messages.INFO, f"Error while creating command, please try again.")
        return redirect(agent_detail,agent_name=agentsst)    
4ad8724b   Alexis Koralewski   Add view to see a...
400

8e1b0fa9   Alexis Koralewski   Add login require...
401
@login_required
4ad8724b   Alexis Koralewski   Add view to see a...
402
403
def agents_commands(request):
    agents_cmds = AgentCmd.objects.all()
483e981c   Alexis Koralewski   Improve agents co...
404
    cmd_status = AgentCmd.CMD_STATUS_CODES
4ad8724b   Alexis Koralewski   Add view to see a...
405
406
407
    url_filters = ""
    start_datetime = ""
    end_datetime = ""
483e981c   Alexis Koralewski   Improve agents co...
408
    selected_cmd_status = ""
4ad8724b   Alexis Koralewski   Add view to see a...
409
410
411
    if request.GET.get("start_datetime"):
        start_datetime = request.GET.get("start_datetime")
        end_datetime = request.GET.get("end_datetime")
ec3652a1   Alexis Koralewski   Change column ord...
412
413
    if request.GET.get("selected_cmd_status"):
        selected_cmd_status = request.GET.get("selected_cmd_status")
4ad8724b   Alexis Koralewski   Add view to see a...
414
415
416
417
418
419
420
421
        # if len(start_datetime)>0 or len(end_datetime) > 0:
        #     start_datetime_tz = datetime.datetime.strptime(start_datetime,"%Y-%m-%d %H:%M:%S")
        #     start_datetime_tz = start_datetime_tz.replace(tzinfo=timezone.utc)
        #     end_datetime_tz = datetime.datetime.strptime(end_datetime,"%Y-%m-%d %H:%M:%S")
        #     end_datetime_tz = end_datetime_tz.replace(tzinfo=timezone.utc)
        #     agents_cmds = agents_cmds.filter(s_deposit_time__range=(start_datetime_tz,end_datetime_tz))
        #     url_filters = urlencode({"start_datetime":start_datetime,"end_datetime":end_datetime})
    if request.POST:
4ad8724b   Alexis Koralewski   Add view to see a...
422
423
        start_datetime = request.POST.get("start_datetime")
        end_datetime = request.POST.get("end_datetime")
483e981c   Alexis Koralewski   Improve agents co...
424
425
426
427
428
        filtered_cmd_status = request.POST.get("cmd_status")
        selected_cmd_status = filtered_cmd_status
    if len(start_datetime)>0:
        #start_datetime_tz = datetime.datetime.strptime(start_datetime,"%Y-%m-%d %H:%M:%S")
        start_datetime_tz = datetime.datetime.strptime(start_datetime,"%Y/%m/%d %H:%M")
4ad8724b   Alexis Koralewski   Add view to see a...
429
        start_datetime_tz = start_datetime_tz.replace(tzinfo=timezone.utc)
483e981c   Alexis Koralewski   Improve agents co...
430
431
432
433
434
    else:
        start_datetime_tz = agents_cmds.first().s_deposit_time
    if len(end_datetime) > 0:
        #end_datetime_tz = datetime.datetime.strptime(end_datetime,"%Y-%m-%d %H:%M:%S")
        end_datetime_tz = datetime.datetime.strptime(end_datetime,"%Y/%m/%d %H:%M")
4ad8724b   Alexis Koralewski   Add view to see a...
435
436
437
        end_datetime_tz = end_datetime_tz.replace(tzinfo=timezone.utc)
        agents_cmds = agents_cmds.filter(s_deposit_time__range=(start_datetime_tz,end_datetime_tz))
        url_filters = urlencode({"start_datetime":start_datetime,"end_datetime":end_datetime})
483e981c   Alexis Koralewski   Improve agents co...
438
439
440
441
    else:
        end_datetime_tz = datetime.datetime.now(tz=timezone.utc)
    if selected_cmd_status and selected_cmd_status != "all":
        agents_cmds = agents_cmds.filter(state=selected_cmd_status)
ec3652a1   Alexis Koralewski   Change column ord...
442
        url_filters = urlencode({"start_datetime":start_datetime,"end_datetime":end_datetime,"selected_cmd_status":selected_cmd_status})
4ad8724b   Alexis Koralewski   Add view to see a...
443
    agents_cmds = agents_cmds.exclude(full_name="get_specific_cmds")
8e1b0fa9   Alexis Koralewski   Add login require...
444
    agents_cmds = agents_cmds.exclude(full_name="get_all_cmds")
4ad8724b   Alexis Koralewski   Add view to see a...
445
446
447
448
449
450
451
452
453
454
455
456
    agents_cmds = agents_cmds.order_by("-s_deposit_time","-id")
    paginator = Paginator(agents_cmds, pyros_settings.NB_ELEMENT_PER_PAGE)
    page_number = request.GET.get("page",1)
    try:
        commands = paginator.page(page_number)
    except PageNotAnInteger:
        commands = paginator.page(1)
    except EmptyPage:
        commands = paginator.page(paginator.num_pages)
    # if request.POST:
    #     return redirect(request.META['HTTP_REFERER'])
    return render(request, "dashboard/agents_commands.html", locals())
f2eeeb37   Alexis Koralewski   Add send command ...
457
458
def send_agent_cmd(request):
    if request.POST:
3ec16e74   Alexis Koralewski   Add flexible form...
459
460
461
462
463
464
465
466
        # make a copy because request.POST is immutable
        post_data = request.POST.copy()
        reciever = post_data.get("agent_name")
        cmd_name = post_data.get("cmd_name")
        post_data.pop("agent_name")
        post_data.pop("cmd_name")
        post_data.pop("csrfmiddlewaretoken")
        cmd_args = ""
6fa0909e   Alexis Koralewski   Add button to man...
467
        for arg_name in post_data: 
3ec16e74   Alexis Koralewski   Add flexible form...
468
            cmd_args += post_data.get(arg_name)+ " "
6fa0909e   Alexis Koralewski   Add button to man...
469
470
        if len(cmd_args)>0:
            cmd_args = cmd_args[:-1]
71948bf9   Alexis Koralewski   Change sender to ...
471
        new_cmd = AgentCmd.send_cmd_from_to(request.user,reciever,cmd_name,cmd_args)
f2eeeb37   Alexis Koralewski   Add send command ...
472
        if new_cmd != None:
d22dc7e8   Alexis Koralewski   Change cmd form s...
473
474
            # messages.add_message(request, messages.INFO, f"Command sent !")
            return HttpResponse("OK")
f2eeeb37   Alexis Koralewski   Add send command ...
475
        else:
d22dc7e8   Alexis Koralewski   Change cmd form s...
476
477
            # messages.add_message(request, messages.INFO, f"Error while creating command, please try again.")
            return HttpResponse("Error")
f2eeeb37   Alexis Koralewski   Add send command ...
478
479
    return redirect(agent_detail,agent_name=reciever)

c1a58d54   Alexis Koralewski   Adding views to v...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
def retrieve_log_content(request):
    if request.POST:
        agent_name = request.POST.get("agent_name")
        agent_log_path = os.path.join(os.environ.get("PROJECT_ROOT_PATH"),f"logs/{agent_name}/",f"{agent_name}.log")
        try:
            log = open(agent_log_path,"r")
            log_content = log.readlines()
            
            if len(log_content) > 30:
                log_last_30_lines = log_content[-30:]
                
            else:
                log_last_30_lines = log_content
            log.close()
            return HttpResponse(log_last_30_lines)
        except FileNotFoundError:
            error_message = f"Cannot find agent logs. (Agent log path tried : {agent_log_path})"
            return HttpResponse(error_message)


@login_required
@level_required("Admin", "Unit-PI", "Operator")
def soft_mode(request):
    if request.POST:
        form = MajordomeForm(request.POST)
        if form.is_valid():
            obj, created = Majordome.objects.get_or_create(id=1)
            obj.soft_mode = form.cleaned_data["soft_mode"]
            obj.save()
            new_soft_mode = form.instance.soft_mode
            messages.add_message(request, messages.INFO, f"Changed observatory mode to {new_soft_mode}")
    obj, created = Majordome.objects.get_or_create(id=1)
    form = MajordomeForm(instance=obj)
    soft_mode = obj.soft_mode
    is_auto = soft_mode == Majordome.AUTO_MODE
    return render(request, "dashboard/soft_mode.html", locals())

8e1b0fa9   Alexis Koralewski   Add login require...
517
@login_required
c1a58d54   Alexis Koralewski   Adding views to v...
518
519
def agents_state(request):
    agents = AgentSurvey.objects.all()
483e981c   Alexis Koralewski   Improve agents co...
520
    datetime_now = datetime.datetime.now(tz=timezone.utc)
71948bf9   Alexis Koralewski   Change sender to ...
521
522
523
524
    if request.GET:
        agent_name = request.GET["agent_name"]
        try:
            running_commands = AgentCmd.objects.get(state="CMD_RUNNING",sender=agent_name)
71948bf9   Alexis Koralewski   Change sender to ...
525
526
527
528
529
530
531
            return HttpResponse(running_commands.full_name)
        except AgentCmd.DoesNotExist:
            return HttpResponse("None")
        # running_command_per_agent = {}
        # for cmd in running_commands:
        #     running_command_per_agent = running_commands[cmd.get("sender")] = cmd.get("full_name")
        # return HttpResponse(running_command_per_agent)
9bd7ac9e   Alexis Koralewski   Adding timeout co...
532
533
534
535
536
537
538
    soon_out_of_date_agents = []
    for agent in agents:
        datetime_now_minus_thirty_sec = datetime_now - datetime.timedelta(seconds=30)
        datetime_now_minus_thirty_sec =  datetime_now_minus_thirty_sec.replace(tzinfo=timezone.utc)
        
        if datetime_now_minus_thirty_sec >= agent.updated:
            soon_out_of_date_agents.append(agent.name)
c1a58d54   Alexis Koralewski   Adding views to v...
539
    return render(request, "dashboard/agents_state.html", locals())
9bd7ac9e   Alexis Koralewski   Adding timeout co...
540
541


da13e230   Alexis Koralewski   update UI of webs...
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
def retrieve_main_icon(request):
    if request.is_ajax():
        try:
            weather_status = WeatherWatch.objects.latest('updated')
            plc_mode = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created').plc_mode

            weather = serializers.serialize('json', [weather_status])
            weather = json.loads(weather)
            weather[0]["plc_mode"] = plc_mode
            weather[0]["sitewatch_global_status"] = SiteWatch.objects.latest('updated').global_status

            return HttpResponse(json.dumps(weather), content_type="application/json")
        except WeatherWatch.DoesNotExist:
            raise Http404("No WeatherWatch matches the given query.")



@login_required
#@level_required(2)
def routines(request):
    url_ = reverse('admin:common_request_changelist')
    return redirect(url_)


b9068dc6   Alexis Koralewski   Adding WeatherWat...
566
567
568
569
570
571
572
573
def get_lastest_and_last_x_minutes_before_latest_weather(x:float=0):
    if WeatherWatch.objects.all().exists():
        latest_entry = WeatherWatch.objects.latest('updated')
        # Get datetime of last weather datetime entry minus x
        time_start_range = latest_entry.updated - datetime.timedelta(minutes=x)
        # If we have entries in WeatherWatchHistory
        if WeatherWatchHistory.objects.all().exists():
            # Get the first entry in history that is less or equal than time_start_range
b9068dc6   Alexis Koralewski   Adding WeatherWat...
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
            first_weather_of_time_start = WeatherWatch.objects.filter(updated__lte=time_start_range).order_by("-updated").first()
            if first_weather_of_time_start != None:
                first_weather_of_time_start_id = first_weather_of_time_start.id
                # get all id in history that are after the first_weather_of_time_start
                list_id_weather_history = WeatherWatchHistory.objects.filter(weather__id__gte=first_weather_of_time_start_id).values_list("weather__id",flat=True)
                list_id_weather_history = list(list_id_weather_history)
                # add the last entry of weatherwatch at the end of the list
                list_id_weather_history.append(latest_entry.id)
                # Get all WeatherWatch instance that match those ids
                weather_data = WeatherWatch.objects.filter(id__in=list_id_weather_history).order_by("updated")
                if weather_data.count() > 0:
                    return weather_data 
            else:
                # We don't have a matching history entry yet, return the last entry of WeatherWatch (plot with one dot)
                return latest_entry
            # We don't have an WeatherWatchHistory entry yet, return the last entry of WeatherWatch (plot with one dot)
        return latest_entry
    else:
        # We don't have WeatherWatch data, return empty WeatherWatch (empty plot)
        return WeatherWatch()
da13e230   Alexis Koralewski   update UI of webs...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

def get_latest_from_db_or_empty(db_model):
    return db_model.objects.latest('updated') if db_model.objects.all().exists() else db_model()

def get_latest_plc_device_status_or_empty():
    if PlcDeviceStatus.objects.all().exists() and PlcDeviceStatus.objects.exclude(plc_mode=None).exists():
            plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
    else:
        # Create an empty object 
        plc_device_status = PlcDeviceStatus()
        # Set a dummy date
        #plc_device_status.created='2018-07-26 12:48:49.949228'
        plc_device_status.created=datetime.datetime(2018, 6, 21, 9, 53, 30, 599462)
        plc_device_status.is_safe = False
    return plc_device_status

def get_weather_data():
    #weather_status = WeatherWatch.objects.latest('updated') if WeatherWatch.objects.all().exists() else WeatherWatch()
b9068dc6   Alexis Koralewski   Adding WeatherWat...
612
    #weather_status = get_latest_from_db_or_empty(WeatherWatch)
e5f122b6   Alexis Koralewski   Adding env monito...
613
614
615
616
617
618
619
620
    pyros_config_file = os.environ.get("pyros_config_file")
    config_pyros = ConfigPyros(pyros_config_file).pyros_config
    env_config = config_pyros.get("ENV")
    last_x_minutes = 15
    if env_config:
        last_x_minutes = env_config.get("time_before_plot")

    weather_history = get_lastest_and_last_x_minutes_before_latest_weather(last_x_minutes)
da13e230   Alexis Koralewski   update UI of webs...
621
622
623
    plc_device_status = get_latest_plc_device_status_or_empty()
    plc_mode = plc_device_status.plc_mode
    is_safe = plc_device_status.is_safe
b9068dc6   Alexis Koralewski   Adding WeatherWat...
624
625
626
627
628
629
    if isinstance(weather_history,QuerySet):
        # If we have weather history  + last entry of weather
        weather = serializers.serialize('json', weather_history)
    else:
        # We have only the last entry of weather
        weather = serializers.serialize('json', [weather_history])
da13e230   Alexis Koralewski   update UI of webs...
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
    weather = json.loads(weather)
    ack = Config.objects.get(id=1).ack
    plc_timeout = Config.objects.get(pk=1).plc_timeout_seconds
    #if not PlcDeviceStatus.objects.all().exists() plc_device_status.created='2018-07-26 12:48:49.949228'
    #timeout = (datetime.datetime.now() - LAST_PLC_ITEM_TIME).total_seconds()
    # PM 20190416 TODO: Check error
    #timeout = (datetime.datetime.now() - plc_device_status.created).total_seconds()
    timeout = 0
    weather[0]['max_sunelev'] = SUN_ELEV_DAY_THRESHOLD
    #weather[0]['sunelev'] = 10
    weather[0]['sunelev'] = get_sunelev()
    weather[0]["plc_mode"] = plc_mode
    weather[0]["is_safe"] = is_safe
    weather[0]["ACK"] = ack
    weather[0]["plc_timeout"] = timeout
    weather[0]["max_plc_timeout"] = plc_timeout
    weather[0]["pyros_mode"] = Config.objects.get(id=1).pyros_state
    # only for retrieve_env_navbar:
    sitewatch = get_latest_from_db_or_empty(SiteWatch)
    '''
    if SiteWatch.objects.all().exists():
        sitewatch = SiteWatch.objects.latest('updated')
    else:
        sitewatch = SiteWatch()
        #sitewatch.global_status='KO'
    '''
    weather[0]["sitewatch_global_status"] = sitewatch.global_status
    #weather[0]["sitewatch_global_status"] = SiteWatch.objects.latest('updated').global_status
    #weather[0]["sitewatch_global_status"] = SiteWatch.objects.latest('updated').global_status if SiteWatch.objects.all().exists() else SiteWatch()
    return weather



def weather(request):
    if request.user.is_authenticated:
        return render(request, 'dashboard/reload_weather.html', {'base_template' : "base.html"})                                                                     # return the needed html file
02d94ed3   Alexis Koralewski   Reworking UI of w...
666
    return render(request, 'dashboard/reload_weather.html', {'base_template' : "base.html"})
da13e230   Alexis Koralewski   update UI of webs...
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742

def weather_current(request):
    # PM 20180718
    #if request.is_ajax():
    try:
        weather = get_weather_data()
        return HttpResponse(json.dumps(weather), content_type="application/json")
    except WeatherWatch.DoesNotExist:
        raise Http404("No WeatherWatch matches the given query.")
    '''
    try:
        #weather_status = WeatherWatch.objects.latest('updated')
        #weather_status = WeatherWatch.objects.latest('updated') if WeatherWatch.objects.all().exists() else WeatherWatch()
        EMPTY_TABLE = WeatherWatch.objects.all().exists()
        weather_status = WeatherWatch.objects.latest('updated') if not EMPTY_TABLE else WeatherWatch()
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created') if PlcDeviceStatus.objects.all().exists() else PlcDeviceStatus()
        plc_mode = plc_device_status.plc_mode
        is_safe = plc_device_status.is_safe
        weather = serializers.serialize('json', [weather_status])
        weather = json.loads(weather)
        ack = Config.objects.get(id=1).ack
        plc_timeout = Config.objects.get(pk=1).plc_timeout_seconds
        #timeout = (datetime.datetime.now() - plc_device_status.created).total_seconds()
        LAST_PLC_ITEM_TIME = plc_device_status.created if not EMPTY_TABLE else '2018-07-26 12:48:49.949228'
        timeout = (datetime.datetime.now() - LAST_PLC_ITEM_TIME).total_seconds()
        weather[0]['max_sunelev'] = SUN_ELEV_DAY_THRESHOLD
        weather[0]['sunelev'] = get_sunelev()
        weather[0]["plc_mode"] = plc_mode
        weather[0]["is_safe"] = is_safe
        weather[0]["ACK"] = ack
        weather[0]["plc_timeout"] = timeout
        weather[0]["max_plc_timeout"] = plc_timeout
        weather[0]["pyros_mode"] = Config.objects.get(id=1).pyros_state
        return HttpResponse(json.dumps(weather), content_type="application/json")
    except WeatherWatch.DoesNotExist:
        raise Http404("No WeatherWatch matches the given query.")
    '''

def weather_config(request):
    """ PM 20180926 prototype without database
        http://127.0.0.1:8000/dashboard/weather/config
        Moved to monitoring
        http://127.0.0.1:8000/monitoring/weather/config
    """
    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}
        # 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_current_old(request):
    try:
        if (len(Config.objects.all()) == 1):
            monitoring = int(int(Config.objects.get(id=1).row_data_save_frequency) / 5)
        else:
            monitoring = 60
        if (len(WeatherWatch.objects.all()) > 0):
            weather_info = WeatherWatch.objects.order_by("-id")[:monitoring]                                                            # Use 300 seconds by default with an iteration every 5 seconds                                                                                     # Get the number of data available
        else:                                                                                                                           
            weather_info = None
        return render(request, 'dashboard/_current_weather.html', {'weather_info' : site_current, 'iteration' : monitoring})
    except Config.DoesNotExist:
        return render(request, 'dashboard/_current_weather.html', {'weather_info' : None, 'iteration' : 60})

def site(request):
    if request.user.is_authenticated:
        return render(request, 'dashboard/reload_site.html', {'base_template' : "base.html"})                        # return the needed html file
02d94ed3   Alexis Koralewski   Reworking UI of w...
743
    return render(request, 'dashboard/reload_site.html', {'base_template' : "base.html"})                        # return the needed html file
da13e230   Alexis Koralewski   update UI of webs...
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262

def site_current(request):
    try:
        if (len(Config.objects.all()) == 1):
            monitoring = int(int(Config.objects.get(id=1).row_data_save_frequency) / 5)
        else:
            monitoring = 60
        #if (len(SiteWatch.objects.all()) > 0):
        if SiteWatch.objects.all().exists():
            site_info = SiteWatch.objects.order_by("-id")[:monitoring]                                                               
        else:                                                                                                                           
            site_info = None
        return render(request, 'dashboard/current_site.html', {'site_info' : site_info, 'iteration' : monitoring, 'global_mode' : Config.objects.get(id=1).global_mode})
    except Config.DoesNotExist:
        return render(request, 'dashboard/current_site.html', {'site_info' : None, 'iteration' : 60})

@login_required
@level_required("Admin")
def proposal(request):
    if (len(ScientificProgram.objects.all()) > 0):                              # checking if the observatory table is empty
        proposal_info = ScientificProgram.objects.order_by("-id")[:100]         # Sorting Weather table
        nb_info_proposal = len(proposal_info)                                   # Get the number of data available

    else:                                                                       # if empty set everything to 0 / None (variables are checked in src/templates/scheduler/_current_weather.html)
        proposal_info = None
        nb_info_proposal = 0

    return render(request, 'dashboard/proposal.html', {'proposal_info' : proposal_info, 'nb_info_proposal' : nb_info_proposal})

@login_required
#@level_required(5)
def configUpdate(request):
    instance = get_object_or_404(Config, id=1)
    form = ConfigForm(request.POST or None, instance=instance)
    if form.is_valid():
        form.save()
        return reverse('settings')
    return render(request, 'dashboard/configuration.html', {'form': form}) 
    

@login_required
def devices(request):
    url_ = reverse('admin:common_device_changelist')
    return redirect(url_)

@login_required
def system(request):
    return render(request, 'dashboard/system.html')

@login_required
def system_retrieve_logs(request):
    '''
        Called by the dashboard system page with ajax request every seconds, to get the logs and print them
    '''
    if request.is_ajax():
        alert_logs = Log.objects.filter(agent='Alert manager').order_by("-created")[:MAX_LOGS_LINES]
        scheduler_logs = Log.objects.filter(agent='Scheduler').order_by("-created")[:MAX_LOGS_LINES]
        majordome_logs = Log.objects.filter(agent='Majordome').order_by("-created")[:MAX_LOGS_LINES]
        obs_logs = Log.objects.filter(agent='Observation manager').order_by("-created")[:MAX_LOGS_LINES]
        analyzer_logs = Log.objects.filter(agent='Analyzer').order_by("-created")[:MAX_LOGS_LINES]
        monitoring_logs = Log.objects.filter(agent='Monitoring').order_by("-created")[:MAX_LOGS_LINES]
        return render(request, 'dashboard/system_logs.html', locals())


def schedule(request):
    url_ = reverse('admin:common_schedule_changelist')
    return redirect(url_)


@login_required
#@level_required(6)
def quotas(request):
    url_ = reverse('admin:common_pyrosuser_changelist')
    return redirect(url_)

@login_required
#@level_required(3)
def change_globalMode(request):
    try :
        config = get_object_or_404(Config, id=1)
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty() 
        if config.global_mode == False and plc_device_status.is_safe == False:
            return redirect('states')
        config.global_mode = not config.global_mode
        config.save()
        return redirect('states')
    except Config.DoesNotExist:
        return redirect('states')

@login_required
#@level_required(4)
def change_bypass(request):
    try :
        config = get_object_or_404(Config, id=1)
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty() 
        if plc_device_status.is_safe == True:
            return redirect('states')
        config.bypass = not config.bypass
        config.save()
        return redirect('states')
    except Config.DoesNotExist:
        return redirect('states')

@login_required
#@level_required(3)
def change_lock(request):
    try :
        config = get_object_or_404(Config, id=1)
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty() 
        if config.lock == False and plc_device_status.is_safe == False:
            return redirect('states')
        config.lock = not config.lock
        if (config.lock == True):
            config.ntc = not config.ntc
        config.save()
        return redirect('states')
    except Config.DoesNotExist:
        return redirect('states')





@login_required
#@level_required(3)
def send_command_to_cameraVIS_1(request):

    data = ""
    with open('../simulators/config/grammar.json') as f:
        data = json.load(f, object_pairs_hook=OrderedDict)
        json_str = json.dumps(data)
    return render(request, "dashboard/send_command_cameraVIS_1.html", locals())

@login_required
#@level_required(3)
def submit_command_to_cameraVIS_1(request):
    '''
            function called when a command it submitted for the cameraVIS_1 in the remote control page
            (called in the corresponding control_command file)
    '''

    if request.method == 'POST':
        commands = [request.POST.get("first_command"), request.POST.get("first_param")]
        try: #TODO faire un truc plus joli pour gérer les params queqlue soit leur nombre
            input_0 = request.POST.get("input_number_0")
            input_1 = request.POST.get("input_number_1")
            input_2 = request.POST.get("input_number_2")
            input_3 = request.POST.get("input_number_3")

            if input_0:
                commands.append(input_0)
            if input_1:
                commands.append(input_1)
            if input_2:
                commands.append(input_2)
            if input_3:
                commands.append(input_3)
        except Exception:
            pass
        response = CameraVISRemoteControlDefault(commands, expert_mode=False, chosen_camera="ddrago_r").exec_command()
        #TODO passer en JS pour send les réponses en  AJAX
    return redirect('send_command_to_cameraVIS_1')

@login_required
#@level_required(3)
def submit_command_to_cameraVIS_1_expert(request):
    '''
            function called when a command it submitted in expert mode for the cameraVIS_1 in the remote control page
            (called in the corresponding control_command file)
    '''
    if request.method == 'POST':
        param = request.POST.get("commande_expert")
        if param:
            response = CameraVISRemoteControlDefault(param, expert_mode=True, chosen_camera="ddrago_r").exec_command()
            #os.system("echo \"status :" + response + "\" >> /home/portos/IRAP/pyros/src/status")
            return HttpResponse(json.dumps({'message': "Command send OK", 'response': response}))
        return HttpResponse(json.dumps({'message': "Missing command data"}))
    return redirect('submit_command_to_telescope')




@login_required
#@level_required(3)
def send_command_to_cameraNIR(request):
    data = ""
    with open('../simulators/config/grammar.json') as f:
        data = json.load(f, object_pairs_hook=OrderedDict)
        json_str = json.dumps(data)
    return render(request, "dashboard/send_command_cameraNIR.html", locals())


@login_required
#@level_required(3)
def submit_command_to_cameraNIR(request):
    '''
            function called when a command it submitted for the cameraNIR in the remote control page
            (called in the corresponding control_command file)
    '''
    if request.method == 'POST':
        commands = [request.POST.get("first_command"), request.POST.get("first_param")]
        try: #TODO faire un truc plus joli pour gérer les params queqlue soit leur nombre
            input_0 = request.POST.get("input_number_0")
            input_1 = request.POST.get("input_number_1")
            input_2 = request.POST.get("input_number_2")
            input_3 = request.POST.get("input_number_3")

            if input_0:
                commands.append(input_0)
            if input_1:
                commands.append(input_1)
            if input_2:
                commands.append(input_2)
            if input_3:
                commands.append(input_3)
        except Exception:
            pass
        response = CameraNIRRemoteControlDefault(commands, expert_mode=False).exec_command()
        #TODO passer en JS pour send les réponses en  AJAX
    return redirect('send_command_to_cameraNIR')

@login_required
#@level_required(3)
def submit_command_to_cameraNIR_expert(request):
    '''
            function called when a command it submitted in expert mode for the cameraNIR in the remote control page
            (called in the corresponding control_command file)
    '''
    if request.method == 'POST':
        param = request.POST.get("commande_expert")
        if param:
            response = CameraNIRRemoteControlDefault(param, expert_mode=True).exec_command()
            return HttpResponse(json.dumps({'message': "Command send OK", 'response': response}))
        return HttpResponse(json.dumps({'message': "Missing command data"}))
    return redirect('submit_command_to_cameraNIR')


@login_required
#@level_required(3)
def send_command_to_cameraVIS_2(request):
    data = ""
    with open('../simulators/config/grammar.json') as f:
        data = json.load(f, object_pairs_hook=OrderedDict)
        json_str = json.dumps(data)
    return render(request, "dashboard/send_command_cameraVIS_2.html", locals())

@login_required
#@level_required(3)
def submit_command_to_cameraVIS_2(request):
    '''
            function called when a command it submitted for the cameraVIS_2 in the remote control page
            (called in the corresponding control_command file)
    '''
    if request.method == 'POST':
        commands = [request.POST.get("first_command"), request.POST.get("first_param")]
        try: #TODO faire un truc plus joli pour gérer les params queqlue soit leur nombre
            input_0 = request.POST.get("input_number_0")
            input_1 = request.POST.get("input_number_1")
            input_2 = request.POST.get("input_number_2")
            input_3 = request.POST.get("input_number_3")

            if input_0:
                commands.append(input_0)
            if input_1:
                commands.append(input_1)
            if input_2:
                commands.append(input_2)
            if input_3:
                commands.append(input_3)
        except Exception:
            pass
        response = CameraVISRemoteControlDefault(commands, expert_mode=False, chosen_camera="ddrago_b").exec_command()
        #TODO passer en JS pour send les réponses en  AJAX
    return redirect('send_command_to_cameraVIS_2')

@login_required
#@level_required(3)
def submit_command_to_cameraVIS_2_expert(request):
    '''
            function called when a command it submitted in expert mode for the cameraVIS_2 in the remote control page
            (called in the corresponding control_command file)
    '''
    if request.method == 'POST':
        param = request.POST.get("commande_expert")
        if param:
            response = CameraVISRemoteControlDefault(param, expert_mode=True, chosen_camera="ddrago_b").exec_command()
            return HttpResponse(json.dumps({'message': "Command send OK", 'response': response}))
        return HttpResponse(json.dumps({'message': "Missing command data"}))
    return redirect('submit_command_to_cameraVIS_2')




@login_required
#@level_required(3)
def send_command_to_telescope(request):

    data = ""
    with open('../simulators/config/grammar.json') as f:
        data = json.load(f, object_pairs_hook=OrderedDict)
        json_str = json.dumps(data)
    return render(request, "dashboard/send_command_telescope.html", locals())

@login_required
#@level_required(3)
def submit_command_to_telescope(request):
    '''
            function called when a command it submitted for the Telescope in the remote control page
            (called in control_command.js)
    '''
    if request.method == 'POST':
        commands = [request.POST.get("first_command"), request.POST.get("first_param")]
        try: #TODO faire un truc plus joli pour gérer les params quelque soit leur nombre la c'est du rapide super moche et max 4 input
            input_0 = request.POST.get("input_number_0")
            input_1 = request.POST.get("input_number_1")
            input_2 = request.POST.get("input_number_2")
            input_3 = request.POST.get("input_number_3")

            if input_0:
                commands.append(input_0)
            if input_1:
                commands.append(input_1)
            if input_2:
                commands.append(input_2)
            if input_3:
                commands.append(input_3)
        except Exception:
            pass
        request = ' '.join(commands)
        NEW_MODE = True
        if NEW_MODE:
            TelescopeCommand.objects.create(request=request)
        else:
            response = TelescopeRemoteControlDefault(commands, False).exec_command()
        #TODO passer en JS pour send les réponses en  AJAX
    return redirect('send_command_to_telescope')

@login_required
#@level_required(3)
def submit_command_to_telescope_expert(request):
    '''
                function called when a command it submitted in expert mode for the Telescope in the remote control page
                (called in control_command.js)
    '''
    if request.method == 'POST':
        param = request.POST.get("commande_expert")
        if param:

            request = TelescopeCommand.objects.create(request=param)
            time.sleep(1)
            response = TelescopeCommand.objects.get(pk=request.id).answer
            return HttpResponse(json.dumps({'message': "Command send OK", 'response': response}))
        return HttpResponse(json.dumps({'message': "Missing command data"}))
    return redirect('submit_command_to_telescope')


@login_required
#@level_required(3)
def submit_command_to_plc(request):
    if request.method == 'POST':
        param = request.POST.get("commande_expert")
        if param:
            plc_controller = PLC.PLCController()
            response = "Unrecognized command"
            if param.startswith("GET STATUS ") or param =="GET STATUS": #pas beau a revoir (classe dédiée ?)
                response = plc_controller.getStatus(param)
            elif param.startswith("SWITCH LIGHTS"):
                response = plc_controller.switch_lights(param)
            elif param.endswith(" SHUTTERS"):
                response = plc_controller.manage_shutters(param)
            return HttpResponse(json.dumps({'message': "Command send OK", 'response': response}))
        return HttpResponse(json.dumps({'message': "Missing command data"}))
    return redirect('submit_command_to_plc')


@login_required
#@level_required(3)
def send_command_to_plc(request):
    return render(request, 'dashboard/send_command_to_plc.html')


@login_required
#@level_required(3)
def operator_state(request):
    instance = get_object_or_404(Config, id=1)
    #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
    plc_device_status = get_latest_plc_device_status_or_empty()
    return render(request, 'dashboard/operator_state.html', {'config' : instance, 'is_safe' : plc_device_status.is_safe})

@login_required
#@level_required(3)
def simulator(request):
    try :
        config = get_object_or_404(Config, id=1)
        #plc_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_status = get_latest_plc_device_status_or_empty()
        return(render(request, 'dashboard/simulator.html', locals()))
    except Config.DoesNotExist:
        return redirect('simulator')


@login_required
@level_required("Admin")
def simulator_switch_ack(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.ack = not config.ack
        config.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

# if safe => switch to unsafe
# if unsafe => switch to safe
@login_required
@level_required("Admin")
def simulator_switch_safe(request):
    try :
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty() 
        plc_device_status.is_safe = not plc_device_status.is_safe
        plc_device_status.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_give_auto(request):
    try :
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty() 
        plc_device_status.plc_mode = "AUTO"
        plc_device_status.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_give_manu(request):
    try :
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty() 
        plc_device_status.plc_mode = "MANU"
        plc_device_status.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_give_ko(request):
    try :
        #plc_device_status = PlcDeviceStatus.objects.exclude(plc_mode=None).latest('created')
        plc_device_status = get_latest_plc_device_status_or_empty() 
        plc_device_status.plc_mode = "OFF"
        plc_device_status.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_switch_bypass(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.bypass = not config.bypass
        config.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_switch_lock(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.lock = not config.lock
        config.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_switch_globalMode(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.global_mode = not config.global_mode
        config.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_majordome_restart(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.majordome_state = "OCS-RESTART"
        config.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')

@login_required
@level_required("Admin")
def simulator_majordome_shutdown(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.majordome_state = "OCS-SHUTDOWN"
        config.save()
        return redirect('simulator')
    except Config.DoesNotExist:
        return redirect('simulator')