From 7cb0bdfbedf95ab48b7da46d49f170059ff17096 Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Fri, 29 Sep 2023 15:17:48 +0200
Subject: [PATCH] #187 - Add feedback for pad status

---
 feedbacks.js                 | 32 ++++++++++++++++----------------
 main.js                      |  4 +++-
 project.js                   | 32 +++++++++++++++++++++++++++++++-
 receive/pad_status_update.js | 11 +++++++++++
 4 files changed, 61 insertions(+), 18 deletions(-)
 create mode 100644 receive/pad_status_update.js

diff --git a/feedbacks.js b/feedbacks.js
index 1f331c7..7fb6e32 100644
--- a/feedbacks.js
+++ b/feedbacks.js
@@ -2,31 +2,31 @@ const { combineRgb } = require('@companion-module/base')
 
 module.exports = async function (self) {
 	self.setFeedbackDefinitions({
-		ChannelState: {
-			name: 'Example Feedback',
+		PadStatus: {
+			name: 'Pad Status',
 			type: 'boolean',
-			label: 'Channel State',
+			label: 'Pad Status',
 			defaultStyle: {
 				bgcolor: combineRgb(255, 0, 0),
 				color: combineRgb(0, 0, 0),
 			},
 			options: [
 				{
-					id: 'num',
-					type: 'number',
-					label: 'Test',
-					default: 5,
-					min: 0,
-					max: 10,
+					id: 'name',
+					type: 'textinput',
+					label: 'Pad Name',
+					default: '',
 				},
 			],
-			callback: (feedback) => {
-				console.log('Hello world!', feedback.options.num)
-				if (feedback.options.num > 5) {
-					return true
-				} else {
-					return false
-				}
+			callback: (feedback, context) => {
+                self.log('debug', `Update feedback for pad name ${feedback.options.name}`);
+				const pad = self.currentProject.findPadByName(feedback.options.name);
+                if (pad != null) {
+                    self.log('debug', `Update feedback for pad id ${pad.id} with status ${pad.status}`);
+                    return pad.status === 'PLAY';
+                } else {
+                    return false;
+                }
 			},
 		},
 	})
diff --git a/main.js b/main.js
index ee804df..5b12f73 100644
--- a/main.js
+++ b/main.js
@@ -7,6 +7,7 @@ const WebSocket = require('ws')
 const ProjectUpdate = require("./receive/project_update");
 const Project = require("./project");
 const PadNameUpdate = require("./receive/pad_name_update");
+const PadStatusUpdate = require("./receive/pad_status_update");
 const uuid = require('uuid');
 
 class ModuleInstance extends InstanceBase {
@@ -16,7 +17,8 @@ class ModuleInstance extends InstanceBase {
 
     messageHandlers = {
         'project-current': new ProjectUpdate(),
-        'pad-name-changed': new PadNameUpdate()
+        'pad-name-changed': new PadNameUpdate(),
+        'pad-status-changed': new PadStatusUpdate()
     };
 
     currentProject = new Project({});
diff --git a/project.js b/project.js
index 7ce958b..57530b3 100644
--- a/project.js
+++ b/project.js
@@ -1,6 +1,5 @@
 module.exports = class Project {
     constructor(data) {
-        this.data = data
         this.padCache = {}
 
         // Prefill with current project
@@ -29,7 +28,38 @@ module.exports = class Project {
         delete this.padCache[oldName];
     }
 
+    /**
+     *
+     * @param {string} padId
+     * @param {string} newStatus
+     */
+    updatePadStatus(padId, newStatus) {
+        const pad = this.findPadById(padId);
+        if (pad != null) {
+            pad.status = newStatus;
+        }
+    }
+
+    /**
+     *
+     * @param {string} name
+     * @returns {*}
+     */
     findPadByName(name) {
         return this.padCache[name];
     }
+
+    /**
+     *
+     * @param {string} padId
+     */
+    findPadById(padId) {
+        for (const id in this.padCache) {
+            const pad = this.padCache[id];
+            if (pad.id === padId) {
+                return pad
+            }
+        }
+        return null
+    }
 }
\ No newline at end of file
diff --git a/receive/pad_status_update.js b/receive/pad_status_update.js
new file mode 100644
index 0000000..6a5b03c
--- /dev/null
+++ b/receive/pad_status_update.js
@@ -0,0 +1,11 @@
+const MessageExecutable = require("./message_executable");
+
+module.exports = class PadStatusUpdate extends MessageExecutable {
+
+    handleMessage(plugin, message) {
+        plugin.currentProject.updatePadStatus(message.pad, message.status);
+        plugin.checkFeedbacks();
+    }
+}
+
+// {"pad":"8b7ef494-d735-45f9-ab84-c95e299298a6","status":"PLAY","updateType":"pad-status-changed"}
\ No newline at end of file
-- 
GitLab