forked from LW-Ho/CoAP_CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoOb.py
executable file
·88 lines (75 loc) · 3.12 KB
/
AutoOb.py
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
78
79
80
81
82
83
84
85
86
87
88
import threading
import time
from coapthon.client.helperclient import HelperClient
from CoAPObserve import CoAPObserve
from GetMotes import getAllMotes
import logging
log = logging.getLogger("AutoObserve")
class AutoOb(threading.Thread):
def __init__(self, mote_lists, mote_observe_lists, countDown=None, node=None, group=None, target=None, verbose=None, autoOb_callback=None, object_callback=None):
threading.Thread.__init__(self, group=group, target=target, name=node, verbose=verbose)
self.auto_ob = None
self.record_counter = 0
self.mote_lists = mote_lists
self.mote_observe_lists = mote_observe_lists
self.autoOb_callback = autoOb_callback
self.object_callback = object_callback
self.signal = True
self.countDown = int(countDown)
self.countBR = 0
return
def run(self):
log.info("Starting auto observing nodes.")
print("")
if self.countDown is None or self.countDown < 10:
self.countDown = 60
while self.signal :
time.sleep(self.countDown/2)
s1 = set(self.mote_lists)
temp = []
for obnode in self.mote_observe_lists: # to change element type, then save to oher list. (CoAP to string)
temp.append(obnode.getName())
s2 = set(temp)
result = list(s1.difference(s2)) # compare list,we can know that is not observing.
# try :
for node in result:
try:
#time.sleep(1)
coapObserve = CoAPObserve(node=node, resource="res/bcollect", object_callback=self.object_callback)
coapObserve.printName()
coapObserve.setDaemon(True)
coapObserve.start()
self.mote_observe_lists.append(coapObserve)
except Exception as e:
print (e)
log.info("Error of observe, have more threading... ")
# temp_time = 0
try:
time.sleep(self.countDown/2)
# start = time.time()
# while (temp_time < self.countDown):
# temp_time = time.time() - start
# print 'Watting time : %2.2fs\r' % temp_time, # for testing.
# time.sleep this function will be sleep to dead.
except Exception as e:
print (e)
log.info("Error of count down timer... ")
self.refreshBR() # get new routing table from border router.
log.info("Observe ALL Done.")
self.mote_lists = self.autoOb_callback(self.mote_observe_lists, False)
for node in self.mote_observe_lists:
# print str(node.getName())+" -> Counter Ob : "+str(node.getCountOb())+", Counter Ck : "+str(node.getCountCk())
print "%s -> Counter Ob : %s , Counter Ck : %s " % (node.getName(), node.getCountOb(), node.getCountCk())
node.saveCountCk(node.getCountOb()) # record fresh count number.
# if (node.getCountOb() - node.getCountCk()) > 0: # threshold number.
# node.saveCountCk(node.getCountOb()) # record fresh count number.
# else:
# node.stop()
# self.mote_observe_lists.remove(node)
# node = None
def stop(self):
log.info("Stoping auto observing nodes.")
self.signal = False
return
def refreshBR(self):
self.autoOb_callback(self.mote_observe_lists, True)