# author: Stephen Potter
# updated: 22 June 2025
from telescope import Telescope
import os
import sys
import datetime
import math 
from time import sleep
import socket
import signal # used to capture ctrl-c and exit nicely by closing connection


HOST_40INCH = "10.2.16.90"  # 40inch


def Exit_gracefully(signal, frame): # this should cause prgram to exit here on crt-C
    telescope_comms.disconnect()
    sys.exit(0)


    
if __name__ == "__main__":
    signal.signal(signal.SIGINT, Exit_gracefully) # should capture crt_c 
    print(f"{datetime.datetime.now().isoformat()} [INFO][main][telem.py][main] starting telem server \n")


    telescope_comms = Telescope(host=HOST_40INCH, test=False) # create an instance of the Telescope class
    telescope_comms.connect() # connect via tcpip socket to the telescope server
    sleep(2)

    while True:
        state = telescope_comms.get_state()  # get state from telescope server
        print(state)
        telescope_comms.disconnect()  # disconect from tcpip socket
        sleep(20) # minimum reasonable sleep time
        telescope_comms.connect() #reconnect to tcpip socket
                
    import sys
    sys.exit(0)
                
