From a702ff4e6067e68d17e80a87a308755f5f2886f8 Mon Sep 17 00:00:00 2001
From: "Server (Shared Users)" <shmd01.iptime.org>
Date: Wed, 4 Dec 2019 04:45:07 +0000
Subject: [PATCH] revise

---
 RPi_Client/CoAPthon3/coapclient.py            |  4 +-
 RPi_Client/CoAPthon3/piBeacon.py              |  7 +--
 Server_CoAP/CoAPthon3/coapserver.py           | 12 +++-
 Server_Vue_Express/backend/models/tracking.js |  4 +-
 Server_Vue_Express/backend/routes/api.js      | 57 +++++++++++++++++++
 Server_Vue_Express/backend/views/rpis.pug     | 10 ++++
 6 files changed, 83 insertions(+), 11 deletions(-)
 create mode 100644 Server_Vue_Express/backend/views/rpis.pug

diff --git a/RPi_Client/CoAPthon3/coapclient.py b/RPi_Client/CoAPthon3/coapclient.py
index eab472e..bdb4861 100644
--- a/RPi_Client/CoAPthon3/coapclient.py
+++ b/RPi_Client/CoAPthon3/coapclient.py
@@ -173,7 +173,7 @@ class CoapClient():
         client.stop()
 
     def mkJsonbeaconInfo(self, info): # complete
