WUT Velma robot API
subsystem_diag.py
Go to the documentation of this file.
1 
4 
5 # Copyright (c) 2014, Robot Control and Pattern Recognition Group, Warsaw University of Technology
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # * Neither the name of the Warsaw University of Technology nor the
16 # names of its contributors may be used to endorse or promote products
17 # derived from this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 # DISCLAIMED. IN NO EVENT SHALL <COPYright HOLDER> BE LIABLE FOR ANY
23 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 import xml.dom.minidom as minidom
31 
34  def __init__(self):
35  self.name = None
36  self.value = None
37 
39  def __init__(self):
40  self.state_name = None
41  self.prev_state_name = None
42  self.reason = None
43  self.switch_interval = None
44  self.predicates = None
45 
46  def __init__(self):
47  self.history = []
49  self.current_predicates = None
50  self.current_period = None
51  self.period_histogram = []
52 
53 def parsePredicates(pred_str):
54  pred_list = pred_str.split(",")
55  result = []
56  for cp in pred_list:
57  k_v = cp.split(":")
58  if len(k_v) == 2:
60  pv.name = k_v[0].strip()
61  value_str = k_v[1].strip()
62  if value_str == 't':
63  pv.value = True
64  elif value_str == 'f':
65  pv.value = False
66  else:
67  raise ValueError("wrong predicate value: '" + value_str + "' for predicate: '" + pv.name + "'")
68  result.append(pv)
69  return result
70 
72  result = SubsystemDiag()
73 
74  dom = minidom.parseString(diag_xml)
75  mcd = dom.getElementsByTagName("mcd")
76  if len(mcd) != 1:
77  return result
78 
79  hist = mcd[0].getElementsByTagName("h")
80  if len(hist) == 1:
81  ss_list = hist[0].getElementsByTagName("ss")
82  for ss in ss_list:
84  ss_ev.state_name = ss.getAttribute("n")
85  ss_ev.prev_state_name = ss.getAttribute("p")
86  ss_ev.reason = ss.getAttribute("r")
87  ss_ev.switch_interval = float(ss.getAttribute("t"))
88  ss_ev.predicates = parsePredicates(ss.getAttribute("e"))
89  result.history.append(ss_ev)
90 
91  si = mcd[0].getElementsByTagName("si")
92  if len(si) == 1:
93  ss_list = si[0].getElementsByTagName("ss")
94  for ss in ss_list:
96  ss_ev.state_name = ss.getAttribute("n")
97  ss_ev.prev_state_name = ss.getAttribute("p")
98  ss_ev.reason = ss.getAttribute("r")
99  ss_ev.switch_interval = float(ss.getAttribute("t"))
100  ss_ev.predicates = parsePredicates(ss.getAttribute("e"))
101  result.state_switch_info.append(ss_ev)
102 
103  current_predicates = mcd[0].getElementsByTagName("pr")
104  if len(current_predicates) == 1:
105  result.current_predicates = parsePredicates(current_predicates[0].getAttribute("v"))
106 
107  period = mcd[0].getElementsByTagName("p")
108  if len(period) == 1:
109  result.current_period = float(period[0].childNodes[0].data)
110 
111  # period histogram
112  ph = mcd[0].getElementsByTagName("ph")
113  if len(ph) == 1:
114  histogram_str = ph[0].childNodes[0].data
115  histogram_str = histogram_str.strip()
116  histogram_list_str = histogram_str.split()
117  for v_str in histogram_list_str:
118  result.period_histogram.append( int(v_str) )
119 
120  return result
121