blob: 7715a464b4451044fcb121ad47c49f8bc497a6f2 [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 Pedrole1a58bd2020-04-10 20:46:07 +020021from ..core import log
22from ..core.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 Pedrol664e3832020-06-10 19:30:33 +020050 MainLoop.sleep(sleep)
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010051 self.power_set(True)
52
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010053def 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 Pedrol2b959582020-04-17 16:53:14 +020055 if pwsupply_type == 'sispm':
Pau Espin Pedrola65b5052020-04-19 18:56:43 +020056 from .powersupply_sispm import PowerSupplySispm
Pau Espin Pedrol2b959582020-04-17 16:53:14 +020057 obj = PowerSupplySispm
58 elif pwsupply_type == 'intellinet':
Pau Espin Pedrola65b5052020-04-19 18:56:43 +020059 from .powersupply_intellinet import PowerSupplyIntellinet
Pau Espin Pedrol2b959582020-04-17 16:53:14 +020060 obj = PowerSupplyIntellinet
61 else:
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010062 raise log.Error('PowerSupply type not supported:', pwsupply_type)
Pau Espin Pedrol2b959582020-04-17 16:53:14 +020063
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010064 return obj(pwsupply_opt)
65
66
67
68# vim: expandtab tabstop=4 shiftwidth=4