Cleanup
This commit is contained in:
97
cassini.py
97
cassini.py
@@ -1,5 +1,12 @@
|
||||
#!env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cassini
|
||||
#
|
||||
# Copyright (C) 2023 Vladimir Vukicevic
|
||||
# License: MIT
|
||||
#
|
||||
|
||||
import sys
|
||||
import socket
|
||||
import struct
|
||||
@@ -10,93 +17,14 @@ import logging
|
||||
import random
|
||||
from simple_mqtt_server import SimpleMQTTServer
|
||||
from simple_http_server import SimpleHTTPServer
|
||||
from saturn_printer import SaturnPrinter
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
level=logging.DEBUG, # .INFO
|
||||
format="%(asctime)s,%(msecs)d %(levelname)s: %(message)s",
|
||||
datefmt="%H:%M:%S",
|
||||
)
|
||||
|
||||
SATURN_BROADCAST_PORT = 3000
|
||||
|
||||
SATURN_STATUS_PRINTING = 4
|
||||
SATURN_STATUS_COMPLETE = 16 # ??
|
||||
|
||||
SATURN_CMD_0 = 0 # null data
|
||||
SATURN_CMD_1 = 1 # null data
|
||||
SATURN_CMD_SET_MYSTERY_TIME_PERIOD = 512 # "TimePeriod": 5000
|
||||
SATURN_CMD_START_PRINTING = 128 # "Filename": "X", "StartLayer": 0
|
||||
SATURN_CMD_UPLOAD_FILE = 256 # "Check": 0, "CleanCache": 1, "Compress": 0, "FileSize": 3541068, "Filename": "_ResinXP2-ValidationMatrix_v2.goo", "MD5": "205abc8fab0762ad2b0ee1f6b63b1750", "URL": "http://${ipaddr}:58883/f60c0718c8144b0db48b7149d4d85390.goo" },
|
||||
SATURN_CMD_DISCONNECT = 64 # Maybe disconnect?
|
||||
|
||||
PRINTERS = []
|
||||
|
||||
PRINTER_SEARCH_TIMEOUT = 1
|
||||
|
||||
def handle_exception(loop, context):
|
||||
msg = context.get("exception", context["message"])
|
||||
name = context.get("future").get_coro().__name__
|
||||
logging.error(f"Caught exception from {name}: {msg}")
|
||||
|
||||
class SaturnPrinter:
|
||||
def __init__(self, addr, desc):
|
||||
self.addr = addr
|
||||
self.desc = desc
|
||||
|
||||
# Class method: UDP broadcast search for all printers
|
||||
def find_printers(timeout=1):
|
||||
printers = []
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
with sock:
|
||||
sock.settimeout(PRINTER_SEARCH_TIMEOUT)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, timeout)
|
||||
sock.sendto(b'M99999', ('<broadcast>', SATURN_BROADCAST_PORT))
|
||||
|
||||
now = time.time()
|
||||
while True:
|
||||
if time.time() - now > timeout:
|
||||
break
|
||||
try:
|
||||
data, addr = sock.recvfrom(1024)
|
||||
except socket.timeout:
|
||||
continue
|
||||
else:
|
||||
print(f'Found printer at {addr}')
|
||||
pdata = json.loads(data.decode('utf-8'))
|
||||
printers.append(SaturnPrinter(addr, pdata))
|
||||
return printers
|
||||
|
||||
# Tell this printer to connect to the given mqtt server
|
||||
def connect(self, mqtt, http):
|
||||
self.mqtt = mqtt
|
||||
self.http = http
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
with sock:
|
||||
sock.sendto(b'M66666 ' + str(mqtt.port).encode('utf-8'), self.addr)
|
||||
|
||||
def describe(self):
|
||||
attrs = self.desc['Data']['Attributes']
|
||||
return f"{attrs['Name']} ({attrs['MachineName']})"
|
||||
|
||||
def send_command(self, cmdid, data=None):
|
||||
# generate 16-byte random identifier as a hex string
|
||||
hexstr = '%032x' % random.getrandbits(128)
|
||||
timestamp = int(time.time() * 1000)
|
||||
mainboard = self.desc['Data']['Attributes']['MainboardID']
|
||||
cmd_data = {
|
||||
"Data": {
|
||||
"Cmd": cmdid,
|
||||
"Data": data,
|
||||
"From": 0,
|
||||
"MainboardID": mainboard,
|
||||
"RequestID": hexstr,
|
||||
"TimeStamp": timestamp
|
||||
},
|
||||
"Id": self.desc['Id']
|
||||
}
|
||||
print("SENDING REQUEST: " + json.dumps(cmd_data))
|
||||
self.mqtt.outgoing_messages.put_nowait({'topic': '/sdcp/request/' + mainboard, 'payload': json.dumps(cmd_data)})
|
||||
|
||||
async def create_mqtt_server():
|
||||
mqtt = SimpleMQTTServer('0.0.0.0', 0)
|
||||
await mqtt.start()
|
||||
@@ -127,6 +55,11 @@ async def main():
|
||||
print("No printers found")
|
||||
return
|
||||
|
||||
if len(printers) > 1:
|
||||
print("More than 1 printer found.")
|
||||
print("Usage --printer argument to specify the ID. [TODO]")
|
||||
return
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
cmd = sys.argv[1]
|
||||
|
||||
@@ -134,7 +67,7 @@ async def main():
|
||||
print_printer_status(printers)
|
||||
return
|
||||
|
||||
# Spin up our private mqtt server
|
||||
# Spin up our private servers
|
||||
mqtt, mqtt_port, mqtt_task = await create_mqtt_server()
|
||||
http, http_port, http_task = await create_http_server()
|
||||
|
||||
|
Reference in New Issue
Block a user