Skip to content
Snippets Groups Projects
Commit e275126f authored by Server (Shared Users)'s avatar Server (Shared Users)
Browse files

final of middle output....

parent a6785c88
No related branches found
No related tags found
No related merge requests found
...@@ -18,14 +18,16 @@ from coapthon.utils import parse_uri ...@@ -18,14 +18,16 @@ from coapthon.utils import parse_uri
__author__ = 'Giacomo Tanganelli' __author__ = 'Giacomo Tanganelli'
class CoapClient(): class CoapClient():
def __init__(self): def __init__(self): # 문제없음.
self.ip = "shmd01.iptime.org" self.ip = "shmd01.iptime.org"
self.myMAC = ':'.join(re.findall('..', '%012x' % uuid.getnode())) self.myMAC = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
self.clearMyData()
self.myuuid, self.operate = self.obtainMyID() self.myuuid, self.operate = self.obtainMyID()
if self.operate == 1: if self.operate == 1: # rpNode가 이미 한 개 이상존재하는 경우
self._beacon = BeaconEmit() self._beacon = BeaconEmit()
self.advertiseMe()
def obtainMyID(self): def clearMyData(self): #문제없음.
path = "/rpNodeInfoPath" path = "/rpNodeInfoPath"
path = "coap://" + self.ip + path path = "coap://" + self.ip + path
...@@ -40,7 +42,35 @@ class CoapClient(): ...@@ -40,7 +42,35 @@ class CoapClient():
try: try:
payload = { payload = {
'option' : 0, 'option' : -1, #clear my data from Server
'rpMAC' : self.myMAC
}
response = client.put(path, json.dumps(payload))
print((response.pretty_print()))
string = json.loads(response.payload)['rpuuid']
except KeyboardInterrupt:
print("obtainMyID Stop")
client.stop()
client.stop()
def obtainMyID(self): # 문제없음.
path = "/rpNodeInfoPath"
path = "coap://" + self.ip + path
host, port, path = parse_uri(path)
try:
tmp = socket.gethostbyname(host)
host = tmp
except socket.gaierror:
pass
client = HelperClient(server=(host, port))
try:
payload = {
'option' : 0, #obtain rpuuid from Server
'rpMAC' : self.myMAC 'rpMAC' : self.myMAC
} }
response = client.put(path, json.dumps(payload)) response = client.put(path, json.dumps(payload))
...@@ -54,7 +84,7 @@ class CoapClient(): ...@@ -54,7 +84,7 @@ class CoapClient():
client.stop() client.stop()
return string, json.loads(response.payload)['operate'] return string, json.loads(response.payload)['operate']
def advertiseMe(self): def advertiseMe(self): #문제없음.
seq = self.myuuid seq = self.myuuid
length = 3 length = 3
string ="" string =""
...@@ -64,7 +94,7 @@ class CoapClient(): ...@@ -64,7 +94,7 @@ class CoapClient():
print(map(''.join, zip(*[iter(seq)]*length))) print(map(''.join, zip(*[iter(seq)]*length)))
self._beacon.beacon_Start(string) self._beacon.beacon_Start(string)
def calculateNodePositionAtServer(self): def calculateNodePositionAtServer(self): #문제없음.
path = "/rpNodeInfoPath" path = "/rpNodeInfoPath"
path = "coap://" + self.ip + path path = "coap://" + self.ip + path
...@@ -79,18 +109,22 @@ class CoapClient(): ...@@ -79,18 +109,22 @@ class CoapClient():
try: try:
payload = { payload = {
'option' : 1, 'option' : 1, # obtain my position from Server.
'rpuuid' : self.myuuid 'rpuuid' : self.myuuid
} }
response = client.put(path, json.dumps(payload)) response = client.put(path, json.dumps(payload))
print((response.pretty_print())) print((response.pretty_print()))
if json.loads(response.payload)['operate'] == 0: # Postion 계산이 불가능한 경우
client.stop()
print("RpNode Exit and delete my data in DB")
sys.exit(1)
except KeyboardInterrupt: except KeyboardInterrupt:
print("obtainMyID Stop") print("obtainMyID Stop")
client.stop() client.stop()
client.stop() client.stop()
def putBLEInfo(self): # pragma: no cover def putBLEInfo(self): # 문제 없음.
dev_id = 0 dev_id = 0
try: try:
self.sock = bluez.hci_open_dev(dev_id) self.sock = bluez.hci_open_dev(dev_id)
...@@ -139,7 +173,7 @@ class CoapClient(): ...@@ -139,7 +173,7 @@ class CoapClient():
client.stop() client.stop()
def mkJsonbeaconInfo(self, info): def mkJsonbeaconInfo(self, info): #문제 없음.
n = 2.2 n = 2.2
distance = math.pow(10, (float(info[4]) - float(info[5])) / (10 * n)) distance = math.pow(10, (float(info[4]) - float(info[5])) / (10 * n))
payload = { payload = {
...@@ -150,7 +184,7 @@ class CoapClient(): ...@@ -150,7 +184,7 @@ class CoapClient():
} }
return payload return payload
def regiRelWithNewPi(self, info): def regiRelWithNewPi(self, info): #문제 없음
#새로운 rasbPi를 찾은 경우 새로운 rasbPi를 위치측정기로 사용하기 위해서 server에 관련 정보를 저장하는 method #새로운 rasbPi를 찾은 경우 새로운 rasbPi를 위치측정기로 사용하기 위해서 server에 관련 정보를 저장하는 method
path = "/rpGraphInfoPath" path = "/rpGraphInfoPath"
...@@ -173,7 +207,7 @@ class CoapClient(): ...@@ -173,7 +207,7 @@ class CoapClient():
client.stop() client.stop()
client.stop() client.stop()
def mkJsonRpgraphInfo(self, info): def mkJsonRpgraphInfo(self, info): #문제 없음
n = 2.2 n = 2.2
print(info) print(info)
distance = math.pow(10, ((float(info[4]) - float(info[5])) / (10 * n))) distance = math.pow(10, ((float(info[4]) - float(info[5])) / (10 * n)))
...@@ -186,8 +220,6 @@ class CoapClient(): ...@@ -186,8 +220,6 @@ class CoapClient():
def main(): def main():
measuringDevice = CoapClient() measuringDevice = CoapClient()
if measuringDevice.operate == 1:
measuringDevice.advertiseMe()
measuringDevice.calculateNodePositionAtServer() measuringDevice.calculateNodePositionAtServer()
putBLEInfoThread = threading.Thread(target = measuringDevice.putBLEInfo) putBLEInfoThread = threading.Thread(target = measuringDevice.putBLEInfo)
putBLEInfoThread.start() putBLEInfoThread.start()
......
...@@ -12,7 +12,7 @@ from exampleresources import BleInfoResource, RpNodeInfoResource, RpGraphInfoRes ...@@ -12,7 +12,7 @@ from exampleresources import BleInfoResource, RpNodeInfoResource, RpGraphInfoRes
from pymongo import MongoClient from pymongo import MongoClient
class CoAPServer(CoAP): class CoAPServer(CoAP):
def __init__(self, host, port, multicast=False): def __init__(self, host, port, multicast=False): #문제 없음.
CoAP.__init__(self, (host, port), multicast) CoAP.__init__(self, (host, port), multicast)
self.add_resource('bleInfoPath/', BleInfoResource()) self.add_resource('bleInfoPath/', BleInfoResource())
self.add_resource('rpNodeInfoPath/', RpNodeInfoResource()) self.add_resource('rpNodeInfoPath/', RpNodeInfoResource())
...@@ -39,9 +39,9 @@ class CoAPServer(CoAP): ...@@ -39,9 +39,9 @@ class CoAPServer(CoAP):
print(c['uuid']) print(c['uuid'])
positionCalculationThread = threading.Thread(target = self.positionCalculation, args=([c['uuid']])) positionCalculationThread = threading.Thread(target = self.positionCalculation, args=([c['uuid']]))
positionCalculationThread.start() positionCalculationThread.start()
time.sleep(3.1) time.sleep(2.1)
currentTime = time.time() currentTime = time.time()
trackingCursor = self.trackingCollection.remove({'updateTime': {'lte' : currentTime - 3}}) trackingCursor = self.trackingCollection.remove({'updateTime': {'lte' : currentTime - 2}})
except KeyboardInterrupt: except KeyboardInterrupt:
print("existing updateUserLocation") print("existing updateUserLocation")
...@@ -49,7 +49,7 @@ class CoAPServer(CoAP): ...@@ -49,7 +49,7 @@ class CoAPServer(CoAP):
print(useruuid) print(useruuid)
currentTime = time.time() currentTime = time.time()
trackingCursor = self.trackingCollection.find({'$and':[{'useruuid': useruuid}, trackingCursor = self.trackingCollection.find({'$and':[{'useruuid': useruuid},
{'updateTime':{'$gt' : currentTime - 3}}]}) {'updateTime':{'$gt' : currentTime - 2}}]})
#trackingCursor = self.trackingCollection.find({'$and':[{'useruuid':{'$eq' : useruuid}}]}) #trackingCursor = self.trackingCollection.find({'$and':[{'useruuid':{'$eq' : useruuid}}]})
userLocationData =[] userLocationData =[]
......
...@@ -6,7 +6,7 @@ from pymongo import MongoClient ...@@ -6,7 +6,7 @@ from pymongo import MongoClient
from coapthon import defines from coapthon import defines
from coapthon.resources.resource import Resource from coapthon.resources.resource import Resource
class RpNodeInfoResource(Resource): class RpNodeInfoResource(Resource): #문제 없음.
def __init__(self, name="RpNodeInfo"): def __init__(self, name="RpNodeInfo"):
super(RpNodeInfoResource, self).__init__(name) super(RpNodeInfoResource, self).__init__(name)
self.payload = "RpNodeInfo resource" self.payload = "RpNodeInfo resource"
...@@ -22,8 +22,10 @@ class RpNodeInfoResource(Resource): ...@@ -22,8 +22,10 @@ class RpNodeInfoResource(Resource):
from coapthon.messages.response import Response from coapthon.messages.response import Response
assert(isinstance(response, Response)) assert(isinstance(response, Response))
self.payload = json.loads(self.payload) self.payload = json.loads(self.payload)
if self.payload['option'] == 1: if self.payload['option'] == -1:
self.calculateNodePosition(sefl.payload) self.deleteNodeData(self.payload)
elif self.payload['option'] == 1:
response.payload = self.calculateNodePosition(self.payload)
else : else :
response.payload = self.postRpNodeInfoInDB(self.payload) response.payload = self.postRpNodeInfoInDB(self.payload)
response.code = defines.Codes.CREATED.number response.code = defines.Codes.CREATED.number
...@@ -34,19 +36,28 @@ class RpNodeInfoResource(Resource): ...@@ -34,19 +36,28 @@ class RpNodeInfoResource(Resource):
from coapthon.messages.response import Response from coapthon.messages.response import Response
assert(isinstance(response, Response)) assert(isinstance(response, Response))
self.payload = json.loads(self.payload) self.payload = json.loads(self.payload)
if self.payload['option'] == 1: if self.payload['option'] == -1:
self.calculateNodePosition(self.payload) self.deleteNodeData(self.payload)
else : elif self.payload['option'] == 1:
#coapclient.py에서 calculateNodePositionAtServer()이 호출된 경우
response.payload = self.calculateNodePosition(self.payload)
else :# coapclient.py 에서 obtainMyID()가 호출된 경우
response.payload = self.postRpNodeInfoInDB(self.payload) response.payload = self.postRpNodeInfoInDB(self.payload)
response.code = defines.Codes.CHANGED.number response.code = defines.Codes.CHANGED.number
return self, response return self, response
def postRpNodeInfoInDB(self, info): def deleteNodeData(self, info): #문제 없음
info = info['rpMAC'] info = info['rpMAC']
graphColletion = self.db.RpGraphData
rpuuid = self.collection.find({'rpMAC' : info})[0]['rpuuid']
graphColletion.delete_many({'v2' : rpuuid})
self.collection.delete_many({'rpMAC' : info}) self.collection.delete_many({'rpMAC' : info})
def postRpNodeInfoInDB(self, info): #문제 없음
info = info['rpMAC']
rpuuid = "52528282"+str(uuid.uuid4())[8:] rpuuid = "52528282"+str(uuid.uuid4())[8:]
rpuuid = rpuuid.replace("-","") rpuuid = rpuuid.replace("-","")
if self.collection.count() == 0: if self.collection.count() == 0: # 첫번째 rpNode인 경우
payload = { payload = {
'rpMAC' : info, 'rpMAC' : info,
'rpuuid' : rpuuid 'rpuuid' : rpuuid
...@@ -55,7 +66,7 @@ class RpNodeInfoResource(Resource): ...@@ -55,7 +66,7 @@ class RpNodeInfoResource(Resource):
response = json.dumps({ response = json.dumps({
'rpuuid' : rpuuid, 'rpuuid' : rpuuid,
'operate' : 0 'operate' : 0 # advertise할 필요가 없다.
}) })
else: else:
payload = { payload = {
...@@ -66,35 +77,43 @@ class RpNodeInfoResource(Resource): ...@@ -66,35 +77,43 @@ class RpNodeInfoResource(Resource):
response = json.dumps({ response = json.dumps({
'rpuuid' : rpuuid, 'rpuuid' : rpuuid,
'operate' : 1 'operate' : 1 # advertise를 해야한다.
}) })
return response return response
def calculateNodePosition(self, info): def calculateNodePosition(self, info): # 문제 없음.
print("Start calculateNodePosition") print("Start calculateNodePosition")
info = info['rpuuid'] info = info['rpuuid']
graphColletion = self.db.RpGraphData graphColletion = self.db.RpGraphData
mydoc = self.collection.find({'rpuuid': info}) mydoc = self.collection.find({'rpuuid': info})
if mydoc.count() == 1: response = {
if self.collection.count() == 1: 'operate' : 1
}
if mydoc.count() == 1: # 자기 자신이 RpNodeData에 있는 경우
if self.collection.count() == 1: # 자신이 첫번째 RpNode인 경우
self.collection.update({'rpuuid' : info}, {'$set':{'x': 0.0, 'y': 0.0}}, False) self.collection.update({'rpuuid' : info}, {'$set':{'x': 0.0, 'y': 0.0}}, False)
elif self.collection.count() == 2: elif self.collection.count() == 2: # 자신이 두번째 RpNode인 경우
graphB = graphColletion.find({'v2': info}) graphB = graphColletion.find({'v2': info})
x = graphB[0]['distance'] x = graphB[0]['distance']
self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': 0.0}}, False) self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': 0.0}}, False)
elif self.collection.count() == 3: elif self.collection.count() == 3: # 자신이 세번째 RpNode인 경우
graphC = graphColletion.find({'v2': info}) graphC = graphColletion.find({'v2': info})
for doc in graphC: for doc in graphC:
tempdoc = self.collection.find({'rpuuid': doc['v1']}) tempdoc = self.collection.find({'rpuuid': doc['v1']})
if tempdoc[0]['x'] == 0 and tempdoc[0]['y'] == 0: if tempdoc[0]['x'] == 0 and tempdoc[0]['y'] == 0: # 첫번째 RpNode 즉, (0,0)
vertexA = tempdoc[0] vertexA = tempdoc[0]# 첫번째 RpNode를 vertexA라 한다.
vertexB = tempdoc[0] vertexB = tempdoc[0]#두번째 RpNode를 vertexB라 한다.
#print("kkkkkkkkkkkkkkk", graphColletion.find({'v1': vertexB['rpuuid'], 'v2': info}) )
dis = { dis = {
'a': graphColletion.find({'v1': vertexB['rpuuid'], 'v2': info})[0]['distance'], 'a': graphColletion.find({'v1': vertexB['rpuuid'], 'v2': info})[0]['distance'],
'b': graphColletion.find({'v1': vertexA['rpuuid'], 'v2': info})[0]['distance'], 'b': graphColletion.find({'v1': vertexA['rpuuid'], 'v2': info})[0]['distance'],
'c': vertexB['x'] 'c': vertexB['x']
} }
if ((dis['a'] + dis['b']) <= dis['c']) or dis['a'] == 0 or dis['b'] == 0:
# 3번째 rpNode를 만들 수 없는 경우
print("3번째 rpNode를 만들 수 없습니다.")
response['operate'] = 0
return json.dumps(response)
cosA = (math.pow(dis['b'], 2) + math.pow(dis['c'], 2) - math.pow(dis['a'], 2)) / (2 * dis['b'] * dis['c']) cosA = (math.pow(dis['b'], 2) + math.pow(dis['c'], 2) - math.pow(dis['a'], 2)) / (2 * dis['b'] * dis['c'])
sinA = math.sqrt((1 - math.pow(cosA,2))) sinA = math.sqrt((1 - math.pow(cosA,2)))
x = dis['b'] * cosA x = dis['b'] * cosA
...@@ -113,7 +132,10 @@ class RpNodeInfoResource(Resource): ...@@ -113,7 +132,10 @@ class RpNodeInfoResource(Resource):
nodeLocationData = sorted(nodeLocationData, key=lambda k: k['distance'], reverse = False) nodeLocationData = sorted(nodeLocationData, key=lambda k: k['distance'], reverse = False)
if len(nodeLocationData) < 3: if len(nodeLocationData) < 3:
return # 4번째 이상의 rpNode를 만들 수 없는 경우
print("4번째 이상의 rpNode를 만들 수 없습니다.")
response['operate'] = 0
return json.dumps(response)
nodeList = [] nodeList = []
for i in range(3): for i in range(3):
...@@ -127,13 +149,16 @@ class RpNodeInfoResource(Resource): ...@@ -127,13 +149,16 @@ class RpNodeInfoResource(Resource):
x, y = triangulation.doTriangulation() x, y = triangulation.doTriangulation()
if x == None: if x == None:
return # 4번째 이상의 rpNode를 만들 수 없는 경우
print("4번째 이상의 rpNode를 만들 수 없습니다.")
response['operate'] = 0
return json.dumps(response)
self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': y}}, False) self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': y}}, False)
return json.dump(response)
class RpGraphInfoResource(Resource): #문제 없음
class RpGraphInfoResource(Resource):
def __init__(self, name="RpGraphInfo"): def __init__(self, name="RpGraphInfo"):
super(RpGraphInfoResource, self).__init__(name) super(RpGraphInfoResource, self).__init__(name)
self.payload = "RpGraphInfo resource" self.payload = "RpGraphInfo resource"
...@@ -171,7 +196,7 @@ class RpGraphInfoResource(Resource): ...@@ -171,7 +196,7 @@ class RpGraphInfoResource(Resource):
response.code = defines.Codes.DELETED.number response.code = defines.Codes.DELETED.number
return True, response return True, response
def postRpGraphInfoInDB(self, info): def postRpGraphInfoInDB(self, info): #문제 없음.
info = json.loads(info) info = json.loads(info)
mydoc = self.collection.find({'v1':info['v1'], 'v2':info['v2']}) mydoc = self.collection.find({'v1':info['v1'], 'v2':info['v2']})
if mydoc.count() > 0: if mydoc.count() > 0:
...@@ -194,7 +219,7 @@ class RpGraphInfoResource(Resource): ...@@ -194,7 +219,7 @@ class RpGraphInfoResource(Resource):
'count': 1 'count': 1
}, upsert=True) }, upsert=True)
class BleInfoResource(Resource): class BleInfoResource(Resource): #문제없음.
def __init__(self, name="BleInfo"): def __init__(self, name="BleInfo"):
super(BleInfoResource, self).__init__(name) super(BleInfoResource, self).__init__(name)
self.payload = "BleInfo resource" self.payload = "BleInfo resource"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment