Skip to content

Commit

Permalink
Merge pull request #2 from EduardDopler/bug/1-centralize-gatt-comms
Browse files Browse the repository at this point in the history
Fix Bug-1: Centralize GATT Comms
  • Loading branch information
EduardDopler authored Jun 1, 2024
2 parents 6a06d0c + c521a09 commit e447ca2
Show file tree
Hide file tree
Showing 21 changed files with 698 additions and 567 deletions.
7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>ember mug control</title>
<link
rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>☕️</text></svg>"
/>
</head>
<body>
<h1 class="app__headline">ember mug control</h1>
<emc-connect></emc-connect>
<emc-push-events></emc-push-events>
<emc-bluetooth-connector></emc-bluetooth-connector>
<div id="emc-dashboard">
<emc-battery></emc-battery>
<emc-current-temp></emc-current-temp>
Expand All @@ -17,7 +20,7 @@ <h1 class="app__headline">ember mug control</h1>
<emc-liquid-level></emc-liquid-level>
<emc-color></emc-color>
</div>
<footer>amber-light v1.0.0 • Eduard Dopler • 2024</footer>
<footer>amber-light v%__APP_VERSION__% • Eduard Dopler • 2024</footer>
<script type="module" src="/src/main.js"></script>
</body>
</html>
64 changes: 35 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
{
"name": "amber-light",
"private": true,
"version": "1.0.0",
"version": "1.0.1",
"description": "Web-based dashboard and controls for the Ember Mug²",
"keywords": [
"coffee",
"bluetooth",
"bluetooth-api",
"web",
"web-api"
],
"author": "Eduard Dopler <[email protected]>",
"repository": "github:EduardDopler/amber-light",
"bugs": "https://github.com/EduardDopler/amber-light/issues",
"license": "MIT",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "vite build",
"preview": "vite preview --host"
},
"devDependencies": {
"eslint": "^9.3.0",
"eslint": "^9.4.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"globals": "^15.3.0",
"prettier": "~3.2.5",
"@types/web-bluetooth": "^0.0.20",
"vite": "^5.2.11"
"vite": "^5.2.12"
}
}
10 changes: 0 additions & 10 deletions src/EventMapping.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/PushEventMapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const PushEvent = Object.freeze({
Battery: "battery",
TargetTemp: "target-temp",
CurrentTemp: "current-temp",
LiquidLevel: "liquid-level",
LiquidState: "liquid-state",
});

export const BatteryEvent = Object.freeze({
Charging: "charging",
NotCharging: "not-charging",
});

export const PushEventMapping = Object.freeze({
1: [PushEvent.Battery],
2: [PushEvent.Battery, BatteryEvent.Charging],
3: [PushEvent.Battery, BatteryEvent.NotCharging],
4: [PushEvent.TargetTemp],
5: [PushEvent.CurrentTemp],
// 6 not implemented
7: [PushEvent.LiquidLevel],
8: [PushEvent.LiquidState],
});

export const ValueType = Object.freeze({
...PushEvent,
Color: "color",
});
79 changes: 31 additions & 48 deletions src/components/EmcBattery.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,54 @@
import html from "./EmcBattery.html?raw";
import { battery as batteryUuid } from "../uuids.json";
import { formatTemp } from "../utils.js";

export class EmcBattery extends HTMLElement {
/** @type {boolean} */
static isConnected = false;
/** @type {BluetoothRemoteGATTCharacteristic|null} */
static characteristic = null;

async connectedCallback() {
this.innerHTML = `${html}`;

window.addEventListener("emc-connected", this.onConnected.bind(this));
window.addEventListener("emc-disconnected", this.onDisconnected.bind(this));
window.addEventListener(
"emc-battery-value-changed",
this.updateValue.bind(this),
);
window.addEventListener(
"emc-force-update-values",
this.updateValue.bind(this),
);
this.addEventListener("click", this.updateValue);
}

async updateValue() {
if (!this.constructor.isConnected) return;

const rawValue = await this.constructor.characteristic.readValue();
const batteryValue = rawValue.getUint8(0);
const chargingValue = rawValue.getUint8(1);
const batteryTempValue = rawValue.getUint16(2, true);
/**
* @param {CustomEvent} ev
*/
async updateValue(ev) {
const { value, charging, batteryTemp } = ev.detail;

if (chargingValue === 0) {
this.displayValue({
batteryValue,
batteryTempValue,
});
} else {
this.displayValue({
batteryValue,
batteryTempValue,
isCharging: true,
});
if (typeof value !== "undefined") {
this.setUiValueText(`${value} %`);
}
this.setUiChargingText(charging ? "(Charging)" : "");
if (typeof batteryTemp !== "undefined") {
this.setAttribute("battery-temp", formatTemp(batteryTemp));
}
}

/* ---------------------------------------------------------------------------
* UI
* ------------------------------------------------------------------------ */

/**
* @returns {Element}
*/
get valueEl() {
return this.querySelector("#emc-battery-value");
}

async onConnected(ev) {
this.constructor.characteristic =
await ev.detail.service.getCharacteristic(batteryUuid);
this.constructor.isConnected = true;
await this.updateValue();
/**
* @returns {Element}
*/
get chargingEl() {
return this.querySelector("#emc-battery-charging-value");
}

async onDisconnected() {
this.constructor.characteristic = null;
this.constructor.isConnected = false;
this.displayValue({ batteryValue: "n/a", batteryTempValue: "n/a" });
setUiValueText(text) {
this.valueEl.textContent = text;
}

displayValue({ batteryValue, batteryTempValue, isCharging = false }) {
const elBattery = this.querySelector("#emc-battery-value");
const elCharging = this.querySelector("#emc-battery-charging-value");
elBattery.textContent =
batteryValue === "n/a" ? "n/a" : `${batteryValue} %`;
elCharging.textContent = isCharging ? "(Charging)" : "";
const batteryTempValueFormatted = formatTemp(batteryTempValue);
this.setAttribute("battery-temp", batteryTempValueFormatted);
setUiChargingText(text) {
this.chargingEl.textContent = text;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
<button id="emc-connect" type="button">Connect</button>

<style>
emc-push-events {
emc-bluetooth-connector {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 2rem;

#emc-push-events-checkbox-wrapper label {
margin-left: 0.5em;
#emc-push-events-checkbox-wrapper {
display: flex;
align-items: center;
gap: 0.75em;
}

#emc-push-events-enabled {
width: 1em;
height: 1em;
}

&:not([status="connected"]) label {
opacity: 0.25;
}
}
</style>
Expand Down
Loading

0 comments on commit e447ca2

Please sign in to comment.