Created
March 8, 2021 18:36
-
-
Save fellwell5/e50190d29939670f1975953d5e6f2e2b to your computer and use it in GitHub Desktop.
python-paperang extension files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
import hardware | |
import image_data | |
import tempfile | |
import os, sys | |
import subprocess | |
from watchgod import watch | |
import config | |
from pathlib import Path | |
# edited littleprinter file | |
# will start sirius-client.service if printer is connected | |
# will stop sirius-client.service if printer is disconnected | |
# i disabled sudo password verification for my serivce user | |
# https://phpraxis.wordpress.com/2016/09/27/enable-sudo-without-password-in-ubuntudebian/ | |
class Paperang_Printer: | |
sirius_service = "sirius-client" | |
def __init__(self): | |
Has_Error = False | |
try: | |
self.printer_hardware = hardware.Paperang(config.macaddress) | |
except IndexError: | |
print("Could not find printer with macaddress: "+config.macaddress) | |
Has_Error = True | |
if self.status_sirius_client(): | |
print("Stopping sirius-client") | |
self.stop_sirius_client() | |
print("Sleep 10 seconds") | |
time.sleep(10) | |
sys.exit(0) | |
else: | |
print("sirius-client is not running") | |
print("Sleep 10 seconds") | |
time.sleep(10) | |
sys.exit(0) | |
except: | |
Has_Error = True | |
print("An exception occured") | |
finally: | |
if not Has_Error: | |
if self.status_sirius_client(): | |
print("sirius-client is running") | |
else: | |
print("Starting sirius-client") | |
self.start_sirius_client() | |
def print_sirius_image(self, path): | |
if self.printer_hardware.connected: | |
self.printer_hardware.sendImageToBt(image_data.sirius(path)) | |
# If printer is disconnected a exception will be thrown | |
# and the service will restart the script | |
# if at restart the printer is disconnected it will stop sirius-client | |
def status_sirius_client(self): | |
try: | |
cmd = '/bin/systemctl status {}.service'.format(self.sirius_service) | |
completed = subprocess.run( cmd, shell=True, check=True, stdout=subprocess.PIPE ) | |
except subprocess.CalledProcessError as err: | |
print( 'ERROR:', err ) | |
else: | |
for line in completed.stdout.decode('utf-8').splitlines(): | |
if 'Active:' in line: | |
if '(running)' in line: | |
return True | |
return False | |
def start_sirius_client(self): | |
cmd = 'sudo /bin/systemctl start {}.service'.format(self.sirius_service) | |
completed = subprocess.run( cmd, shell=True, check=True, stdout=subprocess.PIPE ) | |
def stop_sirius_client(self): | |
cmd = 'sudo /bin/systemctl stop {}.service'.format(self.sirius_service) | |
completed = subprocess.run( cmd, shell=True, check=True, stdout=subprocess.PIPE ) | |
if __name__ == '__main__': | |
mmj=Paperang_Printer() | |
# `sirius-client` will write to this folder | |
tmpdir = os.path.join(tempfile.gettempdir(), 'sirius-client') | |
Path(tmpdir).mkdir(parents=True, exist_ok=True) | |
for changes in watch(tmpdir): | |
file = changes.pop()[1] | |
print("Printing " + file) | |
mmj.print_sirius_image(file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import config | |
import time | |
import hardware | |
# disable auto power off for Paperang P1 | |
# i dont know why but it only works for me with the "get_poweroff_time" before and after the set | |
class Paperangg_Printer: | |
def __init__(self): | |
if hasattr(config, "macaddress"): | |
print("attempting to find MAC address \"% s\""% config.macaddress) | |
self.printer_hardware = hardware.Paperang(config.macaddress) | |
else: | |
print("searching for printer...") | |
self.printer_hardware = hardware.Paperang() | |
# having trouble connecting? uncomment the following line and input | |
# your paperang's MAC address directly | |
# self.printer_hardware = hardware.Paperang("AA:BB:CC:DD:EE:FF") | |
def print_self_test(self): | |
if self.printer_hardware.connected: | |
self.printer_hardware.sendSelfTestToBt() | |
self.printer_hardware.disconnect() | |
else: | |
print("printer not connected.") | |
def set_poweroff_time(self, timeout): | |
if self.printer_hardware.connected: | |
self.printer_hardware.sendPowerOffTimeToBt(poweroff_time=timeout) | |
def get_poweroff_time(self): | |
if self.printer_hardware.connected: | |
return self.printer_hardware.queryPowerOffTime() | |
def get_battery_status(self): | |
if self.printer_hardware.connected: | |
return self.printer_hardware.queryBatteryStatus() | |
if __name__ == '__main__': | |
mmj=Paperangg_Printer() | |
mmj.get_poweroff_time() | |
mmj.set_poweroff_time(0) # in seconds; 0 for disable | |
mmj.get_poweroff_time() | |
mmj.get_battery_status() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=python-paperang | |
[Service] | |
Type=simple | |
ExecStart=/usr/bin/python3 /home/pi/python-paperang/littleprinter_service.py | |
Restart=always | |
WorkingDirectory=/home/pi/python-paperang | |
User=pi | |
Group=pi | |
[Install] | |
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=sirius-client | |
[Service] | |
ExecStart=/usr/bin/yarn ts-node bin/client.ts run --uri wss://littleprinter.nordprojects.co/api/v1/connection -p /home/pi/msprinter.printer -d filesystem | |
Restart=always | |
WorkingDirectory=/home/pi/sirius-client | |
User=pi | |
Group=pi | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment