-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from EduardDopler/bug/1-centralize-gatt-comms
Fix Bug-1: Centralize GATT Comms
- Loading branch information
Showing
21 changed files
with
698 additions
and
567 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 16 additions & 3 deletions
19
src/components/EmcPushEvents.html → src/components/EmcBluetoothConnector.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.