import time, random


class EZSV23_dummy():

    def __init__(self, COM='COM4', baud=9600, timeout=0.05):
        pass


    def __enter__(self):
        return self


    def __exit__(self, type, value, traceback):
        pass


    def open(self, COM, baud, timeout):         # open and configure the device
        pass


    def close(self):                        # close the device
        pass


    def _msg(self, msg):
        print(f'Dummy motor: {msg}')


    def tell_position(self):                # current position, in ticks
        self._msg('tell_position')
        position = random.randrange(100000)
        return str(position)


    def tell_status(self):                  # read the status: [ready flag, error code]
        self._msg('tell_status')
        ready_flag = True
        error_code = 0
        return (ready_flag, error_code)           # (ready=bool, error=int)


    def home(self):                         # send the homing command
        self._msg('home')


    def halt_execution(self):  # halt whatever program in executing
        self._msg('halt_execution')


    def position_absolute(self, position):  # goto absolute position
        self._msg(f'position_absolute {position}')


    def position_relative(self, position):  # goto relative position
        self._msg(f'position_relative {position}')


    def motor_off(self):                    # disable servo-control
        self._msg('motor_off')


    def wait_for_completion(self):
        #        delay = random.randrange(10)
        self._msg('wait_for_completion')
        delay = 15
        ready = False
        while(ready==False):
            time.sleep(delay)
            (ready,error) = self.tell_status()


    def servo_here(self):                   # enable servo-control
        self._msg('servo_here')


    def _write(self, command):            # send a command
        pass
