Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 2 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 3 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 4 | try: # python 2 |
| 5 | import gobject |
| 6 | except ImportError: # python 3 |
| 7 | from gi.repository import GObject as gobject |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 8 | import dbus |
| 9 | import dbus.service |
| 10 | import dbus.mainloop.glib |
Brad Bishop | ee1b154 | 2016-05-12 16:55:00 -0400 | [diff] [blame] | 11 | import obmc.sensors |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 12 | from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus |
Brad Bishop | 44d4ad9 | 2016-08-30 19:14:46 -0400 | [diff] [blame] | 13 | |
| 14 | try: |
| 15 | import obmc_system_config as System |
| 16 | has_system = True |
| 17 | except ImportError: |
| 18 | has_system = False |
Yi Li | 54decc8 | 2016-05-05 17:42:56 +0800 | [diff] [blame] | 19 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 20 | DBUS_NAME = 'org.openbmc.Sensors' |
| 21 | OBJ_PATH = '/org/openbmc/sensors' |
| 22 | |
| 23 | |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 24 | class SensorManager(DbusProperties, DbusObjectManager): |
| 25 | def __init__(self, bus, name): |
Brad Bishop | f47f5fa | 2016-09-08 22:29:01 -0400 | [diff] [blame] | 26 | super(SensorManager, self).__init__( |
| 27 | conn=bus, |
| 28 | object_path=name) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 29 | |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 30 | @dbus.service.method( |
| 31 | DBUS_NAME, in_signature='ss', out_signature='') |
| 32 | def register(self, object_name, obj_path): |
| 33 | if obj_path not in self.objects: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 34 | print("Register: "+object_name+" : "+obj_path) |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 35 | sensor = eval('obmc.sensors.'+object_name+'(bus,obj_path)') |
| 36 | self.add(obj_path, sensor) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 37 | |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 38 | @dbus.service.method( |
| 39 | DBUS_NAME, in_signature='s', out_signature='') |
| 40 | def delete(self, obj_path): |
| 41 | if obj_path in self.objects: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 42 | print("Delete: "+obj_path) |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 43 | self.remove(obj_path) |
| 44 | |
| 45 | def SensorChange(self, value, path=None): |
| 46 | if path in self.objects: |
| 47 | self.objects[path].setValue(value) |
| 48 | else: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 49 | print("ERROR: Sensor not found: "+path) |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 50 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 51 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 52 | if __name__ == '__main__': |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 53 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 54 | bus = get_dbus() |
| 55 | root_sensor = SensorManager(bus, OBJ_PATH) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 56 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 57 | # instantiate non-polling sensors |
| 58 | # these don't need to be in separate process |
Brad Bishop | 44d4ad9 | 2016-08-30 19:14:46 -0400 | [diff] [blame] | 59 | if has_system: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 60 | for (id, the_sensor) in list(System.MISC_SENSORS.items()): |
Brad Bishop | 44d4ad9 | 2016-08-30 19:14:46 -0400 | [diff] [blame] | 61 | sensor_class = the_sensor['class'] |
| 62 | obj_path = System.ID_LOOKUP['SENSOR'][id] |
| 63 | sensor_obj = getattr(obmc.sensors, sensor_class)(bus, obj_path) |
| 64 | if 'os_path' in the_sensor: |
| 65 | sensor_obj.sysfs_attr = the_sensor['os_path'] |
| 66 | root_sensor.add(obj_path, sensor_obj) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 67 | |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 68 | mainloop = gobject.MainLoop() |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 69 | |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 70 | root_sensor.unmask_signals() |
| 71 | name = dbus.service.BusName(DBUS_NAME, bus) |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 72 | print("Starting sensor manager") |
Brad Bishop | 670f255 | 2016-08-30 19:12:54 -0400 | [diff] [blame] | 73 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 74 | |
| 75 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |