Telescope.py 4.65 KB
import configparser
import socket
from enum import Enum
import time

CONFIG_FILE = "socket_config.ini"

class RotatorEnum(Enum):
    none = "None"
    synchro = "Synchro"
    tracking = "Tracking"

class MoveModeEnum(Enum):
    goto = "Goto"
    track = "Track"
    goto_track = "GotoTrack"

class CoordsFrameEnum(Enum):
    hadec = "Hadec"
    radec = "Radec"
    altaz = "Altaz"

DOORS_TIME = 5
MOVE_TIME = 5

class Telescope():

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

        ip = config.get("Telescope", "ip")
        port = int(config.get("Telescope", "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 = [
                    ("SPEED", [2, 3], float),
                    ("COORDS", [2, 3], float),
                    ("TRACKING_SPEED", [2, 3], float),
                    ("ACCEL", [2, 3], float),

                    ("ROTATOR", [1, ], RotatorEnum),
                    ("FOCUS", [1, ], float),
                    ("MOVE_MODE", [1, ], MoveModeEnum),
                    ("COORDS_FRAME", [1, ], CoordsFrameEnum),
        ]

        get_msgs = [
                    "POSITION",
                    "STATUS",
                    "SETUP",
        ]

        do_msgs = [
                    ("DOORS", [1, ], bool),
                    ("START", [0, ],),
                    ("ABORT", [0, ],),
                    ("STOP", [0, ],),
                    ("HOMING", [0, ],),
        ]

        ''' 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
                    if cmd == "STOP":
                        move_timer = 0
                    if cmd == "ABORT":
                        move_timer = 0
                    if cmd == "HOMING":
                        print("Start movement (homing)")
                        move_timer = time.time() + MOVE_TIME
                    if 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 message: " + data)

            if len(answer) > 0:
                conn.send(bytes(answer, "UTF-8"))
                print("send: " + answer)
            conn.close()

tel = Telescope()
tel.loop()