Commit bb0fbab1a7b9e0373c2e2a741cc0adb7fa1f2009

Authored by haribo
1 parent 04dd1f06
Exists in master and in 1 other branch dev

Fini de mettre la grammaire en JSON + adaptation des controllers +

adaptation du code qui les appelle
simulators/Device.py
1 1 import configparser
2 2 import socket
3 3 import json
4   -
5   -
6   -# from enum import Enum
7   -# import time
8   -# import os
9   -# from random import randint
  4 +from pydoc import locate
10 5  
11 6 IMAGES_FOLDER = '../src/images'
12 7 CONFIG_FILE = "socket_config.ini"
13 8 GRAMMAR_FILE = "grammar.json"
14 9  
15 10 class Device():
  11 + """
  12 + Generic class for simulators.
  13 + Initializes the sockets and the grammar, reading the CONFIG_FILE.
  14 +
  15 + Classes inheriting from it must call the __init__ function with their name (Telescope, CameraVIS, ...)
  16 + """
  17 +
16 18  
17 19 def __init__(self, simul_name):
18 20  
... ... @@ -33,7 +35,7 @@ class Device():
33 35  
34 36 self.enums = {}
35 37  
36   - with open("grammar.json") as grammar_file:
  38 + with open(GRAMMAR_FILE) as grammar_file:
37 39 grammar = json.load(grammar_file)
38 40  
39 41 enums = grammar["Enums"]
... ... @@ -45,17 +47,30 @@ class Device():
45 47 device = grammar["Camera"]
46 48  
47 49 for item in device["set"]:
48   - self.set_msgs.append((item["name"], item["nb_param"], item["param_type"])) # TODO : transformer les param_type qui ne sont pas des enum
  50 + param_type = item["param_type"]
  51 + if len(param_type) < 4 or param_type[:4] != "Enum":
  52 + ''' This transforms 'int' string to <int> type '''
  53 + param_type = locate(param_type)
  54 + self.set_msgs.append((item["name"], item["nb_param"], param_type))
49 55 for item in device["get"]:
50 56 self.get_msgs.append(item)
51 57 for item in device["do"]:
52   - self.do_msgs.append((item["name"], item["nb_param"], item["param_type"])) # TODO : transformer les param_type qui ne sont pas des enum
  58 + param_type = item["param_type"]
  59 + if len(param_type) < 4 or param_type[:4] != "Enum":
  60 + param_type = locate(param_type)
  61 + self.do_msgs.append((item["name"], item["nb_param"], param_type))
53 62  
54 63 device = grammar[simul_name]
55 64  
56 65 for item in device["set"]:
57   - self.set_msgs.append((item["name"], item["nb_param"], item["param_type"])) # TODO : transformer les param_type qui ne sont pas des enum
  66 + param_type = item["param_type"]
  67 + if len(param_type) < 4 or param_type[:4] != "Enum":
  68 + param_type = locate(param_type)
  69 + self.set_msgs.append((item["name"], item["nb_param"], param_type))
58 70 for item in device["get"]:
59 71 self.get_msgs.append(item)
60 72 for item in device["do"]:
61   - self.do_msgs.append((item["name"], item["nb_param"], item["param_type"])) # TODO : transformer les param_type qui ne sont pas des enum
  73 + param_type = item["param_type"]
  74 + if len(param_type) < 4 or param_type[:4] != "Enum":
  75 + param_type = locate(param_type)
  76 + self.do_msgs.append((item["name"], item["nb_param"], param_type))
... ...
src/devices/CameraNIR.py 0 โ†’ 100644
... ... @@ -0,0 +1,12 @@
  1 +from common.models import *
  2 +from .Device import DeviceController
  3 +
  4 +
  5 +class NIRCameraController(DeviceController):
  6 + """
  7 + Device controller for NIR Camera.
  8 + This class may implement set, get or do functions to handle particularities.
  9 + """
  10 +
  11 + def __init__(self):
  12 + super().__init__("CameraNIR")
... ...
src/devices/CameraVIS.py 0 โ†’ 100644
... ... @@ -0,0 +1,11 @@
  1 +from common.models import *
  2 +from .Device import DeviceController
  3 +
  4 +class VISCameraController(DeviceController):
  5 + """
  6 + Device controller for VIS Camera.
  7 + This class may implement set, get or do functions to handle particularities.
  8 + """
  9 +
  10 + def __init__(self):
  11 + super().__init__("CameraVIS")
