The hot folder takes a PNG in about 10 s and the Hoson queues it, so the last part taken is not the one being ripped. The dialog lists the parts (already taken or still in the folder) and asks where to resume once any was taken; the new round starts there, and the parts from there on that the old machine took are named as Hoson .prt files to delete, in the toast, the card and the history. The film card no longer claims which part prints: '2/3 partes com a máquina'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
232 lines
12 KiB
Python
232 lines
12 KiB
Python
"""Each machine's hot folders (Dropstar's third round, item 10, 2026-10-29/30).
|
|
|
|
FlexiPRINT watches 3. Imprimindo/<máquina>/<C1|C2|C3|PREMIUM>/ and takes a
|
|
file out of it when it rips. When the operator pulls a card to Imprimindo, its
|
|
files go, as a copy, into its machine's folder for the item's contraction
|
|
class; a premium item always goes to PREMIUM, whichever class it carries;
|
|
the order's own file stays in its stage folder. A film longer than a part
|
|
(10 m) is several parts (item 13): all on the same machine, in order. A machine's folder holds
|
|
as many files at once as it has places in Imprimindo (machines.places, three
|
|
since 2026-11-17); the next is written when Flexi takes one.
|
|
|
|
Only the server knows what each folder should get, and the Kanban's
|
|
computers write it: a part is claimed by one computer, so two never write
|
|
the same file and Flexi never prints it twice. A claim that did not finish
|
|
goes back to the queue. A written part that is gone from its folder was
|
|
taken by Flexi, and is never written again. A card that leaves Imprimindo
|
|
(Revisão, Correção, a step back, a cancellation) withdraws what Flexi has not
|
|
taken yet, and its computer deletes it. A step back from Revisão to
|
|
Imprimindo writes again only what Flexi had not taken (resume()).
|
|
|
|
Each part's life is one row of dtf_local.hot_parts:
|
|
queued -> claimed -> written -> consumed, or withdraw -> gone.
|
|
"""
|
|
from datetime import timedelta
|
|
|
|
# The machines' folders, inside the Kanban's main folder (Dropstar, 2026-10-30).
|
|
PRINTING_FOLDER = '3. Imprimindo'
|
|
# A computer that claimed a part and never said it was written.
|
|
CLAIM_TIMEOUT = timedelta(minutes=10)
|
|
CLASSES = ('C1', 'C2', 'C3')
|
|
# The premium product's own folder (Dropstar, 2026-11-10, front 3).
|
|
PREMIUM = 'PREMIUM'
|
|
|
|
|
|
def contraction(order, index):
|
|
"""The item's class for the folder: the one chosen in the Kanban, or what
|
|
the Site measured (C3 when the customer protected the solid areas)."""
|
|
chosen = (order.get('contraction') or {}).get(str(index)) or {}
|
|
if chosen.get('class') in CLASSES:
|
|
return chosen['class']
|
|
item = order['snapshot']['items'][index]
|
|
if item.get('stroke_choice') == 'client_c3':
|
|
return 'C3'
|
|
return item.get('stroke_class') if item.get('stroke_class') in CLASSES else None
|
|
|
|
|
|
def folder_class(order, index):
|
|
"""The folder an item's parts go to: PREMIUM for the premium product,
|
|
whatever contraction class is recorded, else C1/C2/C3."""
|
|
item = order['snapshot']['items'][index]
|
|
return PREMIUM if item.get('mode') == 'premium' else contraction(order, index)
|
|
|
|
|
|
def start(c, order_id):
|
|
"""The order entered Imprimindo: its approved finals queue for its
|
|
machine's folder, item by item, part by part; on a reprint only the items
|
|
chosen to go again (orders.print_items)."""
|
|
# A film's parts in their own order, p2 before p10: by name, a 91 m film
|
|
# would print p10 second.
|
|
files = c.execute('''SELECT f.item_index, f.upload_id, u.name FROM dtf_local.order_files f
|
|
JOIN dtf_local.uploads u ON u.id=f.upload_id JOIN dtf_local.orders o ON o.id=f.order_id
|
|
LEFT JOIN dtf_local.print_parts pp ON pp.order_id=f.order_id AND pp.item_index=f.item_index
|
|
AND pp.upload_id=f.upload_id
|
|
WHERE f.order_id=%s AND f.kind='final' AND f.active
|
|
AND (o.print_items IS NULL OR f.item_index = ANY(o.print_items))
|
|
ORDER BY f.item_index, pp.part NULLS LAST, length(u.name), u.name''', (order_id,)).fetchall()
|
|
round_ = c.execute('SELECT COALESCE(max(round), 0) + 1 AS r FROM dtf_local.hot_parts WHERE order_id=%s',
|
|
(order_id,)).fetchone()['r']
|
|
for seq, row in enumerate(files, 1):
|
|
c.execute('''INSERT INTO dtf_local.hot_parts(order_id,round,seq,item_index,upload_id,name)
|
|
VALUES(%s,%s,%s,%s,%s,%s)''', (order_id, round_, seq, row['item_index'], row['upload_id'], row['name']))
|
|
|
|
|
|
def resume(c, order_id):
|
|
"""A step back from Revisão to Imprimindo, undoing a move made by
|
|
mistake: the parts of the last round Flexi had not taken go back to the
|
|
folder, in their order; what it took was printed and is never written
|
|
again (start() would send the whole order to the machine once more)."""
|
|
last = c.execute('SELECT max(round) AS r FROM dtf_local.hot_parts WHERE order_id=%s', (order_id,)).fetchone()['r']
|
|
if last is None:
|
|
return
|
|
c.execute('''INSERT INTO dtf_local.hot_parts(order_id,round,seq,item_index,upload_id,name)
|
|
SELECT order_id, round + 1, row_number() OVER (ORDER BY seq), item_index, upload_id, name
|
|
FROM dtf_local.hot_parts WHERE order_id=%s AND round=%s AND state<>'consumed' ''', (order_id, last))
|
|
|
|
|
|
def printed(c, order_ids):
|
|
"""The files each order's machine has already taken, {order id: {upload
|
|
ids}}: an order in it was printed before."""
|
|
rows = c.execute('''SELECT order_id, upload_id FROM dtf_local.hot_parts
|
|
WHERE order_id=ANY(%s) AND state IN ('written','consumed')''', (list(order_ids),)).fetchall()
|
|
out = {}
|
|
for row in rows:
|
|
out.setdefault(row['order_id'], set()).add(row['upload_id'])
|
|
return out
|
|
|
|
|
|
def reprint_default(finals, taken, count):
|
|
"""The items to print again: those whose current final the machine has
|
|
not taken. `finals` is [(item, upload id)]; None when all go (never printed,
|
|
or nothing new)."""
|
|
if not taken:
|
|
return None
|
|
changed = sorted({item for item, upload in finals if upload not in taken})
|
|
return changed if changed and len(changed) < count else None
|
|
|
|
|
|
def stop(c, order_id):
|
|
"""The order left Imprimindo or was cancelled: what is not written stays
|
|
unwritten, and what Flexi has not taken is withdrawn from the folder."""
|
|
c.execute('''UPDATE dtf_local.hot_parts SET state=CASE WHEN state IN ('claimed','written') THEN 'withdraw' ELSE 'gone' END,
|
|
done_at=CASE WHEN state IN ('claimed','written') THEN NULL ELSE now() END
|
|
WHERE order_id=%s AND state IN ('queued','claimed','written')''', (order_id,))
|
|
|
|
|
|
def last_round(c, order_ids):
|
|
"""Each order's parts in its last round, in order: {order id: [{seq, name,
|
|
state}]}, for the transfer dialog to ask where the new machine resumes."""
|
|
rows = c.execute('''SELECT h.order_id, h.seq, h.name, h.state FROM dtf_local.hot_parts h
|
|
WHERE h.order_id=ANY(%s) AND h.round=(SELECT max(round) FROM dtf_local.hot_parts x
|
|
WHERE x.order_id=h.order_id) ORDER BY h.order_id, h.seq''', (list(order_ids),)).fetchall()
|
|
out = {}
|
|
for row in rows:
|
|
out.setdefault(row['order_id'], []).append({'seq': row['seq'], 'name': row['name'], 'state': row['state']})
|
|
return out
|
|
|
|
|
|
def prt_name(name):
|
|
"""The Hoson file the RIP makes from a part: its name with .prt."""
|
|
return name.rsplit('.', 1)[0] + '.prt'
|
|
|
|
|
|
def transfer(c, order_id, resume_from=None):
|
|
"""The order changed machine in Imprimindo (Rodada 5, D4). The hot folder
|
|
takes a PNG in about 10 s (Marcus, 2026-12-01), so a part the old machine
|
|
took may still be waiting in its Hoson queue: Gestão says which part the
|
|
new machine resumes from. The old machine's folder loses what it had not
|
|
taken; the new round holds that part onward. Returns (resume_from, the
|
|
.prt files to delete from the old machine's Hoson folder: the parts from
|
|
there on that it had already taken)."""
|
|
last = c.execute('SELECT max(round) AS r FROM dtf_local.hot_parts WHERE order_id=%s',
|
|
(order_id,)).fetchone()['r']
|
|
if last is None:
|
|
return 1, []
|
|
parts = c.execute('''SELECT seq, name, state FROM dtf_local.hot_parts WHERE order_id=%s AND round=%s
|
|
ORDER BY seq''', (order_id, last)).fetchall()
|
|
start = resume_from or 1
|
|
delete = [prt_name(part['name']) for part in parts if part['seq'] >= start and part['state'] == 'consumed']
|
|
stop(c, order_id)
|
|
c.execute('''INSERT INTO dtf_local.hot_parts(order_id,round,seq,item_index,upload_id,name)
|
|
SELECT order_id, %s, row_number() OVER (ORDER BY seq), item_index, upload_id, name
|
|
FROM dtf_local.hot_parts WHERE order_id=%s AND round=%s AND seq >= %s''',
|
|
(last + 1, order_id, last, start))
|
|
return start, delete
|
|
|
|
|
|
def claim(c, user):
|
|
"""The parts due in the folders now, claimed for this computer to write:
|
|
for each machine, the next parts of its orders in Imprimindo, in the order
|
|
they entered it, until the machine has its places' worth in its folder."""
|
|
c.execute('''UPDATE dtf_local.hot_parts SET state='queued', claimed_at=NULL, claimed_by=NULL
|
|
WHERE state='claimed' AND claimed_at < now() - %s''', (CLAIM_TIMEOUT,))
|
|
rows = c.execute('''SELECT h.*, o.machine_id, o.contraction, o.snapshot, COALESCE(m.folder, m.name) AS machine_folder, m.places,
|
|
(SELECT max(v.id) FROM dtf_local.movements v WHERE v.order_id=o.id AND v.to_state='imp') AS entered
|
|
FROM dtf_local.hot_parts h JOIN dtf_local.orders o ON o.id=h.order_id
|
|
JOIN dtf_local.machines m ON m.id=o.machine_id
|
|
WHERE h.state IN ('queued','claimed','written') AND o.state='imp' AND o.cancelled_at IS NULL
|
|
ORDER BY m.created_at, m.id, entered, h.order_id, h.seq FOR UPDATE OF h''').fetchall()
|
|
due, busy, blocked = [], {}, set()
|
|
for row in rows:
|
|
machine = row['machine_id']
|
|
if row['state'] != 'queued':
|
|
busy[machine] = busy.get(machine, 0) + 1
|
|
for row in rows:
|
|
machine, order = row['machine_id'], row['order_id']
|
|
if row['state'] != 'queued' or order in blocked:
|
|
continue
|
|
cls = folder_class(row, row['item_index'])
|
|
# A part without its class waits, and the parts after it with it.
|
|
if busy.get(machine, 0) >= row['places'] or not cls:
|
|
blocked.add(order)
|
|
continue
|
|
# The machine's folder, fixed at its creation: a renamed machine keeps
|
|
# the path FlexiPRINT watches.
|
|
folder = f"{row['machine_folder']}/{cls}"
|
|
c.execute('''UPDATE dtf_local.hot_parts SET state='claimed', claimed_at=now(), claimed_by=%s, folder=%s
|
|
WHERE id=%s''', (user, folder, row['id']))
|
|
busy[machine] = busy.get(machine, 0) + 1
|
|
due.append({**row, 'folder': folder})
|
|
return due
|
|
|
|
|
|
def watched(c):
|
|
"""The parts written and not yet taken by Flexi, and those to delete."""
|
|
return c.execute('''SELECT id, order_id, name, folder, state FROM dtf_local.hot_parts
|
|
WHERE state IN ('written','withdraw') AND folder IS NOT NULL ORDER BY id''').fetchall()
|
|
|
|
|
|
# What a computer reports about a part, and from which states it may.
|
|
REPORTS = {'written': ('claimed',), 'released': ('claimed',), 'consumed': ('written',), 'gone': ('withdraw',)}
|
|
|
|
|
|
def report(c, part_id, what, user):
|
|
"""A computer wrote the part, could not, saw Flexi take it, or deleted a
|
|
withdrawn one. Returns whether the report applied."""
|
|
if what == 'written':
|
|
sql = '''UPDATE dtf_local.hot_parts SET state='written', written_at=now() WHERE id=%s AND state='claimed'
|
|
AND claimed_by=%s RETURNING id'''
|
|
return bool(c.execute(sql, (part_id, user)).fetchone())
|
|
if what == 'released':
|
|
sql = '''UPDATE dtf_local.hot_parts SET state='queued', claimed_at=NULL, claimed_by=NULL, folder=NULL
|
|
WHERE id=%s AND state='claimed' AND claimed_by=%s RETURNING id'''
|
|
return bool(c.execute(sql, (part_id, user)).fetchone())
|
|
new = {'consumed': 'consumed', 'gone': 'gone'}[what]
|
|
return bool(c.execute(f'''UPDATE dtf_local.hot_parts SET state='{new}', done_at=now() WHERE id=%s
|
|
AND state=ANY(%s) RETURNING id''', (part_id, list(REPORTS[what]))).fetchone())
|
|
|
|
|
|
def progress(c, order_ids):
|
|
"""For each order in parts, how many of its parts the machine took
|
|
(Dropstar, 2026-10-30). The hot folder takes a PNG in about 10 s and the
|
|
Hoson queues it (Marcus, 2026-12-01): a part taken may still wait to be
|
|
ripped, so nothing here says which part prints."""
|
|
rows = c.execute('''SELECT h.order_id, count(*) AS parts,
|
|
max(h.seq) FILTER (WHERE h.state='consumed') AS taken,
|
|
count(*) FILTER (WHERE h.state IN ('claimed','written')) AS waiting
|
|
FROM dtf_local.hot_parts h
|
|
WHERE h.order_id=ANY(%s) AND h.round=(SELECT max(round) FROM dtf_local.hot_parts x WHERE x.order_id=h.order_id)
|
|
GROUP BY h.order_id''', (order_ids,)).fetchall()
|
|
return {row['order_id']: {'parts': row['parts'], 'taken': row['taken'] or 0, 'waiting': row['waiting']}
|
|
for row in rows}
|