Initial commit

This commit is contained in:
Sillyfrog 2018-06-04 08:27:11 +10:00
parent 15d8d45d27
commit c84da24975
8 changed files with 272 additions and 1 deletions

7
udev/README.md Normal file
View file

@ -0,0 +1,7 @@
# udev Rules and Commands
This directory contains the commands and rules I add to udev to make turning the printer on and off go smoothly, including re-connecting (my Docker container runs 24x7, however I turn off the printer when not in use).
The `okmd-udev.rules` file should be copied to `/etc/udev/rules.d/`, and updated to have the correct paths and device names for your environment.
The commands (`printer-*`), should also be updated to match your device names, and include your OctoPrint API key.

2
udev/okmd-udev.rules Normal file
View file

@ -0,0 +1,2 @@
ACTION=="add", KERNEL=="ttyUSB*", SUBSYSTEM=="tty", RUN+="/somewhere/printer-connected"
ACTION=="remove", KERNEL=="ttyUSB*", SUBSYSTEM=="tty", RUN+="/somewhere/printer-disconnected"

20
udev/printer-connected Executable file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env python
import time
import os
logf = open('/var/tmp/printer-connected.log', 'a')
DEVNAME = os.environ['DEVNAME']
def log(msg):
logf.write('%s\n' % (msg,))
def main():
log(time.asctime())
log(DEVNAME)
os.system('chmod a+wr %s' % (DEVNAME,))
if __name__ == '__main__':
main()

61
udev/printer-disconnected Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env python
import time
import os
import requests
import sys
logf = open('/var/tmp/printer-connected.log', 'a')
DEVNAME = os.environ['DEVNAME']
RESTART_STATES = [
'Offline',
'Cancelling',
'Operational',
]
OCTOPRINT_URL = 'http://localhost:5000/'
API_KEY = 'XXX'
def log(msg):
logf.write('%s\n' % (msg,))
def currentstate():
headers = {'X-Api-Key': API_KEY}
url = OCTOPRINT_URL + '/api/job'
r = requests.get(url, headers=headers)
return r.json()['state']
def main():
log(time.asctime())
log("Disconnecting: {}".format(DEVNAME))
state = currentstate()
if state not in RESTART_STATES and DEVNAME != '/dev/ttyUSB0':
log("Not restarting. State: {} DEVNAME: {}".format(state, DEVNAME))
# Don't reset the firmware if not in a known safe state
return
headers = {'X-Api-Key': API_KEY}
json = {
"command": "FIRMWARE_RESTART",
}
url = OCTOPRINT_URL + '/api/printer/command'
r = requests.post(
url,
json=json,
headers=headers
)
if (r.status_code == 204):
log("Disconnected OK")
sys.exit(0)
else:
log("Error disconnecting: %s" % (r,))
sys.exit(1)
if __name__ == '__main__':
main()