Skip to content

Commit 8f87f54

Browse files
Manual button state fixes; embrava testing
1 parent 8b2fd8f commit 8f87f54

File tree

5 files changed

+135
-23
lines changed

5 files changed

+135
-23
lines changed

background.js

+45-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
// global vars
22
let presences = [];
33
let totalStreams = 0;
4-
let ledStatus = "off";
4+
let ledStatus;
55
let settings = JSON.parse(localStorage.getItem("settings"));
66

77
//ToDo: Need to handle when there are no settings
88
console.log("Settings:", settings);
9+
if(settings.busy)
10+
ledStatus = settings.busy;
11+
else{
12+
settings.busy = false;
13+
ledStatus = "off";
14+
15+
}
916

1017
// prototype for our presence status object
1118
function Presence(url, streamCount, tabId, tabStatus) {
@@ -44,8 +51,12 @@ function webhook(state){
4451
}
4552
};
4653

47-
if (postBody !== "")
48-
fetchParams.body = JSON.stringify(JSON.parse(postBody)); // ToDo: not sure why I need the parse all of the sudden
54+
if (postBody !== ""){
55+
// fetchParams.body = postBody;
56+
// ToDo: debug
57+
// window.fetchParams = fetchParams;
58+
fetchParams.body = JSON.stringify(postBody); // ToDo: not sure why I need the parse all of the sudden
59+
}
4960
}
5061

5162
console.log(headers);
@@ -54,17 +65,17 @@ function webhook(state){
5465
fetchParams.headers = Object.assign(fetchParams.headers, JSON.parse(headers));
5566

5667

57-
console.log(url, fetchParams);
68+
//console.log(url, fetchParams);
5869

5970
fetch(url, fetchParams)
6071
// In case we care about the response someday
6172
.then(
6273
response => {
63-
console.log("webhook sent. Response code: " + response.status);
74+
console.log("fetch details:", url, fetchParams, response);
6475
response.text().then(text => console.log("response text: " + text))
6576
})
6677
.catch(function (error) {
67-
console.log('Request failed', error);
78+
console.log("fetch request failed details:", url, fetchParams, error);
6879
});
6980
}
7081

@@ -76,24 +87,40 @@ function ledChange(state) {
7687
chrome.tabs.query({active: true, windowType: "normal", currentWindow: true}, function (d) {
7788
let tabId = d[0].id;
7889
chrome.browserAction.setIcon({path: "icons/" + state + '.png'}); //Add tabId: tabId to make it tab specific
79-
8090
});
91+
92+
// ToDo: make HID work
93+
//HID
94+
if(settings.hid){
95+
glow([180,0,0]).catch(err=>console.error(err));
96+
}
8197
}
8298

8399

