summaryrefslogtreecommitdiff
path: root/validation/validlib/anellib.py
blob: f27c44e13786785afd830a3b877d36ffb136cfb9 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
# -*- 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:
        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"""
    letter = parameters["letter"]
    green = "#00DD00"
    criteria = "<td bgcolor=" + green + ">" + \
        "<input class=bt type=submit value=[IO] name=" + \
        letter + str(subindex - 1) + ">" + \
        "</td>"
    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 "}"