-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkassa.py
executable file
·256 lines (218 loc) · 7.81 KB
/
kassa.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import glob
import time
import json
import sys
import traceback
import paho.mqtt.client as mqtt
sessions = {}
class log: # pylint: disable=too-few-public-methods
pass
class Session:
SID = ""
plugins = {}
counter = 0
nextcall = {}
prompt = ""
help = {}
cache = {}
iets = 0
buttons = {}
stock = None
POS = None
log = None
receipt = None
accounts = None
products = None
def import_from(self, module, name):
module = __import__(module, fromlist=[name])
return getattr(module, name)
def __init__(self, SID, client):
self.SID = SID
self.client = client
self.plugins = {}
def startup(self):
print("Startup", self.SID)
for fname in glob.glob("plugins/*.py"):
plugname = fname.split("/")[1].rstrip(".py")
if plugname != "__init__" and not plugname in self.plugins:
if plugname in sys.modules:
del sys.modules[plugname]
print(self.plugins)
for fname in glob.glob("plugins/*.py"):
plugname = fname.split("/")[1].rstrip(".py")
print(plugname)
if plugname != "__init__" and not plugname in self.plugins:
if plugname in sys.modules:
del sys.modules[plugname]
self.plugins[plugname] = self.import_from(
"plugins." + plugname, plugname
)(self.SID, self)
print(plugname, self.import_from("plugins." + plugname, plugname))
self.send_message(True, "message", "loaded plugin " + plugname)
try:
self.help.update(self.plugins[plugname].help())
except:
print(traceback.format_exc())
print(self.plugins)
self.receipt = self.plugins["receipt"]
self.accounts = self.plugins["accounts"]
self.products = self.plugins["products"]
self.stock = self.plugins["stock"]
self.log = self.plugins["log"]
self.POS = self.plugins["POS"]
self.counter += 1
self.send_message(False, "message", "Please wait while loading...")
for _plug, plugin in self.plugins.items():
try:
plugin.startup()
except:
print(traceback.format_exc())
self.send_message(True, "commands", json.dumps(self.help))
self.send_message(True, "message", "Enter product, command or username")
print(self.plugins)
def realcallhook(self, hook, arg):
for _plug, plugin in self.plugins.items():
try:
getattr(plugin, "hook_" + hook)
try:
getattr(plugin, "hook_" + hook)(arg)
except:
print(traceback.format_exc())
except AttributeError:
pass
def callhook(self, hook, arg):
self.realcallhook("pre_" + hook, arg)
self.realcallhook(hook, arg)
self.realcallhook("post_" + hook, arg)
return True
def donext(self, plug, function):
self.nextcall = {"plug": plug, "function": function}
def input(self, text):
if not text:
self.send_message(True, "message", "Enter product, command or username")
self.send_message(True, "buttons", json.dumps({}))
return
self.buttons = {}
done = 0
for plug, plugin in self.plugins.items():
try:
plugin.pre_input(text)
except AttributeError:
pass
except:
print(traceback.format_exc())
self.prompt = ""
if self.nextcall:
try:
plug = self.nextcall["plug"]
func = self.nextcall["function"]
self.nextcall = {}
print(text)
print(self.nextcall)
print(getattr(plug, func))
if getattr(plug, func)(text):
done = 1
except:
print(traceback.format_exc())
if done == 0:
parts = text.split()
for part in parts:
if part:
done = self.handle_part(part) # Call handle_part for each part
if done == 1 and not self.prompt:
self.send_message(True, "message", "Enter product, command or username")
elif not self.prompt:
self.send_message(True, "message", "Unknown product, command or username")
self.callhook("wrong", ())
if not self.nextcall and not self.buttons:
self.send_message(True, "buttons", json.dumps({}))
def handle_part(self, part):
done = 0
if self.nextcall:
try:
plug = self.nextcall["plug"]
func = self.nextcall["function"]
self.nextcall = {}
if getattr(plug, func)(part):
done = 1
except:
print(traceback.format_exc())
if done == 0:
for plug, plugin in self.plugins.items():
try:
if plugin.input(part):
done = 1
break
except AttributeError:
print(traceback.format_exc())
except:
print(traceback.format_exc())
if done == 0:
if self.plugins.get("withdraw") and self.plugins["withdraw"].withdraw(part):
done = 1
if self.plugins.get("accounts") and self.plugins["accounts"].newuser(part):
done = 1
return done
def send_message(self, retain, topic, message):
if (
not "hack42bar/output/session/" + self.SID + "/" + topic in self.cache
or self.cache["hack42bar/output/session/" + self.SID + "/" + topic]
!= message
or len(topic) < 8
):
print(
retain,
"hack42bar/output/session/" + self.SID + "/" + topic,
":",
message,
)
self.cache["hack42bar/output/session/" + self.SID + "/" + topic] = message
if topic == "message":
self.prompt = message
if topic == "buttons":
self.buttons = message
self.client.publish(
"hack42bar/output/session/" + self.SID + "/" + topic, message, 1, retain
)
def get_session(SID, client):
global sessions # pylint: disable=global-variable-not-assigned
if not SID in sessions:
print("Starting new session", SID)
sessions[SID] = Session(SID, client)
sessions[SID].startup()
client.publish("hack42bar/output/sessions", json.dumps(list(sessions.keys())))
return sessions[SID]
def run_session(client, SID, action, data):
if action == "input":
session = get_session(SID, client)
session.input(data.decode())
else:
print("unhandled", action)
def on_connect(client, _userdata, _flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("hack42bar/input/#")
def on_message(client, _userdata, msg):
# print(msg.topic+" "+str(msg.payload))
elms = msg.topic.split("/")
msg = msg.payload
if len(elms) < 3:
return
# try:
run_session(client, elms[3], elms[4], msg)
def run():
while 1:
try:
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_start()
while True:
time.sleep(1)
# client.loop_forever()
except:
time.sleep(5)
if __name__ == "__main__":
run()