连接设备

IoT 驱动程序允许任何 Odoo 模块与连接到物联网盒子的任何设备实时通信。与物联网盒子的通信是双向的,因此 Odoo 客户端可以向任何受支持的设备发送命令,并从中接收信息。

要添加对某个设备的支持,我们只需要:

  • 一个 Interface,用于检测特定类型的已连接设备

  • 一个 Driver,用于与单个设备通信

每次启动时,物联网盒子都会加载所连接的 Odoo 实例上能够找到的所有接口和驱动程序。每个模块都可以包含一个 iot_handlers 目录,该目录会被复制到物联网盒子。该目录的结构如下

your_module
├── ...
└── iot_handlers
    ├── drivers
    │   ├── DriverName.py
    │   └── ...
    │
    └── interfaces
        ├── InterfaceName.py
        └── ...

检测设备

连接到物联网盒子的设备通过 Interfaces 进行检测。每种受支持的连接类型(USB、蓝牙、视频、打印机、串口等)都有一个接口。接口维护一个已检测设备的列表,并将它们与正确的驱动程序相关联。

受支持的设备既会显示在可通过其 IP 地址访问的物联网盒子主页上,也会显示在所连接 Odoo 实例的 IoT 模块中。

接口

接口的作用是维护通过特定连接类型连接的设备列表。创建新接口需要:

  • 扩展 Interface 类

  • 设置 connection_type 类属性

  • 实现 get_devices 方法,该方法应返回一个字典,其中包含有关每个已检测设备的数据。这些数据将作为参数传递给驱动程序的构造函数和 supported 方法。

注解

设置 _loop_delay 属性将修改调用 get_devices 之间的间隔。默认情况下,该间隔设置为 3 秒。

from odoo.addons.hw_drivers.interface import Interface

class InterfaceName(Interface):
    connection_type = 'ConnectionType'

    def get_devices(self):
        return {
            'device_identifier_1': {...},
            ...
        }

驱动程序

接口获取到已检测设备的列表后,会遍历所有具有相同 connection_type 属性的驱动程序,并对所有已检测设备测试各自的 supported 方法。如果驱动程序的 supported 方法返回 True,则会为相应设备创建该驱动程序的实例。

注解

驱动程序的 supported 方法有优先级顺序。子类的 supported 方法总是先于其父类的 supported 方法进行测试。可以通过修改驱动程序的 priority 属性来调整此优先级。

创建新驱动程序需要:

  • 扩展 Driver

  • 设置 connection_type 类属性。

  • 设置 device_type、device_connection 和 device_name 属性。

  • 定义 supported 方法

from odoo.addons.hw_drivers.driver import Driver

class DriverName(Driver):
    connection_type = 'ConnectionType'

    def __init__(self, identifier, device):
        super(NewDriver, self).__init__(identifier, device)
        self.device_type = 'DeviceType'
        self.device_connection = 'DeviceConnection'
        self.device_name = 'DeviceName'

    @classmethod
    def supported(cls, device):
        ...

与设备通信

新设备被检测到并显示在 IoT 模块中后,下一步就是与其通信。由于盒子只有本地 IP 地址,因此只能从同一本地网络访问。所以,通信需要在浏览器端用 JavaScript 进行。

该过程取决于通信方向:- 从浏览器到盒子,通过 Actions 进行;- 从盒子到浏览器,通过 Longpolling 进行

这两种通道都通过同一个 JS 对象 DeviceProxy 访问,该对象使用物联网盒子的 IP 和设备标识符进行实例化。

var DeviceProxy = require('iot.DeviceProxy');

var iot_device = new DeviceProxy({
    iot_ip: iot_ip,
    identifier: device_identifier
});

操作

操作用于指示所选设备执行特定操作,例如拍照、打印小票等。

注解

需要注意的是,盒子不会在此路由上发送任何“答复”,只会发送请求状态。操作的答复(如果有)必须通过长轮询获取。

可以在 DeviceProxy 对象上执行操作。

iot_device.action(data);

在驱动程序中定义一个 action 方法,当从 Odoo 模块调用时将执行该方法。它接收调用期间提供的数据作为参数。

def action(self, data):
    ...

长轮询

当 Odoo 中的任何模块想要读取特定设备的数据时,它会创建一个监听器,该监听器由盒子的 IP/域名和设备标识符标识,并向其传递一个回调函数,每次设备状态发生变化时都会调用该函数。调用回调函数时会以新数据作为参数。

iot_device.add_listener(this._onValueChange.bind(this));

_onValueChange: function (result) {
    ...
}

在驱动程序中,通过调用 event_manager 中的 device_changed 函数来发布事件。随后,监听器上设置的所有回调都会以 self.data 作为参数被调用。

from odoo.addons.hw_drivers.event_manager import event_manager

class DriverName(Driver):
    connection_type = 'ConnectionType'

    def methodName(self):
        self.data = {
            'value': 0.5,
            ...
        }
        event_manager.device_changed(self)