-
Notifications
You must be signed in to change notification settings - Fork 0
/
service
executable file
·68 lines (57 loc) · 1.86 KB
/
service
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
#!/usr/bin/python
import requests, time, traceback, os, sys, socket, json
from hosted import CONFIG
from qrcode.image.pure import PymagingImage
from qrcode.main import QRCode
from ibcm18 import UpdateContent
CONFIG.restart_on_update()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(name, value):
sock.sendto('root/%s:%s' % (name, value), ('127.0.0.1', 4444))
def readfile(filename):
with file(filename, "rb") as f:
return f.read()
UUID = readfile("/space/uuid")
SERIAL = os.environ['SERIAL']
try:
CONTROL_HOST = os.environ['CONTROL_HOST']
except:
CONTROL_HOST = readfile("/etc/control_host")
class QRWriter(object):
def __init__(self, target):
self._target = target
def update_img(self, url):
if os.path.exists(self._target):
return
with file(self._target + ".tmp", "wb") as f:
qr = QRCode(border=2, image_factory=PymagingImage)
qr.add_data(url)
im = qr.make_image()
im.save(f)
os.rename(self._target + ".tmp", self._target)
detail_page_qr = QRWriter("device_details.png")
def fetch_info():
url = "https://%s/api/v1/device/connect/%s/%s" % (CONTROL_HOST, SERIAL, UUID)
resp = requests.get(url, headers = {
'User-Agent': 'device-info package',
})
resp.raise_for_status()
info = resp.json()
print >>sys.stderr, info
send('device_info', json.dumps(dict(
description = info['description'],
location = info['location'],
)))
detail_page_qr.update_img(info['detail_url'])
# 1
if __name__ == "__main__":
uc = UpdateContent(CONFIG['metaurl'], authtoken=CONFIG['authtoken'])
while 1:
try:
#fetch_info()
if uc.update():
send('content', '')
except Exception as e:
traceback.print_exc()
finally:
time.sleep(10)