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 | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 21 | from ..core import log |
| 22 | from ..core.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 | 664e383 | 2020-06-10 19:30:33 +0200 | [diff] [blame] | 50 | MainLoop.sleep(sleep) |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 51 | self.power_set(True) |
| 52 | |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 53 | def get_instance_by_type(pwsupply_type, pwsupply_opt): |
| 54 | """Allocate a PowerSupply child class based on type. Opts are passed to the newly created object.""" |
Pau Espin Pedrol | 2b95958 | 2020-04-17 16:53:14 +0200 | [diff] [blame] | 55 | if pwsupply_type == 'sispm': |
Pau Espin Pedrol | a65b505 | 2020-04-19 18:56:43 +0200 | [diff] [blame] | 56 | from .powersupply_sispm import PowerSupplySispm |
Pau Espin Pedrol | 2b95958 | 2020-04-17 16:53:14 +0200 | [diff] [blame] | 57 | obj = PowerSupplySispm |
| 58 | elif pwsupply_type == 'intellinet': |
Pau Espin Pedrol | a65b505 | 2020-04-19 18:56:43 +0200 | [diff] [blame] | 59 | from .powersupply_intellinet import PowerSupplyIntellinet |
Pau Espin Pedrol | 2b95958 | 2020-04-17 16:53:14 +0200 | [diff] [blame] | 60 | obj = PowerSupplyIntellinet |
| 61 | else: |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 62 | raise log.Error('PowerSupply type not supported:', pwsupply_type) |
Pau Espin Pedrol | 2b95958 | 2020-04-17 16:53:14 +0200 | [diff] [blame] | 63 | |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 64 | return obj(pwsupply_opt) |
| 65 | |
| 66 | |
| 67 | |
| 68 | # vim: expandtab tabstop=4 shiftwidth=4 |