-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmindsphereApiCollection.py
More file actions
1096 lines (783 loc) · 42.4 KB
/
Copy pathmindsphereApiCollection.py
File metadata and controls
1096 lines (783 loc) · 42.4 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import time
from pprint import pprint
import traceback
import sys
import modules.readConfig as config
import os
import pdb
#Custom Modules
from modules.apiHandler import wrapApiCall
from modules.helpers import SimpleError
from os.path import join
#######################################
########### MODULE CONFIG #############
#######################################
# The following block loads parameters from the config an provides them in an easy to use way in this modul:
# Instead of config.<parametername> you can just use <parametername> afterwards
thisModule = sys.modules[__name__]
requiredParamters= "useLocalFilesToPreserveMindSphereData, tenantname, logging"
config.setSimpleConfigParametersToModule(thisModule, requiredParamters)
fetchFromFile = useLocalFilesToPreserveMindSphereData
overwriteResultOfAspectImportWithAFakedSuccess = False #THIS SHOULD NOT BE SET TO TRUE!!!
# File paths
assetsFilePath = os.path.join('temp',tenantname + '_assets.txt')
assetTypesFilePath = os.path.join('temp',tenantname + '_assetTypes.txt')
aspectsFilePath = os.path.join('temp',tenantname + '_aspects.txt')
agentsFilePath = os.path.join('temp',tenantname + '_agents.txt')
genericMindSphereObjectMapper = {
"assets" : {"url":"/api/assetmanagement/v3/assets?size=500", "displayName":"Asset", "filepath":assetsFilePath},
"assetTypes" : {"url":"/api/assetmanagement/v3/assettypes?size=500", "displayName":"Asset-Type", "filepath":assetTypesFilePath},
"aspectTypes" : {"url":"/api/assetmanagement/v3/aspecttypes?size=500", "displayName":"Aspect", "filepath":aspectsFilePath}
}
############## GET ####################
#Device related stuff
def getDataModelObjectsFromMindSphere(mindSphereObjectType):
currentObject = genericMindSphereObjectMapper[mindSphereObjectType]
currentFilePath = currentObject["filepath"]
currentApiPath = currentObject["url"]
currentDisplayName = currentObject["displayName"]
if fetchFromFile:
try:
with open(currentFilePath) as json_file:
objectsFromMindSphere = json.load(json_file)
print(f"... loading {currentDisplayName}-Data from local file now")
return objectsFromMindSphere
except FileNotFoundError:
print(f"... loading {currentDisplayName}-Data from MindSphere Cloud now")
except:
traceback.print_exc()
objectsFromMindSphere = []
totalElements = 0
if logging in ("INFO"):
print("Getting page 1 for {}s now".format(currentDisplayName))
response = wrapApiCall(currentApiPath,"GET","{}")
if int(response["responseStatusCode"])>300:
print("Something went wrong with getting {}s from Mindsphere. Exiting now...".format(currentDisplayName))
print(response["responseText"])
exit(-1) #Todo: Error Handling einbauen?!
result = response["result"]
if result["_embedded"][mindSphereObjectType]:
objectsFromMindSphere.extend(result["_embedded"][mindSphereObjectType])
if result["page"]["totalPages"]:
numberOfReturnPages = int(result["page"]["totalPages"])
if result["page"]["totalElements"]:
totalElements = int(result["page"]["totalElements"])
if numberOfReturnPages > 1:
nextResult = result
for x in range(2, numberOfReturnPages + 1):
if logging in ("INFO", "VERBOSE"):
print("Getting page {} for {}s now".format(x,currentDisplayName))
if nextResult["_links"]["next"]["href"]:
nextLink = nextResult["_links"]["next"]["href"]
nextResult = wrapApiCall(nextLink,"GET","{}")["result"]
objectsFromMindSphere.extend(nextResult["_embedded"][mindSphereObjectType])
if nextResult["page"]["totalElements"]:
totalElements = int(nextResult["page"]["totalElements"])
if len(objectsFromMindSphere) != totalElements:
print("Fetched elements ({}) differ from number of elements according to APIs response PAGE-TOTALELEMENTS ({})".format(len(objectsFromMindSphere), totalElements))
print("This bodes ill and therefore ...exiting now")
exit(-1)
if fetchFromFile:
with open(currentFilePath, 'w') as outfile:
json.dump(objectsFromMindSphere, outfile)
return objectsFromMindSphere
def getAssetsFromMindSphere():
simpleError = SimpleError()
assetsFromMindSphere = getDataModelObjectsFromMindSphere('assets')
return (simpleError, assetsFromMindSphere)
def getAssetTypesFromMindSphere():
simpleError = SimpleError()
assetTypesFromMindSphere = getDataModelObjectsFromMindSphere('assetTypes')
return (simpleError, assetTypesFromMindSphere)
def getAspectsFromMindSphere():
simpleError = SimpleError()
aspectsFromMindSphere = getDataModelObjectsFromMindSphere('aspectTypes')
return (simpleError, aspectsFromMindSphere)
#Agent related stuff
def getAgentsFromMindSphere():
# Attention: The Agent Management API is not in scope of assetmanager-Application -> Update 2020 -> this seems to be not true anymore
# This can be reworked with trying to identify agents via the asset's assetType (if the type is core.mcXXX: it is an agent)
simpleError = SimpleError()
if fetchFromFile:
try:
with open(agentsFilePath) as json_file:
print(f"... loading Agent-Data from local file now")
agentsFromMindSphere = json.load(json_file)
return (simpleError,agentsFromMindSphere)
except FileNotFoundError:
print(f"... loading Agent-Data from MindSphere Cloud now")
pass
except:
traceback.print_exc()
agentsFromMindSphere = []
totalAgents = 0
currentPageNumber = 0
result = wrapApiCall("/api/agentmanagement/v3/agents?page="+ str(currentPageNumber) + "&size=100&sort=name,asc","GET","{}")["result"]
if logging in ("INFO"):
print("Getting page {} for Agents now".format(currentPageNumber +1))
if result and "content" in result:
if result["content"]:
agentsFromMindSphere.extend(result["content"])
if result["totalPages"]:
numberOfReturnPages = int(result["totalPages"])
if result["totalElements"]:
totalAgents = int(result["totalElements"])
if numberOfReturnPages > 1:
nextResult = result
for x in range(2, numberOfReturnPages + 1):
currentPageNumber += 1
if logging in ("INFO", "VERBOSE"):
print("Getting page {} for Agents now".format(x))
nextLink = "/api/agentmanagement/v3/agents?page="+ str(currentPageNumber) + "&size=100&sort=name,asc"
nextResult = wrapApiCall(nextLink,"GET","{}")["result"]
agentsFromMindSphere.extend(nextResult["content"])
if nextResult["totalElements"]:
totalAgents = int(nextResult["totalElements"])
if len(agentsFromMindSphere) != totalAgents:
simpleError.addError("Fetched elements ({}) differ from number of elements according to APIs response PAGE-TOTALELEMENTS ({})".format(len(agentsFromMindSphere), totalAgents))
if fetchFromFile:
with open(agentsFilePath, 'w') as outfile:
json.dump(agentsFromMindSphere, outfile)
return (simpleError,agentsFromMindSphere)
def getDatasourceConfigForAgentAsset(agentAsset):
simpleError = SimpleError()
datasourceConfiguration = None
if agentAsset.typeId != "core.mclib":
# wenn es kein Lib Agent ist, schaut das Abrufen der vollen Datasource Konfig so aus
returnValue = wrapApiCall("/api/mindconnectdevicemanagement/v3/devices/"+ agentAsset.assetId + "/dataConfig","GET",assetTypeToDeriveApplicationScope = agentAsset.typeId)
# Der mindconnctdevicemanagement Endpunkt liefert bei mclib Agenten keine Konfig zurück: "Device configuration does not exist for given assetId xy".
# Für die MindConnect Lib-Elemente muss man den Agentmanagement-Endpunkt verwenden. Dieser ginge zwar auch für mcnano und mciot2040 aber man erhält dort nicht die volle Info.
# Deshalb nimmt man für NANO und IoT2040 den mindconnectdevicemanagement-Endpunkt
else:
returnValue = wrapApiCall("/api/agentmanagement/v3/agents/"+ agentAsset.assetId + "/dataSourceConfiguration","GET",assetTypeToDeriveApplicationScope = agentAsset.typeId)
if not returnValue:
print("Something went wrong with getting datasource Config. Result of API call was empty...")
exit(-1)
datasourceConfiguration = returnValue["result"]
return (simpleError,datasourceConfiguration )
def getAllDatapointMappingsForAgentAsset(agentAsset):
simpleError = SimpleError()
#TODO: IMPORTANT: This will currently return maximum 500 (or less, no idea how much are allowed) mapping entries, due to the laziness of the coder.
#In case you have more mappings, this needs some work
datapointMappingConfigurations = None
result = wrapApiCall('/api/mindconnect/v3/dataPointMappings?filter={"agentId":"'+ agentAsset.assetId + '"}&page=0&size=500',"GET",assetTypeToDeriveApplicationScope = agentAsset.typeId)["result"]
if not result:
print("Something went wrong with getting datasource mapping config. Result of API call was empty...maybe this is even okay, if no mappings exist")
exit(-1)
datapointMappingConfigurations = result["content"]
return (simpleError,datapointMappingConfigurations )
def getDeviceConfigurationForAgentAsset(agentAsset):
# This queries something like /api/mindconnectdevicemanagement/v3/devices/<agentAssetID>
# If Agent is Nano or Iot2040 it will return information about Boarding-Status, IP-Configuration, DHCP, Proxy, SerialNumber:
# For MC Lib Elements there won't be a Device-Config
# If you ask for it in case of a MC Lib Asset, you will get a reply "Insufficient scope for this resource" when using session cookies
# When using app credential token for it, you will receive this 'Device configuration does not exist for given assetId'
# Über /api/mindconnectdevicemanagement/v3/devices/<agentAssetID>/firmware/info könnte man auch noch die Firmware abrufen
simpleError = SimpleError()
deviceInformation = None
if agentAsset.typeId != "core.mclib":
result = wrapApiCall("/api/mindconnectdevicemanagement/v3/devices/"+ agentAsset.assetId,"GET",assetTypeToDeriveApplicationScope = agentAsset.typeId)["result"]
if not result:
print("Something went wrong with getting datasource Config. Result of API call was empty...")
exit(-1)
deviceInformation = result
return (simpleError,deviceInformation)
############# CREATE ##################
def createNewAssetInMindSphere(asset):
relatedFileName = assetsFilePath
print(" °°°° Importing Asset '{}' now ... ".format(asset.name))
bodyAsJson = {}
tenantname = config.tenantname
name = asset.name
if asset.typeId not in (None,""):
assetType = asset.typeId
else:
assetType = config.defaultAssetType #If nothing has been provided: Get Default (shouldnt happen at that point)
if not assetType.startswith("core.") and not assetType.startswith(tenantname + "."):
assetTypeWithPrefix = tenantname + "." + assetType #Add TenantPrefix to AssetType
else: #prefix is already existing
assetTypeWithPrefix = assetType
if asset.assetDescription not in (None,""):
assetDescription = asset.assetDescription
else:
assetDescription = config.defaultAssetDescription #If nothing has been provided: Get Default
if asset.parentId not in (None,""):
parentId = asset.parentId
else:
parentId = config.defaultParentId #If nothing has been provided: Get Default
bodyAsJson["name"] = name
bodyAsJson["typeId"] = assetTypeWithPrefix
bodyAsJson["description"] = assetDescription
bodyAsJson["parentId"] = parentId
if logging == "VERBOSE":
print("Trying to import Asset with following body")
print(bodyAsJson)
returnValue = wrapApiCall("/api/assetmanagement/v3/assets", "POST", bodyAsJson)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
if fetchFromFile:
responseAsDict = json.loads(responseText)
asset.assetId = responseAsDict["assetId"] #TODO -> Auch bei anderen Create Prozessen noch die ID und weitere Parameter an die Klasse hängen
try:
with open(relatedFileName) as json_file:
currentFileContent = json.load(json_file)
currentFileContent.append(responseAsDict)
with open(relatedFileName, 'w') as outfile:
json.dump(currentFileContent, outfile)
except Exception:
traceback.print_exc()
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
def createNewAssetTypeInMindSphere(assetType):
relatedFileName = assetTypesFilePath
print(" °°°° Importing AssetType '{}' now ... ".format(assetType.name))
bodyAsJson = {}
#handling of core asset-types should not be needed, since it should never happen, that the importer tries to import a core.assetType ...should... ;)
if assetType.id.startswith("core"):
print("It should never happen, that the importer tries to import a core.asset ...something is wrong here in the program's logic, please do complain somewhere...exiting now...")
exit(-1)
if assetType.description in (None,""):
assetType.description = config.defaultAssetDescription #If nothing has been provided: Get Default
aspectsForImport = []
for aspect in assetType.getAspects():
aspectDict = {"name": aspect.aspectNameWithinAssetTypeContext,"aspectTypeId":aspect.id}
aspectsForImport.append(aspectDict)
if assetType.ancestorOfTypeId in (None,""):
assetType.ancestorOfTypeId = config.defaultParentAssetTypeId
bodyAsJson["name"] = assetType.name
bodyAsJson["id"] = assetType.id
bodyAsJson["parentTypeId"] = assetType.ancestorOfTypeId
bodyAsJson["description"] = assetType.description
bodyAsJson["aspects"] = aspectsForImport
bodyAsJson["instantiable"] = "true"
bodyAsJson["scope"] = "private"
if logging == "VERBOSE":
print("Trying to import AssetType with following body")
returnValue = wrapApiCall("/api/assetmanagement/v3/assettypes/" + assetType.id, "PUT", bodyAsJson)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
if fetchFromFile:
responseAsDict = json.loads(responseText)
try:
with open(relatedFileName) as json_file:
currentFileContent = json.load(json_file)
currentFileContent.append(responseAsDict)
with open(relatedFileName, 'w') as outfile:
json.dump(currentFileContent, outfile)
except Exception:
traceback.print_exc()
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] = statusCode
returnDict["responseText"] = responseText
return returnDict
def createNewAspectInMindSphere(aspect):
relatedFileName = aspectsFilePath
print(" °°°° Importing Aspect '{}' now ... ".format(aspect.name))
bodyAsJson = {}
if aspect.description not in (None,""):
aspectDescription = aspect.description
else:
aspectDescription = config.defaultAspectDescription #If nothing has been provided: Get Default
variablesForImport = []
for variable in aspect.getVariables():
variableDict = {"name": variable.name,"dataType": variable.dataType,"unit": variable.unit}
if variable.dataType.lower() == "string":
variableDict["length"] = config.maxLengthForStringVariableCreation
variablesForImport.append(variableDict)
bodyAsJson["name"] = aspect.name
bodyAsJson["description"] = aspectDescription
bodyAsJson["variables"] = variablesForImport
bodyAsJson["scope"] = aspect.scope
bodyAsJson["category"] = aspect.category
if logging == "VERBOSE":
print("Trying to import aspect with following body")
print(bodyAsJson)
returnValue = wrapApiCall("/api/assetmanagement/v3/aspecttypes/" + aspect.id, "PUT", bodyAsJson)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
if fetchFromFile:
responseAsDict = json.loads(responseText)
try:
with open(relatedFileName) as json_file:
currentFileContent = json.load(json_file)
currentFileContent.append(responseAsDict)
with open(relatedFileName, 'w') as outfile:
json.dump(currentFileContent, outfile)
except Exception:
traceback.print_exc()
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] = statusCode
returnDict["responseText"] = responseText
return returnDict
def createDataSourceDict(dataSource, typeId):
dataSourceDict = {}
dataPointList = []
dataSourceDict["name"] = dataSource.name
dataSourceDict["description"] = dataSource.description
if typeId == "core.mclib":
"""Style of JSON:
{"name":"asdf","description":"bsdf",
"dataPoints":
[
{"id":"1606389468639",
"name":"goebel",
"description":"boebl",
"type":"DOUBLE",
"unit":"rudi",
"customData":null}
]
,
"customData":null}
"""
dataSourceDict["customData"] = dataSource.customData
for dataPoint in dataSource.dataPoints:
dataPointDict = {}
dataPointDict["customData"] = dataPoint.customData
dataPointDict["name"] = dataPoint.name
dataPointDict["unit"] = dataPoint.unit
dataPointDict["type"] = dataPoint.dataType
dataPointDict["description"] = dataPoint.description
dataPointDict["id"] = dataPoint.dataPointId #This is an arbitrary identifier that should be unique and is used for datapoint mappings later.
dataPointList.append(dataPointDict)
else: # Mind Connect Nano or Iot204
"""Style of JSON:
{
"id":"8933556c-0e06-4995-a1e7-0104933c4821",
"name":"asdf",
"description":"",
"protocol":"S7",
"readCycleInSeconds":"60",
"protocolData":{"ipAddress":"127.0.0.4"},
"dataPoints":
[
{
"id": "984866c4-d2c6-4bcf-83e4-3f4a0b9bd829",
"dataPointId": "9dc409f108d24",
"name": "Ready",
"description": "",
"unit": "-",
"dataType": "BOOLEAN",
"dataPointData": {
"address": "DB435.DBX396.1",
"hysteresis": null,
"onDataChanged": false
}
]
}
"""
#dataSourceDict["id"] = "" #The id will be left out from the datapoint-dictionay and it wil be auto generated
protocol = dataSource.protocol
dataSourceDict["protocolData"] = {}
dataSourceDict["protocol"] = protocol
dataSourceDict["readCycleInSeconds"] = dataSource.readCycleInSeconds
if protocol == "OPCUA":
dataSourceDict["protocolData"]["opcUaServerName"] =dataSource.opcUaServerName
dataSourceDict["protocolData"]["opcUaServerAddress"]= dataSource.opcUaServerAddress
dataSourceDict["protocolData"]["opcUaServerIPAddress"]= dataSource.opcUaServerIPAddress
dataSourceDict["protocolData"]["opcUaCertificateMetadata"]= dataSource.opcUaCertificateMetadata
dataSourceDict["protocolData"]["opcUaCertificate"]= {}
dataSourceDict["protocolData"]["opcUaAuthenticationType"] = None
dataSourceDict["protocolData"]["opcUaSecurityMode"] = dataSource.opcUaSecurityMode
dataSourceDict["protocolData"]["opcUaUsername"] = dataSource.opcUaUsername
dataSourceDict["protocolData"]["opcUaPassword"] = dataSource.opcUaPassword
dataSourceDict["protocolData"]["enableEvents"] = False
if protocol == "S7":
dataSourceDict["protocolData"]["ipAddress"] = dataSource.ipAddress
dataSourceDict["protocolData"]["manualRackAndSlot"] = dataSource.manualRackAndSlot
dataSourceDict["protocolData"]["rackNumber"] = dataSource.rackNumber
dataSourceDict["protocolData"]["slotNumber"] = dataSource.slotNumber
for dataPoint in dataSource.dataPoints:
dataPointDict = {}
dataPointDict["dataPointId"] = dataPoint.dataPointId # This is an arbitrary identifier that should be unique and is used for datapoint mappings later.
dataPointDict["name"] = dataPoint.name
dataPointDict["unit"] = dataPoint.unit
dataPointDict["dataType"] = dataPoint.dataType
dataPointDict["description"] = dataPoint.description
dataPointDict["dataPointData"] = {
"address": dataPoint.address,
"hysteresis": dataPoint.hysteresis,
"onDataChanged": dataPoint.onDataChanged
}
if protocol == "S7" and dataPoint.acquisitionType:
dataPointDict["dataPointData"]["acquisitionType"] = dataPoint.acquisitionType
dataPointList.append(dataPointDict)
dataSourceDict["dataPoints"] = dataPointList
return dataSourceDict
#######################################
def assignReceivedDatapointIds(agentAsset, responseAsDict):
# Probably this is not needed at all, since mappings are only relating to the dataPointIds which are given through the user and which are therefore already known to the python-datamodel
pass
#######################################
#######################################
def createCompletelyNewDataSourcesAndDatapointDefinition(agentAsset):
latestETag = None
simpleError,dataSourceConfiguration = getDatasourceConfigForAgentAsset(agentAsset)
listWithAllDataSources = []
if agentAsset.typeId != "core.mclib":
if dataSourceConfiguration["uploadCycle"] in ("",None):
dataSourceConfiguration["uploadCycle"] = agentAsset.agentData.uploadCycle
else:
if dataSourceConfiguration["configurationId"] in ("","null",None):
dataSourceConfiguration["configurationId"] = str(int(time.time())) # aktuelle epoch zeit als zufällige id hinerlegen
latestETag = dataSourceConfiguration["eTag"]
for dataSource in agentAsset.agentData.dataSources:
listWithAllDataSources.append(createDataSourceDict(dataSource,agentAsset.typeId))
dataSourceConfiguration["dataSources"] = listWithAllDataSources
return _putDatasourceAndDatapointDefinition(agentAsset, dataSourceConfiguration, latestETag)
#######################################
def addDataSourcesAndDatapointDefinition(agentAsset, newDataSources):
latestETag = None
simpleError,dataSourceConfiguration = getDatasourceConfigForAgentAsset(agentAsset)
if agentAsset.typeId == "core.mclib":
latestETag = dataSourceConfiguration["eTag"]
for dataSource in newDataSources:
dataSourceDictonary = createDataSourceDict(dataSource,agentAsset.typeId)
if any(dataSourceDictonary["name"] in dataSource for dataSource in dataSourceConfiguration):
print(f"ATTENTION: Datasource with name '{dataSourceDictonary['name']}' is already existing for the agent {agentAsset.name}. It will not be added.")
continue
dataSourceConfiguration["dataSources"].append(dataSourceDictonary)
return _putDatasourceAndDatapointDefinition(agentAsset, dataSourceConfiguration, latestETag)
#######################################
def deleteDataSourceDefinitions(agentAsset, dataSourceNamesToBeDelete):
latestETag = None
simpleError, dataSourceConfiguration = getDatasourceConfigForAgentAsset(agentAsset)
if agentAsset.typeId == "core.mclib":
latestETag = dataSourceConfiguration["eTag"]
dataSourceConfiguration.pop('id', None)
dataSourceConfiguration.pop('eTag', None)
dataSourceConfiguration["dataSources"] = [datasource for datasource in dataSourceConfiguration["dataSources"] if datasource["name"] not in dataSourceNamesToBeDelete]
return _putDatasourceAndDatapointDefinition(agentAsset, dataSourceConfiguration, latestETag)
#######################################
def _putDatasourceAndDatapointDefinition(agentAsset, dataSourceDefinition, latestETag = None):
ifMatchHeader = {}
if latestETag:
ifMatchHeader = {"if-match":str(latestETag)}
additionalHeaders = ifMatchHeader
dataSourceDefinition.pop('id', None)
dataSourceDefinition.pop('eTag', None)
bodyAsJson = dataSourceDefinition
if logging == "VERBOSE":
print("Trying to import DataSource and Datapoints with following body")
print(bodyAsJson)
# for mc lib elements and etag needs to be set using a PUT method
if agentAsset.typeId != "core.mclib":
# if it is no mc lib agent, the full datasource config will be fetched like this:
returnValue = wrapApiCall("/api/mindconnectdevicemanagement/v3/devices/"+ agentAsset.assetId + "/dataConfig","PUT",bodyAsJson,assetTypeToDeriveApplicationScope = agentAsset.typeId, additionalHeaders= additionalHeaders)
# mindconnctdevicemanagement endpoint does not provide config for mclib agents: "Device configuration does not exist for given assetId xy".
# Therefore MindConnect Lib-Elements require the Agentmanagement-endpoint. This would also work for mcnano and mciot204, but with less information.
# As a consequence for NANO and IoT2040 mindconnectdevicemanagement-endpoint will be used
else:
returnValue = wrapApiCall("/api/agentmanagement/v3/agents/"+ agentAsset.assetId + "/dataSourceConfiguration","PUT",bodyAsJson,assetTypeToDeriveApplicationScope = agentAsset.typeId, additionalHeaders= additionalHeaders)
#The received datapoint IDs need to be saved in the dataPointClass, so that the mappings can applied
if not returnValue:
print("Something went wrong with creating the datasource configuration. Result of API call was empty...")
exit(-1)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
responseAsDict = json.loads(responseText)
assignReceivedDatapointIds(agentAsset, responseAsDict)
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
#######################################
def createDatapointMapping(dataPointMapping, agentAsset):
print(f" °°°° Creating DatpointMapping for variable '{dataPointMapping.variableName}' between '{agentAsset.name}' and '{dataPointMapping.targetAsset.name}' now ... ")
#The received datapoint mapping ID does probably not matter and does not be saved.
if not dataPointMapping.agentId:
dataPointMapping.agentId = dataPointMapping.agentAsset.assetId
bodyAsJson = {
"agentId": dataPointMapping.agentId,
"dataPointId": dataPointMapping.dataPointId,
"entityId": dataPointMapping.targetAsset.assetId,
"propertySetName":dataPointMapping.aspectId,
"propertyName": dataPointMapping.variableName
}
if logging == "VERBOSE":
print("Trying to import Datapoint Mapping with following body")
print(bodyAsJson)
if agentAsset.typeId != "core.mclib":
returnValue = wrapApiCall("/api/mindconnect/v3/dataPointMappings","POST",bodyAsJson, assetTypeToDeriveApplicationScope = agentAsset.typeId)
else:
bodyAsJson["keepMapping"] = True
returnValue = wrapApiCall("/api/mindconnect/v3/dataPointMappings","POST",bodyAsJson, assetTypeToDeriveApplicationScope = agentAsset.typeId)
if not returnValue:
print("Something went wrong with creating a datapoint mapping. Result of API call was empty...")
exit(-1)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
responseAsDict = json.loads(responseText)
assignReceivedDatapointIds(agentAsset, responseAsDict)
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
#######################################
def applyMappingConfigurationToDevice(agentAsset):
returnDict = dict()
bodyAsJson = {}
print(f" °°°° Applying changes to hardware device '{agentAsset.name}' now ...")
if agentAsset.typeId != "core.mclib": #apply changes is only available for hardware devices
returnValue = wrapApiCall(f"/api/mindconnectdevicemanagement/v3/devices/{agentAsset.assetId}/applyChanges","POST",bodyAsJson, assetTypeToDeriveApplicationScope = agentAsset.typeId)
if not returnValue:
print("Something went wrong with applying mapping Configuration. Result of API call was empty...")
exit(-1)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
responseAsDict = json.loads(responseText)
assignReceivedDatapointIds(agentAsset, responseAsDict)
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
else:
returnDict["response"] = "There is no option to apply config for mc.lib elements"
returnDict["statusCode"] = 123
returnDict["responseText"] = "There is no option to apply config for mc.lib elements"
#######################################
def initializeAgentInMindSphere(agentAsset):
if not agentAsset.alreadyExistingInMindSphere:
createNewAgent(agentAsset)
createCompletelyNewDataSourcesAndDatapointDefinition(agentAsset)
#######################################
#######################################
def createNewAgent(asset):
if asset.typeId == "core.mclib":
bodyAsJson = {
"name":asset.assetId,
"securityProfile":asset.agentData.securityProfile,
"entityId":asset.assetId
}
if logging in ("VERBOSE"):
print("Trying to initialize DeviceConfig with following body")
print(bodyAsJson)
returnValue = wrapApiCall("/api/agentmanagement/v3/agents", "POST", bodyAsJson, assetTypeToDeriveApplicationScope=asset.typeId)
else:
networkList = []
for network in asset.agentData.deviceConfiguration.networkInterfaces:
currentNetworkDict = {}
currentNetworkDict["name"] = network.name
if network.DHCP:
currentNetworkDict["DHCP"] = {"enabled":True}
currentNetworkDict["static"] ={}
else:
currentNetworkDict["DHCP"] = {"enabled":False}
currentNetworkDict["IPv4"] = network.IPv4
currentNetworkDict["IPv6"] = network.IPv6
currentNetworkDict["DNS"] = network.DNS
currentNetworkDict["SubnetMask"] = network.subnetMask
currentNetworkDict["Gateway"] = network.gateway
networkList.append(currentNetworkDict)
deviceDict = {
"serialNumber" : asset.agentData.deviceConfiguration.serialNumber,
"deviceType":"NANO" if asset.typeId == "core.mcnano" else "IOT2040",
"networkInterfaces" : networkList
}
bodyAsJson = {
"assetId" : asset.assetId,
"device" : deviceDict,
"agent" : {"name":asset.assetId, "proxy" : {}}
}
if logging in ("VERBOSE"):
print("Trying to initialize DeviceConfig with following body")
print(bodyAsJson)
returnValue = wrapApiCall("/api/mindconnectdevicemanagement/v3/devices" , "POST", bodyAsJson,assetTypeToDeriveApplicationScope=asset.typeId) #Todo: if this should be able to update existing agents device configs, this needs to be a put request
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
#######################################
def getOnboardingKey(agentAsset):
bodyAsJson= {}
if agentAsset.typeId == "core.mclib":
returnValue = wrapApiCall("/api/agentmanagement/v3/agents/" + agentAsset.assetId + "/boarding/configuration", "GET", bodyAsJson,assetTypeToDeriveApplicationScope=agentAsset.typeId)
else:
returnValue = wrapApiCall("/api/mindconnectdevicemanagement/v3/devices/" + agentAsset.assetId + "/onboardingConfig?encrypted=false", "GET", bodyAsJson,assetTypeToDeriveApplicationScope=agentAsset.typeId)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
#######################################
############# DELETE ##################
def deleteAsset(asset):
relatedFileName = assetsFilePath
print(" °°°° Deleting asset '{}' now ... ".format(asset.name))
bodyAsJson = {}
ifMatchHeader = {"if-match":str(asset.etag)}
returnValue = wrapApiCall("/api/assetmanagement/v3/assets/" + asset.assetId, "DELETE", bodyAsJson, additionalHeaders=ifMatchHeader)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
if fetchFromFile:
try:
with open(relatedFileName) as json_file:
oldFileContent = json.load(json_file)
currentFileContent = [d for d in oldFileContent if d.get('assetId') != asset.assetId]
with open(relatedFileName, 'w') as outfile:
json.dump(currentFileContent, outfile)
except Exception:
traceback.print_exc()
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
#######################################
def deleteAssetType(assetType):
relatedFileName = assetTypesFilePath
print(" °°°° Deleting assetType '{}' now ... ".format(assetType.name))
bodyAsJson = {}
ifMatchHeader = {"if-match":str(assetType.etag)}
returnValue = wrapApiCall("/api/assetmanagement/v3/assettypes/" + assetType.id, "DELETE", bodyAsJson, additionalHeaders=ifMatchHeader)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
if fetchFromFile:
try:
with open(relatedFileName) as json_file:
oldFileContent = json.load(json_file)
currentFileContent = [d for d in oldFileContent if d.get('id') != assetType.id]
with open(relatedFileName, 'w') as outfile:
json.dump(currentFileContent, outfile)
except Exception:
traceback.print_exc()
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
#######################################
def deleteAspect(aspect):
relatedFileName = aspectsFilePath
print(" °°°° Deleting aspect '{}' now ... ".format(aspect.name))
bodyAsJson = {}
ifMatchHeader = {"if-match":str(aspect.etag)}
returnValue = wrapApiCall("/api/assetmanagement/v3/aspecttypes/" + aspect.id, "DELETE", bodyAsJson, additionalHeaders=ifMatchHeader)
result = returnValue["result"]
statusCode = returnValue["responseStatusCode"]
responseText = returnValue["responseText"]
if statusCode >=200 and statusCode <300:
if fetchFromFile:
try:
with open(relatedFileName) as json_file:
oldFileContent = json.load(json_file)
currentFileContent = [d for d in oldFileContent if d.get('id') != aspect.id]
with open(relatedFileName, 'w') as outfile:
json.dump(currentFileContent, outfile)
except Exception:
traceback.print_exc()
returnDict = dict()
returnDict["response"] = result
returnDict["statusCode"] =statusCode
returnDict["responseText"] = responseText
return returnDict
############# Timeseries ##################
def writeTimeSeriesData(assetId,aspectName,dataList):
""" URL = /timeseries/{entityId}/{propertySetName}
Body needs to be a list of dictionaries:
[
{
"_time": "2019-02-10T23:01:00Z",
"exampleproperty0": "examplepropertyValue",
"exampleproperty0_qc": "exampleproperty0_qc_Value",
"exampleproperty1": "exampleproperty1Value"
}
]"""
bodyAsJson = json.dumps(dataList)
if logging in ("VERBOSE"):
print("Trying to insert timeseries data with following body now ...")
print(bodyAsJson)
# Add some chunking since the TS API only supports 2000 datapoints
chunkSize = 2000