forked from phaer/kassomat-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkassomat-count-coins.py
executable file
·90 lines (65 loc) · 2.18 KB
/
kassomat-count-coins.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
#!/usr/bin/env python2
import json
import uuid
from redis import StrictRedis
redis = StrictRedis()
pubsub = redis.pubsub()
pubsub.subscribe('hopper-response')
event_pubsub = redis.pubsub()
event_pubsub.subscribe('hopper-event')
def to_int(s):
try:
return int(s)
except ValueError:
return None
def wait_for_message(correlId):
for msg in pubsub.listen():
if msg['type'] != 'message':
continue
data = json.loads(msg['data'])
if data['correlId'] == correlId:
return data
def wait_for_event(event):
for msg in event_pubsub.listen():
if msg['type'] != 'message':
continue
data = json.loads(msg['data'])
if data['event'] == event:
return data
def count_coins():
while True:
raw_value = raw_input("> to count coins now, please enter 'yes': ")
if raw_value != 'yes':
break
smart_empty()
print("waiting for 'smart emptied' event")
smart_emptied = wait_for_event('smart emptied')
print("amount of money emptied: %d" % (int(smart_emptied['amount'])))
get_cashbox_payout_operation_data()
def get_cashbox_payout_operation_data():
print("Sending cashbox-payout-operation-data to the machine:")
correlId = str(uuid.uuid4())
redis.publish('hopper-request', json.dumps({
"cmd": "cashbox-payout-operation-data",
"msgId": correlId
}))
msg = wait_for_message(correlId)
# status = 'success' if msg['result'] == 'ok' else 'error'
status = "ok"
levels = {int(level['value']): int(level['level']) for level in msg['levels']}
print("Quantity of coins emptied:")
for value, count in sorted(levels.items()):
print("%3d Eurocent x %3d" % (value, levels[value]))
def smart_empty():
print("Sending smart-empty to the machine:")
correlId = str(uuid.uuid4())
redis.publish('hopper-request', json.dumps({
"cmd": "smart-empty",
"msgId": correlId
}))
msg = wait_for_message(correlId)
status = 'success' if msg['result'] == 'ok' else 'error'
if __name__ == '__main__':
print("Welcome to Count-o-matic!\n")
count_coins()
print("Bye.")