Skip to content

Commit 35b62f3

Browse files
committed
Additional updates for QoL
- Alerts/AudioPlay error checking got updated and ensured nothing will stop the chain - Added "widget config <id> --update" to CLI - forces Configuration update on Widget side (this should happen automatically but it doesn't, will be fixed properly later) - Adjustments to URLs used in the Endpoint and fixes for default port being overwritten to 0
1 parent e730e2c commit 35b62f3

7 files changed

Lines changed: 175 additions & 92 deletions

File tree

LukeBot.Endpoint/HostEndpoint.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ public IHostBuilder CreateHostBuilder()
5252
string domain = LukeBot.Common.Constants.DEFAULT_SERVER_HTTPS_DOMAIN;
5353
int port = LukeBot.Common.Constants.DEFAULT_SERVER_PORT;
5454

55-
Conf.TryGet<string>(Common.Constants.PROP_STORE_HTTPS_DOMAIN_PROP, out domain);
56-
Conf.TryGet<int>(Common.Constants.PROP_STORE_SERVER_PORT_PROP, out port);
57-
58-
AddUrl(domain, port, ref URLs);
55+
if (Conf.TryGet<string>(Common.Constants.PROP_STORE_HTTPS_DOMAIN_PROP, out string gotDomain))
56+
{
57+
domain = gotDomain;
58+
}
5959

60-
if (!domain.Contains("localhost"))
60+
if (Conf.TryGet<int>(Common.Constants.PROP_STORE_SERVER_PORT_PROP, out int gotPort))
6161
{
62-
// add localhost for local testing purposes
63-
AddUrl("localhost", port, ref URLs);
62+
port = gotPort;
6463
}
6564

65+
AddUrl(domain, port, ref URLs);
66+
6667
Logger.Log().Info("Endpoint using host addresses:");
6768
foreach (string addr in URLs)
6869
{

LukeBot.Widget.Common/IWidgetUserModule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ public interface IWidgetUserModule: IUserModule
2626
public ConfigurationBase GetWidgetConfiguration(string id);
2727
public void ResetConfiguration(string id);
2828
public void SaveConfiguration(string id);
29+
public void PushConfigurationUpdate(string id);
2930
}
3031
}

LukeBot.Widget/IWidget.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ internal string GetWidgetAddress()
7777
int port = LukeBot.Common.Constants.DEFAULT_SERVER_PORT;
7878

7979
string serverAddress = Conf.Get<string>(LukeBot.Common.Constants.PROP_STORE_HTTPS_DOMAIN_PROP);
80-
Conf.TryGet<int>(LukeBot.Common.Constants.PROP_STORE_SERVER_PORT_PROP, out port);
80+
if (Conf.TryGet<int>(LukeBot.Common.Constants.PROP_STORE_SERVER_PORT_PROP, out int gotPort))
81+
{
82+
port = gotPort;
83+
}
8184

8285
return String.Format("https://{0}:{1}/widget/{2}", serverAddress, port, ID);
8386
}
@@ -87,7 +90,10 @@ private string GetWidgetWSAddress()
8790
int port = LukeBot.Common.Constants.DEFAULT_SERVER_PORT;
8891

8992
string serverAddress = Conf.Get<string>(LukeBot.Common.Constants.PROP_STORE_HTTPS_DOMAIN_PROP);
90-
Conf.TryGet<int>(LukeBot.Common.Constants.PROP_STORE_SERVER_PORT_PROP, out port);
93+
if (Conf.TryGet<int>(LukeBot.Common.Constants.PROP_STORE_SERVER_PORT_PROP, out int gotPort))
94+
{
95+
port = gotPort;
96+
}
9197

9298
return String.Format("wss://{0}:{1}/widgetws/{2}", serverAddress, port, ID);
9399
}
@@ -279,6 +285,11 @@ public void ResetConfiguration()
279285
SaveConfiguration();
280286
}
281287

288+
public void PushConfigurationUpdate()
289+
{
290+
OnConfigurationUpdate();
291+
}
292+
282293

283294
internal Task AcquireWS(WebSocket ws)
284295
{

LukeBot.Widget/WidgetUserModule.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ public void ResetConfiguration(string id)
318318
}
319319
}
320320

321+
public void PushConfigurationUpdate(string id)
322+
{
323+
lock (mImplLock)
324+
{
325+
mWidgets[GetActualWidgetId(id)].PushConfigurationUpdate();
326+
}
327+
}
328+
321329
public ConfigurationBase GetWidgetConfiguration(string id)
322330
{
323331
lock (mImplLock)

LukeBot.Widget/Widgets/Alerts.html

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -378,48 +378,57 @@
378378
}
379379

380380
setPath(path) {
381-
this.mAudioBlobPromise = fetch(path);
381+
this.mAudioPath = path;
382382
}
383383

