class Localizer: def __init__(self, search_mac = 'f8:0c:f3:1d:16:49'): self.mgr = IOMgr() self.search_mac = search_mac self.collectors = [] self._add_collector('128.32.156.131') self._add_collector('128.32.156.64') self._add_collector('128.32.156.45') self._add_collector('128.32.156.67') def _add_collector(self, ip): self.collectors.append(Collector(self.mgr, ip, self.search_mac, self.channels[self.chan_idx])) def _average_signals(self): avgs = [] counts = [] for c in self.collectors: data = c.get_data() c.clear_data() counts.append(len(data)) if len(data) == 0: avgs.append(float('-inf')) else: avgs.append(numpy.mean(data, 0)[1]) return (avgs, counts) def _median_signals(self): medians = [] counts = [] for c in self.collectors: data = c.get_data() c.clear_data() counts.append(len(data)) if len(data) == 0: medians.append(float('-inf')) else: data = map(lambda x: x[1], data) medians.append(data[len(data) / 2]) return (medians, counts) def run(self): for c in self.collectors: c.start_channel_cycle() for c in self.collectors: c.start() time.sleep(3) # Initialization time # Collect packets over sample_period seconds sample_period = 3 while True: # Sample for sample_perid seconds self.mgr.poll(sample_period) (medians, counts) = self._median_signals() zone = medians.index(max(medians))+2 with open('../demo/static/zone.json','w') as f: d = {'zone': zone, 'time': int(time.time())} json.dump(d,f) print zone, medians, sum(counts)
def main(): parser = argparse.ArgumentParser() parser.add_argument('-s','--sample-period',type=int,default=10,help='seconds to sample in between updating') parser.add_argument('-g','--enable-graphics',action='store_true',default=False,help='true if you want to display pygame graphics') parser.add_argument('config_file',type=str,help='''specify the config file containing the routers. Format:\nrouter-mac router-ip x y''') args = parser.parse_args() router_ips = [] bssids = [] coords = [] mgr = IOMgr() with open(args.config_file,'r') as f: #loop through config file for line in f.readlines(): if not line or line.startswith('#'): continue if line.startswith('router'): router_ips.append(line.split()[2]) coords.append(map(int,line.split()[-2:])) elif line.startswith('bssid'): bssids.append(line.split()[0]) # create Collector c = pipe.Collector(mgr, args.sample_period, bssids, router_ips) # create Floor floor = Floor('floor4.png',c) # add routers for router,coord in zip(router_ips,coords): floor.add_router(router, coord) if args.enable_graphics: print "#" * 24 print "#Using Pygame graphics!#" print "#" * 24 import pygame pygame.init() screen = pygame.display.set_mode((600,240)) fl = pygame.image.load(os.path.join('floor4.png')) fl = pygame.transform.scale(fl, (600,240)) screen.blit(fl,(0,0)) while True: try: time.sleep(args.sample_period) mgr.poll(args.sample_period) centroids = [] for mac in floor.macs: cent = floor.get_centroid(mac) if cent: print mac print cent centroids.append(cent) else: print 'deleting mac: ',mac floor.r.hdel('client_location',mac) print '-'*20 if args.enable_graphics: screen.blit(fl,(0,0)) for mac in floor.macs: print floor.centroid_store[mac] for cen in floor.centroid_store[mac]: pygame.draw.circle(screen, (0,255,0), map(lambda x: int(x), list(cen)), 5) for cen,col in zip(centroids, [(255,0,0),(0,255,0),(0,0,255),(255,255,0)]): if cen: pygame.draw.circle(screen, col, map(lambda x: int(x), cen), 5) pygame.display.flip() c.clear_data() except KeyboardInterrupt: c.kill() sys.exit(0)