Skip to content

Commit

Permalink
Move led configuration to separate section, some fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Oct 1, 2024
1 parent edc6760 commit 48123e5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
40 changes: 32 additions & 8 deletions www/src/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ApplicationBase} from "./lib/index.js";
import {AppConfigBase, ApplicationBase} from "./lib/index.js";
import {Config} from "./config.js";
import {PropertyConfig} from "./props.js";

Expand All @@ -15,6 +15,7 @@ import {PacketType} from "./cmd.js";

export class Application extends ApplicationBase {
#config;
#reHost = /([?&]host=)(.*)(?:$|&)/;

get propertyConfig() {return PropertyConfig;}

Expand Down Expand Up @@ -43,6 +44,9 @@ export class Application extends ApplicationBase {
await super.begin(root);

this.propertyMeta["apply_sys_config"].control.setOnClick(this.applySysConfig.bind(this));
this.propertyMeta["apply_led_config"].control.setOnClick(this.applySysConfig.bind(this));

this.subscribe(this, this.Event.PropertyCommited, this.onPropCommited.bind(this));
}

async applySysConfig(sender) {
Expand All @@ -53,24 +57,44 @@ export class Application extends ApplicationBase {
try {
await this.ws.request(PacketType.RESTART);

let new_url
if (location.hostname !== "localhost") {
const newHostname = this.config.sysConfig.mdnsName + ".local";
const hostQueryMatch = location.search.match(this.#reHost);
if (hostQueryMatch && hostQueryMatch[2] !== newHostname) {
const new_url = location.search.replace(this.#reHost, `$1${newHostname}`)
setTimeout(() => window.location = new_url, 3000);
} else if (!hostQueryMatch && location.hostname !== "localhost" && location.hostname !== newHostname) {
const url_parts = [
location.protocol + "//",
this.config.sysConfig.mdnsName + ".local",
newHostname,
location.port ? ":" + location.port : "",
"/?" + (location.href.split("?")[1] ?? "")
]

new_url = url_parts.join("");
const new_url = url_parts.join("");
setTimeout(() => window.location = new_url, 3000);
} else {
new_url = location.href;
setTimeout(() => window.location.reload(), 3000);
}

setTimeout(() => window.location = new_url, 3000);
} catch (err) {
console.log("Unable to send restart signal", err);
sender.setAttribute("data-saving", false);
}
}

onPropCommited(config, {key}) {
if (key === "ledType") {
this.config.refreshLedMode();

for (const propKey of [
"sysConfig.ledRPin",
"sysConfig.ledGPin",
"sysConfig.ledBPin",
"sysConfig.ledWPin",
"sysConfig.ledCPin",
"sysConfig.ledPin"
]) {
this.emitEvent(this.Event.Notification, {key: propKey, value: this.config.getProperty(propKey)});
}
}
}
}
4 changes: 4 additions & 0 deletions www/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export class Config extends AppConfigBase {
mqttConvertBrightness: parser.readBoolean(),
};

this.refreshLedMode();
}

refreshLedMode() {
this.singleLedMode = this.ledType === 0;
this.sysConfig.ledPin = this.sysConfig.ledRPin;

Expand Down
4 changes: 2 additions & 2 deletions www/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ if ("serviceWorker" in navigator) {
const StatusElement = document.getElementById("status");

const app = new Application(gateway);
app.subscribe(this, app.Event.Connected, () => {
app.subscribe(window, app.Event.Connected, () => {
StatusElement.style.visibility = "collapse";
});

app.subscribe(this, app.Event.Disconnected, () => {
app.subscribe(window, app.Event.Disconnected, () => {
StatusElement.innerText = "NOT CONNECTED";
StatusElement.style.visibility = "visible";
});
Expand Down
2 changes: 1 addition & 1 deletion www/src/lib
39 changes: 22 additions & 17 deletions www/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,7 @@ export const PropertyConfig = [{
{key: "nightMode.switchInterval", title: "Switch Interval", type: "time", kind: "Uint16", cmd: PacketType.NIGHT_MODE_INTERVAL},
]
}, {
key: "settings", section: "Settings", collapse: "true", props: [
{key: "sysConfig.mdnsName", title: "mDNS Name", type: "text", kind: "FixedString", maxLength: 32, cmd: PacketType.SYS_CONFIG_MDNS_NAME},

{type: "title", label: "WiFi"},
{key: "sysConfig.wifiMode", title: "Mode", type: "select", kind: "Uint8", cmd: PacketType.SYS_CONFIG_WIFI_MODE, list: "wifiMode"},
{key: "sysConfig.wifiSsid", title: "WiFi SSID", type: "text", kind: "FixedString", maxLength: 32, cmd: PacketType.SYS_CONFIG_WIFI_SSID},
{key: "sysConfig.wifiPassword", title: "WiFi Password", type: "password", kind: "FixedString", maxLength: 32, cmd: PacketType.SYS_CONFIG_WIFI_PASSWORD},

{type: "title", label: "WiFi Extra"},
{key: "sysConfig.wifiConnectionCheckInterval", title: "Connection Check Interval", type: "int", kind: "Uint32", cmd: PacketType.SYS_CONFIG_WIFI_CONNECTION_CHECK_INTERVAL},
{key: "sysConfig.wifiMaxConnectionAttemptInterval", title: "Max Connection Attempt Interval", type: "int", kind: "Uint32", cmd: PacketType.SYS_CONFIG_WIFI_MAX_CONNECTION_ATTEMPT_INTERVAL},

{type: "title", label: "Button"},
{key: "sysConfig.button_enabled", title: "Enabled", type: "trigger", kind: "Boolean", cmd: PacketType.SYS_CONFIG_BUTTON_ENABLED},
{key: "sysConfig.button_pin", title: "Pin", type: "int", kind: "Uint8", cmd: PacketType.SYS_CONFIG_BUTTON_PIN, min: 0, max: 32},
{key: "sysConfig.button_high_state", title: "High State", type: "trigger", kind: "Boolean", cmd: PacketType.SYS_CONFIG_BUTTON_HIGH_STATE},

key: "led_settings", section: "LED Settings", collapse: true, props: [
{type: "title", label: "LED"},
{key: "ledType", title: "LED Type", type: "select", kind: "Uint8", cmd: PacketType.SYS_LED_TYPE, list: "ledType"},

Expand All @@ -91,6 +75,27 @@ export const PropertyConfig = [{
{key: "sysConfig.ledMaxTemperature", title: "Max Temperature", type: "int", kind: "Uint16", cmd: PacketType.SYS_CONFIG_LED_MAX_TEMPERATURE, visibleIf: "showTemperature"},
{key: "sysConfig.ledMinBrightness", title: "Min Brightness", type: "int", kind: "Uint16", cmd: PacketType.SYS_CONFIG_LED_MIN_BRIGHTNESS},

{type: "title", label: "Actions", extra: {m_top: true}},
{key: "apply_led_config", type: "button", label: "Apply Settings"}
]
},{
key: "settings", section: "Settings", collapse: "true", props: [
{key: "sysConfig.mdnsName", title: "mDNS Name", type: "text", kind: "FixedString", maxLength: 32, cmd: PacketType.SYS_CONFIG_MDNS_NAME},

{type: "title", label: "WiFi"},
{key: "sysConfig.wifiMode", title: "Mode", type: "select", kind: "Uint8", cmd: PacketType.SYS_CONFIG_WIFI_MODE, list: "wifiMode"},
{key: "sysConfig.wifiSsid", title: "WiFi SSID", type: "text", kind: "FixedString", maxLength: 32, cmd: PacketType.SYS_CONFIG_WIFI_SSID},
{key: "sysConfig.wifiPassword", title: "WiFi Password", type: "password", kind: "FixedString", maxLength: 32, cmd: PacketType.SYS_CONFIG_WIFI_PASSWORD},

{type: "title", label: "WiFi Extra"},
{key: "sysConfig.wifiConnectionCheckInterval", title: "Connection Check Interval", type: "int", kind: "Uint32", cmd: PacketType.SYS_CONFIG_WIFI_CONNECTION_CHECK_INTERVAL},
{key: "sysConfig.wifiMaxConnectionAttemptInterval", title: "Max Connection Attempt Interval", type: "int", kind: "Uint32", cmd: PacketType.SYS_CONFIG_WIFI_MAX_CONNECTION_ATTEMPT_INTERVAL},

{type: "title", label: "Button"},
{key: "sysConfig.button_enabled", title: "Enabled", type: "trigger", kind: "Boolean", cmd: PacketType.SYS_CONFIG_BUTTON_ENABLED},
{key: "sysConfig.button_pin", title: "Pin", type: "int", kind: "Uint8", cmd: PacketType.SYS_CONFIG_BUTTON_PIN, min: 0, max: 32},
{key: "sysConfig.button_high_state", title: "High State", type: "trigger", kind: "Boolean", cmd: PacketType.SYS_CONFIG_BUTTON_HIGH_STATE},

{type: "title", label: "SYSTEM EXTRA"},
{key: "sysConfig.powerChangeTimeout", title: "Power Change Timeout", type: "int", kind: "Uint32", cmd: PacketType.SYS_CONFIG_POWER_CHANGE_TIMEOUT},
{key: "sysConfig.wifiConnectFlashTimeout", title: "WiFi Connect Flash Timeout", type: "int", kind: "Uint32", cmd: PacketType.SYS_CONFIG_WIFI_CONNECT_FLASH_TIMEOUT},
Expand Down

0 comments on commit 48123e5

Please sign in to comment.