2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00
Lukas aa7eaaab3a
Machine integration (#4824)
* Added initial draft for machines

* refactor: isPluginRegistryLoaded check into own ready function

* Added suggestions from codereview

* Refactor: base_drivers -> machine_types

* Use new BaseInvenTreeSetting unique interface

* Fix Django not ready error

* Added get_machines function to driver

- get_machines function on driver
- get_machine function on driver
- initialized attribute on machine

* Added error handeling for driver and machine type

* Extended get_machines functionality

* Export everything from plugin module

* Fix spelling mistakes

* Better states handeling, BaseMachineType is now used instead of Machine Model

* Use uuid as pk

* WIP: machine termination hook

* Remove termination hook as this does not work with gunicorn

* Remove machine from registry after delete

* Added ClassProviderMixin

* Check for slug dupplication

* Added config_type to MachineSettings to define machine/driver settings

* Refactor helper mixins into own file in InvenTree app

* Fixed typing and added required_attributes for BaseDriver

* fix: generic status import

* Added first draft for machine states

* Added convention for status codes

* Added update_machine hook

* Removed unnecessary _key suffix from machine config model

* Initil draft for machine API

* Refactored BaseInvenTreeSetting all_items and allValues method

* Added required to InvenTreeBaseSetting and check_settings method

* check if all required machine settings are defined and refactor: use getattr

* Fix: comment

* Fix initialize error and python 3.9 compability

* Make machine states available through the global states api

* Added basic PUI machine admin implementation that is still in dev

* Added basic machine setting UI to PUI

* Added machine detail view to PUI admin center

* Fix merge issues

* Fix style issues

* Added machine type,machine driver,error stack tables

* Fix style in machine/serializers.py

* Added pui link from machine to machine type/driver drawer

* Removed only partially working django admin in favor of the PUI admin center implementation

* Added required field to settings item

* Added machine restart function

* Added restart requird badge to machine table/drawer

* Added driver init function

* handle error functions for machines and registry

* Added driver errors

* Added machine table to driver drawer

* Added back button to detail drawer component

* Fix auto formatable pre-commit

* fix: style

* Fix deepsource

* Removed slug field from table, added more links between drawers, remove detail drawer blur

* Added initial docs

* Removed description from driver/machine type select and fixed disabled driver select if no machine type is selected

* Added basic label printing implementation

* Remove translated column names because they are now retrieved from the api

* Added printer location setting

* Save last 10 used printer machine per user and sort them in the printing dialog

* Added BasePrintingOptionsSerializer for common options

* Fix not printing_options are not properly casted to its internal value

* Fix type

* Improved machine docs

* Fix docs

* Added UNKNOWN status code to label printer status

* Skip machine loading when running migrations

* Fix testing?

* Fix: tests?

* Fix: tests?

* Disable docs check precommit

* Disable docs check precommit

* First draft for tests

* fix test

* Add type ignore

* Added API tests

* Test ci?

* Add more tests

* Added more tests

* Bump api version

* Changed driver/base driver naming schema

* Added more tests

* Fix tests

* Added setting choice with kwargs and get_machines with initialized=None

* Refetch table after deleting machine

* Fix test

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
2024-02-14 14:13:47 +00:00

7.0 KiB

title
title
Machines

Machines

InvenTree has a builtin machine registry. There are different machine types available where each type can have different drivers. Drivers and even custom machine types can be provided by plugins.

Registry

The machine registry is the main component which gets initialized on server start and manages all configured machines.

Initialization process

The machine registry initialization process can be divided into three stages:

  • Stage 1: Discover machine types: by looking for classes that inherit the BaseMachineType class
  • Stage 2: Discover drivers: by looking for classes that inherit the BaseDriver class (and are not referenced as base driver for any discovered machine type)
  • Stage 3: Machine loading:
    1. For each MachineConfig in database instantiate the MachineType class (drivers get instantiated here as needed and passed to the machine class. But only one instance of the driver class is maintained along the registry)
    2. The driver.init_driver function is called for each used driver
    3. The machine.initialize function is called for each machine, which calls the driver.init_machine function for each machine, then the machine.initialized state is set to true

Machine types

Each machine type can provide a different type of connection functionality between inventree and a physical machine. These machine types are already built into InvenTree.

Built-in types

Name Description
Label printer Directly print labels for various items.

Example machine type

If you want to create your own machine type, please also take a look at the already existing machine types in machines/machine_types/*.py. The following example creates a machine type called abc.

from django.utils.translation import gettext_lazy as _
from plugin.machine import BaseDriver, BaseMachineType, MachineStatus

class ABCBaseDriver(BaseDriver):
    """Base xyz driver."""

    machine_type = 'abc'

    def my_custom_required_method(self):
        """This function must be overridden."""
        raise NotImplementedError('The `my_custom_required_method` function must be overridden!')

    def my_custom_method(self):
        """This function can be overridden."""
        raise NotImplementedError('The `my_custom_method` function can be overridden!')

    required_overrides = [my_custom_required_method]

class ABCMachine(BaseMachineType):
    SLUG = 'abc'
    NAME = _('ABC')
    DESCRIPTION = _('This is an awesome machine type for ABC.')

    base_driver = ABCBaseDriver

    class ABCStatus(MachineStatus):
        CONNECTED = 100, _('Connected'), 'success'
        STANDBY = 101, _('Standby'), 'success'
        PRINTING = 110, _('Printing'), 'primary'

    MACHINE_STATUS = ABCStatus
    default_machine_status = ABCStatus.DISCONNECTED

Machine Type API

The machine type class gets instantiated for each machine on server startup and the reference is stored in the machine registry. (Therefore machine.NAME is the machine type name and machine.name links to the machine instances user defined name)

::: machine.BaseMachineType options: heading_level: 5 show_bases: false members: - machine_config - name - active - initialize - update - restart - handle_error - get_setting - set_setting - check_setting - set_status - set_status_text

Drivers

Drivers provide the connection layer between physical machines and inventree. There can be multiple drivers defined for the same machine type. Drivers are provided by plugins that are enabled and extend the corresponding base driver for the particular machine type. Each machine type already provides a base driver that needs to be inherited.

Example driver

A basic driver only needs to specify the basic attributes like SLUG, NAME, DESCRIPTION. The others are given by the used base driver, so take a look at Machine types. The following example will create an driver called abc for the xyz machine type. The class will be discovered if it is provided by an installed & activated plugin just like this:

from plugin import InvenTreePlugin
from plugin.machine.machine_types import ABCBaseDriver

class MyXyzAbcDriverPlugin(InvenTreePlugin):
    NAME = "XyzAbcDriver"
    SLUG = "xyz-driver"
    TITLE = "Xyz Abc Driver"
    # ...

class XYZDriver(ABCBaseDriver):
    SLUG = 'my-xyz-driver'
    NAME = 'My XYZ driver'
    DESCRIPTION = 'This is an awesome XYZ driver for a ABC machine'

Driver API

::: machine.BaseDriver options: heading_level: 5 show_bases: false members: - init_driver - init_machine - update_machine - restart_machine - get_machines - handle_error

Settings

Each machine can have different settings configured. There are machine settings that are specific to that machine type and driver settings that are specific to the driver, but both can be specified individually for each machine. Define them by adding a MACHINE_SETTINGS dictionary attribute to either the driver or the machine type. The format follows the same pattern as the SETTINGS for normal plugins documented on the SettingsMixin

class MyXYZDriver(ABCBaseDriver):
    MACHINE_SETTINGS = {
        'SERVER': {
            'name': _('Server'),
            'description': _('IP/Hostname to connect to the cups server'),
            'default': 'localhost',
            'required': True,
        }
    }

Settings can even marked as 'required': True which prevents the machine from starting if the setting is not defined.

Machine status

Machine status can be used to report the machine status to the users. They can be set by the driver for each machine, but get lost on a server restart.

Codes

Each machine type has a set of status codes defined that can be set for each machine by the driver. There also needs to be a default status code defined.

from plugin.machine import MachineStatus, BaseMachineType

class XYZStatus(MachineStatus):
    CONNECTED = 100, _('Connected'), 'success'
    STANDBY = 101, _('Standby'), 'success'
    DISCONNECTED = 400, _('Disconnected'), 'danger'

class XYZMachineType(BaseMachineType):
    # ...

    MACHINE_STATUS = XYZStatus
    default_machine_status = XYZStatus.DISCONNECTED

And to set a status code for a machine by the driver.

class MyXYZDriver(ABCBaseDriver):
    # ...
    def init_machine(self, machine):
        # ... do some init stuff here
        machine.set_status(XYZMachineType.MACHINE_STATUS.CONNECTED)

MachineStatus API

::: machine.machine_type.MachineStatus options: heading_level: 5 show_bases: false

Free text

There can also be a free text status code defined.

class MyXYZDriver(ABCBaseDriver):
    # ...
    def init_machine(self, machine):
        # ... do some init stuff here
        machine.set_status_text("Paper missing")