Skip to content

Commit a08e278

Browse files
author
Othman Moumni Abdou
authored
PyDataMailBox #1 Add getdata method (#2)
1 parent b980105 commit a08e278

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## History
22

3+
### 0.2.2
4+
5+
- Add `getdata` method
6+
37
### 0.2.1
48

59
- Add global request timeout

docs/home.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ client.getewon(name="test")
2525
# Iterate on all the data by chunk
2626
for data in client.iterate_syncdata():
2727
pass
28+
29+
# get tag dat for a given period
30+
client.getdata(1, 1, '2021-07-15T12:30:20', '2021-07-15T12:30:33')
2831
```

pydatamailbox/client.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,39 @@ def syncdata(self, last_transaction_id=None):
126126
data["lastTransactionId"] = last_transaction_id
127127
return self._request(url=self._build_url("syncdata"), data=data)
128128

129+
def getdata(self, ewon_id, tag_id, from_ts, to_ts, limit=None):
130+
"""
131+
``getdata`` is used as a “one-shot” request to retrieve filtered data based on specific
132+
criteria. It is not destined to grab historical data with the same timestamp or enormous
133+
data involving the use of the moreData filter.
134+
135+
:param ewon_id: The ID of the single Ewon gateway for which data from DataMailbox is requested.
136+
:param tag_id: ID of the single tag for which data from DataMailbox is requested.
137+
:param from_ts: Timestamp after which data should be returned. No data older than this time stamp will
138+
be sent in ISO format.
139+
:param to_ts: Timestamp before which data should be returned. No data newer than this time stamp
140+
will be sent in ISO format
141+
:param limit: The maximum amount of historical data returned.
142+
The historical data is the historical tag values but also the historical alarms. If you set the
143+
limit to 4, the response will consist in 4 historical tag values and 4 historical alarms (if
144+
available) for each tag of each Ewon gateway allowed bu the Talk2M token.
145+
If the size of the historical data saved in the DataMailbox exceeds this limit, only the
146+
oldest historical data will be returned and the result contains a moreDataAvailable value
147+
indicating that more data is available on the server.
148+
If limit is not used or is too high, the DataMailbox uses a limit pre-defined in the system
149+
(server-side).
150+
"""
151+
data = {
152+
**self.data,
153+
"ewonId": ewon_id,
154+
"tagId": tag_id,
155+
"from": from_ts,
156+
"to": to_ts,
157+
}
158+
if limit:
159+
data["limit"] = limit
160+
return self._request(url=self._build_url("getdata"), data=data)
161+
129162
def iterate_syncdata(self, last_transaction_id=None):
130163
"""
131164
Returns an iterator on syncdata.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
test_suite="tests",
4343
tests_require=test_requirements,
4444
url="https://github.com/optimdata/pydatamailbox",
45-
version="0.2.1",
45+
version="0.2.2",
4646
zip_safe=False,
4747
)

tests/test_pydatamailbox.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ def syncdata(request, context):
4141
"moreDataAvailable": "lastTransactionId" not in request._request.body,
4242
}
4343

44+
def getdata(request, context):
45+
return {
46+
"ewons": [
47+
{
48+
**ewon,
49+
"tags": [
50+
{
51+
**tag,
52+
"history": [
53+
{"date": "2021-07-15T12:30:22Z", "value": 1},
54+
{"date": "2021-07-15T12:30:28Z", "value": 0},
55+
{"date": "2021-07-15T12:30:29Z", "value": 1},
56+
],
57+
}
58+
],
59+
"lastSynchroDate": "2021-07-16T14:59:40Z",
60+
"timeZone": "Europe/Paris",
61+
}
62+
],
63+
"success": True,
64+
}
65+
4466
ewon = {"id": 1, "name": "test"}
4567
tag = {
4668
"id": 1,
@@ -85,6 +107,7 @@ def syncdata(request, context):
85107
},
86108
)
87109
self.post("https://data.talk2m.com/syncdata", json=syncdata)
110+
self.post("https://data.talk2m.com/getdata", json=getdata)
88111
self.post(
89112
"https://m2web.talk2m.com/t2mapi/getaccountinfo",
90113
json={
@@ -150,6 +173,7 @@ def test_datamailbox():
150173
assert client.syncdata()
151174
assert client.syncdata(last_transaction_id=1)
152175
assert list(client.iterate_syncdata())
176+
assert client.getdata(1, 1, "2021-07-15T12:30:20", "2021-07-15T12:30:33")
153177
with pytest.raises(DataMailboxArgsError):
154178
client.getewon()
155179

0 commit comments

Comments
 (0)