tasks.py 4.66 KB
from __future__ import absolute_import
from django.conf import settings
from common.models import *
from celery.task import Task

import logger.config as l
import sys
import os
import time
import json

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

SIMULATOR_VALUE = None

class simulator(Task):
    shouldClean = False

    def loadJson(self, path):
        json_data = open(path, 'r')
        # if (not json_data.is_open()):

    def run(self):
        print('-------------------------------')
        print(SIMULATOR_VALUE)

        # END OF EXECTUION (KILLING WEB SERVER AN PROCESS)
        if (self.shouldClean == True):
            os.system("%s/../pyrosrun.sh kill_simulation"%settings.BASE_DIR)

if __name__ == "__main__":
    sim = simulator(Task)
    sim.shouldClean = False
    sim.run()


"""
class SimulatorRoutines():
    def __init__(self, dtime):
        self.dtime = dtime
        self.dicto = {
            "SCIENTIFIC_PROGRAMS" : self.createScientificPrograms,
            "USER_LEVELS" : self.createUserLevels,
            "PYROS_USERS" : self.createPyrosUsers,
            "AUTH_USERS" : self.createAuthUsers,
            "REQUESTS" : self.createRequest,
            "ALBUMS" : self.createAlbums,
            "PLANS" : self.createPlans,
            "DETECTORS" : self.createDetector,
            "TELESCOPES" : self.createTelescope,
            "DEVICES" : self.createDev
        }

    USER PART
    # First create some scientific programs
    def createScientificPrograms(self, params, default=False):
        sc = []
        i = 0
        # for (value in self.data["SCIENTIFIC_PROGRAMS"]):

        if (quantity == -1):
            return (sc)
        while (quantity > i):
            sc[i] = ScientificProgram(name="Sc_test-%d"%quantity, desc="Simulation", quota=(100 / quantity), priority=i)
            sc[i].save
            i += 1
        return (sc)

    # create some user levels with diferent priorities
    def createUserLevels(self, params, default=False):
        pass

    # create some pyros users -> country, user_id, user_level_id
    def createPyrosUsers(self, params, default=False):
        pass

    # create auth users
    def createAuthUsers(self, params, default=False):
        pass




    DEVICE PART
    # if you want to create an album you must have a detector
    # device, telescope
    def createDetector(self, params, default=False):
        pass

    # detector must have a telescope
    # device
    def createTelescope(self, params, default=False):
        pass

    # detector AND telescope must have a device
    def createDevice(self, params, default=False):
        dc = []
        i = 0

        if (quantity == -1):
            # TODO create from class array loaded by loadJson
            return (dc)
        while (quantity > i):
            dc[i] = Device(name="testdevice-%d"%i, desc="Simulator",
                create=datetime.datetime.now(), updated=datetime.datetime.now(),
                is_online="1", status="Online")
            i += 1
        return (dc)



    ROUTINE PART
    # You can now create Requests
    # pyros_user_id, scientific_program_id
    def createRequest(self, params, default=False):
        pass

    # requests must have sequences
    # Request
    def createSequences(self, params, default=False):
        pass

    # Sequences must have albums and albums must have a detector
    # sequence, detector
    def createAlbums(self, params, default=False):
        pass

    # albums must have plans
    # album id, plan id
    def createPlans(self, params, default=False):
        pass

    SIMULATOR PART
    def prepareDatabase(self):
        for (key, value in self.data):
            for (name, func in self.dicto):
                if (name == key):
                    ret = func(value)
                    if (not ret):
                        return (False)
        return (True)

    def loadJson(self, path):
        json_data = open(path, 'r')
        if (not json_data.is_open()):
            return (False)
        self.init = True
        data1 = json.load(json_data)
        self.data = data1
        json_data.close()
        return (True)

    def process(self):

        for (key, value in dicto):
            array_object = value()
            for (value in array_object):
                value.save()
        pass
class simulator(Task):
    shouldClean = False

    def run(self):
        simu = SimulatorRoutines(100, 2)
        simu.loadJson("%s/../public/simulation/data.json"%settings.BASE_DIR)
        simu.prepareDatabase()
        simu.create
        simu.process()

        # END OF EXECTUION (KILLING WEB SERVER AN PROCESS)
        if (self.shouldClean == True):
            os.system("%s/../pyrosrun.sh kill_simulation"%settings.BASE_DIR)

"""