Skip to content

Commit

Permalink
Introduce Error Handling
Browse files Browse the repository at this point in the history
This patch introduces basic error handling. This ensures that errors are
being logged and that the display reflects the error instead of just
showing an inactive state.
  • Loading branch information
lkiesow committed Feb 13, 2024
1 parent e27c64f commit 95bf374
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
40 changes: 26 additions & 14 deletions assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,37 @@ fetch('/config')
setInterval(updateTimer, 2000);
})

/**
* Update the view
*/
function updateView(active) {
// Update text
document.getElementById('text').innerText = active.text;

// Update colors
const body = document.getElementsByTagName('body')[0];
body.style.backgroundColor = active.background;
body.style.color = active.color;

// Update logo
document.getElementById('logo').src = active.image.replace(/\s/g, '');
}

/**
* Check for capture agent status
*/
function updateTimer() {
fetch('/status')
.then(response => response.json())
.then(capturing => {
.then(response => {
if (!response.ok) {
const active = config.unknown;
active.text = response.statusText;
updateView(active);
throw Error(response.statusText);
}
return response.json()
}).then(capturing => {
console.debug('capturing', capturing)
const active = capturing ? config.capturing : config.idle;

// Update text
document.getElementById('text').innerText = active.text;

// Update colors
const body = document.getElementsByTagName('body')[0];
body.style.backgroundColor = active.background;
body.style.color = active.color;

// Update logo
document.getElementById('logo').src = active.image.replace(/\s/g, '');
updateView(capturing ? config.capturing : config.idle);
})
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Config struct {
Display struct {
Capturing DisplayConfig `json:"capturing"`
Idle DisplayConfig `json:"idle"`
Unknown DisplayConfig `json:"unknown"`
}

Listen string
Expand Down Expand Up @@ -120,9 +121,15 @@ func setupRouter() *gin.Engine {
req.SetBasicAuth(config.Opencast.Username, config.Opencast.Password)
resp, err := client.Do(req)
if err != nil {
log.Println(err)
c.JSON(http.StatusBadGateway, nil)
return
}
if resp.StatusCode != 200 {
log.Println(resp)
c.JSON(resp.StatusCode, nil)
return
}
bodyText, err := ioutil.ReadAll(resp.Body)
s := string(bodyText)
var result AgentStateResult
Expand Down
5 changes: 5 additions & 0 deletions opencast-ca-display.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ display:
bdf+Ja02rbN0Tm63v7bb1/63qhYAAAAAAAAAAAAAAAAAAAAAAAAAAIDY8w9T
Gxe/25sMhQAAAABJRU5ErkJggg==
unknown:
text: Unknown
color: white
background: black


# IP address and port to bind to
listen: 127.0.0.1:8080

0 comments on commit 95bf374

Please sign in to comment.