client_telescope_controller_gemini_run.py 6.83 KB
#!/usr/bin/env python3


import pprint
import sys


sys.path.append("..")
from devices_channel.client.telescope_controller_gemini import TelescopeControllerGEMINI 

#DEBUG = False
DEBUG = True


"""
class essai:
    a = 'a'
    c = 'c'
    def __init__(self):
        self.b = 'bB'
    def __get__(self, instance, owner):
        return self.b
    '''
    def __set__(self, instance, value):
        self.b = value
    def __str__(self):
        return self.b
    '''
class user:
    e = essai()

class GenericResult:
    ''' Usage:
        res = execute(command)
        print("result is", res)
        if res.ko: raise UnexpectedReturnCode()
        if res.ok:
            ...
    '''
    # By default, bad result
    ok = False
    ko = True
    
    def __init__(self, native_result:str, ok=False):
        self.txt = native_result
        self.ok = ok
        self.ko = not ok
    '''
    def __str__(self):
        return self.native_result
    def __repr__(self):
        return self.native_result
    def __get__(self, instance, owner):
        return self.b
    def __set__(self, instance, value):
        self.b = value
    '''
"""


        

def main():
    
    # No argument => connexion to REAL TELESCOPE (DISTANT)
    if len(sys.argv) == 1: 
        # Port local AK 8085 = redirigé sur l’IP du tele 192.168.0.12 sur port 11110
        HOST, PORT = "82.64.28.71", 11110
    
    # Args given => connexion to LOCAL simulator server
    else:
        HOST, PORT = "localhost", 11110

    #tele_ctrl = SocketClient_UDP_TCP(HOST, PORT)
    with TelescopeControllerGEMINI(HOST, PORT, DEBUG) as tele_ctrl:
    
        # (optional) Only useful for TCP (does nothing for UDP)
        tele_ctrl._connect_to_device()
        
        #execute_commands_before_init(tele_ctrl)
    
        # Initialize telescope config (ack, date, time)
        #tele_ctrl.do_init()

        #execute_commands_after_init(tele_ctrl)
    
        #_mes_tests_temporaires_avirer(tele_ctrl)
        
        # Do MANUAL mode execution
        while True:
     
            tele_ctrl.print_available_commands()
            #req = input("\n(EXPERT MODE) REQUEST TO SERVER [ex: '6' (ACK), ':GD#' (Get Dec), ':GR#' (Get RA)'], (ENTER to quit): ").strip()
            cmd = input("REQUEST TO SERVER (ENTER to quit): >>> ").strip()
            if not cmd: break
    
            res = tele_ctrl.execute_cmd(cmd)
            print("result is", str(res))
            if res.ok: print("OK")
            #print("result.txt is", res.txt)
            
        # END: park telescope    
        ###tele_ctrl.do_PARK()
    
        #tele_ctrl.close()



def execute_commands_before_init(tele_ctrl):
        print("RA is", tele_ctrl.get_ra())
        
        ack = tele_ctrl.get_ack()
        print("ack is", ack)
        print("ack.txt is", ack)
        if ack.ok: print("ack is ok")
        if ack.ko: print("ack is ko")
        
        
def execute_commands_after_init(tele_ctrl):
    
        #radec = tele_ctrl.get("RA-DEC")
        res = tele_ctrl.get_radec()
        print("RA-DEC is", res)
        
        '''
        GOTO and MOVE:
        -------------
        '''
        '''
        tele_ctrl.do_goto(ra='21:00:00',dec='+20:00:00')
        tele_ctrl.do_goto(ra='22:00:00',dec='+30:00:00')
        tele_ctrl.do_move_dir('east','4', 'slew')
        '''
        
        # In order to execute a GEMINI (non generic) command:
        res = tele_ctrl.execute_native_cmd(':GR#')
        print("res is", res)


