55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
import linuxcnc
|
|
import time
|
|
import paho.mqtt.client as mqtt
|
|
|
|
lcnc_stat = linuxcnc.stat()
|
|
|
|
MQTT_HOST='192.168.1.1'
|
|
MQTT_PORT=1883
|
|
TOPIC='lcnc/job/progress'
|
|
|
|
DELAY = 5
|
|
we_started = False
|
|
running = False
|
|
|
|
# The callback for when the client receives a CONNACK response from the server.
|
|
def on_connect(client, userdata, flags, rc):
|
|
print('mqtt connected')
|
|
|
|
client = mqtt.Client()
|
|
client.on_connect = on_connect
|
|
|
|
client.connect_async(MQTT_HOST, MQTT_PORT, 60)
|
|
client.loop_start()
|
|
|
|
print("Starting main loop")
|
|
|
|
time.sleep(1)
|
|
|
|
filename = None
|
|
last_line = None
|
|
global lc
|
|
state = {
|
|
'lc': float(0)
|
|
}
|
|
|
|
while True:
|
|
lcnc_stat.poll()
|
|
|
|
if filename is None or filename != lcnc_stat.file:
|
|
print("Updating file: {}".format(lcnc_stat.file))
|
|
filename = lcnc_stat.file
|
|
|
|
f = open(filename, 'r')
|
|
state['lc'] = sum(1 for line in f)
|
|
print("line count: {}".format(state['lc']))
|
|
|
|
if last_line is None or last_line != lcnc_stat.motion_line:
|
|
completion = int(lcnc_stat.motion_line) / float(state['lc'])
|
|
print(state['lc'], lcnc_stat.motion_line, completion)
|
|
print("Progress: {}%".format(completion * 100))
|
|
last_line = lcnc_stat.motion_line
|
|
|
|
print(type(completion), str(completion))
|
|
client.publish(TOPIC, str(completion))
|