Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New script for setting values. ATTENTION: IT DOESN'T WORK YET due to …
…an error: 'Cannot set value for ... because the value is not changeable via SOAP'.
  • Loading branch information
weiher committed Mar 9, 2018
1 parent 4ef194f commit b40cdaf
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Email.py
Expand Up @@ -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:
Expand Down
14 changes: 11 additions & 3 deletions 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'

Expand All @@ -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'
183 changes: 183 additions & 0 deletions 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)


0 comments on commit b40cdaf

Please sign in to comment.