# @private
def _mes_tests_temporaires_avirer(tele_client):
    
    print("\n...Execution de mes tests temporaires...\n")

    print("res is", tele_client.run_func('formated_cmd','GD'))
    print("dict is")
    pprint.pprint(tele_client._cmd)
    
    '''
    u = user()
    print(u.e)

    res = GenericResult('G',False)
    print("res is", res)
    if res.ok: print("res is ok")
    if not res.ko: print("res is not ko")
    if res.txt == 'G': print("res is G")
    '''

    tele_client.do_move_dir('EAST','4', 'slew')
    tele_client.do_goto(ra='21:00:00',dec='+20:00:00')
    '''
    TEST ENVOI DIRECT:
    
    #data = 0x01000000:06#
    data = b'01000000:06#'
    
    ##data = b'01000000:GD#'
    #data = 0x010000003a474423
    #data = b'010000003a474423'
    
    #data_to_send = tele_client._format_data_to_send(data)
    tele_client._send_data(data)
    #data_received = tele_client.receive_data()
    data_received = tele_client.mysock.recv(1024)
    print("data rec = ", data_received)
    '''
    
    # Send some commands to the Telescope
    #pos = tele_client.get_position()
    #dec = tele_client.get("DEC")
    #ra = tele_client.get("RA")
    '''
    tele_client.get("NONE")
    tele_client.set("NONE", 1)
    tele_client.do("NONE")
    '''
    
    # TEST BAS NIVEAU POUR ACK 06:
    '''
    >>> a=b'\x3A'
    >>> a
    b':'
    >>> a.decode()
    ':'

    >>> a=b'\x47'
    >>> a
    b'G'
    >>> a.decode()
    'G'

    >>> a=b'\x44'
    >>> a
    b'D'
    >>> a.decode()
    'D'
    
    >>> a=b'\x23'
    >>> a
    b'#'
    >>> a.decode()
    '#'
    >>> tele_client.mysock.sendto(b'\x00\x00\x00\x01\x00\x00\x00\x00\x3A\x47\x44\x23\x00', (HOST, PORT))
    RECEIVED (ALL BYTES): b'\x00\x00\x00\x01\x00\x00\x00\x00+90.000000#\x00'
    
    >>> a=b'\x52'
    >>> a
    b'R'
    >>> a.decode()
    'R'
    >>> tele_client.mysock.sendto(b'\x00\x00\x00\x01\x00\x00\x00\x00\x3A\x47\x52\x23\x00', (HOST, PORT))
    RECEIVED (ALL BYTES): b'\x00\x00\x00\x01\x00\x00\x00\x008.911844#\x00'
    
    >>> tele_client.mysock.sendto(b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x06\x00\x00', (HOST, PORT))
    RECEIVED (ALL BYTES): b'\x00\x00\x00\x01\x00\x00\x00\x00G#\x00'

    '''
    #tele_client.mysock.sendto(b'00030000:GD#\x00', (HOST, PORT))
    #tele_client.mysock.sendto(b'00030000\x06\x00', (HOST, PORT))
    #tele_client.mysock.sendto(b'00030000\x00\x06\x00', (HOST, PORT))
    #tele_client.mysock.sendto(b'00030000\0x06\x00', (HOST, PORT))
    #tele_client.mysock.sendto(b'00010000\0x06\x00', (HOST, PORT))
    '''
    # :GD#
    ###tele_client.mysock.sendto(b'\x00\x00\x00\x01\x00\x00\x00\x00\x3A\x47\x44\x23\x00', (HOST, PORT))
    
    # :GR#
    ###tele_client.mysock.sendto(b'\x00\x00\x00\x01\x00\x00\x00\x00\x3A\x47\x52\x23\x00', (HOST, PORT))
    
    # ACK 06 OK !!! :
    
    tele_client.mysock.sendto(b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x06\x00\x00', (HOST, PORT))
    RECEIVED (ALL BYTES): b'\x00\x00\x00\x01\x00\x00\x00\x00G#\x00'
    
    tele_client.mysock.sendto(b'00010000\x00\x06\x00\x00', (HOST, PORT))
    RECEIVED (ALL BYTES): b'00010000G#\x00'
    '''
    '''
    # wrong: b'00010000\\x00\\x06\\x00\x00'
    # ok:
    \x05\x00\x00\
    #tele_client.mysock.sendto(b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00', (HOST, PORT))
    #tele_client.mysock.sendto(b'00010000\x00\x05\x00\x00', (HOST, PORT))
    tele_client.mysock.sendto(b'00010000\x00\x06\x00\x00', (HOST, PORT))
    data_received = tele_client.receive_data()
    '''



# Execution
main()