Agent.py 18.6 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
VERSION = "0.4"


"""
=================================================================
    IMPORT PYTHON PACKAGES
=================================================================
"""

#from __future__ import absolute_import

import time
from datetime import datetime, timedelta
import os


"""TODO:

- 1 log par agent + lastlog (30 dernières lignes)
- 1 table par agent
- table agents_log avec log minimum pour affichage dans dashboard, et ordre chrono intéressant pour suivi activité : nom agent, timestamp, message
- table agent_<agent-name>_vars : nom variable, value, desc

"""


# from django.core.exceptions import ObjectDoesNotExist
# from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.conf import settings as djangosettings

import utils.Logger as L

from config.configpyros import ConfigPyros

"""
import observation_manager
import observation_manager.tasks
import scheduler
import scheduler.tasks as sched_task
import monitoring.tasks
import alert_manager.tasks
"""
# from common.models import *
from common.models import Config, Log, PlcDeviceStatus
from common.models import AgentSurvey, Command
from dashboard.views import get_sunelev
from devices.TelescopeRemoteControlDefault import TelescopeRemoteControlDefault

"""
from devices.CameraNIR import NIRCameraController
from devices.CameraVIS import VISCameraController
from devices.Dome import DomeController
from devices.PLC import PLCController
from devices.Telescope import TelescopeController
from majordome.MajordomeDecorators import *
from utils.JDManipulator import *
"""

from threading import Thread




"""
=================================================================
    GENERAL MODULE CONSTANT DEFINITIONS
=================================================================
"""

DEBUG_FILE = False

log = L.setupLogger("AgentLogger", "Agent")




"""
=================================================================
    class Agent
=================================================================
"""