84100
function statusControl(port) {
85101

102+
86103
totalStreams = presences.reduce((total, p) => total + p.streamCount, 0);
87104
let liveTabs = presences.reduce((total, p) => total + p.live(), 0);
88105

89106
console.log("total stream count is: " + totalStreams);
90107
console.log("active tab count is: " + liveTabs);
91108

109+
110+
// ToDo: need to manage manual status changes from on/off button
111+
112+
/*
113+
* When to turn ON the light:
114+
* Manual - err on side of being on too often
115+
* TURN ON: if WebRTC active and LED is off
116+
* TURN OFF: if WebRTC not active, not manual busy, and the LED is off
117+
* */
118+
92119
// Update the badge text
93120
chrome.browserAction.setBadgeText({text: liveTabs.toString()});
94121

95122
// Turn the LED off
96-
if (totalStreams === 0 && ledStatus === 'on') {
123+
if (totalStreams === 0 && ledStatus === 'on' && !settings.busy) {
97124
ledChange("off");
98125
ledStatus = "off";
99126
}
@@ -110,7 +137,8 @@ function statusControl(port) {
110137
}
111138

112139
}
113-
// For debugging
140+
/*
141+
// ToDo: remove this if the above works; For debugging
114142
else if (totalStreams === 0 && ledStatus === 'off') {
115143
console.log("No WebRTC streams")
116144
} else if (totalStreams > 0 && ledStatus === 'on') {
@@ -120,6 +148,7 @@ function statusControl(port) {
120148
else {
121149
console.error("unhandled condition: ", totalStreams, ledStatus)
122150
}
151+
*/
123152

124153
// Update the pop-up text
125154
port.postMessage({
@@ -230,11 +259,17 @@ chrome.runtime.onConnect.addListener(function (port) {
230259
if (message.type === "command") {
231260
console.log("popup command", message);
232261
if(message.command){
262+
// ToDo: ***manage manual status
263+
console.log("DEBUG:", message.command);
264+
settings.busy = message.command === "on";
233265
ledChange(message.command);
266+
ledStatus = "on";
267+
234268
}
235269
}
236-
237270

271+
272+
// ToDo: I don't remember if this is required to load settings background.js or it is already shared from popup.js
238273
// Update settings data
239274
if (message.type === "settings") {
240275
console.log("popup settings", message);

include/embrava.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
async function glow(rgb) {
2+
let device = await openDevice();
3+
await colorize(device, rgb );
4+
}
5+
6+
async function handleDisconnectClick() {
7+
let device = await openDevice();
8+
if( !device ) return;
9+
let acolor = [ 0, 0, 0 ]; // off
10+
await colorize(device, acolor);
11+
await device.close();
12+
}
13+
14+
async function openDevice() {
15+
const vendorId = 0x2c0d; // embrava.com
16+
const productId = 0x000c; // blynclight standard
17+
const device_list = await navigator.hid.getDevices();
18+
let device = device_list.find(d => d.vendorId === vendorId && d.productId === productId);
19+
20+
if (!device) {
21+
let devices = await navigator.hid.requestDevice({
22+
filters: [{ vendorId, productId }],
23+
});
24+
console.log("devices:", devices);
25+
device = devices[0];
26+
if( !device ) return null;
27+
}
28+
if (!device.opened) {
29+
await device.open();
30+
}
31+
console.log("device opened:",device);
32+
return device;
33+
}
34+
35+
async function colorize(device, [r, g, b] ) {
36+
if(!device) return;
37+
const data = Uint8Array.from([ r,b,g, 0x00, 0x00, 0x40, 0x02, 0xFF22 ]);
38+
// 4th parameter is light control, 0 is stable, 70 is fast blink?, 100 is medium blink?
39+
try {
40+
await device.sendReport(0, data); //If the HID device does not use report IDs, set reportId to 0.
41+
} catch (error) {
42+
console.error(error);
43+
}
44+
}

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"run_at": "document_start"
88
} ],
99
"background": {
10-
"scripts": ["background.js"],
10+
"scripts": ["background.js", "include/embrava.js"],
1111
"persistent": true
1212
},
1313
"browser_action": {

popup.html

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<button type="button" class="btn btn-primary busy" id="notBusyBtn">Not busy</button>
3434
<span style="float: right"><a href="#" id="settingsToggle">⚙️</a></span>
3535
<div id="settings" style="display:none" class="well">
36+
<input id="embrava" type="checkbox">
37+
<label for="embrava">Embrava BlyncLight</label><br>
38+
3639
<h4>Webhook settings</h4>
3740
<ul class="nav nav-tabs">
3841
<li class="active">

popup.js

+42-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ $(document).ready( function () {
77

88
let busyBtn = $("#busyBtn");
99
let notBusyBtn = $("#notBusyBtn");
10+
let hid = false;
11+
let busy = false;
1012

1113
let port = chrome.extension.connect({
1214
name: "WebRTC Presence popup"
@@ -19,11 +21,13 @@ $(document).ready( function () {
1921

2022
if(msg.data.status) {
2123
if (msg.data.status === "on") {
24+
busy = true;
2225
console.log("turn busy button on");
2326
busyBtn.removeClass("hidden");
2427
notBusyBtn.addClass("hidden");
2528
} else if (msg.data.status === "off") {
2629
//busyBtn.addClass("hidden");
30+
busy = false;
2731
busyBtn.addClass("hidden");
2832
notBusyBtn.removeClass("hidden");
2933
}
@@ -55,15 +59,19 @@ $(document).ready( function () {
5559
if(e.target.innerText==="Busy"){
5660
console.log("on button clicked; turning off");
5761
port.postMessage({type: "command", command:"off"});
62+
busy = false;
5863
}
5964
else if(e.target.innerText==="Not busy"){
6065
console.log("off button clicked; turning on");
6166
port.postMessage({type: "command", command:"on"});
67+
busy = true;
6268
}
6369
else{
6470
console.log("on/off button handler error", e);
6571
}
6672

73+
sendSettings();
74+
6775
});
6876

6977
// Function for sending the form data back to background.js
@@ -75,6 +83,9 @@ $(document).ready( function () {
7583
settings.onMethod = $("#onGetSwitch")[0].checked ? "GET" : "POST";
7684
settings.offMethod = $("#offGetSwitch")[0].checked ? "GET" : "POST";
7785

86+
settings.hid = hid;
87+
settings.busy = busy;
88+
7889
console.log(settings);
7990

8091
port.postMessage({type: "settings", data: settings});
@@ -85,24 +96,39 @@ $(document).ready( function () {
8596

8697

8798
// Get settings data from a localStorage and populate the forms
88-
let formData = JSON.parse(localStorage.getItem("settings"));
89-
console.log("saved settings:", formData);
99+
let settingsData = JSON.parse(localStorage.getItem("settings"));
100+
console.log("saved settings:", settingsData);
101+
102+
// Update the UI if we have saved settings
103+
if(settingsData){
104+
105+
hid = settingsData.hid;
106+
busy = settingsData.busy;
107+
108+
$("#embrava").prop("checked", settingsData.hid);
90109

91-
// Update the forms if we have saved settings
92-
if(formData){
110+
if(settingsData.busy){
111+
busyBtn.removeClass("hidden");
112+
notBusyBtn.addClass("hidden");
113+
}
114+
else{
115+
busyBtn.addClass("hidden");
116+
notBusyBtn.removeClass("hidden");
117+
}
93118

94-
onForm["onUrl"].value = formData["onUrl"] || "";
95-
onForm["onHeaders"].value = formData["onHeaders"] || "";
96-
onForm["onPostBody"].value = formData["onPostBody"] || "";
97-
onForm["onGetSwitch"].checked = formData["onMethod"] === "GET";
119+
120+
onForm["onUrl"].value = settingsData["onUrl"] || "";
121+
onForm["onHeaders"].value = settingsData["onHeaders"] || "";
122+
onForm["onPostBody"].value = settingsData["onPostBody"] || "";
123+
onForm["onGetSwitch"].checked = settingsData["onMethod"] === "GET";
98124
if(onForm["onGetSwitch"].checked)
99125
$(".onPostBody").hide();
100126

101127

102-
offForm["offUrl"].value = formData["offUrl"] || "";
103-
offForm["offHeaders"].value = formData["offHeaders"] || "";
104-
offForm["offGetSwitch"].checked = formData["offMethod"] === "GET";
105-
offForm["offPostBody"].value = formData["offPostBody"] || "";
128+
offForm["offUrl"].value = settingsData["offUrl"] || "";
129+
offForm["offHeaders"].value = settingsData["offHeaders"] || "";
130+
offForm["offGetSwitch"].checked = settingsData["offMethod"] === "GET";
131+
offForm["offPostBody"].value = settingsData["offPostBody"] || "";
106132
if(offForm["offGetSwitch"].checked)
107133
$(".offPostBody").hide();
108134

@@ -119,6 +145,10 @@ $(document).ready( function () {
119145
$(".offPostBody").toggle();
120146
});
121147

148+
$("#embrava").change(()=>{
149+
hid = !hid;
150+
});
151+
122152
$("#saveButton").click(()=>{
123153
sendSettings();
124154
localStorage.setItem("settings", JSON.stringify(settings));

0 commit comments

Comments
 (0)