1
+ """This module provides a script for getting the available data types and units that can be used with Cayenne."""
2
+
3
+ import sys
4
+ import inspect
5
+ from myDevices .cloud .apiclient import CayenneApiClient
6
+
7
+ if __name__ == "__main__" :
8
+ args = sys .argv [1 :]
9
+ if '--help' in args or '-h' in args :
10
+ print ('Usage: python3 -m myDevices.datatypes [-h] [PATTERN ...]\n \n '
11
+ 'Search for PATTERN in any of the available Cayenne data types and units.\n '
12
+ 'If no PATTERN is specified all available data types and units are displayed.\n '
13
+ 'The case of PATTERN is ignored.\n \n '
14
+ 'Example: python3 -m myDevices.datatypes temp humidity\n \n '
15
+ 'Options:\n '
16
+ ' -h, --help Display this help message and exit' )
17
+ sys .exit ()
18
+ api_client = CayenneApiClient ('https://api.mydevices.com' )
19
+ data_types = api_client .getDataTypes ().json ()
20
+ if 'error' in data_types :
21
+ print ('Error {}. Could not retrieve data types. Please try again later.' .format (data_types ['statusCode' ]))
22
+ sys .exit ()
23
+ output = []
24
+ for data_type in data_types :
25
+ # Ignore LT100GPS type since that is a custom type that is not intended for general use.
26
+ if data_type ['data_type' ] != 'LT100GPS' :
27
+ units = ''
28
+ for unit in data_type ['units' ]:
29
+ payload_unit = unit ['payload_unit' ] if unit ['payload_unit' ] else 'null'
30
+ units += ('\' {}\' ({}), ' .format (payload_unit , unit ['unit_label' ]))
31
+ units = units [:- 2 ]
32
+ entry = '{}\n Type: \' {}\' \n Units: {}\n ' .format (data_type ['data_type' ], data_type ['payload_type' ], units )
33
+ if not args :
34
+ output .append (entry )
35
+ else :
36
+ for arg in args :
37
+ if entry .lower ().find (arg .lower ()) != - 1 :
38
+ output .append (entry )
39
+ output .sort (key = lambda x : x .lower ())
40
+ for item in output :
41
+ print (item )
0 commit comments