CameraVIS.py 4.39 KB
import configparser
import socket
from enum import Enum
import time

CONFIG_FILE = "socket_config.ini"

EXPOSURE_TIME = 5
SHUTTER_TIME = 3
COOLER_TIME = 10

class ReadmodeEnum(Enum):
    ramp = "Ramp"
    # TODO: définir les modes de lecture

class ShutterEnum(Enum):
    synchro = "Synchro"
    closed = "Closed"
    opened = "Opened"


class CameraVIS():
    def __init__(self):
        config = configparser.ConfigParser()
        config.read(CONFIG_FILE)

        ip = config.get("CameraVIS", "ip")
        port = int(config.get("CameraVIS", "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(12)

        self.set_msgs = [
            ("WINDOW", [4, ], int),
            ("READMODE", [1, ], ReadmodeEnum),
            ("FILENAME", [1, ], str),
            ("HEADER", [1, ], dict),
            ("READOUT_FREQUENCY", [1, ], float),
            ("EXPOSURE", [1, ], int),
            ("BINNING", [2, ], int),
        ]

        self.get_msgs = [
            "STATUS",
            "SETUP",
            "TEMPERATURE",
            "TIMER",
        ]

        self.do_msgs = [
            ("COOLER", [2, ], float),
            ("SHUTTER", [1, ], ShutterEnum),
            ("START", [0, ]),
            ("ABORT", [0, ]),
            ("STOP", [0, ]),
        ]

        ''' I will just fill the attributes with their names and params without regarding them '''
        self.attributes = {}

        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
        pass

        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("Stopped cooling")
            if shutter_timer != 0 and time.time() > shutter_timer:
                shutter_timer = 0
                print("Stopped shutter")
            if exposure_timer != 0 and time.time() > exposure_timer:
                exposure_timer = 0
                print("Stopped exposure")
                # TODO: créer une image

            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 == "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":
                        answer = "GET TIMER answer not implemented"
                    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 = 0
                    elif cmd == "ABORT":
                        exposure_timer = 0
                    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 = CameraVIS()
cam.loop()