Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bofh69 committed Mar 18, 2024
1 parent 7439ee7 commit 13b4ee1
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions nfc2klipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
"""Program to set current filament & spool in klipper."""

import argparse
import asyncio
import json
import socket

import nfc
import requests

SPOOL = "SPOOL"
FILAMENT = "FILAMENT"
NDEF_TEXT_TYPE = "urn:nfc:wkt:T"
VERSION = "0.0.2"

parser = argparse.ArgumentParser()
# description="Fetches filaments from Spoolman and creates SuperSlicer filament configs.",

parser.add_argument("--version", action="version", version="%(prog)s 0.0.1")
parser.add_argument("--version", action="version", version="%(prog)s " + VERSION)

parser.add_argument(
"-c",
Expand All @@ -36,14 +40,43 @@
+ " for format",
)

#parser.add_argument(
# "-u",
# "--url",
# metavar="URL",
# default="http://mainsailos.local",
# help="URL for the moonraker installation",
#)

parser.add_argument(
"-u",
"--url",
metavar="URL",
default="http://mainsailos.local",
help="URL for the moonraker installation",
"-k",
"--klipper-uds",
metavar="PATH",
default="/tmp/klippy_uds",
help="Path to Klipper's API socket.",
)

class KlipperConnection:
def __init__(self):
self.reader = None
self.writer = None
self.msg_id = 1

async def connect(self, path):
(self.reader, self.writer) = await self.open_unix_connection(self.path)

async def send(self, method, params):
cmd = {"id": self.msg_id,
"method": method,
"params": params
}
self.msg_id += 1
string = json.dumps(cmd, separators=(',', ':'))
self.writer.write(string.encode() + "\003")
await self.writer.drain()

async def send_gcode(self, gcode):
await send("gcode/script", {"script": gcode})

def set_spool_and_filament(url: str, spool: int, filament: int):
"""Calls moonraker with the current spool & filament"""
Expand All @@ -61,12 +94,13 @@ def set_spool_and_filament(url: str, spool: int, filament: int):

print(f"Sending spool #{spool}, filament #{filament} to klipper", flush=True)

commands = {
"commands": [
f"SET_ACTIVE_SPOOL ID={spool}",
f"SET_ACTIVE_FILAMENT ID={filament}",
]
}
# commands = {
# "commands": [
# f"SET_ACTIVE_SPOOL ID={spool}",
# f"SET_ACTIVE_FILAMENT ID={filament}",
# ]
# }


# In case the post fails, we might not know if the server has received
# it or not, so set them to None:
Expand Down

0 comments on commit 13b4ee1

Please sign in to comment.