HarkTV | |
---|---|
Owner | Space |
Status | Working |
Hostname | harktv.dhcp.nurd.space |
Location | Zaal 2 |
Tool | No |
Tool category |
What
Workstation in Zaal 2. It has a barcode scanner which can be used for Partkeepr.
Auto shutdown
We want this machine to shutdown when space is closed. We check MQTT for this in a simple python script. We use paho, so let's install that:
apt-get install python-paho-mqtt
Normally the script would ask for a password to execute the shutdown command. We can avoid this with following line in /etc/sudoers (use visudo for this):
hark harktv =NOPASSWD: /bin/systemctl poweroff
Now we just run a cronjob every 5 minutes:
*/5 * * * * ~/space_status_shutdown.py
The script:
- !/usr/bin/env python
- 2019 The_Niz
- checks space status and shuts machine down if space is closed
import paho.mqtt.client as mqtt
import os
- The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
if rc==0:
client.connected_flag=True #set flag
print("Connected with result code "+str(rc))
else:
print("Bad connection Returned code=",rc)
client.subscribe("space/state")
- The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if msg.payload=="False":
print("Space is uit")
os.system('sudo /bin/systemctl poweroff')
else:
print("Space is aan")
client.loop_stop()
client.disconnect()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("arbiter.vm.nurd.space", 1883, 60)
client.loop_forever()