-
Notifications
You must be signed in to change notification settings - Fork 0
/
rat.py
119 lines (97 loc) · 3.02 KB
/
rat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
import json
from server import config
import sys
import time
import RPi.GPIO as GPIO
import subprocess
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(22, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN)
__URL = "http://88.198.162.251"
__URL = config.SERVER_URL
__SETTINGS = "/api/settings"
__MESSAGE = "/api/messages"
__YT_AUTH_URL = "/api/yt_auth_url"
__YT_AUTH_CODE = "/api/yt_auth_code"
__LOG = "/api/log"
__UPLOAD_PHOTO = "/api/upload_photo"
__CLEAR = "/clear"
__AUTH = config.HTTP_AUTH
def log(msg):
#print msg
l = open("/tmp/log", "a")
l.write("{2} - {0}: {1}\n".format(sys.argv[0], msg, time.time()))
l.close()
def get_wrapper(url, auth={}):
for attempt in xrange(0,4):
try:
return requests.get(url, auth=auth)
except requests.exceptions.ConnectionError:
log("GET error. sleep and try again. attempt: {0}".format(attempt))
time.sleep(attempt**3+2)
log("GOING FOR REBOOT")
p = subprocess.Popen("reboot", shell=True)
p.communicate()
return None
def post_wrapper(url, data={}, auth={}, files=None):
for attempt in xrange(0,4):
try:
if not files:
return requests.post(url, data=data, auth=__AUTH)
else:
return requests.post(url, data=data, auth=__AUTH, files=files)
except requests.exceptions.ConnectionError:
time.sleep(attempt**3+2)
return None
def set_red_led(state):
GPIO.output(23,state)
def set_green_led(state):
GPIO.output(22,state)
def get_settings():
settings = get_wrapper(__URL+__SETTINGS, auth=__AUTH)
if settings:
return settings.json()
else:
return None
def post_yt_auth_url(url):
return post_wrapper(__URL+__YT_AUTH_URL, data=json.dumps({'url': url}), auth=__AUTH)
def get_yt_auth_url():
r = get_wrapper(__URL+__YT_AUTH_URL, auth=__AUTH)
if r:
return r.json()['url']
else:
return None
def clear_yt_auth_url():
r = get_wrapper(__URL+__YT_AUTH_URL+__CLEAR, auth=__AUTH)
if r:
return r.json()['status']
else:
return None
def post_yt_auth_code(code):
return post_wrapper(__URL+__YT_AUTH_CODE, data=json.dumps({'code': code}), auth=__AUTH)
def get_yt_auth_code():
r = get_wrapper(__URL+__YT_AUTH_CODE, auth=__AUTH)
if r:
return r.json()['code']
else:
return None
def clear_yt_auth_code():
r = get_wrapper(__URL+__YT_AUTH_CODE+__CLEAR, auth=__AUTH)
if r:
return r.json()['status']
else:
return None
def post_log(msg):
log(msg)
return post_wrapper(__URL+__LOG, data=json.dumps({'log': msg, 'src': sys.argv[0], 'time': int(time.time())}), auth=__AUTH)
def get_log():
return get_wrapper(__URL+__LOG, auth=__AUTH).json()['logs']
def upload_photo():
files = {'file': open('/tmp/photo.jpg', 'rb')}
return post_wrapper(__URL+__UPLOAD_PHOTO, auth=__AUTH, files=files)
if r:
return r.json()['status']
else:
return None