Sections
Timeline
Sub-Sections
Download
Unified Diff
Zip Archive
Metanav
Preferences
About Trac
Links
Slowchop Studios
Gerald Kaszuba
Advertisement

Changeset 53

Show
Ignore:
Timestamp:
25/06/08 08:11:38 (7 months ago)
Author:
gak
Message:
 
Location:
wirelessheatmap/trunk
Files:
2 added
1 modified

Legend:

Unmodified
Added
Removed
  • wirelessheatmap/trunk/heat.py

    r52 r53  
    1212 
    1313from pyglet.window import key 
     14from pyglet.window import mouse 
    1415from pyglet import window 
    1516from pyglet import image 
     
    1718from pyglet.gl import * 
    1819 
    19 class AccessPoint(object): 
    20  
    21     def __init__(self, *bits): 
    22         bits = map(lambda a: a.strip(), bits) 
    23         self.bssid = bits[0] 
    24         self.first_seen = bits[1] 
    25         self.last_seen = bits[2] 
    26         self.channel = int(bits[3]) 
    27         self.speed = int(bits[4]) 
    28         self.privacy = bits[5] 
    29         self.cipher = bits[6] 
    30         self.auth = bits[7] 
    31         self.power = float(bits[8]) 
    32         self.beacons = int(bits[9]) 
    33         self.iv = bits[10] 
    34         self.ip = bits[11] 
    35         self.id_length = int(bits[12]) 
    36         self.essid = bits[13] 
    37         self.key = bits[14] 
    38  
    39     def __hash__(self): 
    40         return hash(self.bssid) 
    41  
    42     def __repr__(self): 
    43         return self.essid 
    44  
     20from ap import AccessPoint 
    4521 
    4622class Heat(object): 
     
    5127 
    5228        self.window = window.Window( 
    53             640, 580, 
    54             caption='Heat', \ 
     29#            640, 480, 
     30            1024, 768, 
     31#            caption='Heat', \ 
     32#            fullscreen=True, 
    5533            ) 
    5634        self.window.on_mouse_press = self.on_mouse_press 
     35        self.window.on_mouse_drag = self.on_mouse_drag 
     36        self.window.on_mouse_scroll = self.on_mouse_scroll 
    5737        self.window.on_key_release = self.on_key_release 
    5838         
    5939        self.current_ap = '' 
     40        
     41        # defaults before image loads 
     42        self.cellsize = 12 
     43        self.img_zoom = 1. 
    6044 
    6145        if self.cfg.image_file: 
    6246            self.img = image.load(self.cfg.image_file) 
     47            self.img_zoom = float(self.window.width) / self.img.width 
     48            zoomier = float(self.window.height) / self.img.height 
     49            if zoomier < self.img_zoom: 
     50                self.img_zoom = zoomier 
     51 
     52            self.cellsize = self.img.width / 100 
     53 
    6354        else: 
    6455            self.img = None 
    6556 
    66         self.img_pos = 0, 0 
    67         self.img_zoom = 1. 
     57        self.img_pos = [0., 0.] 
    6858 
    6959        self.font_size = 20 
     
    7464        self.last_dump_read = 0 
    7565                 
    76         self.cellsize = 12 
    7766        self.samples = 10 
    7867        self.grid = {} 
    7968 
     69        self.grid_margin = 100  # pixels surrounding the image 
     70 
    8071    def parse_config(self): 
    8172        self.cfg = ConfigParser() 
    82         self.opt = OptionParser( \ 
    83             usage='usage: %prog [options]') 
     73        self.opt = OptionParser(usage='usage: %prog [options]') 
    8474 
    8575        # Configuration loading and saving 
     
    163153            self.window.clear() 
    164154             
    165             glLoadIdentity() 
    166155            if self.current_ap: 
    167156                self.calc_heat() 
     157 
     158                self.map_matrix() 
    168159                self.draw_heat() 
    169160                self.draw_strength() 
    170  
     161                 
     162                glLoadIdentity() 
    171163                self.draw_text('%s %s %f' % (self.current_ap, 
    172164                    self.ap[self.current_ap].essid,  
     
    174166 
    175167            if self.img: 
     168                self.map_matrix() 
    176169                glEnable(GL_BLEND) 
    177170                glBlendFunc(GL_SRC_ALPHA, GL_ONE) 
    178                 glLoadIdentity() 
    179                 glScalef(0.5, 0.5, 1) 
    180                 glColor3f(1, 1, 1) 
     171                glColor4f(1, 1, 1, 0.5) 
    181172                self.img.blit(0, 0) 
    182173                glDisable(GL_BLEND) 
     
    185176            self.window.dispatch_events() 
    186177#            time.sleep(0.5) 
    187      
     178    
     179    def map_matrix(self): 
     180        glLoadIdentity() 
     181        glTranslatef(self.img_pos[0], self.img_pos[1], 0) 
     182        glScalef(self.img_zoom, self.img_zoom, 1) 
     183 
     184    def screen2map(self, x, y): 
     185        x -= self.img_pos[0] 
     186        y -= self.img_pos[1] 
     187        x /= self.img_zoom 
     188        y /= self.img_zoom 
     189        return x, y 
     190 
    188191    def draw_strength(self): 
    189192        if not self.current_ap: 
     
    205208        glBegin(GL_POINTS) 
    206209        for pos, strength in self.store[self.current_ap].items(): 
    207             s = (strength - min_str) / (max_str - min_str) 
     210            s = 0.5 + (strength - min_str) / (max_str - min_str) 
    208211            glColor3f(s, s, s) 
    209212            glVertex2f(pos[0], pos[1], 0) 
     
    218221        self.grid = {} 
    219222         
    220         print 'calc' 
    221          
    222         for x in xrange(0, 400, self.cellsize): 
    223             
    224             for y in xrange(0, 800, self.cellsize): 
     223        gmin = -self.grid_margin 
     224        gmaxx = self.img.width + self.grid_margin 
     225        gmaxy = self.img.height + self.grid_margin 
     226 
     227        for x in range(gmin, gmaxx, self.cellsize): 
     228            for y in range(gmin, gmaxy, self.cellsize): 
    225229 
    226230                distance_strengths = [] 
     
    241245                heat /= self.samples 
    242246                self.grid[x, y] = heat 
    243         print 'calc done' 
    244247 
    245248    def draw_heat(self): 
     
    248251        max_heat = max(self.grid.values()) 
    249252        min_heat = min(self.grid.values()) 
    250  
    251253        if not max_heat: 
    252254            return 
    253         if max_heat - min_heat == 0: 
     255        if max_heat == min_heat: 
    254256            return 
    255257 
     
    276278        return colorsys.hsv_to_rgb((1 - val) * 2. / 3, 1, val * 0.6) 
    277279 
    278     def on_mouse_press(self, x, y, buttons, modifiers): 
    279         for ap in self.ap.values(): 
    280             self.store[ap.bssid][x, y] = ap.power 
    281         self.save_data() 
    282      
     280    def on_mouse_press(self, x, y, button, modifiers): 
     281        if button == mouse.LEFT: 
     282            x, y = self.screen2map(x, y) 
     283            for ap in self.ap.values(): 
     284                self.store[ap.bssid][x, y] = ap.power 
     285            self.save_data() 
     286    
     287    def on_mouse_drag(self, x, y, dx, dy, button, modifiers): 
     288        if button == mouse.RIGHT: 
     289            self.img_pos[0] += dx 
     290            self.img_pos[1] += dy 
     291 
     292    def on_mouse_scroll(self, x, y, scroll_x, scroll_y): 
     293        if scroll_y > 0: 
     294            self.img_zoom /= 0.8 * scroll_y 
     295        if scroll_y < 0: 
     296            self.img_zoom *= 0.8 * -scroll_y 
     297 
    283298    def on_key_release(self, symbol, mods): 
    284299        self.grid = {}