from time import sleep, time
import gpiozero as GP
from signal import pause

import sys
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP

import report

args = report.get_args(sys.argv[1:])
if args is None:
  exit(1)

id, base_url, ext = args

l = GP.LED(18)

b = GP.Button(16)

l.source = b

status = False

rsa = report.load_rsa_key_file("private.pem")


last_report = 0

def reportAndSetTime(val):
      global id, base_url, ext, rsa, last_report
      if report.report(rsa, id, base_url, ext, val):
        last_report = time()

reportAndSetTime(False)
while True:
    now_pressed = b.is_pressed
    if now_pressed == status:
        if now_pressed:
            b.wait_for_release(5)
        else:
            b.wait_for_press(5)
        now_pressed = b.is_pressed
    if now_pressed != status or time() - last_report > 120:
        status = now_pressed
        print "status now: " + str(status)

        reportAndSetTime(status)

        sleep(1)
    sys.stdout.flush()
