-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHal.py
132 lines (99 loc) · 3.45 KB
/
Hal.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
119
120
121
122
123
124
125
126
127
128
129
130
# This file is part of FoodPuter.
# Foobar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Foobar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#the stuff that talks to HAL
from threading import Thread
import sys
import hashlib
import putil
import time
import urllib2
import random
import json
import Foodputer
ACCEPT = 0
DENY = 1
NOFUNDS = 2
# URL = 'https://hal.osaa.dk/food'
URL = 'http://localhost:8080'
LOCAL = 0
class Validator(Thread):
def __init__(self, pincheck):
Thread.__init__(self)
self.pincheck = pincheck
self.alive = 1
def abort(self):
putil.trace("kill validator")
self.alive = 0
def run(self):
pin = self.pincheck.pin
#for debugging and mocking
if (LOCAL):
data = ACCEPT
if pin == "p4":
data = DENY
elif pin == "p3":
data = NOFUNDS
time.sleep(1)
if self.alive:
self.pincheck.handle_hal(data)
return
data = Foodputer.get_order()
#remove token from data, but use it in sha1-digest
msg = "{}{}{}{}".format(data['name'], data['total'],data['token'], pin)
digest = hashlib.sha512(msg).hexdigest()
data['signature'] = digest
payload = json.dumps(data)
ret = None
try:
print "PAYLOAD is: ", payload
resp = urllib2.urlopen(URL, payload)
print "resp info", resp.info()
txt = resp.read()
print "JSON is: ", txt
ret = json.loads(txt)
except (urllib2.URLError, urllib2.HTTPError), e:
putil.trace("could not contact hal!!")
self.alive = False
self.pincheck.handle_fail(e)
except:
putil.trace("Other error {}".format(sys.exc_info()[0]))
if self.alive:
self.pincheck.handle_hal(ret)
class id_fetcher(Thread):
def __init__(self, rfid):
Thread.__init__(self)
self.rfid = rfid
self.alive = 1
def abort(self):
putil.trace("Killing the fetcher")
self.alive = 0
def run(self):
nr = self.rfid.nr
#for debugging and mocking
if (LOCAL):
data = {'user': "bent hansen", 'token':"TokenToken"} if nr != "r2" else None
time.sleep(1)
self.rfid.handle_hal(data)
return
data = None
try:
resp = urllib2.urlopen("{}/{}".format(URL, nr))
print resp.info()
data = json.loads(resp.read())
except (urllib2.URLError, urllib2.HTTPError), e:
putil.trace("could not contact hal!!")
self.alive = False;
self.rfid.handle_fail(e)
except:
putil.trace("Other error {}".format(sys.exc_info()[0]))
if self.alive:
self.rfid.handle_hal(data)