# -*- coding: utf-8 -*- """Module allowing to command and control the Anel io's/power plugs. The parameters are the following: - "target": io or power plug - used for the messages printed to the user - "switch_path": HTML page allowing to switch on/off the target - "status_path": HTML page allowing to retrieve the status on/off of the target - "body": body of the switching request - "letter": letter identifying the target in the statuses or switching requests - "config":configuration of the web server (IP address, login and password) - "size": number of the io's/power plugs What is called "key" is a couple identifying the power plug (resp. io): - the first element is the index of the power strip (resp. group of io's), - the second element is the index of the power plug (resp. io) on the power strip (resp. group of io's) """ import base64 import re import httplib def switch(command, keys, parameters): """Switch on/off the io's/power plugs identified by the keys""" (index, subindexes) = keys if type(subindexes) != list: subindexes = [subindexes] assert (command == "off" or command == "on") print "Switch", command, "the", parameters["target"], "(s)", keys page = statuses_page(index, parameters) new_subindexes = [] for subindex in subindexes: if command != status_aux(page, subindex, parameters): new_subindexes.append(subindex) send_switching_request(index, new_subindexes, parameters) def send_request(index, path, body, parameters): """Send an HTTP request to the web server managing the io's/power plugs""" if body == "": method = "GET" else: method = "POST" (address, login, password) = parameters["config"][index] token = base64.standard_b64encode(login + ":" + password) headers = {"Authorization": "Basic " + token} connection = httplib.HTTPConnection(address) connection.request(method, path, body, headers) response = connection.getresponse() connection.close() return response.read() def send_switching_request(index, subindexes, parameters): """Send a switching request (on/off) to the web server managing the io's/power plugs""" body = parameters["body"] assert body != "" path = parameters["switch_path"] letter = parameters["letter"] for subindex in subindexes: assert subindex <= parameters["size"] body = body + "&" + letter + str(subindex - 1) + "=S" send_request(index, path, body, parameters) def status_aux(page, subindex, parameters): """Return the status of the io's/power plugs contained in the page""" assert subindex <= parameters["size"] letter = parameters["letter"] green = "#00DD00" criteria = "" + \ "" + \ "" if re.search(criteria, page) is None: status = "off" else: status = "on" return status def statuses_page(index, parameters): """Return the page containing the status of the io/power plug identified by the index""" return send_request(index, parameters["status_path"], "", parameters) def status(key, parameters): """Return the status of the io/power plug identified by key""" (index, subindex) = key page = statuses_page(index, parameters) return status_aux(page, subindex, parameters) def indexes(parameters): """Return a list containing the index of each power strip or group of io's""" return parameters["config"].keys() def subindexes(parameters): """Return a list containing the index of each power strip or group of io's""" return range(1, parameters["size"] + 1) def switch_off_all(parameters): """Switch off all io's/power plugs""" target = parameters["target"] print "Switch off each", target, "...{" for index in indexes(parameters): switch("off", (index, subindexes(parameters)), parameters) print "}" def check_control(parameters): """Check that all io'spower plugs can be controlled""" target = parameters["target"] for index in indexes(parameters): switch("off", (index, subindexes(parameters)), parameters) for subindex in subindexes(parameters): key = (index, subindex) assert status(key, parameters) == "off" switch("on", key, parameters) assert status(key, parameters) == "on" print "OK: The", target, key, "can be controlled" print "OK: The group of", target + "s", index, "can be controlled"