diff --git a/RPi_Client/CoAPthon3/coapclient.py b/RPi_Client/CoAPthon3/coapclient.py index 30f3a96da5f18a6945092dc66129da281782666b..18c68fdf55fd8134999cd75ad1f31931cee87bbf 100644 --- a/RPi_Client/CoAPthon3/coapclient.py +++ b/RPi_Client/CoAPthon3/coapclient.py @@ -18,7 +18,7 @@ from coapthon.utils import parse_uri __author__ = 'Giacomo Tanganelli' class CoapClient(): - def __init__(self): # 문제없음. + def __init__(self): # complete self.ip = "shmd01.iptime.org" self.myMAC = ':'.join(re.findall('..', '%012x' % uuid.getnode())) self.clearMyData() @@ -27,7 +27,7 @@ class CoapClient(): self._beacon = BeaconEmit() self.advertiseMe() - def clearMyData(self): #문제없음. + def clearMyData(self): # complete path = "/rpNodeInfoPath" path = "coap://" + self.ip + path @@ -54,7 +54,7 @@ class CoapClient(): client.stop() - def obtainMyID(self): # 문제없음. + def obtainMyID(self): # complete path = "/rpNodeInfoPath" path = "coap://" + self.ip + path @@ -83,7 +83,7 @@ class CoapClient(): client.stop() return string, json.loads(response.payload)['operate'] - def advertiseMe(self): #문제없음. + def advertiseMe(self): # complete seq = self.myuuid length = 3 string ="" @@ -93,7 +93,7 @@ class CoapClient(): print(map(''.join, zip(*[iter(seq)]*length))) self._beacon.beacon_Start(string) - def calculateNodePositionAtServer(self): #문제없음. + def calculateNodePositionAtServer(self): # complete path = "/rpNodeInfoPath" path = "coap://" + self.ip + path @@ -124,7 +124,7 @@ class CoapClient(): client.stop() client.stop() - def putBLEInfo(self): # 문제 없음. + def putBLEInfo(self): # complete dev_id = 0 try: self.sock = bluez.hci_open_dev(dev_id) @@ -165,7 +165,7 @@ class CoapClient(): response = client.put(path, json.dumps(payload)) print((response.pretty_print())) - time.sleep(1) + time.sleep(0.05) except KeyboardInterrupt: print("PutBLEInfo Stop") @@ -173,8 +173,8 @@ class CoapClient(): client.stop() - def mkJsonbeaconInfo(self, info): #문제 없음. - n = 2.2 + def mkJsonbeaconInfo(self, info): # complete + n = 4 distance = math.pow(10, (float(info[4]) - float(info[5])) / (10 * n)) payload = { 'rpuuid' : self.myuuid, @@ -184,7 +184,7 @@ class CoapClient(): } return payload - def regiRelWithNewPi(self, info): #문제 없음 + def regiRelWithNewPi(self, info): # complete #새로운 rasbPi를 찾은 경우 새로운 rasbPi를 위치측정기로 사용하기 위해서 server에 관련 정보를 저장하는 method path = "/rpGraphInfoPath" @@ -207,8 +207,8 @@ class CoapClient(): client.stop() client.stop() - def mkJsonRpgraphInfo(self, info): #문제 없음 - n = 3 + def mkJsonRpgraphInfo(self, info): # complete + n = 2.2 print(info) distance = math.pow(10, ((float(info[4]) - float(info[5])) / (10 * n))) payload = { diff --git a/Server_CoAP/CoAPthon3/coapserver.py b/Server_CoAP/CoAPthon3/coapserver.py index 62624f5caeac3a99d8fd8850b5c66c08a3543270..b46d53cd31ba8bbded5144ba1479ec9de4d485fd 100644 --- a/Server_CoAP/CoAPthon3/coapserver.py +++ b/Server_CoAP/CoAPthon3/coapserver.py @@ -12,7 +12,7 @@ from exampleresources import BleInfoResource, RpNodeInfoResource, RpGraphInfoRes from pymongo import MongoClient class CoAPServer(CoAP): - def __init__(self, host, port, multicast=False): #문제 없음. + def __init__(self, host, port, multicast=False): # complete CoAP.__init__(self, (host, port), multicast) self.add_resource('bleInfoPath/', BleInfoResource()) self.add_resource('rpNodeInfoPath/', RpNodeInfoResource()) @@ -28,28 +28,29 @@ class CoAPServer(CoAP): self.userNodeCollection = self.db.UserNodeData self.trackingCollection = self.db.TrackingData self.usersCollection = self.db.users + self.updatePeriod = 1.5 + self.limitDistance = 40 except: print("Could not connect to MongoDB") - def updateUserLocation(self): + def updateUserLocation(self): # complete try: while True: cursor = self.usersCollection.find() for c in cursor: - print(c['uuid']) positionCalculationThread = threading.Thread(target = self.positionCalculation, args=([c['uuid']])) positionCalculationThread.start() - time.sleep(10.1) + time.sleep(self.updatePeriod + 0.1) currentTime = time.time() - trackingCursor = self.trackingCollection.remove({'updateTime': {'lte' : currentTime - 10}}) + trackingCursor = self.trackingCollection.remove({'updateTime': {'$lte' : currentTime - self.updatePeriod}}) except KeyboardInterrupt: print("existing updateUserLocation") - def positionCalculation(self, useruuid): + def positionCalculation(self, useruuid): # complete but need to check triangulation.py print(useruuid) currentTime = time.time() trackingCursor = self.trackingCollection.find({'$and':[{'useruuid': useruuid}, - {'updateTime':{'$gt' : currentTime - 10}}]}) + {'updateTime':{'$gt' : currentTime - self.updatePeriod}},{'distance': {'$lt' : self.limitDistance}}]}) #trackingCursor = self.trackingCollection.find({'$and':[{'useruuid':{'$eq' : useruuid}}]}) userLocationData =[] diff --git a/Server_CoAP/CoAPthon3/dbClear.py b/Server_CoAP/CoAPthon3/dbClear.py new file mode 100644 index 0000000000000000000000000000000000000000..89141dc5e43d838d90a47e211c09bcff3bc26ddb --- /dev/null +++ b/Server_CoAP/CoAPthon3/dbClear.py @@ -0,0 +1,22 @@ +from pymongo import MongoClient + +class MyMongoDB(): + def __init__(self): #문제 없음. + try: + self.conn = MongoClient('mongodb://iotuser:iotsystem@localhost/IoT_System', 27017) + self.db = self.conn.IoT_System + self.piNodeCollection = self.db.RpNodeData + self.piGraphCollection = self.db.RpGraphData + self.userNodeCollection = self.db.UserNodeData + self.trackingCollection = self.db.TrackingData + self.usersCollection = self.db.users + except: + print("Could not connect to MongoDB") + def clear(self): + self.piNodeCollection.remove() + self.piGraphCollection.remove() + self.userNodeCollection.remove() + self.trackingCollection.remove() + +db = MyMongoDB() +db.clear() \ No newline at end of file diff --git a/Server_CoAP/CoAPthon3/exampleresources.py b/Server_CoAP/CoAPthon3/exampleresources.py index 5eeee8470938e38ae125eeed39d679b38f9402b3..c86b61489ae0cf8b58477a64642dda65f65359d1 100644 --- a/Server_CoAP/CoAPthon3/exampleresources.py +++ b/Server_CoAP/CoAPthon3/exampleresources.py @@ -6,7 +6,7 @@ from pymongo import MongoClient from coapthon import defines from coapthon.resources.resource import Resource -class RpNodeInfoResource(Resource): #문제 없음. +class RpNodeInfoResource(Resource): def __init__(self, name="RpNodeInfo"): super(RpNodeInfoResource, self).__init__(name) self.payload = "RpNodeInfo resource" @@ -41,20 +41,21 @@ class RpNodeInfoResource(Resource): #문제 없음. elif self.payload['option'] == 1: #coapclient.py에서 calculateNodePositionAtServer()이 호출된 경우 response.payload = self.calculateNodePosition(self.payload) - else :# coapclient.py 에서 obtainMyID()가 호출된 경우 + else : + # coapclient.py 에서 obtainMyID()가 호출된 경우 response.payload = self.postRpNodeInfoInDB(self.payload) response.code = defines.Codes.CHANGED.number return self, response - def deleteNodeData(self, info): #문제 없음 + 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.delete_many({'v2' : rpuuid}) - self.collection.delete_many({'rpMAC' : info}) + graphColletion.remove({'v2' : rpuuid}) + self.collection.remove({'rpMAC' : info}) - def postRpNodeInfoInDB(self, info): #문제 없음 + def postRpNodeInfoInDB(self, info): # complete info = info['rpMAC'] rpuuid = "52528282"+str(uuid.uuid4())[8:] rpuuid = rpuuid.replace("-","") @@ -82,7 +83,7 @@ class RpNodeInfoResource(Resource): #문제 없음. }) return response - def calculateNodePosition(self, info): # 문제 없음. + def calculateNodePosition(self, info): # complete , need to check 4-th case. print("Start calculateNodePosition") info = info['rpuuid'] graphColletion = self.db.RpGraphData @@ -103,21 +104,27 @@ class RpNodeInfoResource(Resource): #문제 없음. 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라 한다. - vertexB = tempdoc[1] - vertexB = tempdoc[0]#두번째 RpNode를 vertexB라 한다. - vertexA = tempdoc[1] + 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: + if ((dis['a'] + dis['b']) <= dis['c']) or dis['a'] == 0 or dis['b'] == 0 or dis['c'] == 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']) + if cosA >= 1 or cosA <= 0 : + # 3번째 rpNode를 만들 수 없는 경우 + print("3번째 rpNode를 만들 수 없습니다.") + response['operate'] = 0 + return json.dumps(response) + sinA = math.sqrt((1 - math.pow(cosA,2))) x = dis['b'] * cosA y = dis['b'] * sinA @@ -166,7 +173,7 @@ class RpNodeInfoResource(Resource): #문제 없음. response['operate'] = 0 return json.dumps(response) -class RpGraphInfoResource(Resource): #문제 없음 +class RpGraphInfoResource(Resource): # complete def __init__(self, name="RpGraphInfo"): super(RpGraphInfoResource, self).__init__(name) self.payload = "RpGraphInfo resource" @@ -204,9 +211,9 @@ class RpGraphInfoResource(Resource): #문제 없음 response.code = defines.Codes.DELETED.number return True, response - def postRpGraphInfoInDB(self, info): #문제 없음. + def postRpGraphInfoInDB(self, info): # complete info = json.loads(info) - mydoc = self.collection.find({'v1':info['v1'], 'v2':info['v2']}) + mydoc = self.collection.find({'v1':info['v1'], 'v2':info['v2'], 'distance': {'$lte' : 40}}) if mydoc.count() > 0: distance = (mydoc[0]['sum']+info['distance']) / (mydoc[0]['count'] + 1) @@ -227,7 +234,7 @@ class RpGraphInfoResource(Resource): #문제 없음 'count': 1 }, upsert=True) -class BleInfoResource(Resource): #문제없음. +class BleInfoResource(Resource): # complete def __init__(self, name="BleInfo"): super(BleInfoResource, self).__init__(name) self.payload = "BleInfo resource" diff --git a/Server_Vue_Express/backend/public/chart.js b/Server_Vue_Express/backend/public/chart.js index 847d52d422dc6a90b4425a4b7a385dd218eaea21..ea4a9684d8cad0b478250ba78da1bd6c01183607 100644 --- a/Server_Vue_Express/backend/public/chart.js +++ b/Server_Vue_Express/backend/public/chart.js @@ -21,7 +21,7 @@ var options = { yAxes: [ { ticks: { - max:30, + beginAtZero: true } } @@ -29,7 +29,7 @@ var options = { xAxes: [ { ticks: { - max:20, + beginAtZero: true } } diff --git a/Server_Vue_Express/backend/routes/api.js b/Server_Vue_Express/backend/routes/api.js index bcff263330df83e0d72a7753cd6db481f93ebc48..27b11072f6bac0240fe85da40a0981e343b8d25c 100644 --- a/Server_Vue_Express/backend/routes/api.js +++ b/Server_Vue_Express/backend/routes/api.js @@ -111,7 +111,9 @@ router.get('/findUserByUUID/:id', function(req, res, next) { for(i in temp){ console.log(i, temp[i].rpuuid) await rpNode.find({'rpuuid': temp[i].rpuuid},(err,output)=>{ - //console.log(temp[i].distance) + console.log(output) + if(output.length == 0) + break; rpNodeData.push({ rpuuid : temp[i].rpuuid, x: output[0].x,