Blame view

simulators/telescope/telescopeSimulator.py 5.53 KB
605b65e5   Jeremy   Update simulators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
import sys

PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

from utils.Device import Device
from utils.StatusManager import StatusManager

class TelescopeSimulator(Device, StatusManager):
    status = {"status" : "VALID"}
    doors_timer = 5
    move_time = 5

    def __init__(self, argv):
        super().__init__(argv)
        self.setDevice("Telescope")
f7dd3df1   Jeremy   Update simulators...
19
        self.setStatusManager("telescopeSimulator", argv)
605b65e5   Jeremy   Update simulators
20
21
22
23
24

    def telescopePrint(self, string: str):
        print("telescopeSimulator : " + string)

    def handleConnection(self):
f7dd3df1   Jeremy   Update simulators...
25
26
        for co in self.connections:
            data = self.readBytes(128, co)
605b65e5   Jeremy   Update simulators
27

f7dd3df1   Jeremy   Update simulators...
28
29
            answer = ""
            cut_data = data.split(" ")
605b65e5   Jeremy   Update simulators
30

f7dd3df1   Jeremy   Update simulators...
31
32
33
34
35
36
37
38
39
40
41
42
            if (len(cut_data) < 2):
                self.telescopePrint("Received invalid message on socket : " + data)
                continue
            cmd_type = cut_data[0]
            cmd = cut_data[1]
            args = cut_data[2:]
            if (cmd_type == "GET"):
                if (cmd == "STATUS"):
                    answer = self.create_status()
                else:
                    answer = "Invalid cmd for GET: " + cmd
                    self.telescopePrint(answer)
605b65e5   Jeremy   Update simulators
43
            else:
f7dd3df1   Jeremy   Update simulators...
44
                self.telescopePrint("Ignored message " + data)
605b65e5   Jeremy   Update simulators
45

f7dd3df1   Jeremy   Update simulators...
46
47
            if (len(answer) > 0):
                self.sendMessage(answer, co)
605b65e5   Jeremy   Update simulators
48
49
50
51
52
53
54
55
56
57
58
59
        return (0)

    def loop(self):
        i = 0

        if (self.ended == 0):
            self.telescopePrint("Not entry for telescope found in config file : " + self.config_file)
            return (0)
        while (True):
            self.updateStatus(i)
            if (self.isConnected()):
                self.handleConnection()
605b65e5   Jeremy   Update simulators
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
            i += 1
            if (i > self.ended):
                return (0)
        return (1)

    def run(self):
        print("Telescope simulator running")
        self.parse()
        self.configSocket()
        self.loop()
        return (0)

if (__name__ == "__main__"):
    sim = TelescopeSimulator(sys.argv)
    sim.run()


# class Telescope(Device):
#
#     def __init__(self):
#         super().__init__("Telescope")
#
#         ''' I will just fill the attributes with their names and params without regarding them '''
#         self.attributes = {}
#
#         self.doors_status = "True"
#         self.status = "IDLE"
#
#
#     def loop(self):
#         move_timer = 0
#         doors_timer = 0
#
#         while True:
#             try:
#                 conn, addr = self.sock.accept()
#             except socket.error as e:
#                 print("There was a socket error: " + repr(e))
#                 break
#
#             data = conn.recv(128).decode()
#
#             # print("Received: " + data)
#
#             '''
#                 Pour le moment, les messages sont de taille variable, contenant :
#                     CMD_TYPE CMD ARG1 ARG2 ...
#                 On considère que ce qu'on reçoit peut être faux, mais que dans tous les cas, on recoit le message au complet.
#                 Pas de souci s'async puisqu'on prend une connection à la fois.
#             '''
#
#             answer = ""
#             cut_data = data.split(" ")
#
#             if move_timer != 0 and time.time() > move_timer:
#                 move_timer = 0
#                 status = "IDLE"
#                 print("Stopped movement")
#             if doors_timer != 0 and time.time() > doors_timer:
#                 doors_timer = 0
#                 print("Stopped doors")
#
#             if len(cut_data) < 2:
#                 print("Invalid message: " + data)
#             else:
#                 cmd_type = cut_data[0]
#                 cmd = cut_data[1]
#                 args = cut_data[2:]
#                 if cmd_type == "SET":
#                     self.attributes[cmd] = args
#                 elif cmd_type == "GET":
#                     if cmd == "POSITION":
#                         answer = "101.287 -16.716"
#                     elif cmd == "STATUS":
#                         answer = self.status
#                     elif cmd == "SETUP":
#                         answer = "GET SETUP answer not implemented"
#                     else:
#                         answer = "Invalid cmd for GET: " + cmd
#                         print(answer)
#                 elif cmd_type == "DO":
#                     if cmd == "START":
#                         print("Start movement")
#                         move_timer = time.time() + MOVE_TIME
#                     elif cmd == "STOP":
#                         move_timer = 0
#                     elif cmd == "ABORT":
#                         move_timer = 0
#                     elif cmd == "HOMING":
#                         print("Start movement (homing)")
#                         move_timer = time.time() + MOVE_TIME
#                     elif cmd == "DOORS":
#                         if len(args) > 0:
#                             if args[0] != self.doors_status:
#                                 print("Start door")
#                                 self.doors_status = args[0]
#                                 doors_timer = time.time() + DOORS_TIME
#                     else:
#                         print("Invalid cmd for DO: " + cmd)
#                 else:
#                     print("Invalid message: " + data)
#
#             if len(answer) > 0:
#                 conn.send(bytes(answer, "UTF-8"))
#                 # print("send: " + answer)
#             conn.close()
#
# tel = Telescope()
# tel.loop()