Device.py
2.55 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
import configparser
import socket
import json
from pydoc import locate
IMAGES_FOLDER = '../src/images'
CONFIG_FILE = "socket_config.ini"
GRAMMAR_FILE = "grammar.json"
class Device():
"""
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, ...)
"""
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 = {}
with open(GRAMMAR_FILE) as grammar_file:
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"]:
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))
for item in device["get"]:
self.get_msgs.append(item)
for item in device["do"]:
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))
device = grammar[simul_name]
for item in device["set"]:
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))
for item in device["get"]:
self.get_msgs.append(item)
for item in device["do"]:
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))