Parcourir la source

Data collection through websockets

rigwild il y a 4 ans
Parent
commit
546cd0d999

+ 15 - 0
config.messagesId.js

@@ -0,0 +1,15 @@
+// List of IDs for messages sent using WebSockets
+
+// Message IDs for experiments events
+export const EXPERIMENT = {
+  // An experiment was started
+  STARTED: 'EXPERIMENT_STARTED',
+
+  // An experiment was started
+  DATA: 'EXPERIMENT_DATA',
+
+  // An experiment was validated
+  VALIDATED: 'EXPERIMENT_VALIDATED'
+}
+
+export default { EXPERIMENT }

+ 1 - 1
server/routes/getImage.js

@@ -117,7 +117,7 @@ const router = express.Router()
  * @param {string} sceneName the scene to get the image from
  * @param {number|"min"|"max"|"median"} quality the requested quality
  * @param {boolean} [nearestQuality=false] if selected quality not availabie, select the nearest one
- * @returns {Promise<Image>} the link and path to the image
+ * @returns {Promise<Image>} the image data
  */
 export const getImage = async (sceneName, quality, nearestQuality = false) => {
   const throwErrIfTrue = x => {

+ 2 - 1
server/routes/getImageExtracts.js

@@ -181,9 +181,10 @@ const cutImage = async (image, xExtracts, yExtracts) => {
 
       // Zone number of the extract `00020`
       const fileNameCount = (extracts.length + 1).toString().padStart(5, '0')
+      const fileNameQuality = image.quality.toString().padStart(5, '0')
 
       // File name of the extract : `Scene2_zone00199_100.png`
-      const extractName = `${image.sceneName}_zone${fileNameCount}_${image.quality}.${image.ext}`
+      const extractName = `${image.sceneName}_zone${fileNameCount}_${fileNameQuality}.${image.ext}`
 
       // Configured path to the image (Check defined convention)
       const pathToImage = [image.sceneName, extractsDirName, `x${xExtracts}_y${yExtracts}`, `zone${fileNameCount}`, extractName]

+ 11 - 1
src/mixins/ExperimentBase/index.vue

@@ -7,6 +7,7 @@
 <script>
 import { mapGetters, mapActions } from 'vuex'
 import { API_ROUTES } from '@/functions'
+import { EXPERIMENT as experimentMsgId } from '@/../config.messagesId'
 
 export default {
   name: 'ExperimentBase',
@@ -29,6 +30,9 @@ export default {
     ...mapGetters(['getHostURI', 'getExperimentProgress', 'isExperimentDone'])
   },
   mounted() {
+    if (!this.getExperimentProgress({ experimentName: this.experimentName, sceneName: this.sceneName }).experimentName)
+      this.sendMessage({ msgId: experimentMsgId.STARTED })
+
     // Check if the experiment is already finished
     if (this.experimentName && this.sceneName && this.isExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName })) {
       console.warn('Redirected from experiment. You can\'t go back in an experiment after finishing it.')
@@ -56,7 +60,13 @@ export default {
       // console.log('Saved data from local state to store.', this.$data)
     },
 
-    async finishExperiment() {
+    // Finish an experiment, sending full data to the server
+    // Don't forget to surcharge this function when using this mixin to add more data
+    finishExperiment() {
+      const obj = Object.assign(this.$data, { sceneName: this.sceneName })
+      obj.loadingMessage = undefined
+      obj.loadingErrorMessage = undefined
+      this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
       this.setExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName, done: true })
       this.$router.push(`/experiments/${this.experimentName}`)
     },

+ 27 - 0
src/mixins/ExperimentBaseExtracts/index.vue

@@ -10,6 +10,7 @@ import ExperimentBase from '@/mixins/ExperimentBase'
 
 import { mapGetters } from 'vuex'
 import { API_ROUTES, findNearestUpper, findNearestLower } from '@/functions'
+import { EXPERIMENT as experimentMsgId } from '@/../config.messagesId'
 
 export default {
   name: 'ExperimentBaseExtracts',
@@ -97,6 +98,9 @@ export default {
         this.extracts[index].nextQuality = findNearestUpper(data.info.image.quality, this.qualities)
         this.extracts[index].precQuality = findNearestLower(data.info.image.quality, this.qualities)
         this.extracts[index].loading = false
+
+        // Sending event to WebSocket server
+      // this.sendMessage({ msgId: experimentMsgId.DATA, msg: obj })
       }
       catch (err) {
         // TODO: toast message if fail
@@ -106,6 +110,29 @@ export default {
         this.extracts[index].loading = false
         this.saveProgress()
       }
+    },
+
+    // Finish an experiment, sending full data to the server
+    // Don't forget to surcharge this function when using this mixin to add more data
+    finishExperiment() {
+      const obj = {
+        experimentName: this.experimentName,
+        sceneName: this.sceneName,
+        extractConfig: this.extractConfig,
+        extracts: this.extracts.map(x => ({
+          index: x.index,
+          link: x.link,
+          nextQuality: x.nextQuality,
+          precQuality: x.precQuality,
+          quality: x.quality,
+          zone: x.zone
+        })),
+        qualities: this.qualities,
+        referenceImage: this.referenceImage
+      }
+      this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
+      this.setExperimentDone({ experimentName: this.experimentName, sceneName: this.sceneName, done: true })
+      this.$router.push(`/experiments/${this.experimentName}`)
     }
   }
 }

+ 2 - 2
src/store/actions.js

@@ -52,8 +52,8 @@ export default {
     else throw new Error('Could not connect to WebSocket server. Host is not configured.')
   },
 
-  sendMessage(_, message) {
-    Vue.prototype.$socket.send(JSON.stringify(message) || message)
+  sendMessage(_, { msgId, msg = undefined }) {
+    Vue.prototype.$socket.send(JSON.stringify({ msgId, msg }))
   },
 
   async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {