-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.py
More file actions
executable file
·78 lines (66 loc) · 1.88 KB
/
tester.py
File metadata and controls
executable file
·78 lines (66 loc) · 1.88 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
import urllib2
import time
import json
import random
import thread
from threading import Semaphore, BoundedSemaphore
import serial
motes = []
historian = 'http://127.0.0.1:4001'
sr = 'http://127.0.0.1:8442/serviceregistry'
def getSREnpoints():
client = urllib2.build_opener(urllib2.HTTPHandler)
jsonr = """{
"service": {
"serviceDefinition": "_ahfc-dm-historian._http._tcp",
"interfaces": [
"HTTPS-TLS-JSON+SENML"
],
"serviceMetadata": {
"security"=""
}
},
"pingProviders": false,
"metadataSearch": false
}"""
request = urllib2.Request(sr + '/query', data=jsonr)
request.add_header('Content-Type', 'application/json')
request.get_method = lambda: 'PUT'
ret = client.open(request)
str = ret.read()
jsono = json.loads(str)
print jsono['serviceQueryData'][0]['providedService']['interfaces'][0]
return str
def worker(mote, delay):
while True:
try:
sensor_sem.acquire()
json ='[\n {\"bn\":\"' + mote + '\", \"bt\":'+str(int(time.time()))+'},\n'
json += ' {"n":"/3303/0/5700","u":"Cel","v":' +str(round(random.uniform(22.0, 22.5), 2))+'}\n'
sensor_sem.release()
#json += ' {"n":"/3303/0/5700","u":"Cel", "t": 1, "v":' +str(round(random.uniform(12.0, 22.5), 2))+'}\n'
json += ']'
print json
client = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(historian + '/storage/'+mote, data=json)
request.add_header('Content-Type', 'application/json')
request.get_method = lambda: 'PUT'
resp = client.open(request)
time.sleep(delay)
except:
print ""#Error: unable to create client com"
#for x in range(1, 2):
# motes.append("mote"+str(x))
motes.append("mote91")
#url = getSREnpoints()
#print url
#exit(0)
sensor_sem = BoundedSemaphore(value=1)
try:
for mote in motes:
thread.start_new_thread( worker, (mote, 5) )
except:
print "Error: unable to start thread"
while 1:
pass