From 465084c433c9be2994ca5412ed8e4bb8a7386205 Mon Sep 17 00:00:00 2001
From: Antoine Rochebois <antoine.rochebois@insa-lyon.fr>
Date: Sat, 2 Jan 2021 19:47:33 +0100
Subject: [PATCH] NFC UID to team peering

---
 client/src/App.vue                    | 13 ++++++++--
 client/src/components/TeamAdder.vue   |  7 +++--
 client/src/components/TeamElement.vue | 37 ++++++++++++++++++++++++---
 client/src/main.js                    |  1 +
 client/src/usbNfcReader.js            | 22 ++++++++++++++++
 server/db/team.route.js               | 34 +++++++++++++++++++++---
 6 files changed, 103 insertions(+), 11 deletions(-)

diff --git a/client/src/App.vue b/client/src/App.vue
index bc36790..25126e7 100644
--- a/client/src/App.vue
+++ b/client/src/App.vue
@@ -85,7 +85,7 @@
 <script>
 import PageFooter from "./components/PageFooter.vue";
 import UsbNfcReader from "./usbNfcReader.js"
-var usbNfcReader = new UsbNfcReader();
+
 export default {
   name: "app",
   data() {
@@ -93,6 +93,9 @@ export default {
       showNav: false
     };
   },
+  created() {
+    this.usbNfcReader = new UsbNfcReader();
+  },
   methods: {
     logout() {
       let uri = "//localhost:3000/logout";
@@ -102,7 +105,13 @@ export default {
       });
     },
     connectUsb() {
-      usbNfcReader.selectAndConnect()
+      this.usbNfcReader.selectAndConnect()
+    },
+    readTag() {
+      this.usbNfcReader.sendString('READ_')
+    },
+    readTagUid() {
+      this.usbNfcReader.sendString('GETID_')
     }
   },
   components: {
diff --git a/client/src/components/TeamAdder.vue b/client/src/components/TeamAdder.vue
index db36b08..f6b004e 100644
--- a/client/src/components/TeamAdder.vue
+++ b/client/src/components/TeamAdder.vue
@@ -66,8 +66,11 @@ export default {
       this.count++;
     },
     removeMemberField() {
-      this.team.members.pop();
-      this.count--;
+      if (this.count > 0) {
+        this.team.members.pop();
+        this.count--;
+      }
+      
     }
   }
 };
diff --git a/client/src/components/TeamElement.vue b/client/src/components/TeamElement.vue
index 6849db6..f548d9e 100644
--- a/client/src/components/TeamElement.vue
+++ b/client/src/components/TeamElement.vue
@@ -21,12 +21,12 @@
             <p class="heading" v-else>
               Aucune carte associée !
             </p>
-            <button v-if="team.cardId" class="button is-danger">
+            <button v-if="team.cardId" @click="removeCardPairing()" class="button is-danger">
               Dissocier la carte
             </button>
             <button
               v-else
-              @click="showPairingModal = !showPairingModal"
+              @click="manageCardPairing()"
               class="button is-warning"
             >
               Associer une carte
@@ -83,7 +83,7 @@
             </div>
             <div class="message-body">
               Veuillez scanner une carte sur le lecteur pour l'associer à cette
-              équipe :
+              équipe.
             </div>
           </div>
         </div>
@@ -122,6 +122,37 @@ export default {
         console.log("bien supprimé !", response);
         this.teamArray.splice(this.teamArray.indexOf(this.team), 1);
       });
+    },
+
+    manageCardPairing: function() {
+      this.showPairingModal = !this.showPairingModal
+      this.$parent.$parent.usbNfcReader.readTagUid()
+      .then(response => {
+        console.log(response)
+        this.team.cardId = response
+        let uri = `//localhost:3000/school/updateTeamCard/${this.team._id}`;
+        this.axios
+        .post(uri, this.team, { withCredentials: true })
+        .then(response => {
+          console.log("Carte ajoutée !", response);
+          this.showPairingModal = false;
+        }).catch(response => {
+          this.showPairingModal = false;
+          this.team.cardId = null;
+          console.log("Carte déjà utilisée !", response)
+        })
+        
+        })
+    },
+
+    removeCardPairing: function() {
+      let uri = `//localhost:3000/school/removeTeamCard/${this.team._id}`;
+        this.axios
+        .post(uri, this.team, { withCredentials: true })
+        .then(response => {
+          console.log("Carte dissociée !", response);
+          this.team.cardId = null;
+        });
     }
   }
 };
