ddwrt.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of the Willow Garage nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34 # Revision $Id: gossipbot.py 1013 2008-05-21 01:08:56Z sfkwc $
35 
36 import os, sys, string, time, getopt, re
37 import StringIO
38 
39 import rospy
40 from wifi_ddwrt.msg import *
41 from pr2_msgs.msg import AccessPoint
42 
43 from mechanize import Browser
44 from std_msgs.msg import Header
45 import csv
46 
47 import gc
48 
50  for item in gc.garbage:
51  if type(item) == dict:
52  for s in item.keys_iter():
53  del item[s]
54  del gc.garbage[:]
55 
56 class WifiAP:
57  def __init__(self, hostname, username, password):
58  self.hostname = hostname
59  self.username = username
60  self.password = password
61 
62  def newBrowser(self):
63  # Create new browsers all the time because its data structures grow
64  # unboundedly (texas#135)
65  br = Browser()
66  br.add_password(self.hostname, self.username, self.password)
67  br.set_handle_robots(None)
68  return br
69 
70  def fetchSiteSurvey(self):
71  url = "http://%s/Site_Survey.asp" % self.hostname
72 
73  response = self.newBrowser().open(url)
74 
75  body = response.read()
76 
77  #make sure that we put a stamp on things
78  header = Header()
79  header.stamp = rospy.Time.now()
80  networks = []
81  survey = SiteSurvey(header, networks)
82 
83  lines = body.split("\n")
84  for i in range(len(lines)):
85  if lines[i].startswith("var table = "):
86  break
87 
88  aplines = []
89  for j in range(i+1, len(lines)):
90  if lines[j].startswith(");"): break
91  line = lines[j].strip()
92  if not line: continue
93  if line[0] == ",": line = line[1:]
94 
95  aplines.append(line)
96 
97  fp = StringIO.StringIO(string.join(aplines, '\n'))
98  reader = csv.reader(fp)
99  for row in reader:
100  essid = row[0]
101  macattr = row[2]
102  channel = int(row[3])
103  rssi = int(row[4])
104  noise = int(row[5])
105  beacon = int(row[6])
106 
107  network = Network(macattr, essid, channel, rssi, noise, beacon)
108  survey.networks.append(network)
109  return survey
110 
111  def fetchBandwidthStats(self, interface):
112  url = "http://%s/fetchif.cgi?%s" % (self.hostname, interface)
113  response = self.newBrowser().open(url)
114  body = response.read()
115 
116  lines = body.split("\n")
117 
118  if len(lines) > 1:
119  line = lines[1].strip()
120  iparts = line.split(":", 1)
121  parts = iparts[1].split()
122  print interface, parts
123 
124 
125  def fetchCurrentAP(self):
126  url = "http://%s/Status_Wireless.live.asp" % self.hostname
127  response = self.newBrowser().open(url)
128  body = response.read()
129 
130  line = None
131  lines = body.split("\n")
132 
133  d = {}
134  for line in lines:
135  line = line[1:-1]
136  line = line.replace(" ", "")
137  parts = line.split("::", 1)
138  if len(parts) == 2:
139  d[parts[0]] = parts[1]
140 
141  essid = d.get('wl_ssid', '')
142  wl_channel = d.get('wl_channel', '').split()[0]
143  channel = int(wl_channel)
144  rate = d.get('wl_rate', '')
145 
146  signal = None
147  noise = None
148  snr = None
149  quality = None
150 
151  tx_power = d.get('wl_xmit', '')
152 
153  active_wireless = d.get('active_wireless', None)
154  ap = None
155  if active_wireless:
156  active_wireless = active_wireless.replace("'", "")
157  parts = active_wireless.split(",")
158  macaddr = parts[0]
159  interface = parts[1]
160  if len(parts) == 7:
161  signal = int(parts[4])
162  noise = int(parts[5])
163  snr = int(parts[6])
164  quality = signal * 1.24 + 116
165  else:
166  signal = int(parts[5])
167  noise = int(parts[6])
168  snr = int(parts[7])
169  quality = int(parts[8])/10
170 
171  #self.fetchBandwidthStats(interface)
172 
173  #make sure that we put a stamp on things
174  header = Header()
175  header.stamp = rospy.Time.now()
176 
177  ap = AccessPoint(header=header,
178  essid=essid,
179  macaddr=macaddr,
180  signal=signal,
181  noise=noise,
182  snr=snr,
183  channel=channel,
184  rate=rate,
185  quality=quality,
186  tx_power=tx_power)
187 
188  return ap
189 
190 
191 def loop():
192  rospy.init_node("wifi_ddwrt")
193 
194  router_ip = rospy.get_param('~router', 'wifi-router')
195  username = rospy.get_param('~username', 'root')
196  password = rospy.get_param('~password', '')
197 
198  ap = WifiAP(router_ip, username, password)
199 
200  pub1 = rospy.Publisher("ddwrt/sitesurvey", SiteSurvey)
201  pub2 = rospy.Publisher("ddwrt/accesspoint", AccessPoint)
202 
203  r = rospy.Rate(.5)
204  lastTime = 0
205  last_ex = ''
206  while not rospy.is_shutdown():
207  breakUpTrash() # Needed because mechanize leaves data structures that the GC sees as uncollectable (texas#135)
208  try:
209  if time.time() - lastTime > 60:
210  survey = ap.fetchSiteSurvey()
211  pub1.publish(survey)
212  lastTime = time.time()
213  node = ap.fetchCurrentAP()
214  if node: pub2.publish(node)
215  last_ex = ''
216  except Exception as e:
217  if e != last_ex:
218  rospy.logwarn("Caught exception %s" % e)
219  last_ex = e
220  r.sleep()
221 
222 def test():
223  router_ip = rospy.get_param('~router_ip', 'wifi-router')
224  username = rospy.get_param('~username', 'root')
225  password = rospy.get_param('~password', '')
226 
227  ap = WifiAP(router_ip, username, password)
228  while 1:
229  if 0:
230  survey = ap.fetchSiteSurvey()
231  print survey
232  if 1:
233  node = ap.fetchCurrentAP()
234  print node
235 
236 def usage(progname):
237  print __doc__ % vars()
238 
239 def main(argv, stdout, environ):
240  progname = argv[0]
241  optlist, args = getopt.getopt(argv[1:], "", ["help", "test",])
242 
243  testflag = 0
244 
245  for (field, val) in optlist:
246  if field == "--help":
247  usage(progname)
248  return
249  elif field == "--test":
250  testflag = 1
251 
252  if testflag:
253  test()
254  return
255 
256  loop()
257 
258 if __name__ == "__main__":
259  main(sys.argv, sys.stdout, os.environ)
260 
261 
def __init__(self, hostname, username, password)
Definition: ddwrt.py:57
def newBrowser(self)
Definition: ddwrt.py:62
def loop()
Definition: ddwrt.py:191
def fetchCurrentAP(self)
Definition: ddwrt.py:125
def fetchSiteSurvey(self)
Definition: ddwrt.py:70
def fetchBandwidthStats(self, interface)
Definition: ddwrt.py:111
def breakUpTrash()
Definition: ddwrt.py:49
def main(argv, stdout, environ)
Definition: ddwrt.py:239
def test()
Definition: ddwrt.py:222
def usage(progname)
Definition: ddwrt.py:236


wifi_ddwrt
Author(s): Scott Hassan , Eitan Marder-Eppstein
autogenerated on Wed May 12 2021 02:21:31