Map Sitewise Models and Assets to Python objects with CRUD operations
git clone https://github.com/paszin/SitewisePyObjects
pip install SitewisePyObjects
or
pip install git+https://github.com/paszin/SitewisePyObjects
from SitewisePyObjects.Model import Model
# create a new model
m = Model(assetModelName="My Dummy Model", assetModelDescription="Created with SitewisePyObjects")
m.create(doWait=True) # doWait=True means wait till the model state changes to ACTIVE
m.add_attribute("location", "STRING", doUpdate=False)
m.add_attribute("id", "STRING", doUpdate=False)
m.add_measurement("intensity", "x", "INTEGER") # only update once (less request to aws)
# print model & view properties
print(m)
for k, v in m:
print(k, ":", v)
<Model: My Dummy Model - 37d022c2-ab15-47c6-9add-662f7d800367>
assetModelId : 37d022c2-ab15-47c6-9add-662f7d800367
assetModelArn : arn:aws:...367
assetModelName : Pascals Dummy Model Updated
assetModelDescription : Created with SitewisePyObjects
assetModelProperties : [...]
assetModelHierarchies : []
assetModelCompositeModels : []
# Update
m.assetModelName = "My Dummy Model Updated"
m.update()
# Delete
m.delete()
# Get existing Model by name
m = Model.fetch_by_name("A Model")
# get all models with name "*Dummy*" and list their assets
from SitewisePyObjects.Model import Model
from SitewisePyObjects.Asset import Asset
import boto3
client = boto3.client("iotsitewise")
filter_function = lambda name: "Dummy" in name
for mi in client.list_asset_models()["assetModelSummaries"]:
if filter_function(mi["name"]):
m = Model.fetch_by_name(mi["name"])
print(m)
for a in m.get_assets():
print("\t", a)
<Model: Pascals Dummy Model - 2803d879-d652-41e3-a89c-0d2481de800e>
<Asset: Pascals Dummy Asset 1>
<Asset: Pascals Dummy Asset 2>
<Model: Pascals Dummy Model Updated - 36f7c25d-48ce-4139-8c2d-8dd7b9ce68fb>
<Asset: Pascals Dummy Asset>
# create an asset if not exists, otherwise fetch from sitewise
# m is a Model
from SitewisePyObjects.Asset import Asset
assetName = "Pascals Dummy Asset"
a = Asset(assetName=assetName, assetModelId=m.assetModelId)
try:
a.create()
except:
print("Unable to create, try to fetch from Sitewise")
a = Asset.fetch_by_name(assetName)
if a is None:
print("not a top level asset -> use assetModelId")
a = Asset.fetch_by_name(assetName, assetModelId=m.assetModelId)
else:
print("created")
a
# get all information from sitewise
a.fetch(deep=True) #deep fetch means fetching model and attributes as well
print(a)
print(a.model)
print(a.attributes)
<Asset: Pascals Dummy Asset>
<Model: My Dummy Model - b731b66c-a013-4fa3-a3d3-84eb0970f923>
{'location': <location: None>, 'id': <id: None>, 'intensity': <intensity: None>}
# update an attribute
a.attributes["intensity"].update(value=100)
print(a.attributes["intensity"])
a.fetch(deep=True)
# print again, to verify that the data got uploaded
print(a.attributes["intensity"])
<intensity: 100>
<intensity: 100>
(only checked features are supported!)
- CRUD Operations for Models
- find Model by name
- find Model by name out of 50+ models
- CRUD Operations for Asset
- find Asset by name and given model id
- get assets from Model
- support for 50+ assets
- CRUD operations for tags
- Update properties of Asset (SHOULD WORK!)
- Update measurements of Asset
- delete all Asset of a Model -->
a.delete() for a in model.get_assets()
- handle hierarchies
- add attribute to a Model
- wait for Asset deletions
- wait for Model deletions
- wait for Model creation
- handle timestamps in AssetAttributes
- update measurement of Asset
- add transform to Model
- add metric to Model