wifi_monitor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 
19 
20 import threading
21 import sys
22 
23 import rospy
24 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
25 from cob_msgs.msg import AccessPoint
26 
27 DIAG_NAME = 'Wifi Status (ddwrt)'
28 WARN_TIME = 30
29 ERROR_TIME = 60
30 
31 
32 def wifi_to_diag(msg):
33  stat = DiagnosticStatus()
34 
35  stat.name = DIAG_NAME
36  stat.level = DiagnosticStatus.OK
37  stat.message = 'OK'
38 
39  stat.values.append(KeyValue(key='ESSID', value=msg.essid))
40  stat.values.append(KeyValue(key='Mac Address', value=msg.macaddr))
41  stat.values.append(KeyValue(key='Signal', value=str(msg.signal)))
42  stat.values.append(KeyValue(key='Noise', value=str(msg.noise)))
43  stat.values.append(KeyValue(key='Sig/Noise', value=str(msg.snr)))
44  stat.values.append(KeyValue(key='Channel', value=str(msg.channel)))
45  stat.values.append(KeyValue(key='Rate', value=msg.rate))
46  stat.values.append(KeyValue(key='TX Power', value=msg.tx_power))
47  stat.values.append(KeyValue(key='Quality', value=str(msg.quality)))
48 
49  return stat
50 
51 def mark_diag_stale(diag_stat = None, error = False):
52  if not diag_stat:
53  diag_stat = DiagnosticStatus()
54  diag_stat.message = 'No Updates'
55  diag_stat.name = DIAG_NAME
56  else:
57  diag_stat.message = 'Updates Stale'
58 
59  diag_stat.level = DiagnosticStatus.WARN
60  if error:
61  diag_stat.level = DiagnosticStatus.ERROR
62 
63  return diag_stat
64 
65 class WifiMonitor(object):
66  def __init__(self):
67  self._mutex = threading.Lock()
68 
69  self._last_msg = None
70  self._last_update_time = None
71  self._start_time = rospy.get_time()
72 
73  self._diag_pub = rospy.Publisher('/diagnostics', DiagnosticArray, queue_size=50)
74 
75  self._ddwrt_sub = rospy.Subscriber('ddwrt/accesspoint', AccessPoint, self._cb)
76 
77  def _cb(self, msg):
78  with self._mutex:
79  self._last_msg = msg
80  self._last_update_time = rospy.get_time()
81 
82  def publish_stats(self):
83  with self._mutex:
84  if self._last_msg:
85  ddwrt_stat = wifi_to_diag(self._last_msg)
86 
87  update_diff = rospy.get_time() - self._last_update_time
88  if update_diff > WARN_TIME:
89  ddwrt_stat = mark_diag_stale(ddwrt_stat)
90  if (rospy.get_time() - self._last_update_time) > ERROR_TIME:
91  ddwrt_stat = mark_diag_stale(ddwrt_stat, True)
92 
93  ddwrt_stat.values.append(KeyValue(key='Time Since Update', value=str(update_diff)))
94  else:
95  error_state = (rospy.get_time() - self._start_time) > ERROR_TIME
96  ddwrt_stat = mark_diag_stale(None, error_state)
97  ddwrt_stat.values.append(KeyValue(key='Time Since Update', value="N/A"))
98 
99  msg = DiagnosticArray()
100  msg.header.stamp = rospy.get_rostime()
101  msg.status.append(ddwrt_stat)
102 
103  self._diag_pub.publish(msg)
104 
105 
106 if __name__ == '__main__':
107  try:
108  rospy.init_node('ddwrt_diag')
109  except rospy.exceptions.ROSInitException:
110  print('Wifi monitor is unable to initialize node. Master may not be running.')
111  sys.exit(2)
112 
113  wifi_monitor = WifiMonitor()
114  rate = rospy.Rate(1.0)
115 
116  try:
117  while not rospy.is_shutdown():
118  rate.sleep()
119  wifi_monitor.publish_stats()
120  except KeyboardInterrupt:
121  pass
122  except Exception as e:
123  import traceback
124  traceback.print_exc()
125 
126  sys.exit(0)
127 
def wifi_to_diag(msg)
Definition: wifi_monitor.py:32
def mark_diag_stale(diag_stat=None, error=False)
Definition: wifi_monitor.py:51


cob_monitoring
Author(s): Florian Weisshardt , Felix Messmer
autogenerated on Wed Apr 7 2021 03:03:11