blob: f1af961530b58fa353daa8232e48463580fe0095 [file] [log] [blame]
Norman James42c1be82015-10-22 14:34:26 -05001#!/usr/bin/python -u
Norman James90baede2015-09-02 20:32:49 -05002
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 Jamesa3e47c42015-10-18 14:43:10 -050017OBJ_NAME = '/org/openbmc/sensors'
18ENUM_INTF = 'org.openbmc.Object.Enumerate'
Norman James90baede2015-09-02 20:32:49 -050019
Norman Jamescfc2b442015-10-31 17:31:46 -050020class SensorManager(Openbmc.DbusProperties):
Norman James90baede2015-09-02 20:32:49 -050021 def __init__(self,bus,name):
22 dbus.service.Object.__init__(self,bus,name)
Norman James362a80f2015-09-14 14:04:39 -050023 bus.add_signal_receiver(self.SensorChangedHandler,
Norman James9e6acf92015-09-08 07:00:04 -050024 dbus_interface = 'org.openbmc.SensorValue',
25 signal_name = 'Changed', path_keyword='path')
Norman James32e74e22015-09-15 21:28:06 -050026 bus.add_signal_receiver(self.SensorErrorHandler,
27 dbus_interface = 'org.openbmc.SensorValue',
28 signal_name = 'Error', path_keyword='path')
Norman James90baede2015-09-02 20:32:49 -050029 bus.add_signal_receiver(self.NormalThreshold,
Norman James5d78b4d2015-09-05 13:34:34 -050030 dbus_interface = 'org.openbmc.SensorThreshold',
Norman James90baede2015-09-02 20:32:49 -050031 signal_name = 'Normal', path_keyword='path')
32 bus.add_signal_receiver(self.WarningThreshold,
Norman James5d78b4d2015-09-05 13:34:34 -050033 dbus_interface = 'org.openbmc.SensorThreshold',
Norman James90baede2015-09-02 20:32:49 -050034 signal_name = 'Warning', path_keyword='path')
35 bus.add_signal_receiver(self.CriticalThreshold,
Norman James5d78b4d2015-09-05 13:34:34 -050036 dbus_interface = 'org.openbmc.SensorThreshold',
Norman James90baede2015-09-02 20:32:49 -050037 signal_name = 'Critical', path_keyword='path')
38
39 self.sensor_cache = {}
Norman Jamescfc2b442015-10-31 17:31:46 -050040 self.ObjectAdded(name,DBUS_NAME);
41 self.ObjectAdded(name,ENUM_INTF);
Norman James362a80f2015-09-14 14:04:39 -050042
Norman Jamesa3e47c42015-10-18 14:43:10 -050043 @dbus.service.method(ENUM_INTF,
Norman James2d1ee892015-09-16 23:13:45 -050044 in_signature='', out_signature='a{sa{sv}}')
Norman Jamesa3e47c42015-10-18 14:43:10 -050045 def enumerate(self):
Norman James362a80f2015-09-14 14:04:39 -050046 return self.sensor_cache;
47
Norman James362a80f2015-09-14 14:04:39 -050048
Norman James90baede2015-09-02 20:32:49 -050049 @dbus.service.method(DBUS_NAME,
Norman James5d78b4d2015-09-05 13:34:34 -050050 in_signature='s', out_signature='v')
Norman James90baede2015-09-02 20:32:49 -050051 def getSensor(self,path):
Norman James362a80f2015-09-14 14:04:39 -050052 val = 0
Norman James90baede2015-09-02 20:32:49 -050053 if (self.sensor_cache.has_key(path) == True):
54 val = self.sensor_cache[path]['value']
Norman James362a80f2015-09-14 14:04:39 -050055 else:
Norman James8c6d8382015-10-06 07:47:16 -050056 print "ERROR: Unknown sensor: "+path
Norman James90baede2015-09-02 20:32:49 -050057 return val
Norman James362a80f2015-09-14 14:04:39 -050058
Norman James32e74e22015-09-15 21:28:06 -050059 ## Signal handlers
60 def SensorErrorHandler(self,path = None):
61 self.initSensorEntry(path)
62 self.sensor_cache[path]['error'] = True
63
Norman James362a80f2015-09-14 14:04:39 -050064 def SensorChangedHandler(self,value,units,path = None):
Norman James9e6acf92015-09-08 07:00:04 -050065 self.initSensorEntry(path)
66 self.sensor_cache[path]['value'] = value
67 self.sensor_cache[path]['units'] = units
Norman James90baede2015-09-02 20:32:49 -050068
69 @dbus.service.signal(DBUS_NAME)
70 def CriticalThreshold(self, path = None):
71 print "Critical: "+path
Norman James5d78b4d2015-09-05 13:34:34 -050072 self.initSensorEntry(path)
Norman James90baede2015-09-02 20:32:49 -050073 self.sensor_cache[path]['threshold'] = "CRITICAL"
74
Norman James90baede2015-09-02 20:32:49 -050075 @dbus.service.signal(DBUS_NAME)
76 def WarningThreshold(self, path = None):
Norman James5d78b4d2015-09-05 13:34:34 -050077 print "Warning:"+path
78 self.initSensorEntry(path)
Norman James90baede2015-09-02 20:32:49 -050079 self.sensor_cache[path]['threshold'] = "WARNING"
80
Norman James90baede2015-09-02 20:32:49 -050081 @dbus.service.signal(DBUS_NAME)
82 def NormalThreshold(self, path = None):
83 print "Normal: "+path
Norman James5d78b4d2015-09-05 13:34:34 -050084 self.initSensorEntry(path)
Norman James90baede2015-09-02 20:32:49 -050085 self.sensor_cache[path]['threshold'] = "NORMAL"
86
Norman James5d78b4d2015-09-05 13:34:34 -050087 def initSensorEntry(self,path):
88 if (self.sensor_cache.has_key(path) == False):
89 self.sensor_cache[path] = {}
Norman James90baede2015-09-02 20:32:49 -050090
Norman James5d78b4d2015-09-05 13:34:34 -050091
Norman James90baede2015-09-02 20:32:49 -050092if __name__ == '__main__':
93 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -050094 bus = Openbmc.getDBus()
Norman James90baede2015-09-02 20:32:49 -050095 name = dbus.service.BusName(DBUS_NAME,bus)
96 obj = SensorManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -050097 mainloop = gobject.MainLoop()
Norman James90baede2015-09-02 20:32:49 -050098
99 print "Running Sensor Manager"
100 mainloop.run()
101