-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
port_management.py
180 lines (139 loc) · 4.88 KB
/
port_management.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""I/O Port Management API.
The I/O port management API makes it possible to retrieve information about the ports and apply product dependent configurations
"""
import attr
from .api import APIItem, APIItems, Body
URL = "/axis-cgi/io/portmanagement.cgi"
API_DISCOVERY_ID = "io-port-management"
API_VERSION = "1.0"
@attr.s
class SetPort:
"""Port configuration class."""
port: str = attr.ib()
usage: str = attr.ib(default=None)
direction: str = attr.ib(default=None)
name: str = attr.ib(default=None)
normalState: str = attr.ib(default=None)
state: str = attr.ib(default=None)
@attr.s
class PortSequence:
"""Port sequence class."""
port: str = attr.ib()
sequence: list = attr.ib(factory=list)
@attr.s
class Sequence:
"""Sequence class."""
state: str = attr.ib()
time: int = attr.ib()
class IoPortManagement(APIItems):
"""I/O port management for Axis devices."""
def __init__(self, request: object) -> None:
super().__init__({}, request, URL, Port)
async def update(self) -> None:
raw = await self.get_ports()
self.process_raw(raw)
@staticmethod
def pre_process_raw(raw: dict) -> dict:
"""Return a dictionary of ports."""
if not raw:
return {}
if raw.get("data", {}).get("numberOfPorts", 0) == 0:
return {}
ports = raw["data"]["items"]
return {port["port"]: port for port in ports}
async def get_ports(self) -> dict:
"""This CGI method can be used to retrieve information about all ports on the device and their capabilities."""
return await self._request(
"post",
URL,
json=attr.asdict(
Body("getPorts", API_VERSION),
filter=attr.filters.exclude(attr.fields(Body).params),
),
)
async def set_ports(self, ports: list) -> None:
"""Configures anything from one to several ports.
Some of the available options are:
* Setting a nice name that can be used in the user interface.
* Configuring the states and what constitutes a normal and triggered state respectively.
This will make triggers activate in either open or closed circuits.
The reason the change is treated as a nice name is because it doesn’t affect the underlying behavior of the port.
Devices with configurable ports can change the direction to either input or output.
"""
await self._request(
"post",
URL,
json=attr.asdict(
Body("setPorts", API_VERSION, params=ports),
filter=lambda attr, value: value is not None,
),
)
async def set_state_sequence(self, sequence: PortSequence) -> None:
"""Applies a sequence of state changes with a delay in milliseconds between states."""
await self._request(
"post",
URL,
json=attr.asdict(Body("setStateSequence", API_VERSION, params=sequence)),
)
async def get_supported_versions(self) -> dict:
"""This CGI method can be used to retrieve a list of supported API versions."""
return await self._request(
"post",
URL,
json=attr.asdict(
Body("getSupportedVersions", API_VERSION),
filter=attr.filters.include(attr.fields(Body).method),
),
)
class Port(APIItem):
"""I/O port management port."""
@property
def configurable(self) -> bool:
"""Is port configurable."""
return self.raw["configurable"]
@property
def direction(self) -> str:
"""Direction of port.
<input|output>.
"""
return self.raw["direction"]
@property
def name(self) -> str:
"""Name of port."""
return self.raw["name"]
@property
def normalState(self) -> str:
"""Normal state of port.
<open|closed>.
"""
return self.raw["normalState"]
@property
def port(self) -> str:
"""Index of port."""
return self.raw["port"]
@property
def state(self) -> str:
"""State of port.
<open|closed>.
"""
return self.raw["state"]
@property
def usage(self) -> str:
"""Usage of port."""
return self.raw["usage"]
async def set_state(self, set_port: SetPort) -> None:
"""Set port state."""
await self._request(
"post",
URL,
json=attr.asdict(
Body("setPorts", API_VERSION, params=[set_port]),
filter=lambda attr, value: value is not None,
),
)
async def open(self) -> None:
"""Open port."""
await self.set_state(SetPort(self.port, state="open"))
async def close(self) -> None:
"""Close port."""
await self.set_state(SetPort(self.port, state="closed"))