class Agent:
    # (EP) do this so that Majordome can be run from a thread, and called with thread.start():
    # class Majordome(Task, Thread):

    # FOR TEST ONLY
    # Run this agent in simulator mode
    SIMULATOR_MODE = True
    SIMULATOR_COMMANDS = iter([
        "go_active",

        "go_idle",
        "specific_not_executed_because_idle",

        "go_active",
        "specific_executed_because_not_idle",

        "stop"
    ])
    #_simulator_current_cmd_idx = 0
    #_current_test_cmd = None
    #_nb_test_cmds = 0

    # Run for real, otherwise just print messages without really doing anything
    FOR_REAL = True

    #COMMANDS_PEREMPTION_HOURS = 48
    COMMANDS_PEREMPTION_HOURS = 60/60

    name = "Generic Agent"
    mainloop_waittime = 3
    subloop_waittime = 2
    status = None
    mode = None
    config = None

    # Statuses
    STATUS_LAUNCH = "LAUNCHED"
    STATUS_INIT = "INITIALIZING"
    STATUS_MAIN_LOOP = "IN_MAIN_LOOP"
    STATUS_PROCESS_LOOP = "IN_PROCESS_LOOP"
    STATUS_EXIT = "EXITING"

    # Modes
    MODE_ACTIVE = "ACTIVE"
    MODE_IDLE = "IDLE"

    DEFAULT_CONFIG_FILE_NAME = "config_unit_simulunit1.xml"
    CONFIG_DIR = "config"

    _agent_survey = None

    _iter_num = 0

    def __init__(self, name:str=None, config_filename:str=None):
        self.set_mode(self.MODE_IDLE)
        self.set_status(self.STATUS_LAUNCH)
        self.name = name
        if not config_filename:
            #config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml'
            config_filename = self.DEFAULT_CONFIG_FILE_NAME
        #config_file_path, _ = os.path.split(config_filename)
        if config_filename == os.path.basename(config_filename):
            config_filename = os.path.abspath(self.CONFIG_DIR + os.sep + config_filename)
        print("Config file used is", config_filename)
        #print("current path", os.getcwd())
        #print("this file path :", __file__)
        #print("config file path is", config_filename)
        # Instantiate an object for configuration
        #print("config file path is ", config_abs_filename)
        self.config = ConfigPyros(config_filename)
        if self.config.get_last_errno() != self.config.NO_ERROR:
            raise Exception(f"Bad config file name '{config_filename}', error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}")

        # Create 1st survey if none
        #tmp = AgentSurvey.objects.filter(name=self.name)
        #if len(tmp) == 0:
        #nb_agents = AgentSurvey.objects.filter(name=self.name).count()
        #if nb_agents == 0:
        if not AgentSurvey.objects.filter(name=self.name).exists():
            self._agent_survey = AgentSurvey.objects.create(name=self.name, validity_duration_sec=60, mode=self.mode, status=self.status)
            print("Agent survey is", self._agent_survey)
            #self._agent_survey = AgentSurvey(name=self.name, validity_duration_sec=60, mode=self.mode, status=self.status)
            #self._agent_survey.save()


    def __str__(self):
        return "I am agent " + self.name

    def run(self, FOR_REAL: bool = True):
        """
            FOR_REAL: set to False if you don't want Majordome to send commands to devices
        """

        self.FOR_REAL = FOR_REAL

        self.load_config()

        self.init()

        '''
        print()
        print(self)
        print("FOR REAL ?", self.FOR_REAL)
        print("DB3 used is:", djangosettings.DATABASES["default"]["NAME"])

        # SETUP
        try:
            self.config = get_object_or_404(Config, id=1)
            # By default, set mode to SCHEDULER (False = REMOTE, which should never be the default)
            self.config.global_mode = True
            self.config.save()
            # self.config = Config.objects.get(pk=1)
            # self.config = Config.objects.get()[0]
        except Exception as e:
            # except Config.ObjectDoesNotExist:
            print("Config read (or write) exception", str(e))
            return -1
        '''

        # Main loop
        while True:

            print()
            print()
            #print("-"*80)

            print("-"*20, f"MAIN LOOP ITERATION {self._iter_num} (START)", "-"*20)
            self.set_status(self.STATUS_MAIN_LOOP)
            self.show_mode_and_status()

            self.load_config()

            self.update_survey()

            if self.SIMULATOR_MODE: self.simulator_send_command_to_myself()

            # generic cmd in json format
            print("---")
            cmd = self.get_next_command()

            if cmd: cmd = self.general_process(cmd)
            '''
            if self.config.majordome_state == "STOP":
                break
            if self.config.majordome_state == "RESTART":
                self.config.majordome_restarted = True
                self.closing_mode = self.config.majordome_state
                self.config.majordome_state = "RUNNING"
                self.config.save()
            '''

            # Sub-level loop (only if ACTIVE)
            if self.is_active():
                self.set_status(self.STATUS_PROCESS_LOOP)
                if cmd: self.specific_process(cmd)
            print("---")

            # Every N iterations, delete old commands
            N=3
            if (self._iter_num % N) == 0: self.purge_commands()

            self.waitfor(self.mainloop_waittime)

            print("-"*20, "MAIN LOOP ITERATION (END)", "-"*20)
            #print("-"*80)

            #self.do_log(LOG_DEBUG, "Ending main loop iteration")

            self._iter_num += 1


    def purge_commands(self):
        """
        Delete commands (which I am recipient of) older than COMMANDS_PEREMPTION_HOURS (like 48h)

        NB: datetime.utcnow() is equivalent to datetime.now(timezone.utc)
        """

        COMMAND_PEREMPTION_DATE_FROM_NOW = datetime.utcnow() - timedelta(hours = self.COMMANDS_PEREMPTION_HOURS)
        print("peremption date", COMMAND_PEREMPTION_DATE_FROM_NOW)
        old_commands = Command.objects.filter(
            # only commands for me
            receiver = self.name,
            # only pending commands
            sender_deposit_time__lt = COMMAND_PEREMPTION_DATE_FROM_NOW,
        )
        if old_commands.exists():
            print("Found old commands to delete:")
            for cmd in old_commands: print(cmd)
            old_commands.delete()

    def waitfor(self, nbsec):
            print(f"Now, waiting for {nbsec} seconds...")
            time.sleep(nbsec)

    def set_status(self, status:str):
        print(f"Switching from status {self.status} to status {status}")
        self.status = status
        return False

    def set_mode(self, mode:str):
        print(f"Switching from mode {self.mode} to mode {mode}")
        self.mode = mode

    def is_active(self):
        return self.mode == self.MODE_ACTIVE

    def set_active(self):
        self.set_mode(self.MODE_ACTIVE)

    def set_idle(self):
        self.set_mode(self.MODE_IDLE)

    def show_mode_and_status(self):
        print(f"CURRENT MODE is {self.mode} (with status {self.status})")

    def die(self):
        self.set_status(self.STATUS_EXIT)

    """
    suspend/resume
    """
    def suspend(self):
        """
        TODO:
        Mode IDLE (doit rester à l'écoute d'un resume, 
        et doit continuer à alimenter les tables pour informer de son état via tables agents_logs, 
        et lire table agents_command pour reprendre via resume, 
        et update la table agents_survey pour donner son status "idle"
        """
        self.set_idle()
        return True

    def resume(self):
        """
        Quit suspend() mode
        """
        self.set_active()
        return True

    def set_mode_from_config(self, agent_alias):
        # --- Get the startmode of the AgentX
        modestr = self.config.get_paramvalue(agent_alias,'general','startmode')
        if self.config.get_last_errno() != self.config.NO_ERROR:
            raise Exception(f"error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}")        
        if (modestr == None):
            return True
        # --- Set the mode according the startmode value
        mode = self.MODE_IDLE
        if modestr.upper() == 'RUN':
            mode = self.MODE_ACTIVE
        self.set_mode(mode)
        return True


    """
    =================================================================
    Generic methods that may be specialized (overriden) by subclasses
    =================================================================
    """

    def init(self):
        print("Initializing...")
        self.set_status(self.STATUS_INIT)

    def load_config(self):
        """
        TODO:
        only si date fichier xml changée => en RAM, un objet Config avec méthodes d'accès, appelle le parser de AK (classe Config.py indépendante)
        """
        print("Loading the config file...")
        #config_filename = 'c:/srv/develop/pyros/config/config_unit_simulunit1.xml'
        #config.set_configfile(config_filename)
        self.config.load()
        if self.config.get_last_errno() != self.config.NO_ERROR:
            raise Exception(f"error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}")
        # --- display informations
        # --- Get all the assembly of this unit[0] (mount + channels)
        if self.config.is_config_contents_changed():
            print("--------- Components of the unit -----------")
            print("Configuration file is {}".format(self.config.get_configfile()))            
            alias = self.config.get_aliases('unit')[0]
            namevalue = self.config.get_paramvalue(alias,'unit','name')
            print("Unit alias is {}. Name is {}".format(alias,namevalue))
            unit_subtags = self.config.get_unit_subtags()
            for unit_subtag in unit_subtags:
                aliases = self.config.get_aliases(unit_subtag)
                for alias in aliases:
                    namevalue = self.config.get_paramvalue(alias,unit_subtag,'name')
                    print("Unit {} alias is {}. Name is {}".format(unit_subtag,alias,namevalue))
            print("------------------------------------------")
            #params = self.config.get_params(unit_alias)
            #for param in params:
            #    print("Unit component is {}".format(param))

        """
        # self.config = Config.objects.get(pk=1)
        try:
            self.config = get_object_or_404(Config, id=1)
            # By default, set mode to SCHEDULER (False = REMOTE, which should never be the default)
            # self.config.global_mode = True
            # self.config.save()
            # self.config = Config.objects.get(pk=1)
            # self.config = Config.objects.get()[0]
        except Exception as e:
            # except Config.ObjectDoesNotExist:
            # except Config.DoesNotExist:
            print("Config read (or write) exception", str(e))
            # return self.config
            # return -1
            return False
        """

    def update_survey(self):
        print("Updating the survey database table...")
        self._agent_survey = AgentSurvey.objects.get(name=self.name)
        self._agent_survey.mode = self.mode
        self._agent_survey.status = self.status
        self._agent_survey.save()


    def simulator_send_command_to_myself(self):
        #self._current_test_cmd = "go_idle" if self._current_test_cmd=="go_active" else "go_active"
        #if self._nb_test_cmds == 4: self._current_test_cmd = "stop"
        cmd = next(self.SIMULATOR_COMMANDS, None)
        if not cmd: return
        agent_command = Command.objects.create(sender=self.name, receiver=self.name, command=cmd)
        #self._simulator_current_cmd_idx += 1
        #self._nb_test_cmds += 1


    def get_next_command(self)->Command:
        """
        Return next command (read from the DB command table) which is relevant to this agent
        Commands are read in chronological order
        """

        print("Looking for new commands from the database ...")

        # 1) Is there a command currently being processed (status CMD_RUNNING) ?
        # If so, return
        if Command.objects.filter(
            # only commands for me
            receiver = self.name,
            # only pending commands
            receiver_status_code = Command.CMD_STATUS_CODES.CMD_RUNNING,
        ).exists():
            print("There is currently a running command, so I do nothing (wait for end of execution)")
            return None

        # 2) Get only the oldest PENDING command which I am recipient of
        cmd = Command.objects.filter(
            # only commands for me
            receiver = self.name,
            # only pending commands
            receiver_status_code = Command.CMD_STATUS_CODES.CMD_PENDING,
        ).order_by('sender_deposit_time').first()
        #).order_by('sender_deposit_time')
        #print("all commands", next_command)
        #next_command = next_command.first()
        if not cmd: return None
        print(f"Got command {cmd.command} sent by agent {cmd.sender} at {cmd.sender_deposit_time}")
        print(cmd)

        # 3) Update read time to say that the command has been READ
        ##assert cmd.receiver_read_time is None # f"Command {cmd} should not have been already read !!"
        cmd.set_as_read()
        return cmd


    def general_process(self, cmd:Command)->Command:

        # Precondition : command cmd has already been read
        assert cmd.receiver_read_time is not None # f"Command {cmd} should have been already read !!"

        print(f"Starting general processing of command {cmd.command} sent by agent {cmd.sender} at {cmd.sender_deposit_time}")
        #print(cmd)

        # If expired command, change its status to expired and return
        elapsed_time = cmd.receiver_read_time - cmd.sender_deposit_time
        max_time = timedelta(seconds = cmd.validity_duration_sec)
        print(f"Elapsed time is {elapsed_time}, (max is {max_time})")
        if elapsed_time > max_time:
            print("This command is expired, so mark it as expired, and ignore it")
            #cmd.delete()
            cmd.set_as_outofdate()
            return None

        # If cmd is generic, execute it, change its status to executed, and return
        if cmd.is_generic():
            print("This command is generic, execute it...")
            self.exec_generic_cmd(cmd)
            if cmd.command == "stop": exit(0)
            return None

        # cmd is not generic
        else:
            # cmd is not generic but, as I am idle, change its status to SKIPPED, ignore it, and return
            if self.mode == self.MODE_IDLE:
                print("This command is not generic but, as I am IDLE, I mark it SKIPPED and ignore it")
                cmd.set_as_skipped()
                return None

        # Je suis pas idle et cde pas générique: je la traite pas, elle sera traitée par core_process :
        # attendre que cette commande soit exécutée avant de passer à la commande suivante (situation “bloquante” normale)
        print("This command is not generic and, as I am not IDLE, I pass it to the specific processing")
        print("(then I will wait for this command to be EXECUTED before going to next command)")
        return cmd


    def exec_generic_cmd(self, cmd:Command):
        cmd.set_as_running()
        # Executing command
        if cmd.command == "go_active": self.set_active()
        elif cmd.command == "go_idle": self.set_idle()
        elif cmd.command == "stop": pass
        time.sleep(1)
        cmd.set_as_executed()
        print("...Generic cmd has been executed")



    def do_log(self):
        """
        log à 2 endroits ou 1 seul
        - in file
        - in db
        """
        print("Logging data...")


    # @abstract
    # to be implemented by subclasses
    def specific_process(self, cmd:Command):
        #raise NotImplemented()
        """
        Sublevel Loop (only if ACTIVE) :
        PLUS TARD, maybe :start_process_thread() dans un thread : ensuite, à chaque tour de boucle il regarde si c'est fini ou pas, et si fini recommence
        """
        assert self.is_active()
        # TODO: LOG



    """ 
    ===================================
    OLD FUNCTIONS TO BE REMOVED 
    ===================================
    """

    def _plc_is_not_auto(self):
        if not self.plc_is_connected():
            return True
        # now, self.plc_status has been updated, so check it:
        return self.plc_status.plc_mode != "AUTO"


    def plc_is_auto(self):
        return not self._plc_is_not_auto()


    def plc_is_safe(self):
        if not self.plc_is_connected():
            return False
        # now, self.plc_status has been updated, so check it:
        return self.plc_status.is_safe


    def is_night(self):
        return get_sunelev() < -10