Bolting up printer colors

This commit is contained in:
Reid D McKenzie 2025-01-29 00:00:56 -07:00
parent 65b712972b
commit 9bf7813260
10 changed files with 107 additions and 13 deletions

View file

@ -38,6 +38,7 @@ class GcodeAnalysis:
max_bed: int
max_end: int
filament: str
color: str
nozzle: int
@ -95,6 +96,11 @@ def analyze_gcode_str(text: str) -> Optional[GcodeAnalysis]:
else:
return None
if "filament_colour" in opts:
kwargs["color"] = opts["filament_colour"]
else:
return None
return GcodeAnalysis(**kwargs)

View file

@ -122,6 +122,7 @@ def handle_edit_printers():
args["id"] = int(args["id"])
args["enabled"] = int(args["enabled"])
args["filament_id"] = int(args["filament_id"])
args["color_id"] = int(args["color_id"])
args["chassis_id"] = int(args["chassis_id"])
args["nozzle_diameter"] = float(args["nozzle_diameter"])
ctx.db.edit_printer(**args)

View file

@ -0,0 +1,45 @@
-- name: migration-0001-create-filament-color#
CREATE TABLE IF NOT EXISTS filament_color (
id INTEGER PRIMARY KEY AUTOINCREMENT
, code TEXT
, name TEXT DEFAULT NULL
, UNIQUE(code)
);
INSERT OR IGNORE INTO filament_color (id, code, name) VALUES (1, '#DDDDDD', 'Any');
INSERT OR IGNORE INTO filament_color (code, name) VALUES ('#000000', 'Black');
INSERT OR IGNORE INTO filament_color (code, name) VALUES ('#FFFFFF', 'White');
-- name: migration-0002-create-orange-color#
INSERT OR IGNORE INTO filament_color (code, name) VALUES ('#FF6925', 'Orange');
-- name: create-color^
INSERT OR IGNORE INTO filament_color (
code
)
VALUES (
:code
)
RETURNING
id
;
-- name: fetch-color^
SELECT *
FROM filament_color
WHERE
code = :code
;
-- name: set-color-name!
UPDATE filament_color
SET
name = :name
WHERE
id = :cid
;
-- name: list-color
SELECT *
FROM filament_color
;

View file

