Created
January 24, 2020 11:46
-
-
Save keyz182/7f0bd8e8a17c6145a77809fc377b8fe2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import serial, time, json, io, os, requests, datetime, sys, traceback, logging | |
from systemd import journal | |
log = logging.getLogger('timelapse') | |
log.addHandler(journal.JournalHandler()) | |
log.setLevel(logging.INFO) | |
port = '/dev/serial/by-id/usb-Duet3D_Duet-if00' | |
baud = 115200 | |
ser = serial.Serial(port, baud) | |
snapshot_folder = '/home/pi/snapshots/' | |
def log_print(*msg, file=sys.stdout): | |
dt = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') | |
print(dt, *msg, file=file) | |
out = '{} {}'.format(dt, ' '.join([str(m) for m in msg])) | |
log.info(out) | |
def err_print(*msg, file=sys.stdout): | |
dt = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S') | |
print(dt, *msg, file=file) | |
out = '{} {}'.format(dt, ' '.join([str(m) for m in msg])) | |
log.exception(out) | |
log_print(ser.name) | |
while True: | |
try: | |
tdata = ser.read() # Wait forever for anything | |
time.sleep(1) # Sleep (or inWaiting() doesn't give the correct value) | |
data_left = ser.inWaiting() # Get the number of characters ready to be read | |
tdata += ser.read(data_left) # Do the read and combine it with the first character | |
log_print(tdata) | |
tdata = None | |
ser.write(b'M36\n') | |
tdata = ser.read() # Wait forever for anything | |
time.sleep(1) # Sleep (or inWaiting() doesn't give the correct value) | |
data_left = ser.inWaiting() # Get the number of characters ready to be read | |
tdata += ser.read(data_left) # Do the read and combine it with the first character | |
tdata = tdata.decode().replace('\n\nok\n','') | |
log_print(tdata) | |
file_data = json.loads(tdata) | |
if 'err' in file_data and file_data['err'] == 1: | |
log_print('No print running') | |
else: | |
filename = file_data['fileName'] | |
log_print('Filename is {}'.format(filename)) | |
current_log_print = "{}-{}".format(datetime.datetime.now().strftime("%Y-%m-%d"), os.path.basename(filename)) | |
timelapse_folder = os.path.expanduser(snapshot_folder) | |
timelapse_folder = os.path.abspath(os.path.join(timelapse_folder, current_log_print)) | |
os.makedirs(timelapse_folder, exist_ok=True) | |
r = requests.get('http://127.0.0.1:8080/?action=snapshot', timeout=5, stream=True) | |
if r.status_code == 200: | |
now = datetime.datetime.now() | |
pic = os.path.join(timelapse_folder, now.strftime("%Y%m%dT%H%M%S") + ".jpg") | |
with open(pic, 'wb') as f: | |
for chunk in r: | |
f.write(chunk) | |
log_print("Picture taken!", pic) | |
else: | |
log_print('Failed to get timelapse snapshot.', file=sys.stderr) | |
except serial.serialutil.SerialException as e: | |
try: | |
ser = serial.Serial(port, baud) | |
print(ser.name) | |
except serial.serialutil.SerialException as ex: | |
err_print('Somethings buggered up serially yo') | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) | |
traceback.print_exception(exc_type, exc_value, exc_traceback,limit=2, file=sys.stdout) | |
except Exception as e: | |
err_print('Somethings buggered up yo') | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) | |
traceback.print_exception(exc_type, exc_value, exc_traceback,limit=2, file=sys.stdout) |
This file contains hidden or 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
import time, io, os, requests, datetime, sys, traceback | |
def log_print(*msg, file=sys.stdout): | |
print(datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), *msg, file=file) | |
while True: | |
r = requests.get('http://127.0.0.1:8080/?action=snapshot', timeout=5, stream=True) | |
if r.status_code == 200: | |
now = datetime.datetime.now() | |
pic = os.path.join('/home/pi/snapshots/manual', now.strftime("%Y%m%dT%H%M%S") + ".jpg") | |
with open(pic, 'wb') as f: | |
for chunk in r: | |
f.write(chunk) | |
log_print("Picture taken!", pic) | |
else: | |
log_print('Failed to get timelapse snapshot.', file=sys.stderr) | |
time.sleep(15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment