Repository navigation
Expand file tree
/
Copy pathprotocoldriver.py
More file actions
142 lines (111 loc) · 5.33 KB
/
Copy pathprotocoldriver.py
File metadata and controls
142 lines (111 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright (C) 2026 YIQISOFT
# SPDX-License-Identifier: Apache-2.0
"""
The ProtocolDriver abstract base class.
`ProtocolDriver` is a low-level device-specific interface used by other components of an
EdgeX Device Service to interact with a specific class of devices. Device service
implementations subclass this ABC and implement every method; Python implementations
signal errors by raising exceptions.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Dict, List
from ..models import CommandRequest, CommandValue
if TYPE_CHECKING:
from .service import DeviceServiceSDK
#: protocol name -> protocol properties map.
Protocols = Dict[str, Dict[str, Any]]
class ProtocolDriver(ABC):
"""A low-level device-specific interface used by other components of an EdgeX
Device Service to interact with a specific class of devices.
All methods are abstract; device services must implement each of them.
"""
@abstractmethod
def initialize(self, sdk: "DeviceServiceSDK") -> None:
"""Perform protocol-specific initialization for the device service.
The given SDK can be used to push asynchronous events/readings to Core Data via
`sdk.async_values_channel()` and to send discovered devices via
`sdk.discovered_device_channel()`.
Args:
sdk: The initialized DeviceServiceSDK instance.
"""
@abstractmethod
def handle_read_commands(self, device_name: str, protocols: Protocols,
reqs: List[CommandRequest]) -> List[CommandValue]:
"""Handle a slice of CommandRequest structs, each representing a
ResourceOperation for a specific device resource, and return the resulting
CommandValues.
Args:
device_name: The name of the Device being read.
protocols: The Device's protocol properties.
reqs: The resource operations to execute.
Returns:
The list of read CommandValues. Raise an exception on failure.
"""
@abstractmethod
def handle_write_commands(self, device_name: str, protocols: Protocols,
reqs: List[CommandRequest],
params: List[CommandValue]) -> None:
"""Handle a slice of CommandRequest structs, each representing a
ResourceOperation for a specific device resource. Since these are actuation
commands, the params provide the parameters for the individual command.
Args:
device_name: The name of the Device being written.
protocols: The Device's protocol properties.
reqs: The resource operations to execute.
params: The parameters (CommandValues) for the individual commands.
Raises:
An exception if the write fails.
"""
@abstractmethod
def start(self) -> None:
"""Run Device Service startup tasks after the SDK has been completely initialized.
This allows the Device Service to safely use DeviceServiceSDK interface features.
"""
@abstractmethod
def stop(self, force: bool) -> None:
"""Instruct the protocol-specific DS code to shutdown gracefully, or, if `force`
is True, immediately. The driver is responsible for closing any in-use channels,
including the channel used to send async readings (if supported).
Args:
force: If True, shutdown immediately.
"""
@abstractmethod
def add_device(self, device_name: str, protocols: Protocols,
admin_state: str) -> None:
"""Callback invoked when a new Device associated with this Device Service is added.
Args:
device_name: The name of the added Device.
protocols: The Device's protocol properties.
admin_state: The Device's admin state (e.g. "LOCKED" / "UNLOCKED").
"""
@abstractmethod
def update_device(self, device_name: str, protocols: Protocols,
admin_state: str) -> None:
"""Callback invoked when a Device associated with this Device Service is updated.
Args:
device_name: The name of the updated Device.
protocols: The Device's protocol properties.
admin_state: The Device's admin state (e.g. "LOCKED" / "UNLOCKED").
"""
@abstractmethod
def remove_device(self, device_name: str, protocols: Protocols) -> None:
"""Callback invoked when a Device associated with this Device Service is removed.
Args:
device_name: The name of the removed Device.
protocols: The Device's protocol properties.
"""
@abstractmethod
def discover(self) -> None:
"""Trigger protocol specific device discovery. The discovered devices are written
asynchronously to the channel passed to the implementation via `initialize()`;
they may be added to the Device Service based on a set of acceptance criteria
(i.e. Provision Watchers).
"""
@abstractmethod
def validate_device(self, device: Any) -> None:
"""Trigger the device's protocol properties validation. Raise an exception if the
validation fails; the incoming Device will not be added into EdgeX.
Args:
device: The Device (models.Device) whose protocol properties are validated.
"""