Blame view

simulators/CameraNIR.py 3.85 KB
b53dbffe   haribo   Date: 22/07/2016
1
2
3
import socket
from enum import Enum
import time
aed99094   haribo   Debug routine MGR...
4
5
import os
from random import randint
6a1a30ed   carens_p   grammar for instr...
6
from Device import Device
b53dbffe   haribo   Date: 22/07/2016
7

aed99094   haribo   Debug routine MGR...
8
IMAGES_FOLDER = '../src/images'
b53dbffe   haribo   Date: 22/07/2016
9
10
11
12
13

EXPOSURE_TIME = 5
SHUTTER_TIME = 3
COOLER_TIME = 10

b53dbffe   haribo   Date: 22/07/2016
14

6a1a30ed   carens_p   grammar for instr...
15
class CameraNIR(Device):
b53dbffe   haribo   Date: 22/07/2016
16

b53dbffe   haribo   Date: 22/07/2016
17
    def __init__(self):
6a1a30ed   carens_p   grammar for instr...
18
        super().__init__("CameraNIR")
b53dbffe   haribo   Date: 22/07/2016
19
20
21

        ''' I will just fill the attributes with their names and params without regarding them '''
        self.attributes = {}
aed99094   haribo   Debug routine MGR...
22
        self.attributes["FILENAME"] = "default_nir_" + str(randint(1, 10000))
b53dbffe   haribo   Date: 22/07/2016
23
24
25
26
27
28

        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
b53dbffe   haribo   Date: 22/07/2016
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

        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"
00fa2456   haribo   Debug adaption code
51
                print("Ended cooling")
b53dbffe   haribo   Date: 22/07/2016
52
53
            if shutter_timer != 0 and time.time() > shutter_timer:
                shutter_timer = 0
00fa2456   haribo   Debug adaption code
54
55
56
57
                print("Ended shutter")
            if exposure_timer > 0 and time.time() > exposure_timer:
                exposure_timer = -1
                print("Ended exposure")
db882807   haribo   Date: 02/08/2016
58
59
                with open(os.path.join(IMAGES_FOLDER, self.attributes["FILENAME"]), 'w'):
                    pass
b53dbffe   haribo   Date: 22/07/2016
60
61
62
63
64
65
66
67

            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":
aed99094   haribo   Debug routine MGR...
68
69
                    if cmd == "FILENAME":
                        if len(args) > 0:
db882807   haribo   Date: 02/08/2016
70
                            self.attributes["FILENAME"] = " ".join(args)
aed99094   haribo   Debug routine MGR...
71
72
73
74
                        else:
                            print("An argument is needed for the FILENAME command")
                    else:
                        self.attributes[cmd] = args
b53dbffe   haribo   Date: 22/07/2016
75
76
77
78
79
80
81
82
                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":
00fa2456   haribo   Debug adaption code
83
84
85
86
                        if exposure_timer > 0:
                            answer = str(int(exposure_timer - time.time()))
                        else:
                            answer = str(exposure_timer)
b53dbffe   haribo   Date: 22/07/2016
87
88
89
90
91
92
93
                    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":
00fa2456   haribo   Debug adaption code
94
                        exposure_timer = -1
b53dbffe   haribo   Date: 22/07/2016
95
                    elif cmd == "ABORT":
00fa2456   haribo   Debug adaption code
96
                        exposure_timer = -1
b53dbffe   haribo   Date: 22/07/2016
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
                    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()

00fa2456   haribo   Debug adaption code
112
cam = CameraNIR()
b53dbffe   haribo   Date: 22/07/2016
113
cam.loop()