Skip to content

Commit f3b82c4

Browse files
committed
Updated documentation for metrics
1 parent c0ac978 commit f3b82c4

1 file changed

Lines changed: 59 additions & 5 deletions

File tree

README.md

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ You can install the library by running `pip install openpaygo` or adding `openpa
4444

4545
You can use the `generate_token()` function to generate an OpenPAYGOToken Token. The function takes the following parameters, and they should match the configuration in the hardware of the device:
4646

47-
- `secret_key` (required): The secret key of the device. Must be passed as an hexadecimal string (with 32 characters).
47+
- `secret_key` (required): The secret key of the device. Must be passed as an hexadecimal string with 32 characters (e.g. `dac86b1a29ab82edc5fbbc41ec9530f6`).
4848
- `count` (required): The token count used to make the last token.
4949
- `value` (optional): The value to be passed in the token (typically the number of days of activation). Optional if the `token_type` is Disable PAYG or Counter Sync. The value must be between 0 and 995.
5050
- `token_type` (optional): Used to set the type of token (default is Add Time). Token types can be found in the `TokenType` class: ADD_TIME, SET_TIME, DISABLE_PAYG, COUNTER_SYNC.
@@ -100,7 +100,7 @@ device.save() # We save the new count that we set for the device
100100
You can use the `decode_token()` function to generate an OpenPAYGOToken Token. The function takes the following parameters, and they should match the configuration in the hardware of the device:
101101

102102
- `token` (required): The token that was given by the user, as a string
103-
- `secret_key` (required): The secret key of the device
103+
- `secret_key` (required): The secret key of the device as a string with 32 hexadecimal characters (e.g. `dac86b1a29ab82edc5fbbc41ec9530f6`)
104104
- `count` (required): The token count of the last valid token. When a device is new, this is 1.
105105
- `used_counts` (optional): An array of recently used token counts, as returned by the function itself after the last valid token was decoded. This allows for handling unordered token entry.
106106
- `starting_code` (optional): If not provided, it is generated according to the method defined in the standard (SipHash-2-4 of the key, transformed to digit by the same method as the token generation).
@@ -164,19 +164,73 @@ elif token_type == TokenType.INVALID:
164164
You can use the `MetricsRequestHandler` object to create a new OpenPAYGO Metrics request from start to finish. It accepts the following initial inputs:
165165
- `serial_number` (required): The serial number of the device
166166
- `data_format` (optional): The data format, provided as dictionnary matching the data format object specifications.
167-
- `secret_key` (optional): The secret key provided as a string containing 32 hexadecimal characters. Required if `auth_method` is defined.
167+
- `secret_key` (optional): The secret key provided as a string containing 32 hexadecimal characters (e.g. `dac86b1a29ab82edc5fbbc41ec9530f6`). Required if `auth_method` is defined.
168168
- `auth_method` (optional): One of the auth method contained in the `AuthMethod` class.
169169

170170
It provides the following methods:
171171
- `set_timestamp(timestamp)`: Used to set the `timestamp` of the request.
172172
- `set_request_count(request_count)`: Used to set the `request_count` of the request.
173+
- `set_data(data)`: Used to set the `data` of the request, should be set in simple format as a dictionnay.
174+
- `set_historical_data(data)`: Used to set the `historical_data` of the request, should be set in simple format as a dictionnary. The data is assumed to be separated by the `historical_data_interval` unless an explicit timestamp is provided.
175+
- `get_simple_request_payload()`: Returns the payload in simple format as a string containing JSON and including the authentication signature.
176+
- `get_condensed_request_payload()`: Returns the payload in condensed format as a string containing JSON and including the authentication signature. It requires `data_format` to be set. The data is automatically condensed from the set data and the data format and the signature is automatically generated.
177+
178+
179+
**Example - Full Request flow from device side:**
180+
181+
```python
182+
from openpaygo import MetricsRequestHandler, AuthMethod
183+
import requests
184+
185+
# We assume the users enters a token and that the device state is saved in my_device_state
186+
...
187+
188+
metrics_request = MetricsRequestHandler(
189+
serial_number=my_device_state.serial_number,
190+
secret_key=my_device_state.secret_key,
191+
data_format=my_device_state.data_format,
192+
auth_method=AuthMethod.RECURSIVE_DATA_AUTH
193+
)
194+
195+
metrics_requestset_timestamp(1611583070)
196+
metrics_requestset_data({
197+
"token_count": 3,
198+
"tampered": False,
199+
"firmware_version": "1.2.3"
200+
})
201+
# Here we assume that the data we send is separated by 60 seconds as per the data format
202+
metrics_requestset_historical_data([
203+
{
204+
"panel_voltage": 12.31,
205+
"battery_voltage": 12.32,
206+
"panel_current": 1.23,
207+
"battery_current": -1.23,
208+
},
209+
{
210+
"panel_voltage": 12.30,
211+
"battery_voltage": 12.31,
212+
"panel_current": 1.22,
213+
"battery_current": -1.21,
214+
}
215+
])
216+
payload = metrics_requestget_condensed_request_payload()
217+
218+
# We can now proceed to send the payload to the URL
219+
# It looks something like `{"sn":"aaa111222","df":1234,"ts":1611583070,"d":[3,false,"1.2.3"],"hd":[[12.31,12.32,1.23,-1.23],[12.3,12.31,1.22,-1.21]],"a":"raa5cb1fda302cf94e"}`
220+
response = requests.post('https://<base_url>/dd', data=payload, headers={'Content-Type':'application/json'})
221+
try:
222+
response.json().get('tkl', [])
223+
for tokens in tkl:
224+
# Here we decode the tokens received from the server and apply them (see example above)
225+
...
226+
```
173227

174228

175229
### Handling a Request and Generating a Response (Server Side)
176230

177231
You can use the `MetricsResponseHandler` object to process your OpenPAYGO Metrics request from start to finish. It accepts the following initial inputs:
178232
- `metrics_payload` (required): The OpenPAYGO Metrics payload, as a string containing the JSON payload.
179-
- `secret_key` (optional): The secret key provided as a string containing 32 hexadecimal characters
233+
- `secret_key` (optional): The secret key provided as a string containing 32 hexadecimal characters (e.g. `dac86b1a29ab82edc5fbbc41ec9530f6`)
180234
- `data_format` (optional): The data format, provided as dictionnary matching the data format object specifications.
181235

182236
It provides the following methods:
@@ -191,7 +245,7 @@ It provides the following methods:
191245
- `get_answer()`: Will return the answer as a string based on the request and the data added to answer, it will automatically handle the authentication and fomatting.
192246

193247

194-
**Example - Full Request flow:**
248+
**Example - Full Request flow from server side:**
195249

196250
```python
197251
from openpaygo import MetricsHandler

0 commit comments

Comments
 (0)