GithubHelp home page GithubHelp logo

vanderbilt-design-studio / python-ultimaker-printer-api Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 1.0 36 KB

An Ultimaker Printer API Client implementation in Python

License: MIT License

Python 100.00%
ultimaker 3d-printing api python python3 pypi-package

python-ultimaker-printer-api's Introduction

python-ultimaker-printer-api

An Ultimaker Printer API Client implementation in Python derived from Swagger documentation (see http://printer_ip/docs/api/) and request testing.

Build Status

Coverage Status

Demo

See https://github.com/vanderbilt-design-studio/poller-pi/blob/master/printers.py. This powers printer feed retrieval for https://vanderbilt.design/ (scroll down to the bottom)

Usage

from ultimaker import Printer, Identity, CredentialsDict
IDENTITY = Identity('Application', 'Anonymous')
IP = '192.168.1.18'
PORT = 80

credentials_dict = CredentialsDict('credentials.json')

printer = Printer(IP, PORT, IDENTITY)
printer.put_system_display_message("It's over, Anakin!", "Acquire high ground")

mDNS

If your local network supports mDNS (some school/corproate networks disable it), printers can be automatically discovered with the zeroconf package.

ultimaker_application_name = 'Application'
ultimaker_user_name = 'Anonymous
ultimaker_credentials_filename = './credentials.shelve'


from typing import Dict, List
import logging
import json
import socket
import base64
import uuid
import shelve

from zeroconf import ServiceInfo
from ultimaker import Printer, Credentials, Identity
import imagehash

class PrinterListener:

  def __init__(self, credentials_dict: shelve.Shelf):
    self.printers_by_name: Dict[str, Printer] = {}
    self.credentials_dict: shelve.Shelf = credentials_dict

  def remove_service(self, zeroconf, type, name):
    del self.printers_by_name[name]
    logging.info(f"Service {name} removed")

  def add_service(self, zeroconf, type, name):
    info: ServiceInfo = zeroconf.get_service_info(type, name)
    if len(info.addresses) == 0:
      logging.warning(f"Service {name} added but had no IP address, cannot add")
      return
    address = socket.inet_ntoa(info.addresses[0])
    identity = Identity(ultimaker_application_name, ultimaker_user_name)
    credentials = self.credentials_dict.get(str(printer.get_system_guid()), None)
    self.printers_by_name[name] = Printer(address, info.port, identity,
                                          credentials)
    logging.info(f"Service {name} added with guid: {printer.get_system_guid()}")

  def printer_jsons(self) -> List[Dict[str, str]]:
    printer_jsons: List[Dict[str, str]] = []
    # Convert to list here to prevent concurrent changes by zeroconf affecting the loop
    for printer in list(self.printers_by_name.values()):
      try:
        printer_status_json: Dict[str, str] = printer.into_ultimaker_json()
        printer_jsons.append(printer_status_json)

        if printer.credentials is not None and str(printer.get_system_guid(
        )) not in self.credentials_dict:
          logging.info(
              f'Did not see credentials for {printer.get_system_guid()} in credentials, adding and saving'
          )
          self.credentials_dict[str(printer.get_system_guid())] = printer.credentials
          self.credentials_dict.sync()
      except Exception as e:
        if type(e) is KeyboardInterrupt:
          raise e
        logging.warning(
            f'Exception getting info for printer {printer.get_system_guid()}, it may no longer exist: {e}'
        )
    return printer_jsons


if __name__ == '__main__':
  from zeroconf import ServiceBrowser, Zeroconf
  zeroconf = Zeroconf()
  shelf = shelve.open(ultimaker_credentials_filename)
  listener = PrinterListener(shelf)
  browser = ServiceBrowser(zeroconf, "_ultimaker._tcp.local.", listener)
  try:
    input('Press enter to exit\n')
  finally:
    print('Exiting...')
    shelf.close()
    zeroconf.close()

python-ultimaker-printer-api's People

Contributors

sameer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jellespijker

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.