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