-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_script.py
333 lines (271 loc) · 18.4 KB
/
example_script.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import time
from agrirouter.api.enums import CapabilityType
from agrirouter.api.env import Qa
from agrirouter.service.parameter.authorization import AuthUrlParameter
from agrirouter.service.parameter.onboarding import OnboardParameters
from agrirouter.service.authorization import AuthorizationService
from agrirouter.service.dto.response.messaging import OnboardResponse
from agrirouter.service.messaging.common import MqttMessagingService, HttpMessagingService
from agrirouter.service.messaging.message_sending import ListEndpointsService, CapabilitiesService, SubscriptionService, \
QueryHeaderService
from agrirouter.service.parameter.messaging import QueryHeaderParameters, ListEndpointsParameters, \
CapabilitiesParameters, SubscriptionParameters
from agrirouter.service.onboarding import SecuredOnboardingService
from agrirouter.api.enums import Gateways
from agrirouter.generated.messaging.request.payload.endpoint.subscription_pb2 import Subscription
from agrirouter.generated.messaging.request.payload.endpoint.capabilities_pb2 import CapabilitySpecification
from agrirouter.util.uuid_util import UUIDUtil
from google.protobuf.timestamp_pb2 import Timestamp
from agrirouter.generated.messaging.request.payload.account.endpoints_pb2 import ListEndpointsQuery
from agrirouter.generated.messaging.request.payload.feed.feed_requests_pb2 import ValidityPeriod
public_key = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzGt41/+kSOTlO1sJvLIN
6RAFaOn6GiCNX/Ju0oVT4VMDHfuQMI5t9+ZgBxFmUhtY5/eykQfYJVGac/cy5xyk
F/1xpMzltK7pfo7XZpfHjkHLPUOeaHW0zE+g2vopQOARKE5LSguCBUhdtfFuiheR
IP0EU+MtEQDhlfiqYLAJkAvZHluCH9q6hawn0t/G873jlzsrXBqIgKboXqyz1lRE
SvMyqX04Xwaq1CgAZjHXBVWvbuOriCR0P2n13/nkCgBgLd/ORwVilb4GQDXkkCSg
uOVcRU3s/KG/OVJTonHVlLvDzBA5GLrpZMpzC4EfzXBM98s4Vj6IOAIQeY84Sppj
qwIDAQAB
-----END PUBLIC KEY-----"""
private_key = """-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDMa3jX/6RI5OU7
Wwm8sg3pEAVo6foaII1f8m7ShVPhUwMd+5Awjm335mAHEWZSG1jn97KRB9glUZpz
9zLnHKQX/XGkzOW0rul+jtdml8eOQcs9Q55odbTMT6Da+ilA4BEoTktKC4IFSF21
8W6KF5Eg/QRT4y0RAOGV+KpgsAmQC9keW4If2rqFrCfS38bzveOXOytcGoiApuhe
rLPWVERK8zKpfThfBqrUKABmMdcFVa9u46uIJHQ/afXf+eQKAGAt385HBWKVvgZA
NeSQJKC45VxFTez8ob85UlOicdWUu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CKuV5ZO+XTmAKREA7Gj7XKQ7XGU1sldf+/Q5AMkXgQKBgQC4lXL9zLV/vfWUTPSR
qlGcS2+WYtjWPapDZa+7zlxGdPgOTri4nJO69Bs9ReLlzsYKSBihfpWPxcl9sS65
KFrlBkR/vzKYjCFXB6cmMP61mUrgGQRoYJQBetAyEiXZL3zjt1R/Dndk0kHkVmHr
HjmgzBRxXFy5uph6Ue6dxyszaA==
-----END PRIVATE KEY-----"""
onboarding_response_mqtt_data = {
"deviceAlternateId": "2145df0e-3451-46cb-bf23-23191af66fce",
"capabilityAlternateId": "523e4623-68d2-43d4-a0cc-e2ada2f68b5e",
"sensorAlternateId": "1489638c-7bed-4205-ad77-8d11efdc779f",
"connectionCriteria": {
"gatewayId": "2",
"host": "dke-qa.eu10.cp.iot.sap",
"port": 8883,
"clientId": "2145df0e-3451-46cb-bf23-23191af66fce",
"measures": "measures/2145df0e-3451-46cb-bf23-23191af66fce",
"commands": "commands/2145df0e-3451-46cb-bf23-23191af66fce"
},
"authentication": {
"type": "PEM",
"secret": "JNKdNg8R0lwmFgvrUfOCc7inebr0h?!7Z9wL",
"certificate": "-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIE6zAdBgoqhkiG9w0BDAEDMA8ECMkL85F+LbPbAgMCAAAEggTI1CmRlnDUStBv\nTycvaRVFMCk1OuynhiOYRF6HBFFXBCxWKZa3WqTShLdf9iCel/NgtdZIiQsoD1LL\nMxVyh8pWAfLQ+pDJLvM6suQjHALt8dW5iTeCZ7R1gzFvPJ+xnDGFFytN7HmGSvHM\nQbcCOuEeIu8U6ENa6/+WmUwK9/ZMkLNqDHVKEGpI+lSJs8JWEE+S3Klmsxuq0dvz\nh6o3V7RKFwMfUZOQLHezGBDjLfEBdP+d2G87CY+LSzinL8pFhLwyrXFKfYWYoT0m\n5PkDdjfiVq3SJIUoQWnGrjaVVw4TV3WSxmhQnWbDwOQydr8DAiBxDMYoeK3rePpC\nwh6KATnBrovq1icqjonYDE0T+3Rs2SU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C6cOs8NAnc\nyVmnvLDu8RUWu9pWkqFHhMvSqdkUCTYORZ9mUTm/Kmv6ss2NaYT4uUBZTskwnAa9\nFLdj+DV2NG0OQl3NYr8CAwEAAaOB0jCBzzBIBgNVHR8EQTA/MD2gO6A5hjdodHRw\nczovL3Rjcy5teXNhcC5jb20vY3JsL1RydXN0Q29tbXVuaXR5SUkvU0FQSW9UQ0Eu\nY3JsMAwGA1UdEwEB/wQCMAAwJQYDVR0SBB4wHIYaaHR0cDovL3NlcnZpY2Uuc2Fw\nLmNvbS9UQ1MwDgYDVR0PAQH/BAQDAgbAMB0GA1UdDgQWBBSRf8DUjowgQ+6amVIs\njd7zM7VWqjAfBgNVHSMEGDAWgBSVt7P1WN7VtLNYRuDypsl4Tr0tdTANBgkqhkiG\n9w0BAQsFAAOCAQEARzSc9GLpSU3pRJPIfgadHrZ+2KQsPsQ1/fLlASlt4V1Rlxn7\n/tn0gk3sP0X5/TrkO+N0kx1qrLarxWSDiVfaXoPa6Lit30SBPnPLUPPPZeTJOz5r\nTW9PkPPuC39GlM1biVoil2cLZrTr9DMSUoBvR4IVKQoJveQsLwn7Ea+SDPE0uvZV\nbDN6UPGZ2yIiCXO1MODJ6r3A4EDD2MArGgfhGdbvJNAY36ShFJhzfzi0t8linEAA\nxh0vcaEEIkVeEiwiguyGWB69X88cjZ0Q5cCf0r6iu3oQnB57uM5TW12OwXQN1NpQ\neK3EMFSoM6BYJu/3B8TXhNmpNBvD7KYozw9XaA==\n-----END CERTIFICATE-----\n"
}
}
application_id = "8c947a45-c57d-42d2-affc-206e21d63a50" # # store here your application id. You can find it in AR UI
certification_version_id = "edd5d6b7-45bb-4471-898e-ff9c2a7bf56f" # # store here your certification version id. You can find it in AR UI
def example_auth():
print("Authorization...\n")
auth_params = AuthUrlParameter(application_id=application_id, response_type="onboard")
auth_client = AuthorizationService(Qa(), public_key=public_key, private_key=private_key)
auth_url = auth_client.get_auth_request_url(
auth_params) # use this url to authorize the user as described at https://docs.my-agrirouter.com/agrirouter-interface-documentation/latest/integration/authorization.html#perform-authorization
print(f"auth_url={auth_url}")
auth_result_url = input(
"Enter auth_url (the url the user was redirected to after his authorization, see above): ") # the url the user was redirected to after his authorization.
auth_response = auth_client.extract_auth_response(
auth_result_url) # auth_response contains the results of the auth process
auth_client.verify_auth_response(auth_response) # you may verify auth_response to ensure answer was from AR
print(
f"auth_response is successful: {auth_response.is_successful}") # True if user accepted application, False if he rejected
print(
f"auth_response is valid: {auth_response.is_valid}") # Result of verification, if False, response was not validated by public key. Doesn't indicate the auth was successfull. Accessible only after response verifying
# Get dict containing data from auth process you will use for futher communication.
# If auth was rejected, contains {"error"} key.
# If auth was accepted, contains {signature, state, token, credentials{account, expires, regcode}} keys
# Even if response verifying was not processed or failed, the results will be returned. But in that case you act on your risk.
auth_data = auth_response.get_auth_result()
print(f"auth_data: {auth_data}")
return auth_data
def example_onboarding(gateway_id):
auth_data = example_auth()
print("Onboarding...\n")
id_ = "urn:myapp:snr000032dd34" # just unique
time_zone = "+03:00"
onboarding_client = SecuredOnboardingService(Qa(), public_key=public_key, private_key=private_key)
onboarding_parameters = OnboardParameters(id_=id_, application_id=application_id,
certification_version_id=certification_version_id,
gateway_id=gateway_id, time_zone=time_zone,
reg_code=auth_data.get_decoded_token().regcode)
onboarding_verifying_response = onboarding_client.verify(onboarding_parameters)
print(f"onboarding_verifying_response.status_code: {onboarding_verifying_response.status_code}")
print(f"onboarding_verifying_response.text: {onboarding_verifying_response.text}")
onboarding_response = onboarding_client.onboard(onboarding_parameters)
print(f"onboarding_response.status_code: {onboarding_response.status_code}")
print(f"onboarding_response.text: {onboarding_response.text}")
return onboarding_response
def example_list_endpoints_mqtt(onboarding_response_data, foo):
onboarding_response = OnboardResponse()
onboarding_response.json_deserialize(onboarding_response_data)
messaging_service = MqttMessagingService(
onboarding_response=onboarding_response,
on_message_callback=foo
)
list_endpoint_parameters = ListEndpointsParameters(
technical_message_type=CapabilityType.ISO_11783_TASK_DATA_ZIP.value,
direction=ListEndpointsQuery.Direction.Value("SEND_RECEIVE"),
filtered=False,
onboarding_response=onboarding_response,
application_message_id=UUIDUtil.new_uuid(),
application_message_seq_no=1,
)
list_endpoint_service = ListEndpointsService(messaging_service)
messaging_result = list_endpoint_service.send(list_endpoint_parameters)
print("Sent message: ", messaging_result.messages_ids)
# Is needed for waiting of messaging responses from outbox
while True:
time.sleep(1)
def example_set_capabilities(onboarding_response_data, mqtt_message_callback):
onboarding_response = OnboardResponse()
onboarding_response.json_deserialize(onboarding_response_data)
messaging_service = MqttMessagingService(
onboarding_response=onboarding_response,
on_message_callback=mqtt_message_callback
)
capabilities_parameters = CapabilitiesParameters(
onboarding_response=onboarding_response,
application_message_id=UUIDUtil.new_uuid(),
application_message_seq_no=1,
application_id=application_id,
certification_version_id=certification_version_id,
capability_parameters=[
CapabilitySpecification.Capability(technical_message_type=CapabilityType.ISO_11783_TASK_DATA_ZIP.value,
direction="SEND_RECEIVE")
],
enable_push_notification=True,
)
capabilities_service = CapabilitiesService(messaging_service)
capabilities_service.send(capabilities_parameters)
def example_list_endpoints_http(onboarding_response_data):
onboarding_response = OnboardResponse()
onboarding_response.json_deserialize(onboarding_response_data)
messaging_service = HttpMessagingService()
list_endpoint_parameters = ListEndpointsParameters(
technical_message_type=CapabilityType.ISO_11783_TASK_DATA_ZIP.value,
direction=2,
filtered=False,
onboarding_response=onboarding_response,
application_message_id=UUIDUtil.new_uuid(),
application_message_seq_no=1,
)
list_endpoint_service = ListEndpointsService(messaging_service)
messaging_result = list_endpoint_service.send(list_endpoint_parameters)
print("Sent message: ", messaging_result)
return messaging_result
def example_subscription_http(onboarding_response_data):
onboarding_response = OnboardResponse()
onboarding_response.json_deserialize(onboarding_response_data)
messaging_service = HttpMessagingService()
subscription_service = SubscriptionService(messaging_service)
tmt = CapabilityType.ISO_11783_TASK_DATA_ZIP.value
subscription_item = Subscription.MessageTypeSubscriptionItem(technical_message_type=tmt)
subscription_parameters = SubscriptionParameters(
subscription_items=[subscription_item],
onboarding_response=onboarding_response,
application_message_id=UUIDUtil.new_uuid(),
application_message_seq_no=1,
)
messaging_result = subscription_service.send(subscription_parameters)
print("Sent message: ", messaging_result)
return messaging_result
def example_subscription_mqtt(onboarding_response_data, on_msg_callback):
onboarding_response = OnboardResponse()
onboarding_response.json_deserialize(onboarding_response_data)
messaging_service = MqttMessagingService(onboarding_response, on_message_callback=on_msg_callback)
subscription_service = SubscriptionService(messaging_service)
tmt = CapabilityType.ISO_11783_TASK_DATA_ZIP.value
subscription_item = Subscription.MessageTypeSubscriptionItem(technical_message_type=tmt)
subscription_parameters = SubscriptionParameters(
subscription_items=[subscription_item],
onboarding_response=onboarding_response,
application_message_id=UUIDUtil.new_uuid(),
application_message_seq_no=1,
)
messaging_result = subscription_service.send(subscription_parameters)
print("Sent message: ", messaging_result)
# Is needed for waiting of messaging responses from outbox
while True:
time.sleep(1)
def example_query_header_message_http(onboarding_response_data):
onboarding_response = OnboardResponse()
onboarding_response.json_deserialize(onboarding_response_data)
messaging_service = HttpMessagingService()
query_header_service = QueryHeaderService(messaging_service)
sent_from = Timestamp()
sent_to = Timestamp()
validity_period = ValidityPeriod(sent_from=sent_from, sent_to=sent_to)
query_header_parameters = QueryHeaderParameters(
message_ids=[UUIDUtil.new_uuid(), UUIDUtil.new_uuid()],
senders=[UUIDUtil.new_uuid(), UUIDUtil.new_uuid()],
validity_period=validity_period,
onboarding_response=onboarding_response,
application_message_id=UUIDUtil.new_uuid(),
application_message_seq_no=1,
)
messaging_result = query_header_service.send(query_header_parameters)
print("Sent message: ", messaging_result)
return messaging_result
def example_query_header_message_mqtt(onboarding_response_data, on_msg_callback):
onboarding_response = OnboardResponse()
onboarding_response.json_deserialize(onboarding_response_data)
messaging_service = MqttMessagingService(onboarding_response, on_message_callback=on_msg_callback)
query_header_service = QueryHeaderService(messaging_service)
sent_from = Timestamp()
sent_to = Timestamp()
validity_period = ValidityPeriod(sent_from=sent_from, sent_to=sent_to)
query_header_parameters = QueryHeaderParameters(
message_ids=[UUIDUtil.new_uuid(), UUIDUtil.new_uuid()],
senders=[UUIDUtil.new_uuid(), UUIDUtil.new_uuid()],
validity_period=validity_period,
onboarding_response=onboarding_response,
application_message_id=UUIDUtil.new_uuid(),
application_message_seq_no=1,
)
messaging_result = query_header_service.send(query_header_parameters)
print("Sent message: ", messaging_result)
# Is needed for waiting of messaging responses from outbox
while True:
messaging_service.client.loop()
time.sleep(1)
def on_message_callback(client, userdata, msg):
# Define here the way receiving messages will be processed
from agrirouter.service.messaging.decoding import DecodingService
from agrirouter.api.messages import OutboxMessage
outbox_message = OutboxMessage()
outbox_message.json_deserialize(msg.payload.decode().replace("'", '"'))
print(outbox_message.command.message)
decoded_message = DecodingService.decode_response(outbox_message.command.message)
print(decoded_message.response_envelope)
try:
decoded_details = DecodingService.decode_details(decoded_message.response_payload.details)
print(decoded_details)
except Exception as exc:
print("Error in decoding details: ", exc)
if __name__ == "__main__":
onboarding_response_mqtt = example_onboarding(Gateways.MQTT.value)
example_set_capabilities(onboarding_response_mqtt.json_serialize(), on_message_callback)
example_list_endpoints_mqtt(onboarding_response_mqtt.json_serialize(), on_message_callback)
# or for http
# onboarding_response_mqtt = example_onboarding(GateWays.REST.value)
# example_list_endpoints_http(onboarding_response_mqtt.json_serialize())