-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuse_case_4_latest_asset_measurements.py
73 lines (51 loc) · 2.09 KB
/
use_case_4_latest_asset_measurements.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
# -*- coding: utf-8 -*-
"""Example 4
This code prints out the latest measurements from all of your assets.
Example:
$ python use_case_4_latest_asset_measurements.py
"""
from smart_sensor_client.smart_sensor_client import SmartSensorClient
DEFAULT_SETTINGS_FILE = 'settings.yaml'
def run_task(settings_file=DEFAULT_SETTINGS_FILE) -> bool:
# Create the client instance
client = SmartSensorClient(settings_file=settings_file)
# Authenticate
if not client.authenticate():
print('Authentication FAILED')
return False
# Print organization
print('Organization {}, {}'.format(client.organization_id, client.organization_name))
print()
# Get list of plants
plants = client.get_plant_list()
# Iterate the plant list and print all assets therein
for plant in plants:
print('Plant {}, {}:'.format(plant['plantID'], plant['plantName']))
print('Assets:')
# Get list of assets
assets = client.get_asset_list(organization_id=client.organization_id, plant_id=plant['plantID'])
if len(assets) == 0:
print('No assets in this plant')
else:
for asset in assets:
asset_data = client.asset_get_asset_by_id(asset_id=asset['assetID'])
print('Latest measurements of Asset {}, {}:'.format(asset['assetID'], asset['assetName']))
if asset_data is None:
# If there is an error, skip to the next asset
continue
# Iterate all the measurements available and print them
for m in asset_data['measurements']:
# Print measurements that contain values
if m['measurementValue'] is not None:
print(' ' + m['measurementTypeName'].ljust(37) + ':', m['measurementValue'],
'(' + m['timeStamp'] + ')')
print()
print()
return True
# Main body
if __name__ == '__main__':
result = run_task()
if result is True:
print('Task SUCCESS')
else:
print('Task FAILED')