Package mosp_examples :: Module socketplayer_demo_wiggler
[hide private]
[frames] | no frames]

Source Code for Module mosp_examples.socketplayer_demo_wiggler

  1  #!/bin/env python 
  2   
  3  """ A simple demo of the SocketPlayerMonitor demo. 
  4      - creating the player 
  5      - drawing objects 
  6      - drawing objects with limited lifetime 
  7      - removing objects 
  8  """ 
  9   
 10  import sys 
 11  sys.path.append("..") 
 12  import time 
 13  import random 
 14  import struct 
 15   
 16  from mosp.core import Simulation, Person 
 17  from mosp.geo import osm 
 18  from mosp.impl import movement 
 19  from mosp.monitors import * 
 20   
 21  __author__ = "P. Tute" 
 22  __maintainer__ = "B. Henne" 
 23  __contact__ = "henne@dcsec.uni-hannover.de" 
 24  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
 25  __license__ = "GPLv3" 
 26   
 27   
28 -class SocketPlayerDemoWiggler(Person):
29 """Implements a simple person doing only random movement on the map. 30 @author: P. Tute""" 31 next_target = movement.person_next_target_random
32 33
34 -def main():
35 """Defines the simulation, map, monitors, persons.""" 36 # setup works just like every other Simulation 37 s = Simulation(geo=osm.OSMModel('../data/minimap4.osm'), rel_speed=40) 38 # add a SocketPlayerMonitor...after this the monitor will wait until an connection was established 39 m = s.add_monitor(SocketPlayerMonitor, 2) 40 41 # the player will draw a bounding box (as specified by the map) 42 # it is basically a simple rectangle with the id 0 43 # to prevent this from happening set before the simulation starts 44 # m.draw_bb = False 45 46 # persons in a simulation will be send to the player automatically by the monitor 47 s.add_persons(SocketPlayerDemoWiggler, 10, monitor=m) 48 49 50 # the player automatically centers on the middle of the bounding box 51 # should you not want this, you can center it on other coordinates using 52 m.center_on_lat_lon(s.geo.bounds['minlat'], s.geo.bounds['minlon']) 53 # please note that in the current version there is a slight inaccuracy in the way the map is drawn 54 # this means that the player will not center exactly on the specified coordinates 55 56 57 # when the player is running, geometric objects can be drawn using the monitors draw methods 58 # 59 # in this example a circle will be drawn, other objects might need different arguments but work just the same 60 # these arguments are necessary for almost all drawings: 61 # id is a unique identifier. When trying to draw two objects with the same id, the older one will be replaced. 62 # This mechanism can be used to update an object. 63 # Note: IDs also determines the order in which objects are drawn. This might be important when objects are overlapping. 64 # color is allways a 4-tuple of values in the range [0,1] representing RGBA-color, or a string with the name of a color 65 # ttl is available with most drawable objects. It represents a time in seconds, after which the object will be removed from the player. 66 # This might for example be used to signal an event to the viewer 67 # A ttl of 0 means this object will exist until simulation ends or it is deleted otherwise (default) 68 m.draw_circle(id=0, center_lat=s.geo.bounds['minlat'], center_lon=s.geo.bounds['minlon'], radius=50, filled=True, color=(1.0, 0.6, 0.4, 1.0), ttl=10) 69 m.draw_circle(id=1, center_lat=s.geo.bounds['minlat'], center_lon=s.geo.bounds['minlon'], radius=25, filled=True, color='spring-spring-yellow', ttl=10) 70 71 72 # after every object drawn this way, the player will be told to redraw everything 73 # this is not very fast when sending many objects at once 74 # to send many objects at once you should use the monitors socket directly 75 (r, g, b, a) = (1.0, 1.0, 0.5, 0.5) 76 data = struct.pack(m.FORMATS['point'], 77 0, # id 78 s.geo.bounds['minlat'], s.geo.bounds['minlon'], 79 3, # radius 80 r, g, b, a, 81 0) 82 m.conn.send(m.MESSAGES['point'] + data) 83 data = struct.pack(m.FORMATS['point'], 84 1, # id 85 s.geo.bounds['maxlat'], s.geo.bounds['minlon'], 86 6, # radius 87 r, g, b, a, 88 0) 89 m.conn.send(m.MESSAGES['point'] + data) 90 data = struct.pack(m.FORMATS['point'], 91 2, # id 92 s.geo.bounds['maxlat'], s.geo.bounds['maxlon'], 93 10, # radius 94 r, g, b, a, 95 0) 96 m.conn.send(m.MESSAGES['point'] + data) 97 # finish by telling the player to draw. This is not necessary but ensures that changes will be drawn instantly 98 m.conn.send(m.MESSAGES['draw']) 99 100 101 # objects that do not have ttl > 0 will remain in the player until they are removed, replaced or until the simulation ends 102 # deleting an object can be done by calling SocketPlayerMonitor.remove_object() 103 m.draw_circle(2, s.geo.bounds['maxlat'], s.geo.bounds['minlon'], 50, True, (1.0, 1.0, 1.0, 1.0)) 104 m.remove_object(type='circle', id=2) 105 # since we removed the circle before the simulation even starts, it will not appear at all 106 107 108 # the player can create a heatmap while a simulation is running 109 # this is achieved by drawing points in a more performant manner 110 # as a trade-off these 'blips' can not be removed 111 m.add_heatmap_blip(s.geo.bounds['minlat'], s.geo.bounds['maxlon'], 10, (1.0, 0.0, 0.0, 0.2)) 112 113 114 # using real_time=True is highly recommended... 115 s.run(until=500, real_time=True, monitor=True)
116 # after the simulation has ended, you may restart it and tell the player to reconnect 117 118 119 if __name__ == '__main__': 120 main() 121