blob: 5b4843798f41ad2989400c2ab666e1450cd495d5 [file] [log] [blame]
Norman James90baede2015-09-02 20:32:49 -05001#!/usr/bin/env python
2
3import sys
Norman James6f8d0422015-09-14 18:48:00 -05004#from gi.repository import GObject
5import gobject
Norman James90baede2015-09-02 20:32:49 -05006import dbus
7import dbus.service
8import dbus.mainloop.glib
9
10if (len(sys.argv) < 2):
11 print "Usage: sensor_manager.py [system name]"
12 exit(1)
Norman James5d78b4d2015-09-05 13:34:34 -050013System = __import__(sys.argv[1])
Norman James90baede2015-09-02 20:32:49 -050014import Openbmc
15
16DBUS_NAME = 'org.openbmc.managers.Sensors'
Norman James362a80f2015-09-14 14:04:39 -050017OBJ_NAME = '/org/openbmc/managers/Sensors'
Norman James90baede2015-09-02 20:32:49 -050018
19class SensorManager(dbus.service.Object):
20 def __init__(self,bus,name):
21 dbus.service.Object.__init__(self,bus,name)
Norman James362a80f2015-09-14 14:04:39 -050022 bus.add_signal_receiver(self.SensorChangedHandler,
Norman James9e6acf92015-09-08 07:00:04 -050023 dbus_interface = 'org.openbmc.SensorValue',
24 signal_name = 'Changed', path_keyword='path')
Norman James32e74e22015-09-15 21:28:06 -050025 bus.add_signal_receiver(self.SensorErrorHandler,
26 dbus_interface = 'org.openbmc.SensorValue',
27 signal_name = 'Error', path_keyword='path')
Norman James90baede2015-09-02 20:32:49 -050028 bus.add_signal_receiver(self.NormalThreshold,
Norman James5d78b4d2015-09-05 13:34:34 -050029 dbus_interface = 'org.openbmc.SensorThreshold',
Norman James90baede2015-09-02 20:32:49 -050030 signal_name = 'Normal', path_keyword='path')
31 bus.add_signal_receiver(self.WarningThreshold,
Norman James5d78b4d2015-09-05 13:34:34 -050032 dbus_interface = 'org.openbmc.SensorThreshold',
Norman James90baede2015-09-02 20:32:49 -050033 signal_name = 'Warning', path_keyword='path')
34 bus.add_signal_receiver(self.CriticalThreshold,
Norman James5d78b4d2015-09-05 13:34:34 -050035 dbus_interface = 'org.openbmc.SensorThreshold',
Norman James90baede2015-09-02 20:32:49 -050036 signal_name = 'Critical', path_keyword='path')
37
38 self.sensor_cache = {}
Norman James362a80f2015-09-14 14:04:39 -050039
40 @dbus.service.method(DBUS_NAME,
Norman James2d1ee892015-09-16 23:13:45 -050041 in_signature='', out_signature='a{sa{sv}}')
Norman James65a295a2015-09-26 22:21:10 -050042 def getSensors(self):
Norman James362a80f2015-09-14 14:04:39 -050043 return self.sensor_cache;
44
Norman James362a80f2015-09-14 14:04:39 -050045
Norman James90baede2015-09-02 20:32:49 -050046 @dbus.service.method(DBUS_NAME,
Norman James5d78b4d2015-09-05 13:34:34 -050047 in_signature='s', out_signature='v')
Norman James90baede2015-09-02 20:32:49 -050048 def getSensor(self,path):
Norman James362a80f2015-09-14 14:04:39 -050049 val = 0
Norman James90baede2015-09-02 20:32:49 -050050 if (self.sensor_cache.has_key(path) == True):
51 val = self.sensor_cache[path]['value']
Norman James362a80f2015-09-14 14:04:39 -050052 else:
Norman James8c6d8382015-10-06 07:47:16 -050053 print "ERROR: Unknown sensor: "+path
Norman James90baede2015-09-02 20:32:49 -050054 return val
Norman James362a80f2015-09-14 14:04:39 -050055
Norman James32e74e22015-09-15 21:28:06 -050056 ## Signal handlers
57 def SensorErrorHandler(self,path = None):
58 self.initSensorEntry(path)
59 self.sensor_cache[path]['error'] = True
60
Norman James362a80f2015-09-14 14:04:39 -050061 def SensorChangedHandler(self,value,units,path = None):
Norman James9e6acf92015-09-08 07:00:04 -050062 self.initSensorEntry(path)
63 self.sensor_cache[path]['value'] = value
64 self.sensor_cache[path]['units'] = units
Norman James90baede2015-09-02 20:32:49 -050065
66 @dbus.service.signal(DBUS_NAME)
67 def CriticalThreshold(self, path = None):
68 print "Critical: "+path
Norman James5d78b4d2015-09-05 13:34:34 -050069 self.initSensorEntry(path)
Norman James90baede2015-09-02 20:32:49 -050070 self.sensor_cache[path]['threshold'] = "CRITICAL"
71
Norman James90baede2015-09-02 20:32:49 -050072 @dbus.service.signal(DBUS_NAME)
73 def WarningThreshold(self, path = None):
Norman James5d78b4d2015-09-05 13:34:34 -050074 print "Warning:"+path
75 self.initSensorEntry(path)
Norman James90baede2015-09-02 20:32:49 -050076 self.sensor_cache[path]['threshold'] = "WARNING"
77
Norman James90baede2015-09-02 20:32:49 -050078 @dbus.service.signal(DBUS_NAME)
79 def NormalThreshold(self, path = None):
80 print "Normal: "+path
Norman James5d78b4d2015-09-05 13:34:34 -050081 self.initSensorEntry(path)
Norman James90baede2015-09-02 20:32:49 -050082 self.sensor_cache[path]['threshold'] = "NORMAL"
83
Norman James5d78b4d2015-09-05 13:34:34 -050084 def initSensorEntry(self,path):
85 if (self.sensor_cache.has_key(path) == False):
86 self.sensor_cache[path] = {}
Norman James90baede2015-09-02 20:32:49 -050087
Norman James5d78b4d2015-09-05 13:34:34 -050088
Norman James90baede2015-09-02 20:32:49 -050089if __name__ == '__main__':
90 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
91 bus = dbus.SessionBus()
92 name = dbus.service.BusName(DBUS_NAME,bus)
93 obj = SensorManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -050094 mainloop = gobject.MainLoop()
Norman James90baede2015-09-02 20:32:49 -050095
96 print "Running Sensor Manager"
97 mainloop.run()
98