blob: 52bca0a57335964eaae475f803ef5228a5294b5f [file] [log] [blame]
Norman James19e45912015-10-04 20:19:41 -05001#!/usr/bin/env python
2
3import os
4import sys
5import gobject
6import dbus
7import dbus.service
8import dbus.mainloop.glib
9import cPickle
10import json
11
12if (len(sys.argv) < 2):
13 print "Usage: inventory_items.py [system name]"
14 exit(1)
15System = __import__(sys.argv[1])
16import Openbmc
17
18
19INTF_NAME = 'org.openbmc.InventoryItem'
20DBUS_NAME = 'org.openbmc.managers.Inventory'
21
22FRUS = System.FRU_INSTANCES
23FRU_PATH = System.FRU_PATH
24
25class 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
43class 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,
Norman James2891c532015-10-06 08:01:56 -050053 'manufacturer' : "",
Norman James19e45912015-10-04 20:19:41 -050054 }
Norman James19e45912015-10-04 20:19:41 -050055 self.cache = True
Norman James19e45912015-10-04 20:19:41 -050056
57 def getItemDict(self):
58 return self.item
59
Norman James19e45912015-10-04 20:19:41 -050060 @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])
Norman James2891c532015-10-06 08:01:56 -050066 self.item[str(k)] = d.getBaseValue()
Norman James19e45912015-10-04 20:19:41 -050067 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 James19e45912015-10-04 20:19:41 -050073 print "Update Fru State: "+str(self.item['state'])
74
Norman James8c6d8382015-10-06 07:47:16 -050075 def setField(self,field,value):
Norman James2891c532015-10-06 08:01:56 -050076 self.item[field] = value
Norman James8c6d8382015-10-06 07:47:16 -050077
Norman James19e45912015-10-04 20:19:41 -050078 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 James8c6d8382015-10-06 07:47:16 -050091 try:
92 output = open(self.getCacheFilename(), 'wb')
93 ## just pickle dict not whole object
Norman James2891c532015-10-06 08:01:56 -050094 cPickle.dump(self.item,output)
Norman James8c6d8382015-10-06 07:47:16 -050095 except Exception as e:
96 print "ERROR: "+str(e)
97 finally:
98 output.close()
Norman James19e45912015-10-04 20:19:41 -050099
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 James8c6d8382015-10-06 07:47:16 -0500107 try:
108 p = open(filename, 'rb')
109 data2 = cPickle.load(p)
110 for k in data2.keys():
Norman James2891c532015-10-06 08:01:56 -0500111 self.item[k] = data2[k]
Norman James8c6d8382015-10-06 07:47:16 -0500112 except Exception as e:
113 print "ERROR: " +str(e)
114 finally:
115 p.close()
Norman James19e45912015-10-04 20:19:41 -0500116
117
118if __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 James8c6d8382015-10-06 07:47:16 -0500128 obj.setField('is_fru',FRUS[f]['is_fru'])
129 obj.setField('fru_type',FRUS[f]['fru_type'])
Norman James19e45912015-10-04 20:19:41 -0500130 obj.loadFromCache();
131 obj_parent.addItem(obj)
132
133 print "Running Inventory Manager"
134 mainloop.run()
135