-        n = 4
+        n = 2.05
         distance = math.pow(10, (float(info[4]) - float(info[5])) / (10 * n))
         payload = {
             'rpuuid' : self.myuuid,
@@ -207,7 +207,7 @@ class CoapClient():
         client.stop()
 
     def mkJsonRpgraphInfo(self, info): # complete
-        n = 2.2
+        n = 2.05
         distance = math.pow(10, ((float(info[4]) - float(info[5])) / (10 * n)))
         payload = {
             'v1' : self.myuuid,
diff --git a/RPi_Client/CoAPthon3/piBeacon.py b/RPi_Client/CoAPthon3/piBeacon.py
index c2b683d..b437143 100644
--- a/RPi_Client/CoAPthon3/piBeacon.py
+++ b/RPi_Client/CoAPthon3/piBeacon.py
@@ -17,8 +17,7 @@ bct_IBEACONPROFIX = "1E 02 01 1A 1A FF 4C 00 02 15"
 bct_UUID = " 00 00 00 AC E8 B4 E0 C2 7D 20 B6 11 B6 11 C7 74"
 bct_MAJOR = "00 01"
 bct_MINOR = "00 02"
-bct_POWER = "C4 00"
-
+bct_POWER = "C8 00"
 class BeaconEmit():
     def __init__(self):
         self.beacon_TX_config("up")
@@ -50,16 +49,16 @@ class BeaconEmit():
         _bct_ogf = bct_OGF + " "
         _ocf = _ocf + " "
         result = subprocess.check_output("sudo hcitool -i " + bct_BLUETOOTHDEVICE + " cmd " + _bct_ogf + _ocf + _param, shell=True)
+        time.sleep(0.1)
 
     def beacon_TX_DevTrigger(self, _str):
         _bct_uuid = _str
         self.beacon_TX_cmd_format(bct_OCF_format, bct_IBEACONPROFIX, _bct_uuid, bct_MAJOR, bct_MINOR, bct_POWER)
-        sleep(0.3)
 
     def beacon_Start(self, _str):
         try:
             startTime = time.time()
-            repeatTime = 10
+            repeatTime = 15
             print ("BLE EMIT START")
             while True:
                 if time.time() - startTime > repeatTime:
diff --git a/Server_CoAP/CoAPthon3/coapserver.py b/Server_CoAP/CoAPthon3/coapserver.py
index 9807f64..a4a085a 100644
--- a/Server_CoAP/CoAPthon3/coapserver.py
+++ b/Server_CoAP/CoAPthon3/coapserver.py
@@ -76,21 +76,26 @@ class CoAPServer(CoAP):
         for u in userLocationData:
             print(userLocationData)
         
+        if len(userLocationData) == 0:
+            print("userLocationData == 0")
+            return
+        
         rp1uuid = userLocationData[0]['rpuuid']
-        rp1 = self.rpNodeInfoPath.find({'rpuuid' : rp1})
+        rp1 = self.piNodeCollection.find({'rpuuid' : rp1uuid})
 
         if len(userLocationData) < 3:
-            print("userLoicationData < 3")
+            print("userLocationData < 3")
             dd = datetime.datetime.now()
             dd = dd.replace(hour=dd.hour + 9)
             userNodeData = {
                 'rp1': {'rpuuid' : userLocationData[0]['rpuuid'], 'distance': userLocationData[0]['distance']},
                 'useruuid': useruuid,
                 'x' : rp1[0]['x'],
-                'y' : rp1[0]['y']
+                'y' : rp1[0]['y'],
                 'updateTime': str(dd)
             }
             print(userNodeData)
+            self.userNodeCollection.insert_one(userNodeData)
             return
 
         else :
@@ -117,6 +122,7 @@ class CoAPServer(CoAP):
                     'updateTime': str(dd)
                 }
                 print(userNodeData)
+                self.userNodeCollection.insert_one(userNodeData)
                 return
 
             dd = datetime.datetime.now()
diff --git a/Server_Vue_Express/backend/models/tracking.js b/Server_Vue_Express/backend/models/tracking.js
index 3d74d63..24a3fe6 100644
--- a/Server_Vue_Express/backend/models/tracking.js
+++ b/Server_Vue_Express/backend/models/tracking.js
@@ -2,8 +2,8 @@ var mongoose = require('mongoose');
 const Schema = mongoose.Schema;
  
 const trackingSchema = new Schema({
-    rasbpID: { type: String, default: ""  },
-    uuid: { type: String, default: ""  },
+    rpuuid: { type: String, default: ""  },
+    useruuid: { type: String, default: ""  },
     distance: { type: Number, default: 0  },
     updateTime : { type: Number, default: 0  }
 }, { collection : 'TrackingData', // 컬렉션명 지정 
diff --git a/Server_Vue_Express/backend/routes/api.js b/Server_Vue_Express/backend/routes/api.js
index 74be2d4..100fbac 100644
--- a/Server_Vue_Express/backend/routes/api.js
+++ b/Server_Vue_Express/backend/routes/api.js
@@ -141,6 +141,63 @@ router.get('/users', function(req, res, next) {
   
 });
 
+router.get('/rpis', function(req, res, next) {
+  // User.find((err,output) =>{
+  //   console.log(output);
+  //   res.render('users', { data: output });
+  // })
+  Tracking.aggregate([
+    {
+      '$group': {
+        '_id': '$rpuuid'
+      }
+    }
+  ],function(rr,ra){
+    if(ra){
+      console.log(ra);   
+      res.render('rpis',{data:ra});
+    } 
+  });
+});
+
+router.get('/aggregateByRPUUID/:id', function(req, res, next) {
+  // User.find((err,output) =>{
+  //   console.log(output);
+  //   res.render('users', { data: output });
+  // })
+  Tracking.findOne({rpuuid: req.params.id},(err, output) => {
+    console.log(output);
+    const lastUpdateTime = output.updateTime;
+    const lastUpdateTimeObject = new Date(Date(output.updateTime));
+    console.log(Date(lastUpdateTime));
+
+    console.log(output.distance)
+
+    Tracking.aggregate([
+      {
+        '$match': {
+          'rpuuid': output.rpuuid, 
+          'updateTime': {
+            '$gt': lastUpdateTime - 600
+          },
+          distance: {'$lt': 1}
+        }
+      }, {
+        '$group': {
+          '_id': '$useruuid'
+        }
+      }
+    ],function(rr,ra){
+      if(ra){
+        console.log(ra); 
+        res.json({Date : lastUpdateTimeObject.toLocaleString('ko-KR', {
+          timeZone: 'Asia/Seoul'
+        }),count : ra.length,result: ra});  
+      } 
+    })
+  })
+  //res.json({});
+});
 router.post('/', function(req, res, next) {
   console.log(req.body)
   res.render('index', { title: 'Express' });
diff --git a/Server_Vue_Express/backend/views/rpis.pug b/Server_Vue_Express/backend/views/rpis.pug
new file mode 100644
index 0000000..69a80d8
--- /dev/null
+++ b/Server_Vue_Express/backend/views/rpis.pug
@@ -0,0 +1,10 @@
+extends layout
+
+block content
+  hr(size='3px')
+  p
+    - let i=0;
+    for i in data
+      a(href='/api/aggregateByRPUUID/'+i._id) #{i.name} , uuid:#{i._id}
+      br
+      -i++
\ No newline at end of file
-- 
GitLab