连接设备

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

要为设备添加支持,我们所需要的是:

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

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

每次启动时,IoT Box 将加载连接的 Odoo 实例中可以找到的所有接口和驱动程序。每个模块都可以包含一个 iot_handlers 目录,该目录将被复制到 IoT Box。该目录的结构如下:

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

检测设备

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

支持的设备将会出现在IoT Box主页上,您可以通过其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 methods of drivers are given a priority order. The supported method of a child class will always be tested before the one of its parent. This priority can be adjusted by modifying the priority attribute of the Driver.

创建一个新的驱动程序需要:

  • 扩展 Driver

  • 设置 connection_type 类属性。

  • 设置 device_typedevice_connectiondevice_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 访问,该对象使用IoT Box的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) {
    ...
}

在驱动程序中,通过调用 device_changed 函数从 event_manager 发布事件。然后,所有在监听器上设置的回调函数将使用 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)