Pau Espin Pedrol | fcc04f8 | 2019-04-12 16:38:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: class defining a Power Supply object |
| 2 | # |
| 3 | # Copyright (C) 2019 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as |
| 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import urllib.request |
| 21 | import xml.etree.ElementTree as ET |
| 22 | |
| 23 | from . import log |
| 24 | from .powersupply import PowerSupply |
| 25 | |
| 26 | class PowerSupplyIntellinet(PowerSupply): |
| 27 | """PowerSupply implementation to controll Intellinet devices.""" |
| 28 | |
| 29 | # HTTP request timeout, in seconds |
| 30 | PDU_TIMEOUT = 5 |
| 31 | |
| 32 | PDU_CMD_ON = 0 |
| 33 | PDU_CMD_OFF = 1 |
| 34 | |
| 35 | def _url_prefix(self): |
| 36 | return 'http://' + self.device_ip |
| 37 | |
| 38 | def _url_status(self): |
| 39 | return self._url_prefix() + '/status.xml' |
| 40 | |
| 41 | def _url_set_port_status(self, pdu_cmd): |
| 42 | return self._url_prefix() + "/control_outlet.htm?" + "outlet" + str(self.port - 1) + "=1" + "&op=" + str(pdu_cmd) + "&submit=Anwenden" |
| 43 | |
| 44 | def _port_stat_name(self): |
| 45 | # Names start with idx 0, while in ogt we count sockets starting from 1. |
| 46 | return 'outletStat' + str(self.port - 1) |
| 47 | |
| 48 | def _fetch_status(self): |
| 49 | data = urllib.request.urlopen(self._url_status(), timeout = self.PDU_TIMEOUT).read() |
| 50 | if not data: |
| 51 | raise log.Error('empty status xml') |
| 52 | return data |
| 53 | |
| 54 | def _get_port_status(self): |
| 55 | data = self._fetch_status() |
| 56 | root = ET.fromstring(data) |
| 57 | for child in root: |
| 58 | if child.tag == self._port_stat_name(): |
| 59 | return child.text |
| 60 | raise log.Error('no state for %s' % self._port_stat_name()) |
| 61 | |
| 62 | def _set_port_status(self, pdu_cmd): |
| 63 | urllib.request.urlopen(self._url_set_port_status(pdu_cmd),timeout = self.PDU_TIMEOUT).read() |
| 64 | |
| 65 | |
| 66 | ######################## |
| 67 | # PUBLIC - INTERNAL API |
| 68 | ######################## |
| 69 | def __init__(self, conf): |
| 70 | super().__init__(conf, 'intellinet') |
| 71 | mydevid = conf.get('device') |
| 72 | if mydevid is None: |
| 73 | raise log.Error('No "device" attribute provided in supply conf!') |
| 74 | self.set_name('intellinet-'+mydevid) |
| 75 | myport = conf.get('port') |
| 76 | if myport is None: |
| 77 | raise log.Error('No "port" attribute provided in power_supply conf!') |
| 78 | if not int(myport): |
| 79 | raise log.Error('Wrong non numeric "port" attribute provided in power_supply conf!') |
| 80 | self.set_name('intellinet-'+mydevid+'-'+myport) |
| 81 | self.device_ip = mydevid |
| 82 | self.port = int(myport) |
| 83 | |
| 84 | def is_powered(self): |
| 85 | """Get whether the device is powered on or off.""" |
| 86 | return self._get_port_status() == 'on' |
| 87 | |
| 88 | def power_set(self, onoff): |
| 89 | """Turn on (onoff=True) or off (onoff=False) the device.""" |
| 90 | if onoff: |
Pau Espin Pedrol | 873dee3 | 2020-02-12 17:32:50 +0100 | [diff] [blame] | 91 | self.dbg('switchon %s:%u' % (self.device_ip, self.port)) |
Pau Espin Pedrol | fcc04f8 | 2019-04-12 16:38:02 +0200 | [diff] [blame] | 92 | self._set_port_status(self.PDU_CMD_ON) |
| 93 | else: |
Pau Espin Pedrol | 873dee3 | 2020-02-12 17:32:50 +0100 | [diff] [blame] | 94 | self.dbg('switchoff %s:%u' % (self.device_ip, self.port)) |
Pau Espin Pedrol | fcc04f8 | 2019-04-12 16:38:02 +0200 | [diff] [blame] | 95 | self._set_port_status(self.PDU_CMD_OFF) |
| 96 | |
| 97 | |
| 98 | # vim: expandtab tabstop=4 shiftwidth=4 |