blob: 1cf7106044e29fc2cbd8b1b643853a03862ae8bf [file] [log] [blame]
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +01001# osmo_gsm_tester: class defining a Power Supply object
2#
Pau Espin Pedrolfcc04f82019-04-12 16:38:02 +02003# Copyright (C) 2018-2019 by sysmocom - s.f.m.c. GmbH
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +01004#
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
20from abc import ABCMeta, abstractmethod
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020021from . import log
22from .event_loop import MainLoop
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010023
24class 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 Pedrol9a4631c2018-03-28 19:17:34 +020050 MainLoop.sleep(self, sleep)
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010051 self.power_set(True)
52
53
Pau Espin Pedrolfcc04f82019-04-12 16:38:02 +020054from . import powersupply_sispm, powersupply_intellinet
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010055
56KNOWN_PWSUPPLY_TYPES = {
57 'sispm' : powersupply_sispm.PowerSupplySispm,
Pau Espin Pedrolfcc04f82019-04-12 16:38:02 +020058 'intellinet' : powersupply_intellinet.PowerSupplyIntellinet,
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010059}
60
61def register_type(name, clazz):
62 """Register a new PoerSupply child class at runtime."""
63 KNOWN_PWSUPPLY_TYPES[name] = clazz
64
65def 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