<commit_before>#!/usr/bin/env python

import rospy
import time
import scapy.all as sca
import numpy as np
from msg import Rssi

class WifiSensor():
  def __init__(self):
    self.pub = rospy.Publisher("rssi_mean", Rssi, queue_size=10)
    while not rospy.is_shutdown():
      rssi = self.mesRaw()
      msg = Rssi()
      msg.rssi = rssi
      msg.variance = 0.0
      self.pub.publish(msg)
      time.sleep(0.1)
  def parsePacket(self, pkt):
    if pkt.haslayer(sca.Dot11) :
      if pkt.addr2 != None:
        return pkt.addr2, -(256-ord(pkt.notdecoded[-4:-3]))
    return None, None
  def mesRaw(self, n=10):
    data = []
    while(len(data) < n):
      packets = sca.sniff(iface="wlan0", count = n/5)
      for pkt in packets:
        addr, rssi = self.parsePacket(pkt)
        # eduroam
        if addr == "20:b3:99:1b:f7:58":
          data.append(rssi)
    return data<commit_msg>* sniffing is now in its own thread * conforms to new msg standard * some minor improvments<commit_after>#!/usr/bin/env python

import rospy
import time
import scapy.all as sca
import numpy as np
from msg import Rssi
import thread

class WifiSensor():
  def __init__(self):
    # setup shared data for threads and start
    self.data = {}
    self.dataMutex = thread.allocate_lock()
    thread.start_new_thread(self.mesRaw, ())
    # setup main loop
    rospy.init_node("wifisensor")
    self.pub = rospy.Publisher("rssi", Rssi, queue_size=10)
    r = rospy.Rate(10) # 10Hz
    while not rospy.is_shutdown():
      data = {}
      with self.dataMutex:
        data = dict(self.data)
        self.data = {}
      for addr in data.keys():
        msg = Rssi()
        msg.macaddr = addr
        msg.rssi = data[addr]
        self.pub.publish(msg)
      r.sleep()
  def mesRaw(self):
    while not rospy.is_shutdown():
      packets = sca.sniff(iface="wlan0", count = 10)
      for pkt in packets:
        addr, rssi = self.parsePacket(pkt)
        if addr is not None:
          with self.dataMutex:
            if addr in self.data.keys():
              self.data[addr].append(rssi)
            else:
              self.data[addr] = [rssi]
  def parsePacket(self, pkt):
    if pkt.haslayer(sca.Dot11):
      if pkt.addr2 is not None:
        return pkt.addr2, ord(pkt.notdecoded[-4:-3])-256
    return None, None
{'fraction_non_alphanumeric': 0.12179208351457155, 'fraction_numerical': 0.020008699434536755, 'mean_word_length': 2.4074074074074074, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5977650', 'n_tokens_mistral': 834, 'n_tokens_neox': 831, 'n_words': 205}