Skip to content

Commit

Permalink
Show visual notifications, fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Dec 10, 2024
1 parent 20a5ba2 commit 32f0f36
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
29 changes: 29 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@
iframe#next {
opacity: 0;
}
dialog[open] {
width: 100vw;
height: 10vh;
font-family: sans-serif;
font-size: 8vh;
background-color: rgb(87, 148, 242);
border: none;
padding: 1vh;
box-sizing: border-box;
}
dialog[open]:nth-of-type(2) {
top: 10vh;
}
dialog[open]:nth-of-type(3) {
top: 20vh;
}
dialog.warn {
background-color: rgb(250, 222, 42);
}
dialog.alarm {
background-color: rgb(255, 152, 48);
}
dialog.emergency {
background-color: rgb(242, 73, 92);
}
dialog p {
margin: 0 auto;
text-align: center;
}
</style>
</head>
<body>
Expand Down
44 changes: 44 additions & 0 deletions public/infodisplay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const stateUrls = {};
const notifications = {};

function getUrlForState(state) {
if (!stateUrls[state]) {
Expand Down Expand Up @@ -39,6 +40,41 @@ function switchState(state) {
next.setAttribute('src', stateUrl);
}

function isNotificationVisual(notification) {
if (notification.method && notification.method.indexOf('visual') === -1) {
return false;
}
if (!notification.method && notification.state === 'normal') {
return false;
}
return true;
}

function handleNotification(path, notification) {
let element;
if (notifications[path]) {
// We have an element for this
element = notifications[path];
} else if (!isNotificationVisual(notification)) {
// No element, but the alert doesn't have a visual component. We can skip this one
return;
} else {
// New notification, create element
const body = document.querySelector('body');
element = document.createElement('dialog');
notifications[path] = element;
body.appendChild(element);
}
element.className = notification.state;
element.innerHTML = `<p>${notification.message}</p>`;
if (isNotificationVisual(notification)) {
element.show();
} else {
element.close();
// TODO: Destroy element?
}
}

function getConfig(callback) {
fetch('/signalk/v2/api/infodisplay')
.then((res) => res.json())
Expand All @@ -59,6 +95,10 @@ function connect() {
{
path: 'navigation.state',
},
{
path: 'notifications.*',
policy: 'instant',
},
],
}));
});
Expand All @@ -74,6 +114,10 @@ function connect() {
u.values.forEach((v) => {
if (v.path === 'navigation.state') {
switchState(v.value);
return;
}
if (v.path.indexOf('notifications.') === 0) {
handleNotification(v.path, v.value);
}
});
});
Expand Down

0 comments on commit 32f0f36

Please sign in to comment.