Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | #from gi.repository import GObject |
| 5 | import gobject |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | import Openbmc |
| 10 | |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 11 | DBUS_NAME = 'org.openbmc.sensor.Power8Virtual' |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 12 | OBJ_PATH = '/org/openbmc/sensor/virtual/' |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 13 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 14 | class SensorValue(Openbmc.DbusProperties): |
| 15 | IFACE_NAME = 'org.openbmc.SensorValue' |
| 16 | def __init__(self): |
| 17 | Openbmc.DbusProperties.__init__(self) |
| 18 | self.Set(SensorValue.IFACE_NAME,'units',"") |
| 19 | |
| 20 | @dbus.service.method(IFACE_NAME, |
| 21 | in_signature='v', out_signature='') |
| 22 | def setValue(self,value): |
| 23 | changed = False |
| 24 | try: |
| 25 | old_value = self.Get(SensorValue.IFACE_NAME,'value') |
| 26 | if (value != old_value): |
| 27 | changed = True |
| 28 | except: |
| 29 | changed = True |
| 30 | |
| 31 | if (changed == True): |
| 32 | self.Set(SensorValue.IFACE_NAME,'value',value) |
| 33 | self.Changed(self.getValue(),self.getUnits()) |
| 34 | |
| 35 | |
| 36 | |
| 37 | @dbus.service.method(IFACE_NAME, |
| 38 | in_signature='', out_signature='v') |
| 39 | def getValue(self): |
| 40 | return self.Get(SensorValue.IFACE_NAME,'value') |
| 41 | |
| 42 | @dbus.service.method(IFACE_NAME, |
| 43 | in_signature='', out_signature='s') |
| 44 | def getUnits(self): |
| 45 | return self.Get(SensorValue.IFACE_NAME,'units') |
| 46 | |
| 47 | @dbus.service.signal(IFACE_NAME,signature='vs') |
| 48 | def Changed(self,value,units): |
| 49 | pass |
| 50 | |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 51 | class VirtualSensor(SensorValue): |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 52 | def __init__(self,bus,name): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 53 | SensorValue.__init__(self) |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 54 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 55 | |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 56 | HS_IFACE = 'org.openbmc.HostStatus' |
| 57 | class HostStatusSensor(SensorValue): |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 58 | def __init__(self,bus,name): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 59 | SensorValue.__init__(self) |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 60 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 61 | |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 62 | ##override setValue method |
| 63 | @dbus.service.method(SensorValue.IFACE_NAME, |
| 64 | in_signature='v', out_signature='') |
| 65 | def setValue(self,value): |
| 66 | SensorValue.setValue(self,value) |
| 67 | if (value == "BOOTED"): |
| 68 | self.Booted() |
| 69 | |
| 70 | @dbus.service.signal(HS_IFACE,signature='') |
| 71 | def Booted(self): |
| 72 | pass |
| 73 | |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 74 | |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 75 | |
| 76 | if __name__ == '__main__': |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 77 | |
| 78 | sensors = { |
| 79 | 'BootProgress' : None, |
| 80 | 'OperatingSystemStatus' : None, |
| 81 | 'BootCount' : None, |
| 82 | 'OccStatus' : None, |
| 83 | } |
| 84 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 85 | bus = Openbmc.getDBus() |
| 86 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 87 | for instance in sensors.keys(): |
| 88 | sensors[instance]= VirtualSensor(bus,OBJ_PATH+instance) |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 89 | |
Norman James | 18d4591 | 2015-10-18 20:45:06 -0500 | [diff] [blame] | 90 | sensors['HostStatus'] = HostStatusSensor(bus,OBJ_PATH+"HostStatus") |
| 91 | mainloop = gobject.MainLoop() |
| 92 | |
| 93 | ## Initialize sensors |
| 94 | sensors['BootProgress'].setValue("INACTIVE") |
| 95 | sensors['HostStatus'].setValue("OFF") |
| 96 | sensors['OperatingSystemStatus'].setValue("OFF") |
| 97 | sensors['BootCount'].setValue(0) |
| 98 | sensors['OccStatus'].setValue(0) |
| 99 | |
| 100 | print "Starting virtual sensors" |
| 101 | mainloop.run() |
Norman James | 91dac39 | 2015-10-14 09:38:55 -0500 | [diff] [blame] | 102 | |