1# _sel.py
2#
3# openipmi GUI handling for SEL data
4#
5# Author: MontaVista Software, Inc.
6#         Corey Minyard <minyard@mvista.com>
7#         source@mvista.com
8#
9# Copyright 2005 MontaVista Software Inc.
10#
11#  This program is free software; you can redistribute it and/or
12#  modify it under the terms of the GNU Lesser General Public License
13#  as published by the Free Software Foundation; either version 2 of
14#  the License, or (at your option) any later version.
15#
16#
17#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28#  You should have received a copy of the GNU Lesser General Public
29#  License along with this program; if not, write to the Free
30#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31#
32
33import OpenIPMI
34import sys
35from openipmigui import gui_list
36from openipmigui import gui_popup
37from openipmigui import _misc
38
39class EventData:
40    def __init__(self, slist, ev, has_second_data):
41        self.slist = slist
42        self.ev = ev
43        self.second_key = None
44        return
45
46    def HandleMenu(self, event, idx, point):
47        gui_popup.popup(self.slist, event,
48                        [ ("Delete", self.delete, idx) ],
49                        point)
50        return
51
52    def delete(self, idx):
53        self.slist.DelItem(self.key)
54        if (self.second_key != None):
55            self.slist.DelItem(self.second_key)
56            pass
57        self.ev.delete()
58        return
59
60    pass
61
62class EventInfo:
63    def __init__(self):
64        self.sensor = None
65        self.val = None
66        return
67
68    def threshold_event_cb(self, sensor, event_spec, raw_set, raw,
69                           value_set, value, event):
70        self.sensor = sensor.get_name()
71        self.val = event_spec;
72        if (value_set):
73            self.val += str(value)
74            pass
75        if (raw_set):
76            self.val += '(' + str(raw) + ')'
77            pass
78        return
79
80    def discrete_event_cb(self, sensor, event_spec, severity, old_severity,
81                          event):
82        self.sensor = sensor.get_name()
83        self.val = (event_spec + ' ' + str(severity) +
84                    '(' + str(old_severity) + ')')
85        return
86
87    pass
88
89class SELDisplay(gui_list.List):
90    def __init__(self, o, type):
91        self.o = o
92        self.type = type
93        self.numevents = 0
94        gui_list.List.__init__(self, "SEL for " + o.get_name(),
95                               [ ("RecNum", 64), ("Type", 40),
96                                 ("Time/Sensor", 300), ("Data", 400) ])
97
98        self.events = [ ]
99
100        ev = o.first_event()
101        while (ev != None):
102            self.AddEvent(ev)
103            ev = o.next_event(ev)
104            pass
105
106        self.AfterDone()
107        return
108
109    def AddEvent(self, ev):
110        evinfo = EventInfo()
111        ev.call_handler(evinfo)
112
113        data = EventData(self, ev, evinfo.sensor != None)
114        self.events.append(data)
115        data.key = self.Append(str(ev.get_record_id()),
116                               [ str(ev.get_type()), str(ev.get_timestamp()),
117                                 _misc.HexArrayToStr(ev.get_data()) ],
118                               data)
119        if (evinfo.sensor):
120            # Can only delete the using the first item.
121            data.second_key = self.Append("", [ "", evinfo.sensor, evinfo.val ],
122                                          data)
123            pass
124        return
125
126    def ok(self):
127        self.Close()
128        return
129
130    def clear(self):
131        for data in self.events:
132            data.ev.delete()
133            pass
134        self.events = [ ]
135        self.DeleteAllItems()
136        return
137
138    def do_on_close(self):
139        self.events = None
140        return
141
142    pass
143
144class DomainSELDisplay(SELDisplay):
145    def __init__(self, domain_id):
146        self.domain_id = domain_id
147        self.init = True
148        domain_id.to_domain(self)
149        return
150
151    def do_on_close(self):
152        self.init = False
153        self.domain_id.to_domain(self)
154        SELDisplay.do_on_close(self)
155        return
156
157    def domain_cb(self, domain):
158        if (self.init):
159            SELDisplay.__init__(self, domain, "domain")
160            domain.add_event_handler(self)
161        else:
162            domain.remove_event_handler(self)
163            pass
164        return
165
166    def event_cb(self, domain, ev):
167        self.AddEvent(ev)
168        return
169
170    pass
171
172class MCSELDisplay(SELDisplay):
173    def __init__(self, mc_id):
174        self.init = True
175        self.mc_id = mc_id
176        mc_id.to_mc(self)
177        return
178
179    def do_on_close(self):
180        self.init = False
181        self.mc_id.to_mc(self)
182        SELDisplay.do_on_close(self)
183        return
184
185    def mc_cb(self, mc):
186        domain = mc.get_domain()
187        if self.init:
188            SELDisplay.__init__(self, mc, "MC")
189            domain.add_event_handler(self)
190        else:
191            domain.remove_event_handler(self)
192            pass
193        return
194
195    def event_cb(self, domain, ev):
196        mc_id = ev.get_mc_id()
197        if (mc_id.cmp(self.mc_id) == 0):
198            self.AddEvent(ev)
199            pass
200        return
201
202    pass
203