forked from phaer/kassomat-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkassomat-set-coin-levels.py
executable file
·118 lines (96 loc) · 3.18 KB
/
kassomat-set-coin-levels.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
#!/usr/bin/env python2
import json
import uuid
from redis import StrictRedis
redis = StrictRedis()
pubsub = redis.pubsub()
pubsub.subscribe('hopper-response')
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 get_levels():
correlId = str(uuid.uuid4())
redis.publish('hopper-request', json.dumps({
"cmd": "get-all-levels",
"msgId": correlId
}))
msg = wait_for_message(correlId)
return {int(level['value']): int(level['level']) for level in msg['levels']}
def change_levels(levels):
print("""
Please enter the desired coin value.
- empty line to quit
- raw numbers to add coins.
- prefix "-" to remove coins.
- prefix "=" to set an absolute amount of coins
""")
while True:
raw_value = raw_input("> coin value: ")
if raw_value == '':
break
try:
value = int(raw_value)
except ValueError:
value = None
if not value or value not in levels.keys():
print("invalid value, valid are:",
", ".join([str(k) for k in levels.keys()]))
continue
raw_count = raw_input("> count: ")
if len(raw_count) > 1 and raw_count[0] in ('-', '='):
operator = raw_count[0]
raw_count = raw_count[1:]
else:
operator = None
if raw_count == '':
break
try:
count = int(raw_count)
except ValueError:
print("invalid count, please enter an integer.")
continue
if operator == '=':
levels[value] = count
elif operator == '-':
absolute = levels[value] - count
levels[value] = absolute if absolute > 0 else 0
else:
levels[value] = levels[value] + count
print("new: %3d Eurocent x %3d" % (value, levels[value]))
return levels
def set_levels(levels):
print("Sending the following values to the machine:")
for value, count in sorted(levels.items()):
correlId = str(uuid.uuid4())
redis.publish('hopper-request', json.dumps({
"cmd": "set-denomination-level",
"msgId": correlId,
"amount": value,
"level": count
}))
msg = wait_for_message(correlId)
status = 'success' if msg['result'] == 'ok' else 'error'
print("%3d Eurocent x %3d : %s" % (value, levels[value], status))
if __name__ == '__main__':
print("Welcome to kassomat maintenance mode!\n")
print("Waiting for current coin levels\n")
levels = get_levels()
print("The following coins are in the machine:\n")
for value, count in sorted(levels.items()):
print("%3d Eurocent x %3d" % (value, count))
while True:
levels = change_levels(levels)
set_levels(levels)
print("""
want to change something?
- empty line to quit
- "yes" or anything else, really, to try again.
""")
answer = raw_input('> ')
if answer == '':
break
print("Bye.")