Skip to content

Commit bd9c52e

Browse files
Machine registry improvements (#10150)
* Add wrapper function for machine registry * Decorate entrypoint functions * Docstrings * Fix for boolean setting * Add playwright tests * Use proper entrypoints * Ensure settings are fetched correctly * Prevent recursion of machine registry decorator * Fix machine status display * Enhanced warning msg * Add simple machine sample printer * Adds playwright tests for machine UI * re-throw exception * Define 'machine' plugin mixin class * Adjust machine discovery * Use plugin mixins for registering machine types and drivers * Adjust unit test * Remove plugin static files when deactivating * Force machine reload when plugin registry changes * Add plugins specific to testing framework * Add test for plugin loading sequence * Add session caching - Significantly reduce DB hits * Enhanced unit testing and test plugins * Refactor unit tests * Further unit test fixes * Adjust instance rendering * Display table of available drivers * Cleanup * ADjust unit test * Tweak unit test * Add docs on new mixin type * Tweak machine overview docs * Tweak playwright tests * Additional unit test * Add unit test for calling machine func * Enhanced playwright tests * Account for database not being ready
1 parent ed31503 commit bd9c52e

File tree

40 files changed

+1098
-428
lines changed

40 files changed

+1098
-428
lines changed

docs/docs/plugins/develop.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ Supported mixin classes are:
135135
| [EventMixin](./mixins/event.md) | Respond to events |
136136
| [LabelPrintingMixin](./mixins/label.md) | Custom label printing support |
137137
| [LocateMixin](./mixins/locate.md) | Locate and identify stock items |
138+
| [MachineDriverMixin](./mixins/machine.md) | Integrate custom machine drivers
139+
| [MailMixin](./mixins/mail.md) | Send custom emails |
138140
| [NavigationMixin](./mixins/navigation.md) | Add custom pages to the web interface |
139141
| [NotificationMixin](./mixins/notification.md) | Send custom notifications in response to system events |
140142
| [ReportMixin](./mixins/report.md) | Add custom context data to reports |

docs/docs/plugins/machines/label_printer.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
## Label printer
1+
---
2+
title: Label Printer Machines
3+
---
4+
5+
## Label Printer
26

37
Label printer machines can directly print labels for various items in InvenTree. They replace standard [`LabelPrintingMixin`](../mixins/label.md) plugins that are used to connect to physical printers. Using machines rather than a standard `LabelPrintingMixin` plugin has the advantage that machines can be created multiple times using different settings but the same driver. That way multiple label printers of the same brand can be connected.
48

5-
### Writing your own printing driver
9+
### Writing A Custom Driver
10+
11+
To implement a custom label printer driver, you need to write a plugin which implements the [MachineDriverMixin](../mixins/machine.md) and returns a list of label printer drivers in the `get_machine_drivers` method.
612

713
Take a look at the most basic required code for a driver in this [example](./overview.md#example-driver). Next either implement the [`print_label`](#machine.machine_types.LabelPrinterBaseDriver.print_label) or [`print_labels`](#machine.machine_types.LabelPrinterBaseDriver.print_labels) function.
814

9-
### Label printer status
15+
### Label Printer Status
1016

1117
There are a couple of predefined status codes for label printers. By default the `UNKNOWN` status code is set for each machine, but they can be changed at any time by the driver. For more info about status code see [Machine status codes](./overview.md#machine-status).
1218

docs/docs/plugins/machines/overview.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ InvenTree has a builtin machine registry. There are different machine types avai
1313

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

16-
#### Initialization process
16+
#### Initialization Process
1717

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

@@ -24,24 +24,27 @@ The machine registry initialization process can be divided into three stages:
2424
2. The driver.init_driver function is called for each used driver
2525
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
2626

27-
#### Production setup (with a worker)
27+
#### Production Setup
28+
29+
!!! warning "Cache Required"
30+
A [shared cache](../../start/processes.md#cache-server) is required to run the machine registry in production setup with workers.
2831

2932
If a worker is connected, there exist multiple instances of the machine registry (one in each worker thread and one in the main thread) due to the nature of how python handles state in different processes. Therefore the machine instances and drivers are instantiated multiple times (The `__init__` method is called multiple times). But the init functions and update hooks (e.g. `init_machine`) are only called once from the main process.
3033

3134
The registry, driver and machine state (e.g. machine status codes, errors, ...) is stored in the cache. Therefore a shared redis cache is needed. (The local in-memory cache which is used by default is not capable to cache across multiple processes)
3235

3336

34-
### Machine types
37+
### Machine Types
3538

3639
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.
3740

38-
#### Built-in types
41+
#### Builtin Types
3942

4043
| Name | Description |
4144
| --- | --- |
4245
| [Label printer](./label_printer.md) | Directly print labels for various items. |
4346

44-
#### Example machine type
47+
#### Example Machine Type
4548

4649
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`.
4750

@@ -109,24 +112,33 @@ The machine type class gets instantiated for each machine on server startup and
109112

110113
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.
111114

112-
#### Example driver
115+
#### Example Driver
113116

114117
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](#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:
115118

116-
```py
119+
```python
120+
from plugin.mixins import MachineDriverMixin
117121
from plugin import InvenTreePlugin
118122
from plugin.machine.machine_types import ABCBaseDriver
119123

120-
class MyXyzAbcDriverPlugin(InvenTreePlugin):
121-
NAME = "XyzAbcDriver"
122-
SLUG = "xyz-driver"
123-
TITLE = "Xyz Abc Driver"
124-
# ...
125124

126125
class XYZDriver(ABCBaseDriver):
127126
SLUG = 'my-xyz-driver'
128127
NAME = 'My XYZ driver'
129128
DESCRIPTION = 'This is an awesome XYZ driver for a ABC machine'
129+
130+
131+
class MyXyzAbcDriverPlugin(MachineDriverMixin, InvenTreePlugin):
132+
NAME = "XyzAbcDriver"
133+
SLUG = "xyz-driver"
134+
TITLE = "Xyz Abc Driver"
135+
# ...
136+
137+
def get_machine_drivers(self):
138+
"""Return a list of machine drivers for this plugin."""
139+
return [XYZDriver]
140+
141+
130142
```
131143

132144
#### Driver API
@@ -161,7 +173,7 @@ class MyXYZDriver(ABCBaseDriver):
161173

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

164-
### Machine status
176+
### Machine Status
165177

166178
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.
167179

@@ -186,7 +198,7 @@ class XYZMachineType(BaseMachineType):
186198

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

189-
```py
201+
```python
190202
class MyXYZDriver(ABCBaseDriver):
191203
# ...
192204
def init_machine(self, machine):

docs/docs/plugins/mixins/barcode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Barcode Mixin
33
---
44

5-
## Barcode Plugins
5+
## BarcodeMixin
66

77
InvenTree supports decoding of arbitrary barcode data and generation of internal barcode formats via a **Barcode Plugin** interface. Barcode data POSTed to the `/api/barcode/` endpoint will be supplied to all loaded barcode plugins, and the first plugin to successfully interpret the barcode data will return a response to the client.
88

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Machine Mixin
3+
---
4+
5+
## MachineDriverMixin
6+
7+
The `MachineDriverMixin` class is used to implement custom machine drivers (or machine types) in InvenTree.
8+
9+
InvenTree supports integration with [external machines](../machines/overview.md), through the use of plugin-supplied device drivers.
10+
11+
### get_machine_drivers
12+
13+
To register custom machine driver(s), the `get_machine_drivers` method must be implemented. This method should return a list of machine driver classes that the plugin supports.
14+
15+
::: plugin.base.integration.MachineMixin.MachineDriverMixin.get_machine_drivers
16+
options:
17+
show_bases: False
18+
show_root_heading: False
19+
show_root_toc_entry: False
20+
summary: False
21+
members: []
22+
extra:
23+
show_source: True
24+
25+
The default implementation returns an empty list, meaning no custom machine drivers are registered.
26+
27+
### get_machine_types
28+
29+
To register custom machine type(s), the `get_machine_types` method must be implemented. This method should return a list of machine type classes that the plugin supports.
30+
31+
::: plugin.base.integration.MachineMixin.MachineDriverMixin.get_machine_types
32+
options:
33+
show_bases: False
34+
show_root_heading: False
35+
show_root_toc_entry: False
36+
summary: False
37+
members: []
38+
extra:
39+
show_source: True
40+
41+
The default implementation returns an empty list, meaning no custom machine types are registered.
42+
43+
## Sample Plugin
44+
45+
A sample plugin is provided which implements a simple [label printing](../machines/label_printer.md) machine driver:
46+
47+
::: plugin.samples.machines.sample_printer.SamplePrinterMachine
48+
options:
49+
show_bases: False
50+
show_root_heading: False
51+
show_root_toc_entry: False
52+
show_source: True
53+
members: []

docs/docs/plugins/mixins/mail.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Mail Mixin
44

55
## MailMixin
66

7-
The MailMixin class provides basic functionality for processing in- and outgoing mails.
7+
The `MailMixin` class provides basic functionality for processing in- and outgoing mails.
88

99
### Sample Plugin
1010

docs/docs/plugins/mixins/schedule.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The ScheduleMixin class provides a plugin with the ability to call functions at
1616

1717
{{ image("plugin/enable_schedule.png", "Enable schedule integration") }}
1818

19-
### SamplePlugin
19+
### Sample Plugin
2020

2121
An example of a plugin which supports scheduled tasks:
2222

docs/docs/plugins/mixins/ui.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: User Interface Mixin
33
---
44

5-
## User Interface Mixin
5+
## UserInterfaceMixin
66

77
The `UserInterfaceMixin` class provides a set of methods to implement custom functionality for the InvenTree web interface.
88

docs/docs/settings/error_codes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ A plugin is not allowed to override a *final method* from the `InvenTreePlugin`
7575

7676
This is a security measure to prevent plugins from changing the core functionality of InvenTree. The code of the plugin must be changed to not override functions that are marked as *final*.
7777

78+
#### INVE-E12
79+
**Plugin returned invalid machine type - Backend**
80+
81+
An error occurred when discovering or initializing a machine type from a plugin. This likely indicates a faulty or incompatible plugin.
82+
7883
### INVE-W (InvenTree Warning)
7984
Warnings - These are non-critical errors which should be addressed when possible.
8085

docs/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ nav:
228228
- Icon Pack Mixin: plugins/mixins/icon.md
229229
- Label Printing Mixin: plugins/mixins/label.md
230230
- Locate Mixin: plugins/mixins/locate.md
231+
- Machine Mixin: plugins/mixins/machine.md
232+
- Mail Mixin: plugins/mixins/mail.md
231233
- Navigation Mixin: plugins/mixins/navigation.md
232234
- Notification Mixin: plugins/mixins/notification.md
233235
- Report Mixin: plugins/mixins/report.md

0 commit comments

Comments
 (0)