-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
156 lines (128 loc) · 6.1 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* global $CC, Utils, $SD */
/**
* Here are a couple of wrappers we created to help ypu quickly setup
* your plugin and subscribe to events sent by Stream Deck to your plugin.
*/
/**
* The 'connected' event is sent to your plugin, after the plugin's instance
* is registered with Stream Deck software. It carries the current websocket
* and other information about the current environmet in a JSON object
* You can use it to subscribe to events you want to use in your plugin.
*/
$SD.on('connected', (jsonObj) => connected(jsonObj));
function connected(jsn) {
/** subscribe to the willAppear and other events */
$SD.on('com.elgato.template.action.willAppear', (jsonObj) => action.onWillAppear(jsonObj));
$SD.on('com.elgato.template.action.keyUp', (jsonObj) => action.onKeyUp(jsonObj));
$SD.on('com.elgato.template.action.sendToPlugin', (jsonObj) => action.onSendToPlugin(jsonObj));
$SD.on('com.elgato.template.action.didReceiveSettings', (jsonObj) => action.onDidReceiveSettings(jsonObj));
$SD.on('com.elgato.template.action.propertyInspectorDidAppear', (jsonObj) => {
console.log('%c%s', 'color: white; background: black; font-size: 13px;', '[app.js]propertyInspectorDidAppear:');
});
$SD.on('com.elgato.template.action.propertyInspectorDidDisappear', (jsonObj) => {
console.log('%c%s', 'color: white; background: red; font-size: 13px;', '[app.js]propertyInspectorDidDisappear:');
});
};
/** ACTIONS */
const action = {
settings: {},
onDidReceiveSettings: function (jsn) {
console.log('%c%s', 'color: white; background: red; font-size: 15px;', '[app.js]onDidReceiveSettings:');
this.settings = Utils.getProp(jsn, 'payload.settings', {});
this.doSomeThing(this.settings, 'onDidReceiveSettings', 'orange');
/**
* In this example we put a HTML-input element with id='mynameinput'
* into the Property Inspector's DOM. If you enter some data into that
* input-field it get's saved to Stream Deck persistently and the plugin
* will receice the updated 'didReceiveSettings' event.
* Here we look for this setting and use it to change the title of
* the key.
*/
this.saveVariables(jsn);
},
/**
* The 'willAppear' event is the first event a key will receive, right before it gets
* showed on your Stream Deck and/or in Stream Deck software.
* This event is a good place to setup your plugin and look at current settings (if any),
* which are embedded in the events payload.
*/
onWillAppear: function (jsn) {
console.log("You can cache your settings in 'onWillAppear'", jsn.payload.settings);
/**
* "The willAppear event carries your saved settings (if any). You can use these settings
* to setup your plugin or save the settings for later use.
* If you want to request settings at a later time, you can do so using the
* 'getSettings' event, which will tell Stream Deck to send your data
* (in the 'didReceiceSettings above)
*
* $SD.api.getSettings(jsn.context);
*/
this.settings = jsn.payload.settings;
// nothing in the settings pre-fill something just for demonstration purposes
if (!this.settings || Object.keys(this.settings).length === 0) {
this.settings.mynameinput = 'TEMPLATE';
}
this.saveVariables(jsn);
},
onKeyUp: function (jsn) {
this.mqttclient.send(jsn.payload.settings.topic, jsn.payload.settings.payload);
},
onSendToPlugin: function (jsn) {
/**
* this is a message sent directly from the Property Inspector
* (e.g. some value, which is not saved to settings)
* You can send this event from Property Inspector (see there for an example)
*/
const sdpi_collection = Utils.getProp(jsn, 'payload.sdpi_collection', {});
if (sdpi_collection.value && sdpi_collection.value !== undefined) {
this.doSomeThing({[sdpi_collection.key]: sdpi_collection.value}, 'onSendToPlugin', 'fuchsia');
}
},
/**
* This snippet shows, how you could save settings persistantly to Stream Deck software
* It is not used in this example plugin.
*/
saveSettings: function (jsn, sdpi_collection) {
console.log('saveSettings:', jsn);
if (sdpi_collection.hasOwnProperty('key') && sdpi_collection.key != '') {
if (sdpi_collection.value && sdpi_collection.value !== undefined) {
this.settings[sdpi_collection.key] = sdpi_collection.value;
console.log('setSettings....', this.settings);
$SD.api.setSettings(jsn.context, this.settings);
}
}
},
/**
* Here's a quick demo-wrapper to show how you could change a key's title based on what you
* stored in settings.
* If you enter something into Property Inspector's name field (in this demo),
* it will get the title of your key.
*
* @param {JSON} jsn // the JSON object passed from Stream Deck to the plugin, which contains the plugin's context
*
*/
saveVariables: function (jsn) {
if (this.settings && this.settings.hasOwnProperty('mynameinput')) {
$SD.api.setTitle(jsn.context, this.settings.mynameinput);
}
if (this.mqttclient) {
this.mqttclient.disconnect();
}
//TODO: use true UUID instead of Math.random()
this.mqttclient = new Paho.MQTT.Client(jsn.payload.settings.broker, 9001, "Elgato " + Math.random());
try {
this.mqttclient.connect();
} catch (err) {
console.log("Connection Error" + err);
}
},
/**
* Finally here's a methood which gets called from various events above.
* This is just an idea how you can act on receiving some interesting message
* from Stream Deck.
*/
doSomeThing: function (inJsonData, caller, tagColor) {
console.log('%c%s', `color: white; background: ${tagColor || 'grey'}; font-size: 15px;`, `[app.js]doSomeThing from: ${caller}`);
// console.log(inJsonData);
},
};