384384
execute(next, status) {
385-
this.mAudioBlobPromise.then((response) => response.blob())
386-
.then((blob) => {
387-
var audioURL = window.URL.createObjectURL(blob);
388-
this.mAudio = new Audio(audioURL);
389-
this.mAudio.addEventListener("canplaythrough", (event) => {
390-
this.mAudio.play().catch(
391-
(reason) => {
392-
status.fail(`ERROR play failed: ${reason}`);
393-
setTimeout(() => {
394-
next()
395-
}, 5000);
396-
}
397-
);
398-
});
399-
this.mAudio.addEventListener("ended", (value) => {
400-
if (this.mEnsureLongEnough && this.mAudio.duration < 5.0) {
401-
// Wait to make the alert last at least 5 seconds
402-
setTimeout(() => {
403-
next();
404-
}, (5.0 - this.mAudio.duration) * 1000);
385+
fetch(this.mAudioPath)
386+
.catch((error) => {
387+
status.fail(`ERROR fetching audio file: ${error}`);
388+
setTimeout(() => { next() }, 5000);
389+
})
390+
.then((response) => {
391+
if (response.ok) {
392+
return response.blob();
405393
} else {
406-
// Audio was longer than non-message alert, continue
407-
next();
394+
throw new Error(`Bad response from fetching audio file: ${response.status}`);
408395
}
396+
})
397+
.catch((error) => {
398+
status.fail(`ERROR getting response blob: ${error}`);
399+
setTimeout(() => { next() }, 5000);
400+
})
401+
.then((blob) => {
402+
var audioURL = window.URL.createObjectURL(blob);
403+
this.mAudio = new Audio(audioURL);
404+
this.mAudio.addEventListener("canplaythrough", (event) => {
405+
this.mAudio.play().catch(
406+
(reason) => {
407+
status.fail(`ERROR play failed: ${reason}`);
408+
setTimeout(() => { next() }, 5000);
409+
}
410+
);
411+
});
412+
this.mAudio.addEventListener("ended", (value) => {
413+
if (this.mEnsureLongEnough && this.mAudio.duration < 5.0) {
414+
// Wait to make the alert last at least 5 seconds
415+
setTimeout(() => {
416+
next();
417+
}, (5.0 - this.mAudio.duration) * 1000);
418+
} else {
419+
// Audio was longer than non-message alert, continue
420+
next();
421+
}
422+
});
423+
this.mAudio.addEventListener("error", (event) => {
424+
status.fail(`ERROR playing audio alert: ${this.mAudio.error.message}`);
425+
setTimeout(() => { next() }, 5000);
426+
});
427+
this.setupScroll();
428+
}).catch((error) => {
429+
status.fail(`ERROR fetching audio file: ${error}`);
430+
setTimeout(() => { next() }, 5000);
409431
});
410-
this.mAudio.addEventListener("error", (event) => {
411-
status.fail(`ERROR playing audio alert: ${this.mAudio.error.message}`);
412-
setTimeout(() => {
413-
next()
414-
}, 5000);
415-
});
416-
this.setupScroll();
417-
}).catch((error) => {
418-
status.fail(`ERROR fetching audio file: ${error}`);
419-
setTimeout(() => {
420-
next()
421-
}, 5000);
422-
});
423432
}
424433

425434
setupScroll() {

LukeBot.Widget/Widgets/AudioPlay.html

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,46 @@
6767
}
6868

6969
setPath(path) {
70-
this.mAudioBlobPromise = fetch(path);
70+
this.mAudioPath = path;
7171
}
7272

7373
execute(next, status) {
74-
this.mAudioBlobPromise.then((response) => response.blob())
75-
.then((blob) => {
76-
var audioURL = window.URL.createObjectURL(blob);
77-
this.mAudio = new Audio(audioURL);
78-
this.mAudio.addEventListener("canplaythrough", (event) => {
79-
this.mAudio.play()
80-
.then(() => next())
81-
.catch((reason) => {
82-
status.fail(`ERROR playing audio: ${reason}`);
83-
next();
84-
})
74+
fetch(this.mAudioPath)
75+
.catch((error) => {
76+
status.fail(`ERROR fetching audio file: ${error}`);
77+
next();
78+
})
79+
.then((response) => {
80+
if (response.ok) {
81+
return response.blob();
82+
} else {
83+
throw new Error(`Bad response from fetching audio file: ${response.status}`);
84+
}
85+
})
86+
.catch((error) => {
87+
status.fail(`ERROR getting response blob: ${error}`);
88+
next();
89+
})
90+
.then((blob) => {
91+
try {
92+
var audioURL = window.URL.createObjectURL(blob);
93+
this.mAudio = new Audio(audioURL);
94+
this.mAudio.addEventListener("canplaythrough", (event) => {
95+
this.mAudio.play()
96+
.then(() => next())
97+
.catch((reason) => {
98+
status.fail(`ERROR playing audio: ${reason}`);
99+
next();
100+
})
101+
});
102+
} catch (e) {
103+
status.fail(`ERROR caught while trying to play audio: ${e}`);
104+
next();
105+
}
106+
}).catch((error) => {
107+
status.fail(`ERROR playing audio: ${error}`);
108+
next();
85109
});
86-
}).catch((error) => {
87-
status.fail(`ERROR fetching audio file: ${error}`);
88-
next();
89-
});
90110
}
91111

92112
interrupt() {
@@ -109,42 +129,63 @@
109129
}
110130

111131
setPath(path) {
112-
this.mAudioBlobPromise = fetch(path);
132+
this.mAudioPath = path;
113133
}
114134

115135
execute(next, status) {
116-
this.mAudioBlobPromise.then((response) => response.blob())
117-
.then((blob) => {
118-
var audioURL = window.URL.createObjectURL(blob);
119-
this.mAudio = new Audio(audioURL);
120-
this.mAudio.addEventListener("ended", (event) => {
121-
// note that this happens after we send back the response that we're done
122-
// since this is supposed to stack and run in parallel
123-
// as such, there can be NO status modifications here
124-
console.log(`ended, time ${this.mTotalTime} length ${this.mTotalLength}`);
125-
if (this.mTotalTime > this.mTotalLength)
126-
return;
127-
128-
var time = Math.random() * this.mIntervalLength;
129-
this.mTotalTime += this.mMinInterval + time;
130-
setTimeout(() => {
131-
console.log("replay");
132-
this.mAudio.currentTime = 0;
133-
this.mAudio.play();
134-
}, (this.mMinInterval + time) * 1000);
135-
});
136-
this.mAudio.addEventListener("canplaythrough", (event) => {
137-
this.mAudio.play()
138-
.then(() => next())
139-
.catch((reason) => {
140-
status.fail(`ERROR playing audio: ${reason}`);
141-
next();
136+
fetch(this.mAudioPath)
137+
.catch((error) => {
138+
status.fail(`ERROR fetching audio file: ${error}`);
139+
next();
140+
})
141+
.then((response) => {
142+
if (response.ok) {
143+
return response.blob();
144+
} else {
145+
throw new Error(`Bad response from fetching audio file: ${response.status}`);
146+
}
147+
})
148+
.catch((error) => {
149+
status.fail(`ERROR getting response blob: ${error}`);
150+
next();
151+
})
152+
.then((blob) => {
153+
try {
154+
var audioURL = window.URL.createObjectURL(blob);
155+
this.mAudio = new Audio(audioURL);
156+
this.mAudio.addEventListener("ended", (event) => {
157+
// note that this happens after we send back the response that we're done
158+
// since this is supposed to stack and run in parallel
159+
// as such, there can be NO status modifications here
160+
console.log(`ended, time ${this.mTotalTime} length ${this.mTotalLength}`);
161+
if (this.mTotalTime > this.mTotalLength)
162+
return;
163+
164+
var time = Math.random() * this.mIntervalLength;
165+
this.mTotalTime += this.mMinInterval + time;
166+
setTimeout(() => {
167+
console.log("replay");
168+
this.mAudio.currentTime = 0;
169+
this.mAudio.play();
170+
}, (this.mMinInterval + time) * 1000);
171+
});
172+
this.mAudio.addEventListener("canplaythrough", (event) => {
173+
this.mAudio.play()
174+
.catch((reason) => {
175+
status.fail(`ERROR playing audio: ${reason}`);
176+
next();
177+
})
178+
.then(() => next());
142179
});
180+
} catch (e) {
181+
status.fail(`ERROR caught while trying to play audio: ${e}`);
182+
next();
183+
}
184+
})
185+
.catch((error) => {
186+
status.fail(`ERROR playing audio: ${error}`);
187+
next();
143188
});
144-
}).catch((error) => {
145-
status.fail(`ERROR fetching audio file: ${error}`);
146-
next();
147-
});
148189
}
149190

150191
interrupt() {

LukeBot/WidgetCLIProcessor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public WidgetReloadCommand()
8585
[Verb("config", HelpText = "Launches Widget Configuration editor.")]
8686
public class WidgetConfigCommand: WidgetBaseCommand
8787
{
88+
[Option("update", Default = false, HelpText =
89+
"Forces a Configuration push to the Widget. This should happen automatically when editing, but if it doesn't the update can be manually triggered with this flag. Config editor WON'T be started."
90+
)]
91+
public bool Update { get; set; }
8892
}
8993

9094
[Verb("enable", HelpText = "Enable Widget support for current user.")]
@@ -256,6 +260,14 @@ public void HandleConfigCommand(WidgetConfigCommand arg, CLIMessageProxy CLI, ou
256260

257261
try
258262
{
263+
if (arg.Update)
264+
{
265+
GetWidgetUserModule(CLI.GetCurrentUser()).PushConfigurationUpdate(arg.Id);
266+
267+
msg = "Configuration update pushed for widget " + arg.Id;
268+
return;
269+
}
270+
259271
ConfigurationBase config = GetWidgetUserModule(CLI.GetCurrentUser()).GetWidgetConfiguration(arg.Id);
260272

261273
CLI.Message("Starting Widget Configuration Editor...");

0 commit comments

Comments
 (0)