blob: 86fc0103d1222560fcbab12d828750a31dcb8a1d [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
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
54from . import powersupply_sispm
55
56KNOWN_PWSUPPLY_TYPES = {
57 'sispm' : powersupply_sispm.PowerSupplySispm,
58}
59
60def register_type(name, clazz):
61 """Register a new PoerSupply child class at runtime."""
62 KNOWN_PWSUPPLY_TYPES[name] = clazz
63
64def get_instance_by_type(pwsupply_type, pwsupply_opt):
65 """Allocate a PowerSupply child class based on type. Opts are passed to the newly created object."""
66 obj = KNOWN_PWSUPPLY_TYPES.get(pwsupply_type, None)
67 if not obj:
68 raise log.Error('PowerSupply type not supported:', pwsupply_type)
69 return obj(pwsupply_opt)
70
71
72
73# vim: expandtab tabstop=4 shiftwidth=4