This is my current code:
import asyncio
import serial
def repeat(seconds):
def wrap(func):
def decorated(*args):
loop = asyncio.get_event_loop()
loop.call_at(loop.time() + seconds, decorated, *args)
print('scheduled')
func(*args)
return func
return decorated
return wrap
@repeat(10)
def send_command(ser, text):
try:
if text == 'DATA':
print("\nSending Data Request")
ser.write(b'\n022022')
elif text == 'STAY':
print("\nInmediate Stay ARM")
ser.write(b'\n0640010002044D')
elif text == 'AWAY':
print("\nInmediate Away ARM")
ser.write(b'\n0640010003044E')
elif text == 'DISARM':
print("\nDISARM")
ser.write(b'\n0940010001040700055B')
elif text == 'CHIME':
print("\nChime toggle")
ser.write(b'\n0640010007014F')
except serial.SerialException as e:
raise e
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyUSB0', 9600, parity=serial.PARITY_ODD, write_timeout=0, timeout=0)
loop = asyncio.get_event_loop()
loop.call_at(loop.time() + 5, send_command, ser, 'DATA')
loop.run_forever()
In this example I'm trying to schedule the send_command function to run in 5 seconds and every 10 seconds afterwards. It seems to work but I'm a bit confused with the loop.call_at(loop.time() + seconds, decorated, *args)
call. Any comments will be appreciated.