python-terminusgps 37.2.2__py3-none-any.whl → 37.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_terminusgps-37.2.2.dist-info → python_terminusgps-37.4.0.dist-info}/METADATA +1 -1
- {python_terminusgps-37.2.2.dist-info → python_terminusgps-37.4.0.dist-info}/RECORD +7 -6
- terminusgps/wialon/items/unit.py +6 -2
- terminusgps/wialon/renderer.py +188 -0
- terminusgps/wialon/session.py +40 -213
- {python_terminusgps-37.2.2.dist-info → python_terminusgps-37.4.0.dist-info}/WHEEL +0 -0
- {python_terminusgps-37.2.2.dist-info → python_terminusgps-37.4.0.dist-info}/licenses/COPYING +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: python-terminusgps
|
|
3
|
-
Version: 37.
|
|
3
|
+
Version: 37.4.0
|
|
4
4
|
Summary: Provides abstractions/utilities for working with Wialon API, Authorize.NET API, AWS API, and more.
|
|
5
5
|
Project-URL: Documentation, https://docs.terminusgps.com
|
|
6
6
|
Project-URL: Repository, https://github.com/terminusgps/python-terminusgps
|
|
@@ -43,7 +43,8 @@ terminusgps/twilio/caller.py,sha256=RQax2iZ6SYHiuCvBxXkrooRWQ9J8DwKGAjxa8RYjrLI,
|
|
|
43
43
|
terminusgps/wialon/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
44
44
|
terminusgps/wialon/constants.py,sha256=05pLc_0S-x0tT87cQcxOmr8plesjOQxoXfUdjIHwI90,7732
|
|
45
45
|
terminusgps/wialon/flags.py,sha256=M50EdhxQ8IMnJnU0mrHK7-h8Asc6tvNiTOOfd1dBW6A,12815
|
|
46
|
-
terminusgps/wialon/
|
|
46
|
+
terminusgps/wialon/renderer.py,sha256=ubmaLjdJyksO2qiWHbIBvYeDqlFya2br7oJYsdfoyV8,7046
|
|
47
|
+
terminusgps/wialon/session.py,sha256=cMpAdGQQZG8uLYJcGB9NFXk5JBaMNpY6T_QuQffsB_k,10096
|
|
47
48
|
terminusgps/wialon/utils.py,sha256=ntHLAVJSJBdIgJK704xxFNFRopaFz-No9YZZOMmvWO8,11627
|
|
48
49
|
terminusgps/wialon/validators.py,sha256=Z2eci8NzKDHU6oXl5ncgeYXmBYg336k-0R2m0DIxsDY,4346
|
|
49
50
|
terminusgps/wialon/items/__init__.py,sha256=3BVthghekMvhMQSzQgBMlD_phxmKSmp3Zvmo-z0O8Bs,211
|
|
@@ -51,14 +52,14 @@ terminusgps/wialon/items/base.py,sha256=vS8xIo5WLiwzpB81i7aHFnwOHzCf4lQG6azjq79H
|
|
|
51
52
|
terminusgps/wialon/items/resource.py,sha256=Rhy2ig-yxznNhi6ZSaUBbu8ViVx0Sg4n_XS08ijgXsA,18280
|
|
52
53
|
terminusgps/wialon/items/retranslator.py,sha256=2J1x7lNxRck-IEXrmJ7RM9bVL60mrZkfmczOSiUhiO8,3873
|
|
53
54
|
terminusgps/wialon/items/route.py,sha256=2dEUK9o8nwutPE03W-5GUcZrjGvbwLoExVnFV9LyedE,1326
|
|
54
|
-
terminusgps/wialon/items/unit.py,sha256=
|
|
55
|
+
terminusgps/wialon/items/unit.py,sha256=P828NaWkTQncNre3n4h7AedeSxUXPKSxQFpBiliZCBk,12133
|
|
55
56
|
terminusgps/wialon/items/unit_group.py,sha256=HhYMZ9b7UATXeEgHkXN9r5-M_w82fabjDYADCUwBtxQ,4442
|
|
56
57
|
terminusgps/wialon/items/user.py,sha256=pR6OTrm6f7Zo0J8eLvKtWVsdhGi430OxMsMMthGh8YE,5382
|
|
57
58
|
terminusgps/wialon/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
58
59
|
terminusgps/wialon/tests/test_items.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
59
60
|
terminusgps/wialon/tests/test_session.py,sha256=9mBlYchMo9NeqRIZsmxYzrM1zBHorqu4ooxqSNppLpI,1336
|
|
60
61
|
terminusgps/wialon/tests/test_utils.py,sha256=SK4PxJQGECFnzx_EQeRAQfsQ5_3FLaVcis2W9u_ibuI,1730
|
|
61
|
-
python_terminusgps-37.
|
|
62
|
-
python_terminusgps-37.
|
|
63
|
-
python_terminusgps-37.
|
|
64
|
-
python_terminusgps-37.
|
|
62
|
+
python_terminusgps-37.4.0.dist-info/METADATA,sha256=4abh8CpLW2WBcK5GuX4OhzUJGewoOCK7_60AuwefS64,1329
|
|
63
|
+
python_terminusgps-37.4.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
64
|
+
python_terminusgps-37.4.0.dist-info/licenses/COPYING,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
65
|
+
python_terminusgps-37.4.0.dist-info/RECORD,,
|
terminusgps/wialon/items/unit.py
CHANGED
|
@@ -286,8 +286,12 @@ class WialonUnit(WialonBase):
|
|
|
286
286
|
:type: :py:obj:`dict`
|
|
287
287
|
|
|
288
288
|
"""
|
|
289
|
-
return
|
|
290
|
-
|
|
289
|
+
return (
|
|
290
|
+
self.session.wialon_api.core_search_item(
|
|
291
|
+
**{"id": self.id, "flags": flags.DataFlag.UNIT_AVAILABLE_COMMANDS}
|
|
292
|
+
)
|
|
293
|
+
.get("item", {})
|
|
294
|
+
.get("cml", {})
|
|
291
295
|
)
|
|
292
296
|
|
|
293
297
|
@property
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
import secrets
|
|
3
|
+
import sys
|
|
4
|
+
import typing
|
|
5
|
+
|
|
6
|
+
from .session import WialonSession
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class WialonMapRenderer:
|
|
10
|
+
def __init__(self, session: WialonSession) -> None:
|
|
11
|
+
self.session = session
|
|
12
|
+
self.layers = {}
|
|
13
|
+
|
|
14
|
+
def create_messages_layer(
|
|
15
|
+
self,
|
|
16
|
+
name: str,
|
|
17
|
+
unit_id: int | str,
|
|
18
|
+
time_from: datetime.datetime,
|
|
19
|
+
time_to: datetime.datetime,
|
|
20
|
+
enable_trip_detector: bool = False,
|
|
21
|
+
enable_directional_arrows: bool = False,
|
|
22
|
+
enable_points: bool = False,
|
|
23
|
+
enable_annotations: bool = False,
|
|
24
|
+
track_color: str = "FFFF0000",
|
|
25
|
+
point_color: str = "7F00FF00",
|
|
26
|
+
track_width: int = 12,
|
|
27
|
+
flags: int = 0x0004,
|
|
28
|
+
) -> dict[str, typing.Any]:
|
|
29
|
+
"""
|
|
30
|
+
Creates a messages layer and adds it to the renderer.
|
|
31
|
+
|
|
32
|
+
:param name: Name of the new messages layer.
|
|
33
|
+
:type name: :py:obj:`str`
|
|
34
|
+
:param unit_id: A Wialon unit id to retrieve messages for.
|
|
35
|
+
:type unit_id: :py:obj:`int` | :py:obj:`str`
|
|
36
|
+
:param time_from: Start time for the messages interval.
|
|
37
|
+
:type time_from: :py:obj:`~datetime.datetime`
|
|
38
|
+
:param time_to: End time for the messages interval.
|
|
39
|
+
:type time_to: :py:obj:`~datetime.datetime`
|
|
40
|
+
:param enable_trip_detector: Whether or not to enable the trip detector on the layer. Default is :py:obj:`False`.
|
|
41
|
+
:type enable_trip_detector: :py:obj:`bool`
|
|
42
|
+
:param enable_directional_arrows: Whether or not to enable directional arrows on the layer. Default is :py:obj:`False`.
|
|
43
|
+
:type enable_directional_arrows: :py:obj:`bool`
|
|
44
|
+
:param enable_points: Whether or not to enable points of interest on the layer. Default is :py:obj:`False`.
|
|
45
|
+
:type enable_points: :py:obj:`bool`
|
|
46
|
+
:param enable_annotations: Whether or not to annotate the points of interest on the layer. Default is :py:obj:`False`.
|
|
47
|
+
:type enable_annotations: :py:obj:`bool`
|
|
48
|
+
:param track_color: ARGB color to use for the track on the layer. Default is :py:obj:`"FFFF0000"`.
|
|
49
|
+
:type track_color: :py:obj:`str`
|
|
50
|
+
:param point_color: ARGB color to use for the points of interest on the layer. Default is :py:obj:`"7F00FF00"`.
|
|
51
|
+
:type point_color: :py:obj:`str`
|
|
52
|
+
:param track_width: Width (in pixels) of the track on the layer. Default is :py:obj:`12`.
|
|
53
|
+
:type track_width: :py:obj:`int`
|
|
54
|
+
:param flags: Determines which points of interest will be rendered on the layer. Default is :py:obj:`0x0004`.
|
|
55
|
+
:type flags: :py:obj:`int`
|
|
56
|
+
:returns: A Wialon API response.
|
|
57
|
+
:rtype: :py:obj:`dict`
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
response = self.session.wialon_api.render_create_messages_layer(
|
|
61
|
+
**{
|
|
62
|
+
"layerName": name,
|
|
63
|
+
"itemId": int(unit_id),
|
|
64
|
+
"timeFrom": time_from.timestamp(),
|
|
65
|
+
"timeTo": time_to.timestamp(),
|
|
66
|
+
"tripDetector": int(enable_trip_detector),
|
|
67
|
+
"trackColor": "trip" if enable_trip_detector else track_color,
|
|
68
|
+
"trackWidth": track_width,
|
|
69
|
+
"arrows": int(enable_directional_arrows),
|
|
70
|
+
"points": int(enable_points),
|
|
71
|
+
"pointColor": point_color,
|
|
72
|
+
"annotations": int(enable_annotations),
|
|
73
|
+
"flags": flags,
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
self.layers.setdefault(name, {"enabled": False})
|
|
77
|
+
return response if response else {}
|
|
78
|
+
|
|
79
|
+
def enable_layer(self, name: str) -> None:
|
|
80
|
+
"""
|
|
81
|
+
Enables a layer by name in the renderer.
|
|
82
|
+
|
|
83
|
+
:param name: A layer name.
|
|
84
|
+
:type name: :py:obj:`str`
|
|
85
|
+
:returns: Nothing.
|
|
86
|
+
:rtype: :py:obj:`None`
|
|
87
|
+
|
|
88
|
+
"""
|
|
89
|
+
if not self.layers[name]["enabled"]:
|
|
90
|
+
self.session.wialon_api.render_enable_layer(
|
|
91
|
+
**{"layerName": name, "enable": int(True)}
|
|
92
|
+
)
|
|
93
|
+
self.layers[name]["enabled"] = True
|
|
94
|
+
|
|
95
|
+
def disable_layer(self, name: str) -> None:
|
|
96
|
+
"""
|
|
97
|
+
Disables a layer by name in the renderer.
|
|
98
|
+
|
|
99
|
+
:param name: A layer name.
|
|
100
|
+
:type name: :py:obj:`str`
|
|
101
|
+
:returns: Nothing.
|
|
102
|
+
:rtype: :py:obj:`None`
|
|
103
|
+
|
|
104
|
+
"""
|
|
105
|
+
if self.layers[name]["enabled"]:
|
|
106
|
+
self.session.wialon_api.render_enable_layer(
|
|
107
|
+
**{"layerName": name, "enable": int(False)}
|
|
108
|
+
)
|
|
109
|
+
self.layers[name]["enabled"] = False
|
|
110
|
+
|
|
111
|
+
def remove_layer(self, name: str) -> None:
|
|
112
|
+
"""
|
|
113
|
+
Removes a layer by name in the renderer.
|
|
114
|
+
|
|
115
|
+
:param name: A layer name.
|
|
116
|
+
:type name: :py:obj:`str`
|
|
117
|
+
:returns: Nothing.
|
|
118
|
+
:rtype: :py:obj:`None`
|
|
119
|
+
|
|
120
|
+
"""
|
|
121
|
+
self.session.wialon_api.render_remove_layer(**{"layerName": name})
|
|
122
|
+
self.layers.pop(name)
|
|
123
|
+
|
|
124
|
+
def set_locale(
|
|
125
|
+
self,
|
|
126
|
+
timezone: int,
|
|
127
|
+
lang: str = "en",
|
|
128
|
+
flags: int = 1,
|
|
129
|
+
date_format: str = "%Y-%m-%E %H:%M:%S",
|
|
130
|
+
density: int = 1,
|
|
131
|
+
) -> None:
|
|
132
|
+
"""
|
|
133
|
+
Updates the locale options for the renderer.
|
|
134
|
+
|
|
135
|
+
:param timezone: Timezone offset to use in the renderer.
|
|
136
|
+
:type timezone: :py:obj:`int`
|
|
137
|
+
:param lang: A 2-character language code. Default is :py:obj:`"en"`.
|
|
138
|
+
:type lang: :py:obj:`str`
|
|
139
|
+
:param flags: Measurement system to use in the renderer.
|
|
140
|
+
:type flags: :py:obj:`int`
|
|
141
|
+
:param date_format: Format to use for dates in the renderer.
|
|
142
|
+
:type date_format: :py:obj:`str`
|
|
143
|
+
:param density: Tile density to use in the renderer.
|
|
144
|
+
:type density: :py:obj:`int`
|
|
145
|
+
|
|
146
|
+
Flag options:
|
|
147
|
+
|
|
148
|
+
+-------------+--------------+
|
|
149
|
+
| value | measurement |
|
|
150
|
+
+=============+==============+
|
|
151
|
+
| :py:obj:`0` | Metric |
|
|
152
|
+
+-------------+--------------+
|
|
153
|
+
| :py:obj:`1` | US |
|
|
154
|
+
+-------------+--------------+
|
|
155
|
+
| :py:obj:`2` | Imperial |
|
|
156
|
+
+-------------+--------------+
|
|
157
|
+
|
|
158
|
+
Density options:
|
|
159
|
+
|
|
160
|
+
+-------------+-----------+-------+
|
|
161
|
+
| value | tile size | ratio |
|
|
162
|
+
+=============+===========+=======+
|
|
163
|
+
| :py:obj:`1` | 256*256 | 1 |
|
|
164
|
+
+-------------+-----------+-------+
|
|
165
|
+
| :py:obj:`2` | 378*378 | 1.5 |
|
|
166
|
+
+-------------+-----------+-------+
|
|
167
|
+
| :py:obj:`3` | 512*512 | 2 |
|
|
168
|
+
+-------------+-----------+-------+
|
|
169
|
+
| :py:obj:`4` | 768*768 | 3 |
|
|
170
|
+
+-------------+-----------+-------+
|
|
171
|
+
| :py:obj:`5` | 1024*1024 | 4 |
|
|
172
|
+
+-------------+-----------+-------+
|
|
173
|
+
|
|
174
|
+
"""
|
|
175
|
+
self.session.wialon_api.render_set_locale(
|
|
176
|
+
**{
|
|
177
|
+
"tzOffset": timezone,
|
|
178
|
+
"language": lang,
|
|
179
|
+
"flags": flags,
|
|
180
|
+
"formatDate": date_format,
|
|
181
|
+
"density": density,
|
|
182
|
+
}
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
def generate_render_url(self, x: int, y: int, z: int) -> str:
|
|
186
|
+
sid = self.session.id
|
|
187
|
+
random_num = secrets.SystemRandom(sid).randint(1, sys.maxsize)
|
|
188
|
+
return f"https://hst-api.wialon.com/adfurl{random_num}/avl_render/{x}_{y}_{z}/{sid}.png"
|
terminusgps/wialon/session.py
CHANGED
|
@@ -4,54 +4,59 @@ import typing
|
|
|
4
4
|
|
|
5
5
|
import wialon.api
|
|
6
6
|
from django.conf import settings
|
|
7
|
+
from loguru import logger
|
|
8
|
+
|
|
9
|
+
from . import flags as wialon_flags
|
|
10
|
+
|
|
11
|
+
logger.disable(__name__)
|
|
7
12
|
|
|
8
13
|
|
|
9
14
|
@dataclasses.dataclass
|
|
10
15
|
class WialonAPICall:
|
|
11
16
|
action: str
|
|
12
17
|
timestamp: datetime.datetime
|
|
13
|
-
args: tuple
|
|
14
|
-
kwargs: dict
|
|
15
18
|
result: typing.Any = None
|
|
16
19
|
error: Exception | None = None
|
|
17
20
|
|
|
18
21
|
|
|
19
22
|
class Wialon(wialon.api.Wialon):
|
|
20
|
-
def __init__(self, token: str,
|
|
23
|
+
def __init__(self, token: str, *args, **kwargs) -> None:
|
|
21
24
|
super().__init__(*args, **kwargs)
|
|
22
25
|
self.token = token
|
|
23
26
|
self.call_history: list[WialonAPICall] = []
|
|
24
|
-
self.last_call_datetime = None
|
|
25
|
-
self.timeout = timeout
|
|
26
27
|
|
|
27
28
|
@property
|
|
28
29
|
def total_calls(self) -> int:
|
|
29
30
|
return len(self.call_history)
|
|
30
31
|
|
|
32
|
+
@property
|
|
33
|
+
def call_actions(self) -> list[str]:
|
|
34
|
+
return [call.action for call in self.call_history]
|
|
35
|
+
|
|
31
36
|
def token_login(self, *args, **kwargs) -> typing.Any:
|
|
32
|
-
kwargs
|
|
37
|
+
kwargs.setdefault("appName", "python-terminusgps")
|
|
33
38
|
return self.call("token_login", *args, **kwargs)
|
|
34
39
|
|
|
35
|
-
def call(self, action_name: str, *argc, **kwargs) -> typing.Any:
|
|
36
|
-
now = datetime.datetime.now()
|
|
37
|
-
|
|
40
|
+
def call(self, action_name: str, *argc, **kwargs) -> dict[str, typing.Any]:
|
|
38
41
|
try:
|
|
42
|
+
timestamp = datetime.datetime.now()
|
|
39
43
|
result = super().call(action_name, *argc, **kwargs)
|
|
40
44
|
call_record = WialonAPICall(
|
|
41
|
-
action=action_name,
|
|
42
|
-
timestamp=now,
|
|
43
|
-
args=argc,
|
|
44
|
-
kwargs=kwargs,
|
|
45
|
-
result=result,
|
|
45
|
+
action=action_name, timestamp=timestamp, result=result
|
|
46
46
|
)
|
|
47
47
|
return result
|
|
48
48
|
except wialon.api.WialonError as e:
|
|
49
49
|
call_record = WialonAPICall(
|
|
50
|
-
action=action_name, timestamp=
|
|
50
|
+
action=action_name, timestamp=timestamp, error=e
|
|
51
51
|
)
|
|
52
|
-
return
|
|
52
|
+
return {}
|
|
53
53
|
finally:
|
|
54
|
-
|
|
54
|
+
if call_record.result:
|
|
55
|
+
logger.debug("Successfully executed '{}'", action_name)
|
|
56
|
+
elif call_record.error:
|
|
57
|
+
logger.warning(
|
|
58
|
+
"Failed to execute '{}': {}", action_name, call_record.error
|
|
59
|
+
)
|
|
55
60
|
self.call_history.append(call_record)
|
|
56
61
|
|
|
57
62
|
|
|
@@ -63,7 +68,6 @@ class WialonSession:
|
|
|
63
68
|
scheme: str = "https",
|
|
64
69
|
host: str = "hst-api.wialon.com",
|
|
65
70
|
port: int = 443,
|
|
66
|
-
timeout: int = 300,
|
|
67
71
|
) -> None:
|
|
68
72
|
"""
|
|
69
73
|
Starts or continues a Wialon API session.
|
|
@@ -76,12 +80,10 @@ class WialonSession:
|
|
|
76
80
|
:type scheme: :py:obj:`str`
|
|
77
81
|
:param host: Wialon API host url. Default is ``"hst-api.wialon.com"``.
|
|
78
82
|
:type host: :py:obj:`str`
|
|
83
|
+
:param host: Wialon API rendering url. Default is ``"render-maps.wialon.com"``.
|
|
84
|
+
:type host: :py:obj:`str`
|
|
79
85
|
:param port: Wialon API host port. Default is ``443``.
|
|
80
86
|
:type port: :py:obj:`int`
|
|
81
|
-
:param log_level: Level of emitted logs. Default is ``10``.
|
|
82
|
-
:type log_level: :py:obj:`int`
|
|
83
|
-
:param timeout: How long in seconds a session can be active for. Default is ``500`` (5 min).
|
|
84
|
-
:type timeout: :py:obj:`int`
|
|
85
87
|
:returns: Nothing.
|
|
86
88
|
:rtype: :py:obj:`None`
|
|
87
89
|
|
|
@@ -94,12 +96,7 @@ class WialonSession:
|
|
|
94
96
|
self._wsdk_version = None
|
|
95
97
|
self._uid = None
|
|
96
98
|
self._wialon_api = Wialon(
|
|
97
|
-
scheme=scheme,
|
|
98
|
-
host=host,
|
|
99
|
-
port=port,
|
|
100
|
-
sid=sid,
|
|
101
|
-
timeout=timeout,
|
|
102
|
-
token=self.token,
|
|
99
|
+
scheme=scheme, host=host, port=port, sid=sid, token=self.token
|
|
103
100
|
)
|
|
104
101
|
|
|
105
102
|
def __str__(self) -> str:
|
|
@@ -112,12 +109,12 @@ class WialonSession:
|
|
|
112
109
|
"""
|
|
113
110
|
Logs into the Wialon API using :py:meth:`login`.
|
|
114
111
|
|
|
115
|
-
:raises AssertionError: If the Wialon API token
|
|
112
|
+
:raises AssertionError: If the session's Wialon API :py:attr:`token` wasn't set.
|
|
116
113
|
:returns: A valid Wialon API session.
|
|
117
114
|
:rtype: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
118
115
|
|
|
119
116
|
"""
|
|
120
|
-
assert self.token, "Wialon API token
|
|
117
|
+
assert self.token, "Wialon API token wasn't set."
|
|
121
118
|
self.login(self.token)
|
|
122
119
|
return self
|
|
123
120
|
|
|
@@ -281,7 +278,7 @@ class WialonSession:
|
|
|
281
278
|
"""
|
|
282
279
|
return str(self._token)
|
|
283
280
|
|
|
284
|
-
def login(self, token: str, flags: int
|
|
281
|
+
def login(self, token: str, flags: int | None = None) -> str:
|
|
285
282
|
"""
|
|
286
283
|
Logs into the Wialon API and starts a new session.
|
|
287
284
|
|
|
@@ -295,10 +292,17 @@ class WialonSession:
|
|
|
295
292
|
:rtype: :py:obj:`str`
|
|
296
293
|
|
|
297
294
|
"""
|
|
295
|
+
if flags is None:
|
|
296
|
+
flags = (
|
|
297
|
+
wialon_flags.TokenFlag.ONLINE_TRACKING
|
|
298
|
+
| wialon_flags.TokenFlag.VIEW_ACCESS
|
|
299
|
+
| wialon_flags.TokenFlag.MANAGE_NONSENSITIVE
|
|
300
|
+
)
|
|
298
301
|
try:
|
|
302
|
+
print(f"{type(flags) = }")
|
|
299
303
|
response = self.wialon_api.token_login(**{"token": token, "fl": flags})
|
|
300
304
|
self._set_login_response(response)
|
|
301
|
-
return response.get("eid")
|
|
305
|
+
return response.get("eid", "")
|
|
302
306
|
except (wialon.api.WialonError, ValueError):
|
|
303
307
|
raise
|
|
304
308
|
|
|
@@ -314,13 +318,15 @@ class WialonSession:
|
|
|
314
318
|
self.wialon_api.sid = None
|
|
315
319
|
|
|
316
320
|
if response.get("error") != 0:
|
|
317
|
-
|
|
321
|
+
logger.warning(
|
|
322
|
+
"Failed to logout of the session: '{}'", response.get("message")
|
|
323
|
+
)
|
|
318
324
|
|
|
319
325
|
def _set_login_response(self, login_response: dict | None) -> None:
|
|
320
326
|
"""
|
|
321
327
|
Sets the Wialon API session's attributes based on a login response.
|
|
322
328
|
|
|
323
|
-
:param login_response: A
|
|
329
|
+
:param login_response: A dictionary returned from :py:meth:`login`.
|
|
324
330
|
:type login_response: :py:obj:`dict`
|
|
325
331
|
:raises ValueError: If ``login_response`` wasn't provided.
|
|
326
332
|
:returns: Nothing.
|
|
@@ -344,182 +350,3 @@ class WialonSession:
|
|
|
344
350
|
self._username = login_response.get("au")
|
|
345
351
|
self._video_service_url = login_response.get("video_service_url")
|
|
346
352
|
self._wsdk_version = login_response.get("wsdk_version")
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
class WialonSessionManager:
|
|
350
|
-
"""Provides an interface for generating automatically activated Wialon sessions."""
|
|
351
|
-
|
|
352
|
-
def __init__(
|
|
353
|
-
self,
|
|
354
|
-
token: str | None = None,
|
|
355
|
-
lifetime: int = 500,
|
|
356
|
-
session: WialonSession | None = None,
|
|
357
|
-
session_id: str | None = None,
|
|
358
|
-
) -> None:
|
|
359
|
-
"""
|
|
360
|
-
Sets :py:attr:`token` and :py:attr:`lifetime`.
|
|
361
|
-
|
|
362
|
-
:param token: A Wialon API token. Default is :confval:`WIALON_TOKEN`.
|
|
363
|
-
:type token: :py:obj:`str` | :py:obj:`None`
|
|
364
|
-
:param lifetime: How long in seconds a Wialon session can be valid for. Default is ``500``.
|
|
365
|
-
:type lifetime: :py:obj:`int`
|
|
366
|
-
:returns: Nothing.
|
|
367
|
-
:rtype: :py:obj:`None`
|
|
368
|
-
|
|
369
|
-
"""
|
|
370
|
-
self.token = token
|
|
371
|
-
self.lifetime = lifetime
|
|
372
|
-
|
|
373
|
-
target_sid = session.id if session and session.id else session_id
|
|
374
|
-
self.session = WialonSession(sid=target_sid)
|
|
375
|
-
if not target_sid or not self.validate_session():
|
|
376
|
-
self.session.login(token=self.token)
|
|
377
|
-
|
|
378
|
-
def check_active(self) -> bool:
|
|
379
|
-
"""
|
|
380
|
-
Checks if :py:attr:`session` is active.
|
|
381
|
-
|
|
382
|
-
:returns: Whether or not :py:attr:`session` is an active Wialon session.
|
|
383
|
-
:rtype: :py:obj:`bool`
|
|
384
|
-
|
|
385
|
-
"""
|
|
386
|
-
if not self.session.id:
|
|
387
|
-
return False
|
|
388
|
-
if not self.session.wialon_api.last_call_datetime:
|
|
389
|
-
return self.validate_session()
|
|
390
|
-
|
|
391
|
-
now = datetime.datetime.now()
|
|
392
|
-
last_call = self.session.wialon_api.last_call_datetime
|
|
393
|
-
session_expiry = last_call + datetime.timedelta(seconds=self.lifetime)
|
|
394
|
-
|
|
395
|
-
if now > session_expiry:
|
|
396
|
-
return False
|
|
397
|
-
return self.validate_session()
|
|
398
|
-
|
|
399
|
-
def get_session(self, sid: str | None = None) -> WialonSession:
|
|
400
|
-
"""
|
|
401
|
-
Returns a valid Wialon API session.
|
|
402
|
-
|
|
403
|
-
If ``sid`` is provided, tries to continue the session and return it.
|
|
404
|
-
|
|
405
|
-
:param sid: A Wialon API session id.
|
|
406
|
-
:type sid: :py:obj:`str` | :py:obj:`None`
|
|
407
|
-
:returns: A valid Wialon API session.
|
|
408
|
-
:rtype: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
409
|
-
|
|
410
|
-
"""
|
|
411
|
-
if sid is not None:
|
|
412
|
-
self.session = WialonSession(sid=sid)
|
|
413
|
-
if not self.validate_session():
|
|
414
|
-
self.session = WialonSession()
|
|
415
|
-
self.session.login(token=self.token)
|
|
416
|
-
return self.session
|
|
417
|
-
if not self.check_active():
|
|
418
|
-
self.session = WialonSession()
|
|
419
|
-
self.session.login(token=self.token)
|
|
420
|
-
return self.session
|
|
421
|
-
|
|
422
|
-
def validate_session(self) -> bool:
|
|
423
|
-
try:
|
|
424
|
-
result = self.session.wialon_api.core_duplicate()
|
|
425
|
-
return result is not None
|
|
426
|
-
except wialon.api.WialonError:
|
|
427
|
-
return False
|
|
428
|
-
|
|
429
|
-
@property
|
|
430
|
-
def sid(self) -> str | None:
|
|
431
|
-
"""
|
|
432
|
-
Current Wialon session id.
|
|
433
|
-
|
|
434
|
-
:type: :py:obj:`str` | :py:obj:`None`
|
|
435
|
-
|
|
436
|
-
"""
|
|
437
|
-
return self.session.wialon_api.sid if self.session else None
|
|
438
|
-
|
|
439
|
-
@sid.setter
|
|
440
|
-
def sid(self, other: str) -> None:
|
|
441
|
-
"""
|
|
442
|
-
Sets :py:attr:`sid` to ``other``.
|
|
443
|
-
|
|
444
|
-
:param other: A Wialon session id.
|
|
445
|
-
:type other: :py:obj:`str`
|
|
446
|
-
:returns: Nothing.
|
|
447
|
-
:rtype: :py:obj:`None`
|
|
448
|
-
|
|
449
|
-
"""
|
|
450
|
-
if self.session:
|
|
451
|
-
self.session.wialon_api.sid = other
|
|
452
|
-
else:
|
|
453
|
-
self.session = WialonSession(sid=other)
|
|
454
|
-
|
|
455
|
-
@property
|
|
456
|
-
def token(self) -> str:
|
|
457
|
-
"""
|
|
458
|
-
A Wialon API token.
|
|
459
|
-
|
|
460
|
-
:type: :py:obj:`str`
|
|
461
|
-
|
|
462
|
-
"""
|
|
463
|
-
return str(self._token)
|
|
464
|
-
|
|
465
|
-
@token.setter
|
|
466
|
-
def token(self, other: str | None) -> None:
|
|
467
|
-
"""
|
|
468
|
-
Sets :py:attr:`token` to ``other`` if provided.
|
|
469
|
-
|
|
470
|
-
If ``other`` isn't provided, instead sets :py:attr:`token` to :confval:`WIALON_TOKEN`.
|
|
471
|
-
|
|
472
|
-
:param other: A Wialon API token.
|
|
473
|
-
:type other: :py:obj:`str` | :py:obj:`None`
|
|
474
|
-
:returns: Nothing.
|
|
475
|
-
:rtype: :py:obj:`None`
|
|
476
|
-
|
|
477
|
-
"""
|
|
478
|
-
self._token = other if other is not None else settings.WIALON_TOKEN
|
|
479
|
-
|
|
480
|
-
@property
|
|
481
|
-
def session(self) -> WialonSession:
|
|
482
|
-
"""
|
|
483
|
-
A Wialon API session.
|
|
484
|
-
|
|
485
|
-
:type: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
486
|
-
|
|
487
|
-
"""
|
|
488
|
-
return self._session
|
|
489
|
-
|
|
490
|
-
@session.setter
|
|
491
|
-
def session(self, other: WialonSession) -> None:
|
|
492
|
-
"""
|
|
493
|
-
Sets :py:attr:`session` to ``other``.
|
|
494
|
-
|
|
495
|
-
:param other: A Wialon session.
|
|
496
|
-
:type other: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
497
|
-
:returns: Nothing.
|
|
498
|
-
:rtype: :py:obj:`None`
|
|
499
|
-
|
|
500
|
-
"""
|
|
501
|
-
self._session = other
|
|
502
|
-
|
|
503
|
-
@property
|
|
504
|
-
def lifetime(self) -> int:
|
|
505
|
-
"""
|
|
506
|
-
How long in seconds a Wialon session can live for.
|
|
507
|
-
|
|
508
|
-
:type: :py:obj:`int`
|
|
509
|
-
:value: :py:obj:`500`
|
|
510
|
-
|
|
511
|
-
"""
|
|
512
|
-
return self._lifetime
|
|
513
|
-
|
|
514
|
-
@lifetime.setter
|
|
515
|
-
def lifetime(self, other: int) -> None:
|
|
516
|
-
"""
|
|
517
|
-
Sets :py:attr:`lifetime` to ``other``.
|
|
518
|
-
|
|
519
|
-
:param other: An integer.
|
|
520
|
-
:type other: :py:obj:`int`
|
|
521
|
-
:returns: Nothing.
|
|
522
|
-
:rtype: :py:obj:`None`
|
|
523
|
-
|
|
524
|
-
"""
|
|
525
|
-
self._lifetime = other
|
|
File without changes
|
{python_terminusgps-37.2.2.dist-info → python_terminusgps-37.4.0.dist-info}/licenses/COPYING
RENAMED
|
File without changes
|