From 1ae4a51a0ab0dc32606941695fdd48d5b2e47fa3 Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Tue, 19 Sep 2023 19:56:56 +0200
Subject: [PATCH] #187 - Find pad id by user provided name, receive pad name
 updates, cache pads by name in project model for fast access

---
 actions.js                    | 16 +++++++++-------
 main.js                       | 15 ++++++++++++---
 project.js                    | 35 +++++++++++++++++++++++++++++++++++
 receive/message_executable.js |  5 +++++
 receive/pad_name_update.js    |  8 ++++++++
 receive/project_update.js     |  3 ++-
 6 files changed, 71 insertions(+), 11 deletions(-)
 create mode 100644 project.js
 create mode 100644 receive/pad_name_update.js

diff --git a/actions.js b/actions.js
index 43e9caf..f66d62a 100644
--- a/actions.js
+++ b/actions.js
@@ -4,17 +4,19 @@ module.exports = function (self) {
             name: 'Trigger Pad',
             options: [
                 {
-                    id: 'num',
-                    type: 'number',
-                    label: 'Test',
-                    default: 5,
-                    min: 0,
-                    max: 100,
+                    id: 'name',
+                    type: 'textinput',
+                    label: 'Pad Name',
+                    default: '',
                 },
             ],
             callback: async (event) => {
+                const pad = self.currentProject.findPadByName(event.options.name);
+                if (!pad) {
+                    self.log('warning', 'Cannot find a pad with name ' + event.options.name);
+                }
                 const payload = {
-                    'pad': '8b7ef494-d735-45f9-ab84-c95e299298a6'
+                    'pad': pad.id
                 };
                 self.sendToWebSocket('cart-action', payload)
             },
diff --git a/main.js b/main.js
index 5e74222..4bd6130 100644
--- a/main.js
+++ b/main.js
@@ -5,6 +5,8 @@ const UpdateFeedbacks = require('./feedbacks')
 const UpdateVariableDefinitions = require('./variables')
 const WebSocket = require('ws')
 const ProjectUpdate = require("./receive/project_update");
+const Project = require("./project");
+const PadNameUpdate = require("./receive/pad_name_update");
 
 class ModuleInstance extends InstanceBase {
     isInitialized = false
@@ -12,10 +14,11 @@ class ModuleInstance extends InstanceBase {
     wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$'
 
     messageHandlers = {
-        'project-current': new ProjectUpdate()
+        'project-current': new ProjectUpdate(),
+        'pad-name-changed': new PadNameUpdate()
     };
 
-    currentProject = {}
+    currentProject = new Project({});
 
     constructor(internal) {
         super(internal)
@@ -152,8 +155,14 @@ class ModuleInstance extends InstanceBase {
             } else {
                 this.log('debug', `Cannot handle incoming message of type ${message.type}`)
             }
+        } else if (message.updateType != null) {
+            if (this.messageHandlers[message.updateType] != null) {
+                this.messageHandlers[message.updateType].handleMessage(this, message)
+            } else {
+                this.log('debug', `Cannot handle incoming message of updateType ${message.updateType}`)
+            }
         } else {
-            this.log('debug', `Cannot handle incoming message ${message}`)
+            this.log('debug', `Cannot handle incoming message ${data}`)
         }
     }
 }
diff --git a/project.js b/project.js
new file mode 100644
index 0000000..7ce958b
--- /dev/null
+++ b/project.js
@@ -0,0 +1,35 @@
+module.exports = class Project {
+    constructor(data) {
+        this.data = data
+        this.padCache = {}
+
+        // Prefill with current project
+        if (data) {
+            console.log('Create pad cache')
+            for (const pageId in data.pages) {
+                const page = data.pages[pageId]
+                for (const padId in page.pads) {
+                    const pad = page.pads[padId];
+                    if (pad.name !== "") {
+                        this.padCache[pad.name] = pad;
+                        console.log(`Add pad "${pad.name}" to cache`)
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     *
+     * @param {string} oldName
+     * @param {string} newName
+     */
+    updatePadName(oldName, newName) {
+        this.padCache[newName] = this.padCache[oldName];
+        delete this.padCache[oldName];
+    }
+
+    findPadByName(name) {
+        return this.padCache[name];
+    }
+}
\ No newline at end of file
diff --git a/receive/message_executable.js b/receive/message_executable.js
index 34ecf98..5d0039f 100644
--- a/receive/message_executable.js
+++ b/receive/message_executable.js
@@ -1,4 +1,9 @@
 module.exports = class MessageExecutable {
+    /**
+     *
+     * @param {ModuleInstance} plugin
+     * @param {Object} message
+     */
     handleMessage(plugin, message) {
       // Abstract method
     }
diff --git a/receive/pad_name_update.js b/receive/pad_name_update.js
new file mode 100644
index 0000000..d7533f5
--- /dev/null
+++ b/receive/pad_name_update.js
@@ -0,0 +1,8 @@
+const MessageExecutable = require("./message_executable");
+
+module.exports = class PadNameUpdate extends MessageExecutable {
+
+    handleMessage(plugin, message) {
+        plugin.currentProject.updatePadName(message.oldValue, message.newValue);
+    }
+}
\ No newline at end of file
diff --git a/receive/project_update.js b/receive/project_update.js
index 219e055..80a9ebe 100644
--- a/receive/project_update.js
+++ b/receive/project_update.js
@@ -1,8 +1,9 @@
 const MessageExecutable = require("./message_executable");
+const Project = require("../project");
 
 module.exports = class ProjectUpdate extends MessageExecutable {
 
     handleMessage(plugin, message) {
-        plugin.currentProject = message.payload
+        plugin.currentProject = new Project(message)
     }
 }
\ No newline at end of file
-- 
GitLab