diff --git a/RPi_Client/CoAPthon3/coapclient.py b/RPi_Client/CoAPthon3/coapclient.py index 622317e36602f98c590ec661a4012a6de5d3b642..6d8fae6c701034eb22b12e2d252149e28f77d6c3 100644 --- a/RPi_Client/CoAPthon3/coapclient.py +++ b/RPi_Client/CoAPthon3/coapclient.py @@ -21,13 +21,38 @@ class CoapClient(): def __init__(self): # complete self.ip = "shmd01.iptime.org" self.myMAC = ':'.join(re.findall('..', '%012x' % uuid.getnode())) + self.clearMyData() self.myuuid, self.operate = self.obtainMyID() if self.operate == 1: # rpNode가 이미 한 개 이상존재하는 경우 self._beacon = BeaconEmit() self.advertiseMe() + + def clearMyData(self): # complete + 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' : -1, #clear my data from Server + 'rpMAC' : self.myMAC + } + response = client.put(path, json.dumps(payload)) + print((response.pretty_print())) - if self.operate != 2: - self.calculateNodePositionAtServer() + except KeyboardInterrupt: + print("obtainMyID Stop") + client.stop() + + client.stop() def obtainMyID(self): # complete path = "/rpNodeInfoPath" @@ -89,7 +114,10 @@ class CoapClient(): response = client.put(path, json.dumps(payload)) print((response.pretty_print())) if json.loads(response.payload)['operate'] == 0: # Postion 계산이 불가능한 경우 - print("impossble position calculation") + client.stop() + self.clearMyData() + print("RpNode Exit and delete my data in DB") + sys.exit(1) except KeyboardInterrupt: print("obtainMyID Stop") @@ -134,7 +162,7 @@ class CoapClient(): regiRelThread.start() continue jsonpayload = self.mkJsonbeaconInfo(beaconInfo) - if jsonpayload['distance'] < 40: + if jsonpayload['distance'] < 60: payload.append(jsonpayload) response = client.put(path, json.dumps(payload)) @@ -148,19 +176,12 @@ class CoapClient(): def mkJsonbeaconInfo(self, info): # complete n = 2.05 - h = 0 - ad = 0 distance = math.pow(10, (float(info[4]) - float(info[5])) / (10 * n)) - if distance < h: - ad = 0 - else: - ad = math.sqrt(distance * distance - h * h) - payload = { 'rpuuid' : self.myuuid, 'userMAC' : info[0], 'useruuid' : info[1], - 'distance' : ad, + 'distance' : distance, 'updateTime' : time.time() } return payload @@ -200,9 +221,10 @@ class CoapClient(): def main(): measuringDevice = CoapClient() + measuringDevice.calculateNodePositionAtServer() putBLEInfoThread = threading.Thread(target = measuringDevice.putBLEInfo) putBLEInfoThread.start() putBLEInfoThread.join() if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/Server_CoAP/CoAPthon3/exampleresources.py b/Server_CoAP/CoAPthon3/exampleresources.py index cba1937abc044b842bb68ad6f90415b9088f9a3a..f568f2633b222e67b132ce26590105b1bed2e915 100644 --- a/Server_CoAP/CoAPthon3/exampleresources.py +++ b/Server_CoAP/CoAPthon3/exampleresources.py @@ -22,11 +22,9 @@ class RpNodeInfoResource(Resource): from coapthon.messages.response import Response assert(isinstance(response, Response)) self.payload = json.loads(self.payload) - """ if self.payload['option'] == -1: self.deleteNodeData(self.payload) - """ - if self.payload['option'] == 1: + elif self.payload['option'] == 1: response.payload = self.calculateNodePosition(self.payload) else : response.payload = self.postRpNodeInfoInDB(self.payload) @@ -49,44 +47,40 @@ class RpNodeInfoResource(Resource): response.code = defines.Codes.CHANGED.number return self, response + def deleteNodeData(self, info): # complete + info = info['rpMAC'] + graphColletion = self.db.RpGraphData + if self.collection.find({'rpMAC' : info}).count() != 0: + rpuuid = self.collection.find({'rpMAC' : info})[0]['rpuuid'] + graphColletion.remove({'v2' : rpuuid}) + self.collection.remove({'rpMAC' : info}) + def postRpNodeInfoInDB(self, info): # complete - cursor = self.collection.find({'rpMAC' : info['rpMAC']}) - if len(list(cursor)) == 1: - rpuuid = cursor[0]['rpuuid'] + info = info['rpMAC'] + rpuuid = "52528282"+str(uuid.uuid4())[8:] + rpuuid = rpuuid.replace("-","") + if self.collection.count() == 0: # 첫번째 rpNode인 경우 payload = { - 'rpMAC' : info['rpMAC'], + 'rpMAC' : info, 'rpuuid' : rpuuid } + self.collection.insert_one(payload) response = json.dumps({ 'rpuuid' : rpuuid, - 'operate' : 2 # node position calculation할 필요가 없다 + 'operate' : 0 # advertise할 필요가 없다. }) else: - rpuuid = "52528282"+str(uuid.uuid4())[8:] - rpuuid = rpuuid.replace("-","") - if self.collection.count() == 0: # 첫번째 rpNode인 경우 - payload = { - 'rpMAC' : info['rpMAC'], - 'rpuuid' : rpuuid - } - self.collection.insert_one(payload) - - response = json.dumps({ - 'rpuuid' : rpuuid, - 'operate' : 0 # advertise할 필요가 없다. - }) - else: - payload = { - 'rpMAC' : info['rpMAC'], - 'rpuuid' : rpuuid - } - self.collection.insert_one(payload) + payload = { + 'rpMAC' : info, + 'rpuuid' : rpuuid + } + self.collection.insert_one(payload) - response = json.dumps({ - 'rpuuid' : rpuuid, - 'operate' : 1 # advertise를 해야한다. - }) + response = json.dumps({ + 'rpuuid' : rpuuid, + 'operate' : 1 # advertise를 해야한다. + }) return response def calculateNodePosition(self, info): # complete , need to check 4-th case. @@ -102,53 +96,39 @@ class RpNodeInfoResource(Resource): self.collection.update({'rpuuid' : info}, {'$set':{'x': 0.0, 'y': 0.0}}, False) elif self.collection.count() == 2: # 자신이 두번째 RpNode인 경우 graphB = graphColletion.find({'v2': info}) - if len(list(graphB) == 0): - #type 0 : position을 구하지 못하는 경우 - print("2번째 rpNode를 만들 수 없습니다.(exist not one line)") - response['operate'] = 0 - self.collection.update({'rpuuid' : info}, {'$set' : {'type' : 0}}, False) - return json.dumps(response) - else: - x = graphB[0]['distance'] - self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': 0.0, 'type' : 1}}, False) x = graphB[0]['distance'] self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': 0.0}}, False) elif self.collection.count() == 3: # 자신이 세번째 RpNode인 경우 graphC = graphColletion.find({'v2': info}) - if len(list(graphC) < 2): - print("3번째 rpNode를 만들 수 없습니다.(exist not two line)") - response['operate'] = 0 - self.collection.update({'rpuuid' : info}, {'$set' : {'type' : 0}}, False) + for doc in graphC: + tempdoc = self.collection.find({'rpuuid': doc['v1']}) + if tempdoc[0]['x'] == 0 and tempdoc[0]['y'] == 0: # 첫번째 RpNode 즉, (0,0) + vertexA = tempdoc[0]# 첫번째 RpNode를 vertexA라 한다. + elif tempdoc[0]['y'] == 0: + vertexB = tempdoc[0]#두번째 RpNode를 vertexB라 한다. + + dis = { + 'a': graphColletion.find({'v1': vertexB['rpuuid'], 'v2': info})[0]['distance'], + 'b': graphColletion.find({'v1': vertexA['rpuuid'], 'v2': info})[0]['distance'], + 'c': vertexB['x'] + } + if ((dis['a'] + dis['b']) <= dis['c']) or dis['a'] == 0 or dis['b'] == 0 or dis['c'] == 0: + # 3번째 rpNode를 만들 수 없는 경우 + print("3번째 rpNode를 만들 수 없습니다.(no triangle)") + response['operate'] = 1 + 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']) + if cosA >= 1 or cosA <= -1 : + # 3번째 rpNode를 만들 수 없는 경우 + print("3번째 rpNode를 만들 수 없습니다.(impossible cosA)",cosA) + response['operate'] = 1 return json.dumps(response) - else: - for doc in graphC: - tempdoc = self.collection.find({'rpuuid': doc['v1']}) - if tempdoc[0]['x'] == 0 and tempdoc[0]['y'] == 0: # 첫번째 RpNode 즉, (0,0) - vertexA = tempdoc[0]# 첫번째 RpNode를 vertexA라 한다. - elif tempdoc[0]['y'] == 0: - vertexB = tempdoc[0]#두번째 RpNode를 vertexB라 한다. - dis = { - 'a': graphColletion.find({'v1': vertexB['rpuuid'], 'v2': info})[0]['distance'], - 'b': graphColletion.find({'v1': vertexA['rpuuid'], 'v2': info})[0]['distance'], - 'c': vertexB['x'] - } - if ((dis['a'] + dis['b']) <= dis['c']) or dis['a'] == 0 or dis['b'] == 0 or dis['c'] == 0: - # 3번째 rpNode를 만들 수 없는 경우 - print("3번째 rpNode를 만들 수 없습니다.(no triangle)") - response['operate'] = 1 - 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']) - if cosA >= 1 or cosA <= -1 : - # 3번째 rpNode를 만들 수 없는 경우 - print("3번째 rpNode를 만들 수 없습니다.(impossible cosA)",cosA) - response['operate'] = 1 - return json.dumps(response) - sinA = math.sqrt((1 - math.pow(cosA,2))) - x = dis['b'] * cosA - y = dis['b'] * sinA - self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': y}}, False) + sinA = math.sqrt((1 - math.pow(cosA,2))) + x = dis['b'] * cosA + y = dis['b'] * sinA + self.collection.update({'rpuuid' : info}, {'$set':{'x': x, 'y': y}}, False) else: graphD = graphColletion.find({'v2' : info}) nodeLocationData = []