diff --git a/Email.py b/Email.py index f468af3..07ec708 100644 --- a/Email.py +++ b/Email.py @@ -39,7 +39,8 @@ def getMailAddresses (currentHour, mailAddresses, timePeriods): return errorMessage if not foundAPeriod: - errorMessage = "ERROR: The current hour is not contained in any of the time periods specified in the SETTINGS file." + errorMessage = "ERROR: The current hour, " + str(currentHour) + ", is not contained in any of the time periods " \ + "specified in the SETTINGS file." return errorMessage try: diff --git a/SETTINGS b/SETTINGS index bcda6e3..e28eb20 100644 --- a/SETTINGS +++ b/SETTINGS @@ -1,9 +1,14 @@ +# wc_query, wc_getValue WC_USERNAME = 'wsdl' #WC_NODE = '/trees/geographic' WC_NODE = '#km_speicher_fel' WC_URL = 'https://webctrl.rz-berlin.mpg.de' WC_VERBOSITY = +# wc_setValue +WC_NEWVALUE = '0.0' +WC_CHANGEREASON = 'I want it to be like this' + # wc_getReport WC_REPORT_TYPE = '~point-list-report' @@ -17,6 +22,9 @@ WC_TREND_MAXREC = WC_ALARMS_INTERVAL = WC_ALARMS_TAKEACTION = WC_ALARMS_ALERT_PERIODS = '8-16;16-22; 22-8' -WC_ALARMS_ALERT_EMAILS = 'weiher@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de;' -WC_ALARMS_ALERT_EMAILS = 'weiher@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de;' -WC_ALARMS_ALERT_EMAILS = 'weiher@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de' +WC_ALARMS_ALERT_EMAILS = 'weiher@fhi-berlin.mpg.de,stefanweiher@web.de;' +WC_ALARMS_ALERT_EMAILS = 'weiher@fhi-berlin.mpg.de,stefanweiher@web.de;' +WC_ALARMS_ALERT_EMAILS = 'weiher@fhi-berlin.mpg.de,stefanweiher@web.de' +#WC_ALARMS_ALERT_EMAILS = 'junkes@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de;' +#WC_ALARMS_ALERT_EMAILS = 'junkes@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de;' +#WC_ALARMS_ALERT_EMAILS = 'junkes@fhi-berlin.mpg.de # weiher@fhi-berlin.mpg.de' diff --git a/wc_setValue.py b/wc_setValue.py new file mode 100644 index 0000000..7e4deb5 --- /dev/null +++ b/wc_setValue.py @@ -0,0 +1,183 @@ +# -*- coding: utf-8 -*- + +import suds +import sys +import xml +import argparse +import getpass +from Environment import readSettings + + +def main(args): + + #################################################################################################################### + ### Check all arguments + #################################################################################################################### + + errorMessage = "No path to SETTINGS file specified. Provide one via '-f my_settingsFile' or {..., 'settingsFile':'my_settingsFile', ...}" + try: + if args['settingsFile'] is None or not args['settingsFile']: + print(errorMessage) + sys.exit(1) + else: + settingsFile = args['settingsFile'] + except KeyError: + print(errorMessage) + sys.exit(1) + settings = readSettings(settingsFile) + + errorMessage = "No username specified. Provide one via '-u my_username' or {..., 'username':'my_username', ...} or WC_USERNAME=my_username" + try: + if args['username'] is None or not args['username']: + if not settings['WC_USERNAME']: + print(errorMessage) + sys.exit(1) + else: + username = settings['WC_USERNAME'] + else: + username = str(args['username']) + except KeyError: + print(errorMessage) + sys.exit(1) + + # The password cannot be given via environment variable + errorMessage = "No password specified. Provide one via {..., 'password':'my_password', ...}" + try: + if args['password'] is None or not args['password']: + print(errorMessage) + sys.exit(1) + else: + password = args['password'] + except KeyError: + print(errorMessage) + sys.exit(1) + + nodeDefault = '/trees/geographic' + errorMessage = "No node specified. Provide one via '-n my_node' or {..., 'node':'my_node', ...} or WC_NODE=my_node. " \ + "Using a default now: '" + nodeDefault + "'" + try: + if args['node'] is None or not args['node']: + if not settings['WC_NODE']: + print(errorMessage) + node = nodeDefault + else: + node = settings['WC_NODE'] + else: + node = str(args['node']) + except KeyError: + print(errorMessage) + node = nodeDefault + + urlDefault = 'https://webctrl.rz-berlin.mpg.de' + errorMessage = "No URL specified. Provide one via '-url my_url' or {..., 'url':'my_url', ...} or WC_URL=my_url. " \ + "Using a default now: '" + urlDefault + "'" + try: + if args['url'] is None or not args['url']: + if not settings['WC_URL']: + print(errorMessage) + url = urlDefault + else: + url = settings['WC_URL'] + else: + url = str(args['url']) + except KeyError: + print(errorMessage) + url = urlDefault + + errorMessage = "No value to be set was specified. Provide one via '-v my_value' or {..., 'value':'my_value', ...} or " \ + "WC_NEWVALUE=my_value. NO DEFAULT value will be used." + try: + if args['value'] is None or not args['value']: + if not settings['WC_NEWVALUE']: + print(errorMessage) + sys.exit(1) + else: + newValue = settings['WC_NEWVALUE'] + else: + newValue = str(args['value']) + except KeyError: + print(errorMessage) + sys.exit(1) + + reasonDefault = '' + errorMessage = "No reason for changing the value was given. Provide one via '-r my_reason' or {..., 'reason':'my_reason', ...} or " \ + "WC_REASON=my_reason. A default, an empty string, will be used." + try: + if args['reason'] is None or not args['reason']: + if not settings['WC_REASON']: + print(errorMessage) + reason = reasonDefault + else: + reason = settings['WC_REASON'] + else: + reason = str(args['reason']) + except KeyError: + print(errorMessage) + reason = reasonDefault + + + wsdlFile = url + '/_common/webservices/Eval?wsdl' + + #################################################################################################################### + ### Connect to the webCTRL server + #################################################################################################################### + + try: + client = suds.client.Client(wsdlFile, username=username, password=password) + except AttributeError: + print 'Error: Incorrect username and/or password' + except xml.sax._exceptions.SAXParseException: + print 'Error: Incorrect/Misspelled WSDL file. It should be: http(s)://URL?wsdl' + sys.exit(1) + except: + print("Unexpected error:", sys.exc_info()[0]) + print('Perhaps your URL to the WSDL file is not correct.') + sys.exit(1) + + #################################################################################################################### + ### Get the answer from the server and print the output + #################################################################################################################### + + try: + #value = client.service.getValue(node) + value = client.service.setValue(node, newValue, reason) + except suds.WebFault as fault: + print fault + sys.exit(1) + + if value is None: + print "You haven't specified a particular controller. The command could look like this:" + print '$ wc_getValue "#controller/m001"' + else: + print "Present value of " + node + " is: " + value + + +# The argument parser is only called if this script is called as a script/executable (via command line) but not when +# imported by another script +if __name__=='__main__': + if len(sys.argv) < 2: + print "You haven't specified any arguments. Use -h to get more details on how to use this command." + sys.exit(1) + + parser = argparse.ArgumentParser() + parser._action_groups.pop() + required = parser.add_argument_group('required arguments') + optional = parser.add_argument_group('optional arguments (can also be specified via SETTINGS file)') + required.add_argument('--password', '-p', type=str, default=None, help='Password for the login to the WebCTRL server') + required.add_argument('--settingsFile', '-f', type=str, default=None, help='File name of settings containing WC_* variables') + required.add_argument('--username', '-u', type=str, default=None, help='Username for the login to the WebCTRL server') + required.add_argument('--value', '-v', type=str, default=None, help='New value to be set') + required.add_argument('--reason', '-r', type=str, default=None, help='Reason for setting/changing the value') + optional.add_argument('--url', '-url', type=str, default=None, help="URL of the webctrl server like 'http(s)://webctrl.de'.") + optional.add_argument('--node', '-n', type=str, default=None, help='Path to the point or node whose value you want to retrieve') + args = parser.parse_args() + + # Get the password if it hasn't been passed as argument + if args.password is None or not args.password: + args.password = getpass.getpass('No password specified via -p. Please enter your WebCTRL login password: ') + + # Convert the argparse.Namespace to a dictionary via vars(args) + main(vars(args)) + sys.exit(0) + +