# entwendet aus:
# https://github.com/ut-hnl-lab/ultimakerpy

# auch zum lesen:
# https://github.com/MartinBienz/SDPremote?tab=readme-ov-file

import time
from ultimakerpy import UMS3, JobState

def print_started(state):
    if state == JobState.PRINTING:
        time.sleep(6.0)
        return True
    return False

def layer_reached(pos, n):
    if round(pos / 0.2) >= n:  # set layer pitch: 0.2 mm
        return True
    return False

printer = UMS3(name='MyPrinterName')
targets = {
    'job_state': printer.job_state,
    'bed_pos': printer.bed.position,
}

printer.print_from_dialog()  # select file to print
printer.peripherals.camera_streaming()
with printer.data_logger('output2.csv', targets) as dl:
    timer = dl.get_timer()

    # sleep until active leveling finishes
    timer.wait_for_datalog('job_state', print_started)

    for n in range(1, 101):
        # sleep until the printing of specified layer to start
        timer.wait_for_datalog('bed_pos', lambda v: layer_reached(v, n))
        print('printing layer:', n)