... ...
src/devices/Device.py
1 1 from common.models import *
2 2 import socket
  3 +import configparser
  4 +import json
  5 +from pydoc import locate
  6 +
  7 +CONFIG_FILE = "../simulators/socket_config.ini"
  8 +GRAMMAR_FILE = "../simulators/grammar.json"
3 9  
4 10 class DeviceController():
5 11 '''
... ... @@ -7,16 +13,65 @@ class DeviceController():
7 13 '''
8 14  
9 15  
10   - def __init__(self):
  16 + def __init__(self, device_name):
11 17 '''
12 18 The messages and their parameters will be defined in the objects that inherits from this class
13 19 '''
14   - set_msgs = []
15   - get_msgs = []
16   - do_msgs = []
17 20  
18   - self.ip = None
19   - self.port = None
  21 +
  22 + config = configparser.ConfigParser()
  23 + config.read(CONFIG_FILE)
  24 +
  25 + self.ip = config.get(device_name, "ip")
  26 + self.port = int(config.get(device_name, "port"))
  27 +
  28 + self.set_msgs = []
  29 + self.get_msgs = []
  30 + self.do_msgs = []
  31 +
  32 + self.enums = {}
  33 +
  34 + with open(GRAMMAR_FILE) as grammar_file:
  35 + grammar = json.load(grammar_file)
  36 +
  37 + enums = grammar["Enums"]
  38 +
  39 + for enum in enums :
  40 + self.enums[enum] = enums[enum]
  41 +
  42 + if device_name[:6] == "Camera":
  43 + device = grammar["Camera"]
  44 +
  45 + for item in device["set"]:
  46 + param_type = item["param_type"]
  47 + if len(param_type) < 4 or param_type[:4] != "Enum":
  48 + ''' This transforms 'int' string to <int> type '''
  49 + param_type = locate(param_type)
  50 + self.set_msgs.append((item["name"], item["nb_param"], param_type))
  51 + for item in device["get"]:
  52 + self.get_msgs.append(item)
  53 + for item in device["do"]:
  54 + param_type = item["param_type"]
  55 + if len(param_type) < 4 or param_type[:4] != "Enum":
  56 + param_type = locate(param_type)
  57 + self.do_msgs.append((item["name"], item["nb_param"], param_type))
  58 +
  59 + device = grammar[device_name]
  60 +
  61 + for item in device["set"]:
  62 + param_type = item["param_type"]
  63 + if len(param_type) < 4 or param_type[:4] != "Enum":
  64 + param_type = locate(param_type)
  65 + self.set_msgs.append((item["name"], item["nb_param"], param_type))
  66 + for item in device["get"]:
  67 + self.get_msgs.append(item)
  68 + for item in device["do"]:
  69 + param_type = item["param_type"]
  70 + if len(param_type) < 4 or param_type[:4] != "Enum":
  71 + param_type = locate(param_type)
  72 + self.do_msgs.append((item["name"], item["nb_param"], param_type))
  73 +
  74 +
