-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprint_server.py
executable file
·47 lines (34 loc) · 1.2 KB
/
print_server.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
#!/usr/bin/env python3
from paho.mqtt import client as mqttc
# import zpl
import subprocess
import random
import copy
import string
from imzam import settings
LPR_PRINTER_NAME = "Zebra_GK420t"
def on_message(client, userdata, message):
# this is searialized
print(message.topic, message.payload, type(message.payload))
lpr = subprocess.Popen(
["lpr", "-P", LPR_PRINTER_NAME, "-o", "raw"], stdin=subprocess.PIPE
)
lpr.communicate(message.payload)
def run_server():
client_kwargs = copy.copy(settings.MQTT_CLIENT_KWARGS)
# Randomize client id
client_kwargs['client_id'] += '_' + \
"".join(random.choices(string.ascii_letters + string.digits, k=8))
c = mqttc.Client(**client_kwargs)
c.tls_set()
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe(settings.MQTT_PRINTER_TOPIC + "#")
c.on_connect = on_connect
c.on_message = on_message
c.connect(**settings.MQTT_SERVER_KWARGS)
c.loop_forever()
if __name__ == "__main__":
run_server()