Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 1 | # osmo_gsm_tester: class defining a Power Supply object |
| 2 | # |
Pau Espin Pedrol | fcc04f8 | 2019-04-12 16:38:02 +0200 | [diff] [blame] | 3 | # Copyright (C) 2018-2019 by sysmocom - s.f.m.c. GmbH |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 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 | from abc import ABCMeta, abstractmethod |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 21 | from . import log |
| 22 | from .event_loop import MainLoop |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 23 | |
| 24 | class PowerSupply(log.Origin, metaclass=ABCMeta): |
| 25 | |
| 26 | ############## |
| 27 | # PROTECTED |
| 28 | ############## |
| 29 | def __init__(self, conf, name): |
| 30 | """Base constructor. Must be called by subclass.""" |
| 31 | super().__init__(log.C_RUN, name) |
| 32 | self.conf = conf |
| 33 | |
| 34 | ######################## |
| 35 | # PUBLIC - INTERNAL API |
| 36 | ######################## |
| 37 | @abstractmethod |
| 38 | def is_powered(self): |
| 39 | """Get whether the device is powered on or off. Must be implemented by subclass.""" |
| 40 | pass |
| 41 | |
| 42 | @abstractmethod |
| 43 | def power_set(self, onoff): |
| 44 | """Turn on (onoff=True) or off (onoff=False) the device. Must be implemented by subclass.""" |
| 45 | pass |
| 46 | |
| 47 | def power_cycle(self, sleep=0): |
| 48 | """Turns off the device, waits N.N seconds, then turn on the device.""" |
| 49 | self.power_set(False) |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 50 | MainLoop.sleep(self, sleep) |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 51 | self.power_set(True) |
| 52 | |
| 53 | |
Pau Espin Pedrol | fcc04f8 | 2019-04-12 16:38:02 +0200 | [diff] [blame] | 54 | from . import powersupply_sispm, powersupply_intellinet |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 55 | |
| 56 | KNOWN_PWSUPPLY_TYPES = { |
| 57 | 'sispm' : powersupply_sispm.PowerSupplySispm, |
Pau Espin Pedrol | fcc04f8 | 2019-04-12 16:38:02 +0200 | [diff] [blame] | 58 | 'intellinet' : powersupply_intellinet.PowerSupplyIntellinet, |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | def register_type(name, clazz): |
| 62 | """Register a new PoerSupply child class at runtime.""" |
| 63 | KNOWN_PWSUPPLY_TYPES[name] = clazz |
| 64 | |
| 65 | def get_instance_by_type(pwsupply_type, pwsupply_opt): |
| 66 | """Allocate a PowerSupply child class based on type. Opts are passed to the newly created object.""" |
| 67 | obj = KNOWN_PWSUPPLY_TYPES.get(pwsupply_type, None) |
| 68 | if not obj: |
| 69 | raise log.Error('PowerSupply type not supported:', pwsupply_type) |
| 70 | return obj(pwsupply_opt) |
| 71 | |
| 72 | |
| 73 | |
| 74 | # vim: expandtab tabstop=4 shiftwidth=4 |