20 75  
21 76 def init_socket(self):
22 77 if self.ip is None or self.port is None:
... ...
src/devices/NIRCamera.py deleted
... ... @@ -1,58 +0,0 @@
1   -from common.models import *
2   -from .Device import DeviceController
3   -from enum import Enum
4   -import configparser
5   -
6   -CONFIG_FILE = "../simulators/socket_config.ini"
7   -
8   -class ReadmodeEnum(Enum):
9   - ramp = "Ramp"
10   - # TODO: dรฉfinir les modes de lecture
11   -
12   -class ShutterEnum(Enum):
13   - synchro = "Synchro"
14   - closed = "Closed"
15   - opened = "Opened"
16   -
17   -class FilterEnum(Enum):
18   - t = "T"
19   - h = "H"
20   - # TODO: dรฉfinir les noms des filtres
21   -
22   -class NIRCameraController(DeviceController):
23   -
24   - def __init__(self):
25   - super().__init__()
26   -
27   - self.set_msgs = [
28   - ("WINDOW", [4, ], int),
29   - ("READMODE", [1, ], ReadmodeEnum),
30   - ("FILENAME", [1, ], str),
31   - ("HEADER", [1, ], dict),
32   - ("READOUT_FREQUENCY", [1, ], float),
33   - ("NB_IMAGES", [1, ], int),
34   - ("FILTER", [1, ], FilterEnum)
35   - ]
36   -
37   - self.get_msgs = [
38   - "STATUS",
39   - "SETUP",
40   - "TEMPERATURE",
41   - "TIMER",
42   - ]
43   -
44   - self.do_msgs = [
45   - ("COOLER", [2, ], float),
46   - ("SHUTTER", [1, ], ShutterEnum),
47   - ("START", [0, ]),
48   - ("ABORT", [0, ]),
49   - ("STOP", [0, ]),
50   - ]
51   -
52   - config = configparser.ConfigParser()
53   - config.read(CONFIG_FILE)
54   -
55   - self.ip = config.get("CameraNIR", "ip")
56   - self.port = int(config.get("CameraNIR", "port"))
57   -
58   - print(self.ip, self.port)
59 0 \ No newline at end of file
src/devices/PLC.py
1 1 from common.models import *
2 2 from .Device import DeviceController
3   -from enum import Enum
4   -import configparser
5   -
6   -CONFIG_FILE = "../simulators/socket_config.ini"
7 3  
8 4 class PLCController(DeviceController):
  5 + """
  6 + Device controller for PLC.
  7 + This class may implement set, get or do functions to handle particularities.
  8 + """
9 9  
10 10 def __init__(self):
11   - super().__init__()
12   -
13   - self.set_msgs = [
14   - ]
15   -
16   - self.get_msgs = [
17   - "STATUS",
18   - ]
19   -
20   - self.do_msgs = [
21   - ]
22   -
23   - config = configparser.ConfigParser()
24   - config.read(CONFIG_FILE)
25   -
26   - self.ip = config.get("PLC", "ip")
27   - self.port = int(config.get("PLC", "port"))
28   -
29   - print(self.ip, self.port)
30 11 \ No newline at end of file
  12 + super().__init__("PLC")
... ...
src/devices/Telescope.py
1 1 from common.models import *
2 2 from .Device import DeviceController
3   -from enum import Enum
4   -import configparser
5   -
6   -class RotatorEnum(Enum):
7   - none = "None"
8   - synchro = "Synchro"
9   - tracking = "Tracking"
10   -
11   -class MoveModeEnum(Enum):
12   - goto = "Goto"
13   - track = "Track"
14   - goto_track = "GotoTrack"
15   -
16   -class CoordsFrameEnum(Enum):
17   - hadec = "Hadec"
18   - radec = "Radec"
19   - altaz = "Altaz"
20   -
21   -CONFIG_FILE = "../simulators/socket_config.ini"
22 3  
23 4 class TelescopeController(DeviceController):
  5 + """
  6 + Device controller for Telescope.
  7 + This class may implement set, get or do functions to handle particularities.
  8 + """
24 9  
25 10 def __init__(self):
26   - super().__init__()
27   -
28   - self.set_msgs = [
29   - ("SPEED", [2, 3], float),
30   - ("COORDS", [2, 3], float),
31   - ("TRACKING_SPEED", [2, 3], float),
32   - ("ACCEL", [2, 3], float),
33   -
34   - ("ROTATOR", [1, ], RotatorEnum),
35   - ("FOCUS", [1, ], float),
36   - ("MOVE_MODE", [1, ], MoveModeEnum),
37   - ("COORDS_FRAME", [1, ], CoordsFrameEnum),
38   - ]
39   -
40   - self.get_msgs = [
41   - "POSITION",
42   - "STATUS",
43   - "SETUP",
44   - ]
45   -
46   - self.do_msgs = [
47   - ("DOORS", [1, ], bool),
48   - ("START", [0, ],),
49   - ("ABORT", [0, ],),
50   - ("STOP", [0, ],),
51   - ("HOMING", [0, ],),
52   - ]
53   -
54   - config = configparser.ConfigParser()
55   - config.read(CONFIG_FILE)
56   -
57   - self.ip = config.get("Telescope", "ip")
58   - self.port = int(config.get("Telescope", "port"))
59   -
60   - print(self.ip, self.port)
61 11 \ No newline at end of file
  12 + super().__init__("Telescope")
