Blame view

simulators/telescope/telescopeSimulator.py 7.14 KB
32d5d60e   Etienne Pallier   bugfix monitoring...
1
2
#!/usr/bin/env python3

605b65e5   Jeremy   Update simulators
3
4
import os
import sys
5f7e4955   Quentin Durand   Add some commands...
5
import time
605b65e5   Jeremy   Update simulators
6

3224f14a   Jeremy   Fixed some simula...
7
DEBUG_FILE = False
605b65e5   Jeremy   Update simulators
8
9
10
11
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)))

1957226d   Etienne Pallier   Renamed (simulato...
12
from utils.DeviceSim import DeviceSim
605b65e5   Jeremy   Update simulators
13
14
from utils.StatusManager import StatusManager

3224f14a   Jeremy   Fixed some simula...
15

1957226d   Etienne Pallier   Renamed (simulato...
16
class TelescopeSimulator(DeviceSim, StatusManager):
5f7e4955   Quentin Durand   Add some commands...
17
18
    _coords = "0, 0, 0"
    _real_coords = "0, 0 ,0"
605b65e5   Jeremy   Update simulators
19
20
    doors_timer = 5
    move_time = 5
5f7e4955   Quentin Durand   Add some commands...
21
22
23
    _loop_mode = False
    status = {"status": "VALID", "coords": _coords, "real_coords" : _real_coords, "doors_timer": doors_timer, "move_time" : move_time, "loop_mode": _loop_mode}

605b65e5   Jeremy   Update simulators
24
25
26
27

    def __init__(self, argv):
        super().__init__(argv)
        self.setDevice("Telescope")
7e40048f   Quentin Durand   Implementation ba...
28
29
30
31
        if len(argv) == 1:
            self.loop_mode = True
        if (len(argv) > 1):
            self.setStatusManager("telescopeSimulator", argv)
605b65e5   Jeremy   Update simulators
32
33

    def telescopePrint(self, string: str):
3224f14a   Jeremy   Fixed some simula...
34
35
        if DEBUG_FILE:
            print("telescopeSimulator : " + string)
605b65e5   Jeremy   Update simulators
36

5f7e4955   Quentin Durand   Add some commands...
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
77
78

    def deal_command(self, cmd_type, cmd, args):
        answer = ""
        if cmd_type == "GET":
            if cmd == "INFORMATIONS":
                status = {"status": "VALID", "coords": self._coords, "real_coords": self._real_coords, "doors_timer": self.doors_timer,
                          "move_time": self.move_time, "loop_mode": self._loop_mode}
                answer = str(status)
            elif cmd.startswith("COORD"):
                answer = cmd +" " + str(self._coords)
            else:
                answer = "NOT IMPLEMENTED YET"

        elif cmd_type == "SET":
            if cmd.startswith("COORD"):
                self._coords = str(args)
                answer = "COORDS set to " + self._coords
            else:
                answer = "NOT IMPLEMENTED YET"


        elif cmd_type == "DO":
            if cmd == "GOTO":
                self._real_coords = self._coords
                answer = "Starting slew of the mount"

            elif cmd == "HOMING":
                self._coords = "0, 0, 0"
                self._real_coords = "0, 0, 0"
                answer = "Start the Homong of the mount"

            else:
                answer = "NOT IMPLEMENTED YET"




        else:
            answer ="Invalid or not implemented cmd"
        return answer


605b65e5   Jeremy   Update simulators
79
    def handleConnection(self):
f7dd3df1   Jeremy   Update simulators...
80
81
        for co in self.connections:
            data = self.readBytes(128, co)
605b65e5   Jeremy   Update simulators
82

5f7e4955   Quentin Durand   Add some commands...
83
84
            #answer = ""
            cut_data = data.split()
605b65e5   Jeremy   Update simulators
85

f7dd3df1   Jeremy   Update simulators...
86
87
88
89
90
91
            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:]
5f7e4955   Quentin Durand   Add some commands...
92
93
94
95
96
97

            answer = self.deal_command(cmd_type, cmd, args)

            '''
                sleep pour simuler temps de latence de mouvement : probleme sans multi-thread le simulateur est inactif pendant ce temps
            '''
605b65e5   Jeremy   Update simulators
98

f7dd3df1   Jeremy   Update simulators...
99
100
            if (len(answer) > 0):
                self.sendMessage(answer, co)
5f7e4955   Quentin Durand   Add some commands...
101
102
103



605b65e5   Jeremy   Update simulators
104
105
106
107
108
        return (0)

    def loop(self):
        i = 0

7e40048f   Quentin Durand   Implementation ba...
109
        if not self.loop_mode and self.ended == 0:
605b65e5   Jeremy   Update simulators
110
111
            self.telescopePrint("Not entry for telescope found in config file : " + self.config_file)
            return (0)
7e40048f   Quentin Durand   Implementation ba...
112
        while True:
605b65e5   Jeremy   Update simulators
113
            self.updateStatus(i)
7e40048f   Quentin Durand   Implementation ba...
114
            if self.isConnected():
605b65e5   Jeremy   Update simulators
115
                self.handleConnection()
605b65e5   Jeremy   Update simulators
116
            i += 1
7e40048f   Quentin Durand   Implementation ba...
117
118
119
            if not self.loop_mode and i > self.ended:
                return 0
        return 1
605b65e5   Jeremy   Update simulators
120
121

    def run(self):
3224f14a   Jeremy   Fixed some simula...
122
123
        if DEBUG_FILE:
            print("Telescope simulator running")
605b65e5   Jeremy   Update simulators
124
125
126
        self.parse()
        self.configSocket()
        self.loop()
7e40048f   Quentin Durand   Implementation ba...
127
        return 0
605b65e5   Jeremy   Update simulators
128

7e40048f   Quentin Durand   Implementation ba...
129
130

if __name__ == "__main__":
605b65e5   Jeremy   Update simulators
131
132
133
    sim = TelescopeSimulator(sys.argv)
    sim.run()

605b65e5   Jeremy   Update simulators
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# 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()