@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS files (
, FOREIGN KEY(user_id) REFERENCES user(id)
);
-- name: migration-0004-create-file-analysis#
-- name: migration-0001-create-file-analysis#
CREATE TABLE IF NOT EXISTS file_analysis (
id INTEGER PRIMARY KEY AUTOINCREMENT
, max_x INTEGER
@ -21,6 +21,9 @@ CREATE TABLE IF NOT EXISTS file_analysis (
, file_id INTEGER REFERENCES file(id)
);
-- name: migration-0002-file-analysis-color#
ALTER TABLE file_analysis ADD color_id INTEGER REFERENCES filament_color(id) DEFAULT 1;
-- name: create-file^
INSERT INTO files (
user_id
@ -81,6 +84,7 @@ INSERT INTO file_analysis (
, nozzle_diameter
, filament_id
, file_id
, color_id
)
VALUES (
:max_x
@ -91,6 +95,7 @@ VALUES (
, :nozzle
, :filament_id
, :file_id
, :color_id
)
RETURNING
id

View file

@ -81,7 +81,9 @@ SELECT
, fa.max_end
, fa.nozzle_diameter
, fa.filament_id
, fa.color_id
, (SELECT name FROM filament WHERE id = fa.filament_id) AS filament_name
, (SELECT name AS name FROM filament_color WHERE id = fa.color_id) AS color_name
, (SELECT name FROM job_statuses WHERE id = j.status_id) AS status
, j.started_at
, j.time_left

View file

@ -81,6 +81,9 @@ ALTER TABLE printers ADD nozzle_diameter FLOAT DEFAULT 0.4;
-- name: migration-0006-create-printer-level-date#
ALTER TABLE printers ADD last_level_date TEXT DEFAULT NULL;
-- name: migration-0006-create-printer-filament-color#
ALTER TABLE printers ADD color_id INTEGER REFERENCES filament_color(id) DEFAULT 1;
-- name: try-create-printer^
INSERT INTO printers (
name
@ -103,6 +106,7 @@ SELECT
, p.last_poll_date
, p.last_level_date
, p.filament_id
, p.color_id
, p.chassis_id
, p.enabled
, s.name as status
@ -124,6 +128,8 @@ SELECT
, s.name as status
, p.enabled
, f.name as filament_name
, fc.name as filament_color
, fc.code as filament_color_code
, c.name as machine_name
, c.limit_x
, c.limit_y
@ -134,6 +140,7 @@ SELECT
FROM printers p
INNER JOIN printer_statuses s ON p.status_id = s.id
INNER JOIN filament f on p.filament_id = f.id
INNER JOIN filament_color fc on p.color_id = fc.id
INNER JOIN printer_chassis c on p.chassis_id = c.id
;
@ -147,6 +154,7 @@ SELECT
, c.limit_hotend
, p.nozzle_diameter
, p.filament_id
, p.color_id
FROM printers p
LEFT JOIN (SELECT id, printer_id FROM jobs WHERE finished_at IS NULL) j
ON p.id = j.printer_id
@ -187,6 +195,7 @@ SET
, stream_url = :stream_url
, api_key = :api_key
, filament_id = :filament_id
, color_id = :color_id
, chassis_id = :chassis_id
, enabled = :enabled
, nozzle_diameter = :nozzle_diameter

View file

@ -36,19 +36,27 @@
</div>
</div>
<div class="row">
<div class="four columns">
<label for="filament">Filament load</label>
<div class="three columns">
<label for="filament">Filament material</label>
<select name="filament_id">
{% for f in ctx.db.list_filament() %}
<option value="{{f.id}}" {% if printer.filament_id == f.id %}selected{%endif%}>{{f.name}}</option>
{% endfor %}
</select>
</div>
<div class="four columns">
<div class="three columns">
<label for="filament">Filament color</label>
<select name="color_id">
{% for c in ctx.db.list_color() %}
<option value="{{ c.id }}" {% if printer.color_id == c.id %}selected{%endif%}>{{ c.name or c.code }}</option>
{% endfor %}
</select>
</div>
<div class="three columns">
<label for="nozzle_diameter">Nozzle diameter (mm)</label>
<input type="text" name="nozzle_diameter" value="{{ printer.nozzle_diameter}}" />
</div>
<div class="four columns">
<div class="three columns">
<label for="enabled">Printing enabled</label>
<select name="enabled">
<option value="1" {% if printer.enabled %}selected{%endif%}>Enabled</option>

View file

@ -29,6 +29,7 @@
{% endif %}
<div class="job-constraint u-flex u-flex-break">
<label>Material</label>
{{ job.color_name }}
{{ job.filament_name }}
</div>
<div class="job-constraint u-flex u-flex-break">

View file

@ -7,7 +7,12 @@
<label>{{ printer.name }}</label>
<img id="printer_{{printer.id}}_stream" src="{{ printer.stream_url }}" style="max-width: 100%;" />
<span><label>Status</label>{{printer.status}}, {% if printer.enabled %}accepting jobs{%else%}not scheduling{%endif%}</span>
<span><label>Loaded material</label>{{printer.filament_name}}</span>
<span>
<label>Loaded material</label>
<span style="color: {{ printer.filament_color_code }}">{{ printer.filament_color }}</span>
(<span style="color: {{ printer.filament_color_code }}">{{ printer.filament_color_code }}</span>)
{{printer.filament_name}}
</span>
<span><label>Machine</label>{{printer.machine_name}}</span>
<span><label>Nozzle</label>{{ "%.2f"|format(printer.nozzle_diameter) }}mm</span>
<span><label>Limits</label>{{printer.limit_x}}mm x{{printer.limit_y}}mm x{{printer.limit_z}}mm, bed {{printer.limit_bed}}c, end {{printer.limit_hotend}}c</span>

View file

@ -175,6 +175,11 @@ def assign_jobs(app: App, db: Db) -> None:
and printer.limit_bed >= job.max_bed
and printer.nozzle_diameter == job.nozzle_diameter
and printer.filament_id == job.filament_id
and (
printer.color_id == job.color_id
# Note that the default/undefined color is #1
or job.color_id == 1
)
):
db.assign_job(jid=job.id, pid=printer.id)
log.info(f"Mapped job {job.id} to printer {printer.id}")
@ -207,10 +212,11 @@ def push_jobs(app: App, db: Db) -> None:
continue
try:
if not client.files_info("local", Path(file.path).name):
client.upload(file.path)
else:
log.info("Don't need to upload the job!")
# Nuke the file if it exists just in case
if client.files_info("local", Path(file.path).name):
client.delete(f"local/{Path(file.path).name}")
client.upload(file.path)
except HTTPError as e:
if e.response.status_code == 409:
@ -369,15 +375,21 @@ def analyze_files(app: App, db: Db):
continue
log.info(f"Analyzed {file}")
# Ensure there's a color record for the filament color
db.create_color(code=record.color)
color = db.fetch_color(code=record.color)
db.create_analysis(
color_id=color.id,
filament_id=db.create_filament(name=record.filament).id,
file_id=file.id,
max_bed=record.max_bed,
max_end=record.max_end,
max_x=record.max_x,
max_y=record.max_y,
max_z=record.max_z,
max_end=record.max_end,
max_bed=record.max_bed,
nozzle=record.nozzle,
filament_id=db.create_filament(name=record.filament).id,
)