[PATCH] osmo-gsm-tester[master]: Introduce PowerCtrl interface and PowerCtrlSispm

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Thu Mar 8 20:02:00 UTC 2018


Review at  https://gerrit.osmocom.org/7167

Introduce PowerCtrl interface and PowerCtrlSispm

File powerctrl.py defines the interface to be used by child classes
implementing it. It also provides helpers to allocate a child class
based on configuration provided ('type' field).

File powerctrl_sispm.py is an implementation using pysispm [1], as it's
the one used to control the programmable power socket we have right now.

This kind of class will be used in later commits by Nanobts class, as we
want to poweroff the Nanobts completelly when not in use.

Using it requires the following extra dependencies:
$ apt-get install python3-usb
$ pip3 install pysispm

Related: OS#3040

[1] https://github.com/xypron/pysispm

Change-Id: I981c260eca1a61657147e6d83b4226618088223c
---
A src/osmo_gsm_tester/powerctrl.py
A src/osmo_gsm_tester/powerctrl_sispm.py
2 files changed, 144 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/67/7167/1

diff --git a/src/osmo_gsm_tester/powerctrl.py b/src/osmo_gsm_tester/powerctrl.py
new file mode 100644
index 0000000..9d5aa51
--- /dev/null
+++ b/src/osmo_gsm_tester/powerctrl.py
@@ -0,0 +1,69 @@
+# osmo_gsm_tester: class defining a Power Ctrl object
+#
+# Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH
+#
+# Author: Pau Espin Pedrol <pespin at sysmocom.de>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from abc import ABCMeta, abstractmethod
+from . import log, event_loop
+
+class PowerCtrl(log.Origin, metaclass=ABCMeta):
+
+##############
+# PROTECTED
+##############
+    def __init__(self, conf, name):
+        super().__init__(log.C_RUN, name)
+        self.conf = conf
+
+########################
+# PUBLIC - INTERNAL API
+########################
+    @abstractmethod
+    def powered(self):
+        'Get whether the device is powered on or off'
+        pass
+
+    @abstractmethod
+    def power_set(self, onoff):
+        'Turn on (onoff=True) or off (onof=False) the device'
+        pass
+
+    def power_cycle(self, sleep=0):
+        'Turns off the device, waits N.N seconds, then turn on the device'
+        self.power_set(False)
+        event_loop.sleep(self, sleep)
+        self.power_set(True)
+
+
+from . import powerctrl_sispm
+
+KNOWN_PWC_TYPES = {
+        'sispm' : powerctrl_sispm.PowerCtrlSispm,
+}
+
+def register_type(name, clazz):
+    KNOWN_PWC_TYPES[name] = clazz
+
+def get_instance_by_type(pwc_type, pwc_opt):
+    obj = KNOWN_PWC_TYPES.get(pwc_type, None)
+    if not obj:
+        raise log.Error('PowerCtrl type not supported:', pwc_type)
+    return obj(pwc_opt)
+
+
+
+# vim: expandtab tabstop=4 shiftwidth=4
diff --git a/src/osmo_gsm_tester/powerctrl_sispm.py b/src/osmo_gsm_tester/powerctrl_sispm.py
new file mode 100644
index 0000000..f4f9598
--- /dev/null
+++ b/src/osmo_gsm_tester/powerctrl_sispm.py
@@ -0,0 +1,75 @@
+# osmo_gsm_tester: class defining a Power Ctrl object
+#
+# Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH
+#
+# Author: Pau Espin Pedrol <pespin at sysmocom.de>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import sispm
+
+from . import log
+from .powerctrl import PowerCtrl
+
+class PowerCtrlSispm(PowerCtrl):
+
+    device = None
+
+##############
+# PROTECTED
+##############
+    def __init__(self, conf):
+        super().__init__(conf, 'sispm')
+        mydevid = conf.get('device', None)
+        if mydevid is None:
+            raise log.Error('No "device" attribute provided in power_ctrl conf!')
+        self.set_name('sispm-'+mydevid)
+        myport = conf.get('port', None)
+        if myport is None:
+            raise log.Error('No "port" attribute provided in power_ctrl conf!')
+        if not int(myport):
+            raise log.Error('Wrong non numeric "port" attribute provided in power_ctrl conf!')
+        self.port = int(myport)
+
+        devices = sispm.connect()
+        for d in devices:
+            did = sispm.getid(d)
+            self.dbg('detected device:', did)
+            if did == mydevid:
+                self.device = d
+                dmin = sispm.getminport(d)
+                dmax = sispm.getmaxport(d)
+                self.dbg('found matching device: %s min=%d max=%d' % (did, dmin, dmax))
+        if dmin > self.port or dmax < self.port:
+            raise log.Error('Out of range "port" attribute provided in power_ctrl conf!')
+
+
+########################
+# PUBLIC - INTERNAL API
+########################
+    def powered(self):
+        'Get whether the device is powered on or off'
+        return sispm.getstatus(self.device, self.port)
+
+    def power_set(self, onoff):
+        'Turn on (onoff=True) or off (onof=False) the device'
+        if onoff:
+            self.dbg('switchon')
+            sispm.switchon(self.device, self.port)
+        else:
+            self.dbg('switchoff')
+            sispm.switchoff(self.device, self.port)
+
+
+# vim: expandtab tabstop=4 shiftwidth=4

-- 
To view, visit https://gerrit.osmocom.org/7167
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I981c260eca1a61657147e6d83b4226618088223c
Gerrit-PatchSet: 1
Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list