Created
October 4, 2017 10:54
-
-
Save makoto/3962daf3a25bc060739d795f8b83e153 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import sys | |
import os | |
from time import sleep | |
from gpiozero import LED | |
red_led = LED(17) | |
import json | |
import urllib2 | |
ip = '10.0.110.129' | |
host = 'http://' + ip + ':3000' | |
req = urllib2.Request(host + '/api/v1/usage') | |
req.add_header('Content-Type', 'application/json') | |
data = {'in_use':True} | |
response = urllib2.urlopen(req, json.dumps(data)) | |
#if command == "start": | |
# red_led.on() | |
#else: | |
# red_led.off() | |
#sleep(3) | |
path = '/home/pi/mmal/event.log' | |
def alive(threshold): | |
current_time = time.time() | |
file_time = os.stat(path).st_mtime | |
file_age = current_time - file_time | |
print(file_age, threshold) | |
if file_age > threshold: | |
return False | |
else: | |
return True | |
while True: | |
sleep(1) | |
status = open(path).read().rstrip() | |
print("status", status, red_led.is_lit) | |
if status == "start" and red_led.is_lit == False: | |
red_led.on() | |
elif status == "end" and red_led.is_lit == True: | |
red_led.off() | |
else: | |
print("ok", status, red_led.is_lit) | |
data = {'in_use':(status == 'start')} | |
try: | |
response = urllib2.urlopen(req, json.dumps(data)) | |
except Exception: | |
print("Cannot connect to URL endpoint") | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment