Blame view

simulators/device/Device.py 2.58 KB
6a1a30ed   carens_p   grammar for instr...
1
2
3
import configparser
import socket
import json
bb0fbab1   haribo   Fini de mettre la...
4
from pydoc import locate
6a1a30ed   carens_p   grammar for instr...
5

0afcefa9   Jeremy   Reworked simulato...
6
7
8
IMAGES_FOLDER = '../../src/images'
CONFIG_FILE = "../config/socket_config.ini"
GRAMMAR_FILE = "../config/grammar.json"
6a1a30ed   carens_p   grammar for instr...
9
10

class Device():
bb0fbab1   haribo   Fini de mettre la...
11
12
13
14
15
16
17
    """
        Generic class for simulators.
        Initializes the sockets and the grammar, reading the CONFIG_FILE.

        Classes inheriting from it must call the __init__ function with their name (Telescope, CameraVIS, ...)
    """

6a1a30ed   carens_p   grammar for instr...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

    def __init__(self, simul_name):

        config = configparser.ConfigParser()
        config.read(CONFIG_FILE)

        ip = config.get(simul_name, "ip")
        port = int(config.get(simul_name, "port"))

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((ip, port))
        self.sock.listen(122)

        self.set_msgs = []
        self.get_msgs = []
        self.do_msgs = []

        self.enums = {}

bb0fbab1   haribo   Fini de mettre la...
38
        with open(GRAMMAR_FILE) as grammar_file:
6a1a30ed   carens_p   grammar for instr...
39
40
41
42
43
44
45
46
47
48
49
            grammar = json.load(grammar_file)

        enums = grammar["Enums"]

        for enum in enums :
            self.enums[enum] = enums[enum]

        if simul_name[:6] == "Camera":
            device = grammar["Camera"]

            for item in device["set"]:
bb0fbab1   haribo   Fini de mettre la...
50
51
52
53
54
                param_type = item["param_type"]
                if len(param_type) < 4 or param_type[:4] != "Enum":
                    ''' This transforms 'int' string to <int> type '''
                    param_type = locate(param_type)
                self.set_msgs.append((item["name"], item["nb_param"], param_type))
6a1a30ed   carens_p   grammar for instr...
55
56
57
            for item in device["get"]:
                self.get_msgs.append(item)
            for item in device["do"]:
bb0fbab1   haribo   Fini de mettre la...
58
59
60
61
                param_type = item["param_type"]
                if len(param_type) < 4 or param_type[:4] != "Enum":
                    param_type = locate(param_type)
                self.do_msgs.append((item["name"], item["nb_param"], param_type))
6a1a30ed   carens_p   grammar for instr...
62
63
64
65

        device = grammar[simul_name]

        for item in device["set"]:
bb0fbab1   haribo   Fini de mettre la...
66
67
68
69
            param_type = item["param_type"]
            if len(param_type) < 4 or param_type[:4] != "Enum":
                param_type = locate(param_type)
            self.set_msgs.append((item["name"], item["nb_param"], param_type))
6a1a30ed   carens_p   grammar for instr...
70
71
72
        for item in device["get"]:
            self.get_msgs.append(item)
        for item in device["do"]:
bb0fbab1   haribo   Fini de mettre la...
73
74
75
76
            param_type = item["param_type"]
            if len(param_type) < 4 or param_type[:4] != "Enum":
                param_type = locate(param_type)
            self.do_msgs.append((item["name"], item["nb_param"], param_type))