... ...
src/devices/VISCamera.py deleted
... ... @@ -1,59 +0,0 @@
1   -from common.models import *
2   -from .Device import DeviceController
3   -from enum import Enum
4   -import configparser
5   -
6   -CONFIG_FILE = "../simulators/socket_config.ini"
7   -
8   -class ReadmodeEnum(Enum):
9   - ramp = "Ramp"
10   - # TODO: dรฉfinir les modes de lecture
11   -
12   -class ShutterEnum(Enum):
13   - synchro = "Synchro"
14   - closed = "Closed"
15   - opened = "Opened"
16   -
17   -class FilterEnum(Enum):
18   - t = "T"
19   - h = "H"
20   - # TODO: dรฉfinir les noms des filtres
21   -
22   -class VISCameraController(DeviceController):
23   -
24   - def __init__(self):
25   - super().__init__()
26   -
27   - self.set_msgs = [
28   - ("WINDOW", [4, ], int),
29   - ("READMODE", [1, ], ReadmodeEnum),
30   - ("FILENAME", [1, ], str),
31   - ("HEADER", [1, ], dict),
32   - ("READOUT_FREQUENCY", [1, ], float),
33   - ("EXPOSURE", [1, ], int),
34   - ("BINNING", [2, ], int),
35   - ("FILTER", [1, ], FilterEnum)
36   - ]
37   -
38   - self.get_msgs = [
39   - "STATUS",
40   - "SETUP",
41   - "TEMPERATURE",
42   - "TIMER",
43   - ]
44   -
45   - self.do_msgs = [
46   - ("COOLER", [2, ], float),
47   - ("SHUTTER", [1, ], ShutterEnum),
48   - ("START", [0, ]),
49   - ("ABORT", [0, ]),
50   - ("STOP", [0, ]),
51   - ]
52   -
53   - config = configparser.ConfigParser()
54   - config.read(CONFIG_FILE)
55   -
56   - self.ip = config.get("CameraVIS", "ip")
57   - self.port = int(config.get("CameraVIS", "port"))
58   -
59   - print(self.ip, self.port)
60 0 \ No newline at end of file
src/majordome/TaskManager.py
... ... @@ -2,7 +2,7 @@ from common.models import *
2 2  
3 3 from celery.task.control import revoke
4 4 import time
5   -from devices import Telescope, VISCamera, NIRCamera
  5 +from devices import Telescope, CameraVIS, CameraNIR
6 6  
7 7 def delete_pending_tasks(task_names, terminate=False):
8 8 '''
... ... @@ -45,8 +45,8 @@ def delete_pending_alert():
45 45 delete_pending_tasks(["execute_plan"], terminate=False)
46 46  
47 47 tel = Telescope.TelescopeController()
48   - VIScam = VISCamera.VISCameraController()
49   - NIRcam = NIRCamera.NIRCameraController()
  48 + VIScam = CameraVIS.VISCameraController()
  49 + NIRcam = CameraNIR.NIRCameraController()
50 50 tel.do("ABORT")
51 51 VIScam.do("ABORT")
52 52 NIRcam.do("ABORT")
... ...
src/monitoring/tasks.py
... ... @@ -8,8 +8,8 @@ import observation_manager.tasks
8 8 import time
9 9  
10 10 from devices.Telescope import TelescopeController
11   -from devices.VISCamera import VISCameraController
12   -from devices.NIRCamera import NIRCameraController
  11 +from devices.CameraVIS import VISCameraController
  12 +from devices.CameraNIR import NIRCameraController
13 13 from devices.PLC import PLCController
14 14  
15 15 import time
... ...
src/observation_manager/tasks.py
... ... @@ -4,8 +4,8 @@ from celery.task import Task
4 4 from analyzer.tasks import analysis
5 5 from common.models import *
6 6  
7   -from devices import VISCamera as VIS
8   -from devices import NIRCamera as NIR
  7 +from devices import CameraVIS as VIS
  8 +from devices import CameraNIR as NIR
9 9 from devices import Telescope
10 10  
11 11 import time
... ...