python-terminusgps 37.3.0__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.3.0.dist-info → python_terminusgps-37.4.0.dist-info}/METADATA +1 -1
- {python_terminusgps-37.3.0.dist-info → python_terminusgps-37.4.0.dist-info}/RECORD +6 -5
- terminusgps/wialon/renderer.py +188 -0
- terminusgps/wialon/session.py +40 -218
- {python_terminusgps-37.3.0.dist-info → python_terminusgps-37.4.0.dist-info}/WHEEL +0 -0
- {python_terminusgps-37.3.0.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
|
|
@@ -58,7 +59,7 @@ terminusgps/wialon/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZ
|
|
|
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,,
|
|
@@ -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,10 +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 timeout: How long in seconds a session can be active for. Default is ``500`` (5 min).
|
|
82
|
-
:type timeout: :py:obj:`int`
|
|
83
87
|
:returns: Nothing.
|
|
84
88
|
:rtype: :py:obj:`None`
|
|
85
89
|
|
|
@@ -92,12 +96,7 @@ class WialonSession:
|
|
|
92
96
|
self._wsdk_version = None
|
|
93
97
|
self._uid = None
|
|
94
98
|
self._wialon_api = Wialon(
|
|
95
|
-
scheme=scheme,
|
|
96
|
-
host=host,
|
|
97
|
-
port=port,
|
|
98
|
-
sid=sid,
|
|
99
|
-
timeout=timeout,
|
|
100
|
-
token=self.token,
|
|
99
|
+
scheme=scheme, host=host, port=port, sid=sid, token=self.token
|
|
101
100
|
)
|
|
102
101
|
|
|
103
102
|
def __str__(self) -> str:
|
|
@@ -110,12 +109,12 @@ class WialonSession:
|
|
|
110
109
|
"""
|
|
111
110
|
Logs into the Wialon API using :py:meth:`login`.
|
|
112
111
|
|
|
113
|
-
:raises AssertionError: If the Wialon API token
|
|
112
|
+
:raises AssertionError: If the session's Wialon API :py:attr:`token` wasn't set.
|
|
114
113
|
:returns: A valid Wialon API session.
|
|
115
114
|
:rtype: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
116
115
|
|
|
117
116
|
"""
|
|
118
|
-
assert self.token, "Wialon API token
|
|
117
|
+
assert self.token, "Wialon API token wasn't set."
|
|
119
118
|
self.login(self.token)
|
|
120
119
|
return self
|
|
121
120
|
|
|
@@ -279,7 +278,7 @@ class WialonSession:
|
|
|
279
278
|
"""
|
|
280
279
|
return str(self._token)
|
|
281
280
|
|
|
282
|
-
def login(self, token: str, flags: int
|
|
281
|
+
def login(self, token: str, flags: int | None = None) -> str:
|
|
283
282
|
"""
|
|
284
283
|
Logs into the Wialon API and starts a new session.
|
|
285
284
|
|
|
@@ -293,10 +292,17 @@ class WialonSession:
|
|
|
293
292
|
:rtype: :py:obj:`str`
|
|
294
293
|
|
|
295
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
|
+
)
|
|
296
301
|
try:
|
|
302
|
+
print(f"{type(flags) = }")
|
|
297
303
|
response = self.wialon_api.token_login(**{"token": token, "fl": flags})
|
|
298
304
|
self._set_login_response(response)
|
|
299
|
-
return response.get("eid")
|
|
305
|
+
return response.get("eid", "")
|
|
300
306
|
except (wialon.api.WialonError, ValueError):
|
|
301
307
|
raise
|
|
302
308
|
|
|
@@ -312,13 +318,15 @@ class WialonSession:
|
|
|
312
318
|
self.wialon_api.sid = None
|
|
313
319
|
|
|
314
320
|
if response.get("error") != 0:
|
|
315
|
-
|
|
321
|
+
logger.warning(
|
|
322
|
+
"Failed to logout of the session: '{}'", response.get("message")
|
|
323
|
+
)
|
|
316
324
|
|
|
317
325
|
def _set_login_response(self, login_response: dict | None) -> None:
|
|
318
326
|
"""
|
|
319
327
|
Sets the Wialon API session's attributes based on a login response.
|
|
320
328
|
|
|
321
|
-
:param login_response: A
|
|
329
|
+
:param login_response: A dictionary returned from :py:meth:`login`.
|
|
322
330
|
:type login_response: :py:obj:`dict`
|
|
323
331
|
:raises ValueError: If ``login_response`` wasn't provided.
|
|
324
332
|
:returns: Nothing.
|
|
@@ -342,189 +350,3 @@ class WialonSession:
|
|
|
342
350
|
self._username = login_response.get("au")
|
|
343
351
|
self._video_service_url = login_response.get("video_service_url")
|
|
344
352
|
self._wsdk_version = login_response.get("wsdk_version")
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
class WialonSessionManager:
|
|
348
|
-
"""Provides an interface for generating automatically activated Wialon sessions."""
|
|
349
|
-
|
|
350
|
-
def __init__(
|
|
351
|
-
self,
|
|
352
|
-
token: str | None = None,
|
|
353
|
-
lifetime: int = 500,
|
|
354
|
-
session: WialonSession | None = None,
|
|
355
|
-
session_id: str | None = None,
|
|
356
|
-
) -> None:
|
|
357
|
-
"""
|
|
358
|
-
Sets :py:attr:`token` and :py:attr:`lifetime`.
|
|
359
|
-
|
|
360
|
-
:param token: A Wialon API token. Default is :confval:`WIALON_TOKEN`.
|
|
361
|
-
:type token: :py:obj:`str` | :py:obj:`None`
|
|
362
|
-
:param lifetime: How long in seconds a Wialon session can be valid for. Default is ``500``.
|
|
363
|
-
:type lifetime: :py:obj:`int`
|
|
364
|
-
:returns: Nothing.
|
|
365
|
-
:rtype: :py:obj:`None`
|
|
366
|
-
|
|
367
|
-
"""
|
|
368
|
-
self.token = token
|
|
369
|
-
self.lifetime = lifetime
|
|
370
|
-
|
|
371
|
-
target_sid = session.id if session and session.id else session_id
|
|
372
|
-
self.session = WialonSession(sid=target_sid)
|
|
373
|
-
if not target_sid or not self.validate_session():
|
|
374
|
-
self.session.login(token=self.token)
|
|
375
|
-
|
|
376
|
-
def check_active(self) -> bool:
|
|
377
|
-
"""
|
|
378
|
-
Checks if :py:attr:`session` is active.
|
|
379
|
-
|
|
380
|
-
:returns: Whether or not :py:attr:`session` is an active Wialon session.
|
|
381
|
-
:rtype: :py:obj:`bool`
|
|
382
|
-
|
|
383
|
-
"""
|
|
384
|
-
if not self.session.id:
|
|
385
|
-
return False
|
|
386
|
-
if not self.session.wialon_api.last_call_datetime:
|
|
387
|
-
return self.validate_session()
|
|
388
|
-
|
|
389
|
-
now = datetime.datetime.now()
|
|
390
|
-
last_call = self.session.wialon_api.last_call_datetime
|
|
391
|
-
session_expiry = last_call + datetime.timedelta(seconds=self.lifetime)
|
|
392
|
-
|
|
393
|
-
if now > session_expiry:
|
|
394
|
-
return False
|
|
395
|
-
return self.validate_session()
|
|
396
|
-
|
|
397
|
-
def get_session(self, sid: str | None = None) -> WialonSession:
|
|
398
|
-
"""
|
|
399
|
-
Returns a valid Wialon API session.
|
|
400
|
-
|
|
401
|
-
If ``sid`` is provided, tries to continue the session and return it.
|
|
402
|
-
|
|
403
|
-
:param sid: A Wialon API session id.
|
|
404
|
-
:type sid: :py:obj:`str` | :py:obj:`None`
|
|
405
|
-
:returns: A valid Wialon API session.
|
|
406
|
-
:rtype: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
407
|
-
|
|
408
|
-
"""
|
|
409
|
-
if sid is not None:
|
|
410
|
-
self.session = WialonSession(sid=sid)
|
|
411
|
-
if not self.validate_session():
|
|
412
|
-
self.session = WialonSession()
|
|
413
|
-
self.session.login(token=self.token)
|
|
414
|
-
return self.session
|
|
415
|
-
if not self.check_active():
|
|
416
|
-
self.session = WialonSession()
|
|
417
|
-
self.session.login(token=self.token)
|
|
418
|
-
return self.session
|
|
419
|
-
|
|
420
|
-
def validate_session(self) -> bool:
|
|
421
|
-
"""
|
|
422
|
-
Tries to make a Wialon API request and returns whether or not it was successful.
|
|
423
|
-
|
|
424
|
-
:returns: Whether or not validation Wialon API call was successful.
|
|
425
|
-
:rtype: :py:obj:`bool`
|
|
426
|
-
|
|
427
|
-
"""
|
|
428
|
-
try:
|
|
429
|
-
result = self.session.wialon_api.core_duplicate()
|
|
430
|
-
return result is not None
|
|
431
|
-
except wialon.api.WialonError:
|
|
432
|
-
return False
|
|
433
|
-
|
|
434
|
-
@property
|
|
435
|
-
def sid(self) -> str | None:
|
|
436
|
-
"""
|
|
437
|
-
Current Wialon session id.
|
|
438
|
-
|
|
439
|
-
:type: :py:obj:`str` | :py:obj:`None`
|
|
440
|
-
|
|
441
|
-
"""
|
|
442
|
-
return self.session.wialon_api.sid if self.session else None
|
|
443
|
-
|
|
444
|
-
@sid.setter
|
|
445
|
-
def sid(self, other: str) -> None:
|
|
446
|
-
"""
|
|
447
|
-
Sets :py:attr:`sid` to ``other``.
|
|
448
|
-
|
|
449
|
-
:param other: A Wialon session id.
|
|
450
|
-
:type other: :py:obj:`str`
|
|
451
|
-
:returns: Nothing.
|
|
452
|
-
:rtype: :py:obj:`None`
|
|
453
|
-
|
|
454
|
-
"""
|
|
455
|
-
if self.session:
|
|
456
|
-
self.session.wialon_api.sid = other
|
|
457
|
-
else:
|
|
458
|
-
self.session = WialonSession(sid=other)
|
|
459
|
-
|
|
460
|
-
@property
|
|
461
|
-
def token(self) -> str:
|
|
462
|
-
"""
|
|
463
|
-
A Wialon API token.
|
|
464
|
-
|
|
465
|
-
:type: :py:obj:`str`
|
|
466
|
-
|
|
467
|
-
"""
|
|
468
|
-
return str(self._token)
|
|
469
|
-
|
|
470
|
-
@token.setter
|
|
471
|
-
def token(self, other: str | None) -> None:
|
|
472
|
-
"""
|
|
473
|
-
Sets :py:attr:`token` to ``other`` if provided.
|
|
474
|
-
|
|
475
|
-
If ``other`` isn't provided, instead sets :py:attr:`token` to :confval:`WIALON_TOKEN`.
|
|
476
|
-
|
|
477
|
-
:param other: A Wialon API token.
|
|
478
|
-
:type other: :py:obj:`str` | :py:obj:`None`
|
|
479
|
-
:returns: Nothing.
|
|
480
|
-
:rtype: :py:obj:`None`
|
|
481
|
-
|
|
482
|
-
"""
|
|
483
|
-
self._token = other if other is not None else settings.WIALON_TOKEN
|
|
484
|
-
|
|
485
|
-
@property
|
|
486
|
-
def session(self) -> WialonSession:
|
|
487
|
-
"""
|
|
488
|
-
A Wialon API session.
|
|
489
|
-
|
|
490
|
-
:type: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
491
|
-
|
|
492
|
-
"""
|
|
493
|
-
return self._session
|
|
494
|
-
|
|
495
|
-
@session.setter
|
|
496
|
-
def session(self, other: WialonSession) -> None:
|
|
497
|
-
"""
|
|
498
|
-
Sets :py:attr:`session` to ``other``.
|
|
499
|
-
|
|
500
|
-
:param other: A Wialon session.
|
|
501
|
-
:type other: :py:obj:`~terminusgps.wialon.session.WialonSession`
|
|
502
|
-
:returns: Nothing.
|
|
503
|
-
:rtype: :py:obj:`None`
|
|
504
|
-
|
|
505
|
-
"""
|
|
506
|
-
self._session = other
|
|
507
|
-
|
|
508
|
-
@property
|
|
509
|
-
def lifetime(self) -> int:
|
|
510
|
-
"""
|
|
511
|
-
How long in seconds a Wialon session can live for.
|
|
512
|
-
|
|
513
|
-
:type: :py:obj:`int`
|
|
514
|
-
:value: :py:obj:`500`
|
|
515
|
-
|
|
516
|
-
"""
|
|
517
|
-
return self._lifetime
|
|
518
|
-
|
|
519
|
-
@lifetime.setter
|
|
520
|
-
def lifetime(self, other: int) -> None:
|
|
521
|
-
"""
|
|
522
|
-
Sets :py:attr:`lifetime` to ``other``.
|
|
523
|
-
|
|
524
|
-
:param other: An integer.
|
|
525
|
-
:type other: :py:obj:`int`
|
|
526
|
-
:returns: Nothing.
|
|
527
|
-
:rtype: :py:obj:`None`
|
|
528
|
-
|
|
529
|
-
"""
|
|
530
|
-
self._lifetime = other
|
|
File without changes
|
{python_terminusgps-37.3.0.dist-info → python_terminusgps-37.4.0.dist-info}/licenses/COPYING
RENAMED
|
File without changes
|