aboutsummaryrefslogtreecommitdiff
path: root/tools/test-map.py
blob: 5808c1fbbb623d15e0e84f712102877ad736de78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3

import urllib
import urllib2
import httplib
import time
import random
import json

server = 'localhost:5055'
baseUrl = 'http://localhost:8082'
user = { 'email' : 'admin', 'password' : 'admin' }
devices = 500

def login():
    request = urllib2.Request(baseUrl + '/api/session')
    response = urllib2.urlopen(request, urllib.urlencode(user))
    return response.headers.get('Set-Cookie')

def add_device(cookie, unique_id):
    request = urllib2.Request(baseUrl + '/api/devices')
    request.add_header('Cookie', cookie)
    request.add_header('Content-Type', 'application/json')
    device = { 'name' : unique_id, 'uniqueId' : unique_id }
    try:
        response = urllib2.urlopen(request, json.dumps(device))
    except urllib2.HTTPError:
        pass

def send_message(conn, device_id):
    params = (('id', device_id), ('lat', random.uniform(59, 61)), ('lon', random.uniform(29, 31)))
    conn.request('GET', '?' + urllib.urlencode(params))
    conn.getresponse().read()

cookie = login()
conn = httplib.HTTPConnection(server)

for i in range(devices):
    device_id = "{0:0>6}".format(i)
    add_device(cookie, device_id)
    send_message(conn, device_id)

while True:
    device_id = "{0:0>6}".format(random.randint(0, devices))
    send_message(conn, device_id)
    time.sleep(1)