From 22700ccd5b79901e5cb11cf74db5730adc4a0b15 Mon Sep 17 00:00:00 2001 From: Stefan Weiher Date: Mon, 29 May 2017 16:38:22 +0200 Subject: [PATCH] A new argument parser is now used: switched from sys.argv to argparse. Now wc_getTrend works. Changed name from wc_report to wc_getReport. Updated the documentation. --- README | 8 ++--- wc_report.py => wc_getReport.py | 30 ++++++------------- wc_getTrend.py | 53 ++++++++++++++++++++++----------- wc_getValue.py | 20 ++++++------- wc_query.py | 19 ++++++------ 5 files changed, 67 insertions(+), 63 deletions(-) rename wc_report.py => wc_getReport.py (70%) diff --git a/README b/README index ed705a0..f6fd6c3 100644 --- a/README +++ b/README @@ -22,19 +22,17 @@ Running the tools e.g.: webctrl$ ./dist/wc_query/wc_query -When you run the tools without any arguments you will be prompted with how to use the commands and -which arguments to pass. +Use -h or --help with all tools in order to find out more about how to use them. Short explanation of each tool ------------------------------ wc_query : Query the paths and devices starting from /trees/geographic -wc_report : Retrieve a report for a directory or a specific device +wc_getReport : Retrieve a report for a directory or a specific device wc_getValue : Retrieve the current value of a device -wc_getTrend : Retrieve data and date for a given time period and device -(ATTENTION: this command does not yet work because the path to the trend data is not correct) +wc_getTrend : Retrieve data and date for a given time period and device diff --git a/wc_report.py b/wc_getReport.py similarity index 70% rename from wc_report.py rename to wc_getReport.py index bd13ca1..5df9f8f 100644 --- a/wc_report.py +++ b/wc_getReport.py @@ -2,28 +2,15 @@ import sys import numpy import xml +import argparse -reportTypes = ['~schedule-instance', '~effective-schedule', '~point-list-report', '~locked-value', '~network-io', - '~test-and-balance', '~equipment-checkout', '~audit-log', '~alarms', '~alarm_source', '~network_status', - '~module_version', '~security-assignment', '~alarm-messages', '~alarm-actions', '~trend-usage', - '~parameter-mismatch'] +if len(sys.argv) < 2: + print "You haven't specified any arguments but I need a path. Use -h to get more details on how to use this command." -if len(sys.argv) == 1: - print 'Usage of this command:' - print '$ wc_report /trees/geographic/#path/#to/#controller ~report_name' - print 'These are the possible types of reports:' - for rtype in reportTypes: - print rtype - sys.exit(0) -elif len(sys.argv) == 2: - print 'Need exactly two arguments:' - print '$ wc_report /trees/geographic/#path/#to/#controller ~report_name' - for rtype in reportTypes: - print rtype - sys.exit(0) -else: - path = sys.argv[1] - reportType = sys.argv[2] +parser = argparse.ArgumentParser() +parser.add_argument('--path', '-p', type=str, default=None, + help='Path to the point or node for which you want to get a report.') +args = parser.parse_args() username = 'wsdl' password = 'seife' @@ -42,7 +29,7 @@ # Get the report try: - report = client.service.runReport(path, reportType, 'csv') + report = client.service.runReport(args.path, '~point-list-report', 'csv') except suds.WebFault as fault: print fault sys.exit(1) @@ -89,3 +76,4 @@ print c sys.exit(0) + diff --git a/wc_getTrend.py b/wc_getTrend.py index 44f6e8b..c23cff5 100644 --- a/wc_getTrend.py +++ b/wc_getTrend.py @@ -1,15 +1,38 @@ import suds import sys import xml - -if len(sys.argv) == 1: - print 'Usage of this command:' - print '$ wc_getTrend /trees/geographic/#path/#to/#controller/m???' - print 'or:' - print '$ wc_getTrend "#controller/m???"' - sys.exit(1) -else: - path = sys.argv[1] +import argparse + +if len(sys.argv) < 2: + print "You haven't specified any arguments. Need at least a path. Use -h to get more details on how to use this command." + +parser = argparse.ArgumentParser() +parser.add_argument('--path', '-p', type=str, default=None, + help='The full path to the point, or trend log node whose trend data is desired.') +parser.add_argument('--sTime', '-s', type=str, default=None, + help="Start Time. Returns trend data values starting with this time. Like this: 'MM/DD/YYYY HH:MM:SS AM'.") +parser.add_argument('--eTime', '-e', type=str, default=None, + help="End Time. Returns trend data values until this time. Like this: 'MM/DD/YYYY HH:MM:SS PM'.") +parser.add_argument('--limit', '-l', type=bool, default=False, + help='Use True to retrieve maxRecords from the start or False to retrieve maxRecords from the end.') +parser.add_argument('--maxRec', '-m', type=int, default=10, + help='Maximum number of records desired. Use a number >0 to limit records; use 0 to retrieve unlimited records.') +args = parser.parse_args() + +#location = '/trees/geographic/#geb_g/#g_luft/#primzuluft_1_2_stm/#g_primzuluft_trend/m006' +#location = '#g_primzuluft_trend/m006' +#startDate = '05/10/2017 08:00:00 AM' +#endDate = '05/10/2017 01:00:00 PM' +#limitFromStart = False +#maxRecords = 10 + +if args.path == None: + print 'Path is undifined. Type "$ wc_getTrend --help" for details.' + sys.exit(0) +if args.sTime == None: + print 'Start time is undefined. Use -s to define date and time.' +if args.eTime == None: + print 'End time is undefined. Use -e to define date and time.' username = 'wsdl' password = 'seife' @@ -26,19 +49,13 @@ print("Unexpected error:", sys.exc_info()[0]) sys.exit(1) - -location = '#g_umluftkw_trend/m009' -startDate = '05/10/2017 08:00:00 AM' -endDate = '05/10/2017 01:00:00 PM' -limitFromStart = False -maxRecords = 10 - try: - trendData = client.service.getTrendData(location, startDate, endDate, limitFromStart, maxRecords) + trendData = client.service.getTrendData(args.path, args.sTime, args.eTime, args.limit, args.maxRec) except suds.WebFault as fault: print fault sys.exit(1) -print trendData +for x in range(0, args.maxRec*2-1, 2): + print trendData[x] + ' ' + trendData[x+1] sys.exit(0) diff --git a/wc_getValue.py b/wc_getValue.py index a6278ce..be59623 100644 --- a/wc_getValue.py +++ b/wc_getValue.py @@ -1,15 +1,15 @@ import suds import sys import xml +import argparse -if len(sys.argv) == 1: - print 'Usage of this command:' - print '$ wc_getValue /trees/geographic/#path/#to/#controller/m???' - print 'or:' - print '$ wc_getValue "#controller/m???"' - sys.exit(1) -else: - path = sys.argv[1] +if len(sys.argv) < 2: + print "You haven't specified any arguments but I need a path. Use -h to get more details on how to use this command." + +parser = argparse.ArgumentParser() +parser.add_argument('--path', '-p', type=str, default=None, + help='Full path to the point or node whose value is desired. Start querying by typing: $ wc_query /trees/geographic') +args = parser.parse_args() username = 'wsdl' password = 'seife' @@ -27,7 +27,7 @@ sys.exit(1) try: - value = client.service.getValue(path) + value = client.service.getValue(args.path) except suds.WebFault as fault: print fault sys.exit(1) @@ -36,6 +36,6 @@ print "You haven't specified a particular controller. The command could look like this:" print '$ wc_getValue "#controller/m001"' else: - print "Present value of " + path + " is: " + value + print "Present value of " + args.path + " is: " + value sys.exit(0) diff --git a/wc_query.py b/wc_query.py index b40d234..f52d7b4 100644 --- a/wc_query.py +++ b/wc_query.py @@ -1,14 +1,15 @@ import suds import sys import xml +import argparse -if len(sys.argv) == 1: - print 'Usage of this command:' - print '$ wc_query /trees/geographic/#path/#to/#controller' - print 'Start querying by typing: $ wc_query /trees/geographic' - sys.exit(1) -else: - path = sys.argv[1] +if len(sys.argv) < 2: + print "You haven't specified any arguments but I need a path. Use -h to get more details on how to use this command." + +parser = argparse.ArgumentParser() +parser.add_argument('--path', '-p', type=str, default=None, + help='Path to the point or node whose children you want to retrieve. Start querying with "-p /trees/geographic"') +args = parser.parse_args() username = 'wsdl' password = 'seife' @@ -26,8 +27,8 @@ sys.exit(1) try: - print 'Children of "' + path + '":' - print client.service.getChildren(path) + print 'Children of "' + args.path + '":' + print client.service.getChildren(args.path) except suds.WebFault as fault: print fault sys.exit(1)