|
| 1 | +# Copyright 2023 The Autoware Contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from diagnostic_msgs.msg import DiagnosticStatus |
| 16 | +from rclpy.time import Time |
| 17 | + |
| 18 | + |
| 19 | +class DummyStatus: |
| 20 | + def __init__(self): |
| 21 | + self.level = DiagnosticStatus.STALE |
| 22 | + |
| 23 | + |
| 24 | +class BaseUnit: |
| 25 | + def __init__(self, status=DummyStatus()): |
| 26 | + self._parents = [] |
| 27 | + self._children = [] |
| 28 | + self._path = None |
| 29 | + self._type = None |
| 30 | + self._status = status |
| 31 | + |
| 32 | + @property |
| 33 | + def parents(self): |
| 34 | + return self._parents |
| 35 | + |
| 36 | + @property |
| 37 | + def children(self): |
| 38 | + return self._children |
| 39 | + |
| 40 | + @property |
| 41 | + def path(self): |
| 42 | + return self._path |
| 43 | + |
| 44 | + @property |
| 45 | + def kind(self): |
| 46 | + return self._type |
| 47 | + |
| 48 | + |
| 49 | +class NodeUnit(BaseUnit): |
| 50 | + def __init__(self, struct): |
| 51 | + super().__init__() |
| 52 | + self._path = struct.path |
| 53 | + self._type = struct.type |
| 54 | + |
| 55 | + def update(self, status): |
| 56 | + self._status = status |
| 57 | + |
| 58 | + @property |
| 59 | + def level(self): |
| 60 | + return self._status.level |
| 61 | + |
| 62 | + |
| 63 | +class DiagUnit(BaseUnit): |
| 64 | + def __init__(self, struct): |
| 65 | + super().__init__() |
| 66 | + self._path = struct.path |
| 67 | + self._name = struct.name |
| 68 | + self._type = "diag" |
| 69 | + |
| 70 | + def update(self, status): |
| 71 | + self._status = status |
| 72 | + |
| 73 | + @property |
| 74 | + def level(self): |
| 75 | + return self._status.level |
| 76 | + |
| 77 | + |
| 78 | +class UnitLink: |
| 79 | + def __init__(self, parent: BaseUnit, child: BaseUnit): |
| 80 | + self._parent = parent |
| 81 | + self._child = child |
| 82 | + parent._children.append(self) |
| 83 | + child._parents.append(self) |
| 84 | + |
| 85 | + def update(self, status): |
| 86 | + self.status = status |
| 87 | + |
| 88 | + @property |
| 89 | + def parent(self): |
| 90 | + return self._parent |
| 91 | + |
| 92 | + @property |
| 93 | + def child(self): |
| 94 | + return self._child |
| 95 | + |
| 96 | + |
| 97 | +class Graph: |
| 98 | + def __init__(self, msg): |
| 99 | + self._struct_stamp = Time.from_msg(msg.stamp) |
| 100 | + self._status_stamp = None |
| 101 | + self._id = msg.id |
| 102 | + self._nodes = [NodeUnit(struct) for struct in msg.nodes] |
| 103 | + self._diags = [DiagUnit(struct) for struct in msg.diags] |
| 104 | + self._units = self._nodes + self._diags |
| 105 | + self._links = [] |
| 106 | + for struct in msg.links: |
| 107 | + units = self._diags if struct.is_leaf else self._nodes |
| 108 | + nodes = self._nodes |
| 109 | + self._links.append(UnitLink(nodes[struct.parent], units[struct.child])) |
| 110 | + |
| 111 | + def update(self, msg): |
| 112 | + if msg.id == self._id: |
| 113 | + self._status_stamp = Time.from_msg(msg.stamp) |
| 114 | + for node, status in zip(self._nodes, msg.nodes): |
| 115 | + node.update(status) |
| 116 | + for diag, status in zip(self._diags, msg.diags): |
| 117 | + diag.update(status) |
| 118 | + for link, status in zip(self._links, msg.links): |
| 119 | + link.update(status) |
| 120 | + |
| 121 | + @property |
| 122 | + def units(self): |
| 123 | + return self._units |
| 124 | + |
| 125 | + @property |
| 126 | + def links(self): |
| 127 | + return self._links |
0 commit comments