-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexport_devices.py
92 lines (67 loc) · 2.84 KB
/
export_devices.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
############################################################################
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2018 Digi International Inc. All Rights Reserved.
#
############################################################################
VERSION = {
'major': 0,
'minor': 1,
'patch': 0
}
import sys
import csv
import json
from devicecloud import DeviceCloud
from devicecloud.version import __version__
from devicecloud.examples.example_helpers import get_authenticated_dc
from devicecloud.conditions import Attribute
DEBUG = False
def disp_help():
print("""Usage: python {} export_file_path [--debug] [--help]
This script exports registered devices within a Digi Remote Manager (Device Cloud) account to a CSV file.
Required:
export_file_path: the output CSV file name and path
Optional:
--debug, -d: Debug, display registered devices
--help, -h -?: Help, display help
""".format(sys.argv[0]))
sys.exit()
def parse_args(argv):
global DEBUG
if '--help' in sys.argv or '-h' in sys.argv or '-?' in sys.argv:
disp_help()
if '--debug' in sys.argv or '-d' in sys.argv:
DEBUG = True
if len(sys.argv) < 2:
# print sys.argv
disp_help()
return sys.argv
if __name__ == "__main__":
print("Device Exporter - version {major}.{minor}.{patch}".format(**VERSION))
args = parse_args(sys.argv)
print("Opening {}".format(args[1]))
dc = get_authenticated_dc()
devices = dc.devicecore.get_devices()
fieldnames = ['devMac', 'devCellularModemId', 'devConnectwareId', 'xpExtAddr', 'dpLastKnownIp',
'dpGlobalIp', 'dpDeviceType', 'dpDescription', 'dpConnectionStatus', 'dpRestrictedStatus',
'dpFirmwareLevelDesc', 'dpLastConnectTime', 'dpContact', 'dpLocation', 'dpMapLat', 'dpMapLong',
'dpCapabilities', 'dvVendorId', 'dpUserMetaData', 'dpTags', 'dpLastDisconnectTime',
'dpLastUpdateTime', 'dpHealthStatus', 'grpPath', 'dpPanId', 'dpFirmwareLevel',
'devTerminated', 'devEffectiveStartDate', 'grpId', 'cstId', 'devRecordStartDate', 'id',
'dpZigbeeCapabilities']
with open(args[1], 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, quoting=csv.QUOTE_NONNUMERIC)
writer.writeheader()
csv_devices = []
for device in devices:
if DEBUG:
print(json.dumps(device.get_device_json(), indent=4, sort_keys=True))
dvc = device.get_device_json()
csv_devices.append(dvc)
writer.writerows(csv_devices)
print("DONE! Device export complete. Open {} to verify file complete.".format(args[1]))