diff --git a/client/src/main.js b/client/src/main.js
index c8c53d2..5f30676 100644
--- a/client/src/main.js
+++ b/client/src/main.js
@@ -24,6 +24,7 @@ Vue.use(VueAxios, axios);
 
 Vue.config.productionTip = false;
 
+
 const routes = [
   {
     name: "home",
diff --git a/client/src/usbNfcReader.js b/client/src/usbNfcReader.js
index ec97a22..aebbbec 100644
--- a/client/src/usbNfcReader.js
+++ b/client/src/usbNfcReader.js
@@ -43,11 +43,33 @@ export default class UsbNfcReader {
           this.port.onReceiveError = error => {
             console.log('Receive error: ' + error);
           };
+          return true;
         }, error => {
           console.log('Connection error: ' + error);
+          return false;
         });
     }
 
+    readTagUid() {
+      return new Promise((resolve, reject) => {
+        this.sendString("GETID_")
+        this.port.onReceive = data => {
+          let textDecoder = new TextDecoder();
+          let msg = textDecoder.decode(data);
+          msg = msg.replace(/(\r\n|\n|\r)/gm, "");
+          console.log("Reçu :" + msg);
+          let cmdIdentifier = msg.split('_')[0]
+          if (msg == "RECEIVED_GETID;") {
+            console.log("yay ! bien reçu !")
+          } else if (cmdIdentifier == "UID" && msg.slice(-1) == ';') {
+            resolve(msg.split('_')[1].slice(0, -1))
+          } else {
+            reject("Bad message structure received")
+          }
+          
+        }
+      })
+    }
     //Envoie ce string à l'Arduino
     sendString(str) {
         if (this.port !== undefined) {
diff --git a/server/db/team.route.js b/server/db/team.route.js
index 3b663bf..54095e6 100644
--- a/server/db/team.route.js
+++ b/server/db/team.route.js
@@ -42,14 +42,40 @@ router.route('/deleteTeam/:id').delete(function (req, res) {
 });
 //TODO allow operation only if logged in
 router.route('/updateTeamCard/:id').post(function (req, res) {
-  console.log("Request for updating card ID :", req.body.cardId);
-  Team.updateOne({_id: req.params.id}, {cardId: req.body.cardId}, function(err){
+  if (req.user) {
+    console.log("Request for updating card ID :", req.body.cardId);
+    Team.count({schoolUser: req.user.username, cardId: req.body.cardId}, (err, count) => {
+      if (err) res.json(err);
+      else {
+        if (count > 0) {
+          res.status(500).json({error : "Alredy an user with this card"})
+        }
+        else {
+          Team.updateOne({_id: req.params.id, schoolUser: req.user.username}, {cardId: req.body.cardId}, function(err){
+            if(err) res.json(err);
+            else {
+              res.json('Successfully updated card ID');
+              console.log('Updated team card')
+            }
+          });
+        }
+      }
+    })
+    
+  }
+});
+
+router.route('/removeTeamCard/:id').post(function (req, res) {
+  if (req.user) {
+    console.log("Request for removing card ID")
+    Team.updateOne({_id: req.params.id, schoolUser: req.user.username}, {$unset: {cardId: ""}}, function(err){
       if(err) res.json(err);
       else {
-        res.json('Successfully updated card ID');
+        res.json('Successfully removed card ID');
         console.log('Updated team card')
       }
-  });
+    });
+  }  
 });
 
 router.route('/updateTeamPuzzles/:cardId').post(function (req, res) {
-- 
GitLab