Blame view

simulators/camera/cameraNIRSimulator.py 8.83 KB
605b65e5   Jeremy   Update simulators
1
2
3
import os
import sys

3224f14a   Jeremy   Fixed some simula...
4
DEBUG_FILE = False
605b65e5   Jeremy   Update simulators
5
6
7
8
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...
9
10
#(EP renamed) from utils.Device import Device
from utils.DeviceSim import DeviceSim
605b65e5   Jeremy   Update simulators
11
12
from utils.StatusManager import StatusManager

1957226d   Etienne Pallier   Renamed (simulato...
13
14
#(EP) class CameraNIRSimulator(Device, StatusManager):
class CameraNIRSimulator(DeviceSim, StatusManager):
605b65e5   Jeremy   Update simulators
15
16
17
    exposure_time = 5
    shutter_time = 3
    cooler_timer = 5
5f7e4955   Quentin Durand   Add some commands...
18
19
20
21
22
23
24
25
    _window_x1 = 0
    _window_x2 = 0
    _window_y1 = 0
    _window_y2 = 0
    _cooler = "OFF"
    status = {"status": "VALID", "exposure_time" : exposure_time, "shutter_time": shutter_time,
              "cooler_timer" : cooler_timer, "cooler" : _cooler, "win x1" : _window_x1, "win x2" : _window_x2, "win y1" : _window_y1, "win_y2" : _window_y2}

605b65e5   Jeremy   Update simulators
26
27
28
29
30
31
32

    def __init__(self, argv):
        super().__init__(argv)
        self.setDevice("CameraNIR")
        self.setStatusManager("cameraNIRSimulator", argv)

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

5f7e4955   Quentin Durand   Add some commands...
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
77
78
79
    def deal_command(self, cmd_type, cmd, args):
        answer = ""

        if cmd_type == "GET":
            if cmd == "STATUS":
                answer = str({"status": "VALID", "exposure_time" : self.exposure_time, "shutter_time": self.shutter_time,
              "cooler_timer" : self.cooler_timer, "cooler" : self._cooler,
              "win x1" : self._window_x1, "win x2" : self._window_x2,
              "win y1" : self._window_y1, "win_y2" : self._window_y2})
            elif cmd == "TIMER":
                answer = "cooler timer : " + str(self.cooler_timer)
            else:
                answer = "NOT IMPLEMENTED YET"

        elif cmd_type == "SET":
            if cmd == "WINDOW":
                os.system("echo " + str(args) + " >> /home/portos/IRAP/PYROS/argss")
                self._window_x1 = int(args[0])
                self._window_x2 = int(args[1])
                self._window_y1 = int(args[2])
                self._window_y2 = int(args[3])
                answer = "Window coordinates are set to " + str(args)

            else:
                answer = "NOT IMPLEMENTED YET"

        elif cmd_type == "DO":
            if cmd == "COOLER":
                if args[0] == "ON":
                    self._cooler = "ON tmp: " + str(args[1])
                    answer = "COOLER TURNED ON tmp: " + str(args[1])

                elif args[0] == "OFF":
                    self._cooler = "OFF"
                    answer = "COOLER TURNED OFF"

            else:
                answer = "NOT IMPLEMENTED YET"

        else:
            answer = "Invalid or not implemented cmd"

        return answer

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

f7dd3df1   Jeremy   Update simulators...
84
85
            answer = ""
            cut_data = data.split(" ")
605b65e5   Jeremy   Update simulators
86

f7dd3df1   Jeremy   Update simulators...
87
88
89
90
91
92
            if (len(cut_data) < 2):
                self.cameraNIRPrint("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...
93
94
95
96
97

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


            '''
f7dd3df1   Jeremy   Update simulators...
98
99
100
101
102
103
            if (cmd_type == "GET"):
                if (cmd == "STATUS"):
                    answer = self.create_status()
                else:
                    answer = "Invalid cmd for GET: " + cmd
                    self.cameraNIRPrint(answer)
c53a13e0   Jeremy   Updating a lot of...
104
105
            elif (cmd_type == "DO"):
                if (cmd == "CAPTURE"):
3224f14a   Jeremy   Fixed some simula...
106
                    folder_name = args[0]
c53a13e0   Jeremy   Updating a lot of...
107
108
109
110
                    nb_images = int(args[1])
                    duration = args[2]
                    i = 0
                    while i < nb_images:
3224f14a   Jeremy   Fixed some simula...
111
112
113
114
115
                        try:
                            with open(folder_name + os.sep + "nir_image_"+str(i), "w") as f:
                                f.write(folder_name+" "+str(nb_images)+" "+str(duration))
                        except Exception as e:
                            self.cameraNIRPrint(str(e))
c53a13e0   Jeremy   Updating a lot of...
116
117
                        i += 1
                    answer = "CAPTURING"
605b65e5   Jeremy   Update simulators
118
            else:
f7dd3df1   Jeremy   Update simulators...
119
                self.cameraNIRPrint("Ignored message " + data)
5f7e4955   Quentin Durand   Add some commands...
120
121
122
            '''


605b65e5   Jeremy   Update simulators
123

f7dd3df1   Jeremy   Update simulators...
124
125
            if (len(answer) > 0):
                self.sendMessage(answer, co)
605b65e5   Jeremy   Update simulators
126
127
128
129
130
131
        return (0)

    def loop(self):
        i = 0

        if (self.ended == 0):
3224f14a   Jeremy   Fixed some simula...
132
            self.cameraNIRPrint("No entry for cameraNIR found in config file : " + self.config_file)
605b65e5   Jeremy   Update simulators
133
134
135
136
137
            return (0)
        while (True):
            self.updateStatus(i)
            if (self.isConnected()):
                self.handleConnection()
605b65e5   Jeremy   Update simulators
138
139
140
141
142
143
            i += 1
            if (i > self.ended):
                return (0)
        return (1)

    def run(self):
3224f14a   Jeremy   Fixed some simula...
144
145
        if DEBUG_FILE:
            print("cameraNIR simulator running")
605b65e5   Jeremy   Update simulators
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
        self.parse()
        self.configSocket()
        self.loop()
        return (0)

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


# class CameraNIR(Camera):
# 
#     def __init__(self):
#         super().__init__("CameraNIR")
# 
#         ''' I will just fill the attributes with their names and params without regarding them '''
#         self.attributes = {}
#         self.attributes["FILENAME"] = "default_nir_" + str(randint(1, 10000))
# 
#         self.status = "IDLE"
#         self.shutter_status = "CLOSE"
# 
#     def loop(self):
#         # à gérer : la réception des paramètres, le temps de shutter (et son statut), le temps de start, le stop, le abort, la création d'images
# 
#         cooler_timer = 0
#         shutter_timer = 0
#         exposure_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)
# 
#             answer = ""
#             cut_data = data.split(" ")
# 
#             if cooler_timer != 0 and time.time() > cooler_timer:
#                 cooler_timer = 0
#                 status = "IDLE"
#                 print("Ended cooling")
#             if shutter_timer != 0 and time.time() > shutter_timer:
#                 shutter_timer = 0
#                 print("Ended shutter")
#             if exposure_timer > 0 and time.time() > exposure_timer:
#                 exposure_timer = -1
#                 print("Ended exposure")
#                 with open(os.path.join(IMAGES_FOLDER, self.attributes["FILENAME"]), 'w'):
#                     pass
# 
#             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":
#                     if cmd == "FILENAME":
#                         if len(args) > 0:
#                             self.attributes["FILENAME"] = " ".join(args)
#                         else:
#                             print("An argument is needed for the FILENAME command")
#                     else:
#                         self.attributes[cmd] = args
#                 elif cmd_type == "GET":
#                     if cmd == "STATUS":
#                         answer = self.status
#                     elif cmd == "TEMPERATURE":
#                         answer = "GET TEMPERATURE answer not implemented"
#                     elif cmd == "SETUP":
#                         answer = "GET SETUP answer not implemented"
#                     elif cmd == "TIMER":
#                         if exposure_timer > 0:
#                             answer = str(int(exposure_timer - time.time()))
#                         else:
#                             answer = str(exposure_timer)
#                     else:
#                         answer = "Invalid cmd for GET: " + cmd
#                         print(answer)
#                 elif cmd_type == "DO":
#                     if cmd == "START":
#                         exposure_timer = time.time() + EXPOSURE_TIME
#                     elif cmd == "STOP":
#                         exposure_timer = -1
#                     elif cmd == "ABORT":
#                         exposure_timer = -1
#                     elif cmd == "COOLER":
#                         cooler_timer = time.time() + COOLER_TIME
#                     elif cmd == "SHUTTER":
#                         shutter_timer = time.time() + SHUTTER_TIME
#                     else:
#                         print("Invalid cmd for GET: " + cmd)
#                 else:
#                     print("Invalid message: " + data)
# 
#             if len(answer) > 0:
#                 conn.send(bytes(answer, "UTF-8"))
#                 # print("send: " + answer)
# 
#             conn.close()
# 
# cam = CameraNIR()
# cam.loop()