add plugin
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
let websocket = null;
|
||||
let uuid = null;
|
||||
let ctxSettings = new Map();
|
||||
|
||||
const ACTION_MAP = {
|
||||
"ru.wildtail.httpkeys.discord_micro_mute": {
|
||||
actionId: "discord_micro_mute"
|
||||
},
|
||||
"ru.wildtail.httpkeys.discord_mute": {
|
||||
actionId: "discord_mute"
|
||||
},
|
||||
"ru.wildtail.httpkeys.teamspeak_micro_mute": {
|
||||
actionId: "teamspeak_micro_mute"
|
||||
},
|
||||
"ru.wildtail.httpkeys.talk_micro_mute": {
|
||||
actionId: "talk_micro_mute"
|
||||
},
|
||||
"ru.wildtail.httpkeys.system_micro_mute": {
|
||||
actionId: "system_micro_mute"
|
||||
}
|
||||
};
|
||||
|
||||
// подключение от D6
|
||||
function connectElgatoStreamDeckSocket(inPort, inUUID, inRegisterEvent, inInfo) {
|
||||
uuid = inUUID;
|
||||
|
||||
websocket = new WebSocket("ws://127.0.0.1:" + inPort);
|
||||
|
||||
websocket.onopen = function () {
|
||||
websocket.send(JSON.stringify({
|
||||
event: inRegisterEvent,
|
||||
uuid: uuid
|
||||
}));
|
||||
};
|
||||
|
||||
websocket.onmessage = function (evt) {
|
||||
const msg = JSON.parse(evt.data);
|
||||
|
||||
const { event, action, context, payload } = msg;
|
||||
|
||||
if (event === "willAppear") {
|
||||
const s = (payload && payload.settings) || { isOn: false };
|
||||
ctxSettings.set(context, s);
|
||||
setState(context, s.isOn ? 1 : 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event === "didReceiveSettings") {
|
||||
const s = payload.settings || { isOn: false };
|
||||
ctxSettings.set(context, s);
|
||||
setState(context, s.isOn ? 1 : 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event === "keyDown") {
|
||||
handleKeyDown(action, context);
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// обработка нажатия
|
||||
function handleKeyDown(action, context) {
|
||||
|
||||
let s = ctxSettings.get(context) || { isOn: false };
|
||||
const prevState = s.isOn;
|
||||
|
||||
s.isOn = !s.isOn;
|
||||
const newState = s.isOn;
|
||||
|
||||
ctxSettings.set(context, s);
|
||||
|
||||
setState(context, newState ? 1 : 0);
|
||||
|
||||
websocket.send(JSON.stringify({
|
||||
event: "setSettings",
|
||||
context: context,
|
||||
payload: s
|
||||
}));
|
||||
|
||||
const cfg = ACTION_MAP[action];
|
||||
if (!cfg) return;
|
||||
|
||||
post("http://127.0.0.1:16888/press", {
|
||||
actionUUID: action,
|
||||
actionId: cfg.actionId,
|
||||
|
||||
toggleChanged: prevState !== newState,
|
||||
state: newState ? "on" : "off",
|
||||
|
||||
isOn: newState,
|
||||
context: context,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
// смена state
|
||||
function setState(context, stateIndex) {
|
||||
websocket.send(JSON.stringify({
|
||||
event: "setState",
|
||||
context: context,
|
||||
payload: { state: stateIndex }
|
||||
}));
|
||||
}
|
||||
|
||||
// HTTP POST
|
||||
function post(url, data) {
|
||||
fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data)
|
||||
}).catch(err => console.error(err));
|
||||
}
|
||||
Reference in New Issue
Block a user