wifi_monitor.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2010, Willow Garage, Inc.
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
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Willow Garage nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 ##\author Kevin Watts
36 ##\brief Republishes the data from ddwrt/accesspoint onto diagnostics
37 
38 from __future__ import with_statement
39 
40 PKG = 'pr2_computer_monitor'
41 import roslib
42 roslib.load_manifest(PKG)
43 
44 import rospy
45 
46 import threading
47 import sys
48 
49 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
50 from pr2_msgs.msg import AccessPoint
51 
52 DIAG_NAME = 'Wifi Status (ddwrt)'
53 WARN_TIME = 30
54 ERROR_TIME = 60
55 
56 
57 def wifi_to_diag(msg):
58  stat = DiagnosticStatus()
59 
60  stat.name = DIAG_NAME
61  stat.level = DiagnosticStatus.OK
62  stat.message = 'OK'
63 
64  stat.values.append(KeyValue(key='ESSID', value=msg.essid))
65  stat.values.append(KeyValue(key='Mac Address', value=msg.macaddr))
66  stat.values.append(KeyValue(key='Signal', value=str(msg.signal)))
67  stat.values.append(KeyValue(key='Noise', value=str(msg.noise)))
68  stat.values.append(KeyValue(key='Sig/Noise', value=str(msg.snr)))
69  stat.values.append(KeyValue(key='Channel', value=str(msg.channel)))
70  stat.values.append(KeyValue(key='Rate', value=msg.rate))
71  stat.values.append(KeyValue(key='TX Power', value=msg.tx_power))
72  stat.values.append(KeyValue(key='Quality', value=str(msg.quality)))
73 
74  return stat
75 
76 def mark_diag_stale(diag_stat = None, error = False):
77  if not diag_stat:
78  diag_stat = DiagnosticStatus()
79  diag_stat.message = 'No Updates'
80  diag_stat.name = DIAG_NAME
81  else:
82  diag_stat.message = 'Updates Stale'
83 
84  diag_stat.level = DiagnosticStatus.WARN
85  if error:
86  diag_stat.level = DiagnosticStatus.ERROR
87 
88  return diag_stat
89 
90 class WifiMonitor(object):
91  def __init__(self):
92  self._mutex = threading.Lock()
93 
94  self._last_msg = None
95  self._last_update_time = None
96  self._start_time = rospy.get_time()
97 
98  self._diag_pub = rospy.Publisher('/diagnostics', DiagnosticArray, queue_size=10)
99 
100  self._ddwrt_sub = rospy.Subscriber('ddwrt/accesspoint', AccessPoint, self._cb)
101 
102  def _cb(self, msg):
103  with self._mutex:
104  self._last_msg = msg
105  self._last_update_time = rospy.get_time()
106 
107  def publish_stats(self):
108  with self._mutex:
109  if self._last_msg:
110  ddwrt_stat = wifi_to_diag(self._last_msg)
111 
112  update_diff = rospy.get_time() - self._last_update_time
113  if update_diff > WARN_TIME:
114  ddwrt_stat = mark_diag_stale(ddwrt_stat)
115  if (rospy.get_time() - self._last_update_time) > ERROR_TIME:
116  ddwrt_stat = mark_diag_stale(ddwrt_stat, True)
117 
118  ddwrt_stat.values.append(KeyValue(key='Time Since Update', value=str(update_diff)))
119  else:
120  error_state = (rospy.get_time() - self._start_time) > ERROR_TIME
121  ddwrt_stat = mark_diag_stale(None, error_state)
122  ddwrt_stat.values.append(KeyValue(key='Time Since Update', value="N/A"))
123 
124  msg = DiagnosticArray()
125  msg.header.stamp = rospy.get_rostime()
126  msg.status.append(ddwrt_stat)
127 
128  self._diag_pub.publish(msg)
129 
130 
131 if __name__ == '__main__':
132  try:
133  rospy.init_node('ddwrt_diag')
134  except rospy.exceptions.ROSInitException:
135  print('Wifi monitor is unable to initialize node. Master may not be running.')
136  sys.exit(2)
137 
138  wifi_monitor = WifiMonitor()
139  rate = rospy.Rate(1.0)
140 
141  try:
142  while not rospy.is_shutdown():
143  rate.sleep()
144  wifi_monitor.publish_stats()
145  except KeyboardInterrupt:
146  pass
147  except Exception as e:
148  import traceback
149  traceback.print_exc()
150 
151  sys.exit(0)
152 
153 
154 
def wifi_to_diag(msg)
Definition: wifi_monitor.py:57
def mark_diag_stale(diag_stat=None, error=False)
Definition: wifi_monitor.py:76


pr2_computer_monitor
Author(s): Kevin Watts (watts@willowgarage.com)
autogenerated on Tue Jun 1 2021 02:50:51