-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodesCreateAsset.py
78 lines (66 loc) · 3.96 KB
/
NodesCreateAsset.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
import json, string
from NodesApiObjects import nodesAssetTypes, NodesAsset, NodesConnectedAsset
from NodesSession import NodesSession, Environment
from NodesAuthentication import CLIENT_ID, CLIENT_SECRET
session=NodesSession(Environment.ExtTest, CLIENT_ID, CLIENT_SECRET)
def locateDsoIdFromGridArea(gridAreaName):
result = session.getHandle().get(session.getBaseUrl() + "/GridAreas/", headers=session.getHeader())
for area in result.json()['items']:
if area['name']==gridAreaName:
for link in area['links']:
if link['title']=="OperatedByOrganization":
cols=string.split(link['href'], "/") # Extract the ID part of the href link
id=cols[len(cols)-1]
return id
return None
def locateCompanyOfUser(userId):
orgId=None
#Lookup memberships
result = session.getHandle().get(session.getBaseUrl() + "/memberships?userId=" + myUserId,
headers=session.getHeader())
for memships in result.json()['items']:
orgId = memships['organizationId']
#Could be linked to multiple organization. For simplicity select the last one...
return orgId
result = session.getHandle().get(session.getBaseUrl() + "/users/current", headers=session.getHeader())
myUserId = result.json()['id']
myOrgId=locateCompanyOfUser(myUserId) # This sample assumes that my organization is both BRP and FSP, and this ID is used for both references in assignment of asset
dsoOrgId=locateDsoIdFromGridArea("Agder Energi Nett") #The Grid Area shown in portal
# Query Asset Types, and store ID in dicstionary
result = session.getHandle().get(session.getBaseUrl() + "/assetTypes", headers=session.getHeader())
#print json.dumps(result.json(), indent=2)
for atype in result.json()['items']:
nodesAssetTypes[atype['name']]=atype['id']
print "Asset Type IDs ", nodesAssetTypes
# Create the core Asset objected to be stored in NODES
asset=NodesAsset("My weak battery")
asset.assetTypeId=nodesAssetTypes['Battery'] # Assuming battery is found amongs available types
asset.externalReference="AssetHubID5462" #Could be any ID but should be Unique for your organizationb so you can look up the generated ID from the next registration step
asset.createdByUserId=myUserId
asset.lastModifiedByUserId=myUserId
asset.operatedByOrganizationId=myOrgId
jsonStr = json.dumps(asset.__dict__, indent=2)
# Store asset in NODES; not yet assigning it to a particular DSO
result = session.getHandle().post(session.getBaseUrl() + "/assets", headers=session.getPostHeader(), data=jsonStr.encode('utf-8'))
#print json.dumps(result.json(), indent=2)
# Reload asset to see generated Asset ID which needs to be used in assigning asset to grid of DSO
# Query Asset Types, and store ID in dicstionary
result = session.getHandle().get(session.getBaseUrl() + "/assets", headers=session.getHeader())
newAssetId=""
for assetItem in result.json()['items']:
if assetItem['externalReference']==asset.externalReference:
print "Found newly created asset with ID ", assetItem['id']
newAssetId=assetItem['id']
# Create the link between the Asset and a particular DSO + MPID
connectedAsset=NodesConnectedAsset(6.92891, 58.2903, newAssetId, asset.externalReference)
connectedAsset.mpid="705011111111331" # The MPID
connectedAsset.managedByOrganizationId=myOrgId # Assign the ID of the current FSP registering the Asset connection
connectedAsset.suppliedByOrganizationId=myOrgId # Often our own organization is also BRP on the MPID level
connectedAsset.operatedByOrganizationId=dsoOrgId
connectedAsset.createdByUserId=myUserId
connectedAsset.lastModifiedByUserId=myUserId
jsonStr = json.dumps(connectedAsset.__dict__, indent=2)
# Store asset connection to grid (MPID, Long/Lat and DSO ID in particular
result = session.getHandle().post(session.getBaseUrl() + "/assetgridassignments", headers=session.getPostHeader(), data=jsonStr.encode('utf-8'))
#print result
#print json.dumps(result.json(), indent=2)