Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import gobject |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | import cPickle |
| 10 | import json |
| 11 | |
| 12 | if (len(sys.argv) < 2): |
| 13 | print "Usage: inventory_items.py [system name]" |
| 14 | exit(1) |
| 15 | System = __import__(sys.argv[1]) |
| 16 | import Openbmc |
| 17 | |
| 18 | |
| 19 | INTF_NAME = 'org.openbmc.InventoryItem' |
| 20 | DBUS_NAME = 'org.openbmc.managers.Inventory' |
| 21 | |
| 22 | FRUS = System.FRU_INSTANCES |
| 23 | FRU_PATH = System.FRU_PATH |
| 24 | |
| 25 | class Inventory(dbus.service.Object): |
| 26 | def __init__(self,bus,name): |
| 27 | dbus.service.Object.__init__(self,bus,name) |
| 28 | self.objects = [ ] |
| 29 | |
| 30 | def addItem(self,item): |
| 31 | self.objects.append(item) |
| 32 | |
| 33 | @dbus.service.method("org.openbmc.managers.Inventory", |
| 34 | in_signature='', out_signature='a{sa{sv}}') |
| 35 | def getItems(self): |
| 36 | tmp_obj = {} |
| 37 | for item in self.objects: |
| 38 | tmp_obj[str(item.item['name'])]=item.getItemDict() |
| 39 | return tmp_obj |
| 40 | |
| 41 | |
| 42 | |
| 43 | class InventoryItem(dbus.service.Object): |
| 44 | def __init__(self,bus,name): |
| 45 | dbus.service.Object.__init__(self,bus,name) |
| 46 | ## store all properties in a dict so can easily |
| 47 | ## send on dbus |
| 48 | self.item = { |
| 49 | 'name' : name, |
| 50 | 'is_fru' : False, |
| 51 | 'fru_type' : 0, |
| 52 | 'state' : 0, |
| 53 | 'data' : { 'manufacturer' : "" } |
| 54 | } |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 55 | self.cache = True |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 56 | |
| 57 | def getItemDict(self): |
| 58 | return self.item |
| 59 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 60 | @dbus.service.method('org.openbmc.InventoryItem', |
| 61 | in_signature='a{sv}', out_signature='') |
| 62 | def update(self,data): |
| 63 | ## translate dbus data into basic data types |
| 64 | for k in data.keys(): |
| 65 | d = Openbmc.DbusProperty(k,data[k]) |
| 66 | self.item['data'][str(k)] = d.getBaseValue() |
| 67 | self.saveToCache() |
| 68 | |
| 69 | @dbus.service.method("org.openbmc.SensorValue", |
| 70 | in_signature='y', out_signature='') |
| 71 | def setValue(self,data): |
| 72 | self.item['state'] = data |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 73 | print "Update Fru State: "+str(self.item['state']) |
| 74 | |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame^] | 75 | def setField(self,field,value): |
| 76 | self.item['data'][field] = value |
| 77 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 78 | def isCached(self): |
| 79 | return self.cache |
| 80 | |
| 81 | def getCacheFilename(self): |
| 82 | global FRU_PATH |
| 83 | name = self.item['name'].replace('/','.') |
| 84 | filename = FRU_PATH+name[1:]+".fru" |
| 85 | return filename |
| 86 | |
| 87 | def saveToCache(self): |
| 88 | if (self.isCached() == False): |
| 89 | return |
| 90 | print "Caching: "+self.item['name'] |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame^] | 91 | try: |
| 92 | output = open(self.getCacheFilename(), 'wb') |
| 93 | ## just pickle dict not whole object |
| 94 | cPickle.dump(self.item['data'],output) |
| 95 | except Exception as e: |
| 96 | print "ERROR: "+str(e) |
| 97 | finally: |
| 98 | output.close() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 99 | |
| 100 | def loadFromCache(self): |
| 101 | if (self.isCached() == False): |
| 102 | return; |
| 103 | ## overlay with pickled data |
| 104 | filename=self.getCacheFilename() |
| 105 | if (os.path.isfile(filename)): |
| 106 | print "Loading from cache: "+filename |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame^] | 107 | try: |
| 108 | p = open(filename, 'rb') |
| 109 | data2 = cPickle.load(p) |
| 110 | for k in data2.keys(): |
| 111 | self.item['data'][k] = data2[k] |
| 112 | except Exception as e: |
| 113 | print "ERROR: " +str(e) |
| 114 | finally: |
| 115 | p.close() |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 116 | |
| 117 | |
| 118 | if __name__ == '__main__': |
| 119 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 120 | bus = dbus.SessionBus() |
| 121 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 122 | mainloop = gobject.MainLoop() |
| 123 | obj_parent = Inventory(bus, '/org/openbmc/managers/Inventory') |
| 124 | |
| 125 | for f in FRUS.keys(): |
| 126 | obj_path=f.replace("<inventory_root>",System.INVENTORY_ROOT) |
| 127 | obj = InventoryItem(bus,obj_path) |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame^] | 128 | obj.setField('is_fru',FRUS[f]['is_fru']) |
| 129 | obj.setField('fru_type',FRUS[f]['fru_type']) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 130 | obj.loadFromCache(); |
| 131 | obj_parent.addItem(obj) |
| 132 | |
| 133 | print "Running Inventory Manager" |
| 134 | mainloop.run() |
| 135 | |