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