Skip to content

Commit

Permalink
Firmware: Update frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis960 committed Apr 3, 2024
1 parent d6587bd commit da0f00b
Show file tree
Hide file tree
Showing 33 changed files with 847 additions and 656 deletions.
2 changes: 1 addition & 1 deletion ESPlant-Firmware/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
component_hash: null
source:
type: idf
version: 5.3.0
version: 5.2.1
joltwallet/littlefs:
component_hash: 703c642136031deca2579bdc19721a397f9fd9033b1c667c7b89fe23fa394c40
source:
Expand Down
4 changes: 4 additions & 0 deletions ESPlant-Firmware/frontend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 4,
"useTabs": false
}
8 changes: 8 additions & 0 deletions ESPlant-Firmware/frontend/mock/cloudSetupBlumy.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineMock } from "vite-plugin-mock-dev-server";

export default defineMock({
url: "/api/cloudSetup/blumy",
body: {
token: "blumy_c8nu8r9zr23rch"
},
});
10 changes: 10 additions & 0 deletions ESPlant-Firmware/frontend/mock/cloudSetupHttp.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineMock } from "vite-plugin-mock-dev-server";

export default defineMock({
url: "/api/cloudSetup/http",
body: {
sensorId: "45",
url: "http://blumy-server.com",
auth: "???",
},
});
14 changes: 14 additions & 0 deletions ESPlant-Firmware/frontend/mock/cloudSetupMqtt.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineMock } from "vite-plugin-mock-dev-server";

export default defineMock({
url: "/api/cloudSetup/mqtt",
body: {
sensorId: "45",
server: "blumy-server.com",
port: 1883,
username: "blumy-user",
password: "blumy123",
topic: "blumy-topic",
clientId: "blumy-client",
},
});
13 changes: 13 additions & 0 deletions ESPlant-Firmware/frontend/mock/getSensorData.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineMock } from "vite-plugin-mock-dev-server";

export default defineMock({
url: "/api/sensor/data",
body: {
temperature: 25.5,
humidity: 50,
light: 0.7,
moisture: 645,
voltage: 2.8,
usb: true,
},
});
7 changes: 7 additions & 0 deletions ESPlant-Firmware/frontend/mock/getUpdateFirmwareUrl.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineMock } from "vite-plugin-mock-dev-server";

export default defineMock({
url: "/api/update/firmware",
method: "GET",
body: { url: "http://cloud.blumy.com/firmware.bin" },
});
25 changes: 10 additions & 15 deletions ESPlant-Firmware/frontend/mock/networks.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,37 @@ export default defineMock({
{
rssi: -55,
ssid: "MyWiFi",
bssid: "00:11:22:33:44:55",
channel: 1,
secure: 4,
hidden: false,
},
{
rssi: -66,
ssid: "MyWiFi2",
bssid: "00:11:22:33:44:56",
channel: 6,
secure: 3,
hidden: false,
},
{
rssi: -77,
ssid: "MyWiFi3",
bssid: "00:11:22:33:44:57",
channel: 11,
secure: 2,
hidden: false,
},
{
rssi: -77,
ssid: "FRITZ!Box 7490",
bssid: "00:11:22:33:44:57",
channel: 11,
secure: 7,
hidden: false,
},
{
rssi: -30,
ssid: "Strongest WiFi alive",
bssid: "00:11:22:33:44:57",
channel: 11,
secure: 7,
hidden: false,
},
{
rssi: -30,
ssid: "Duplicate Wifi",
secure: 7,
},
{
rssi: -60,
ssid: "Duplicate Wifi",
secure: 7,
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { defineMock } from "vite-plugin-mock-dev-server";

export default defineMock(
{
url: "/api/cloudSetup",
url: "/api/update/firmware",
method: "POST",
body: "OK",
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineMock } from "vite-plugin-mock-dev-server";

export default defineMock(
{
url: "/api/sensor/value",
body: 234,
url: "/api/update/percentage",
body: 20,
}
);
170 changes: 97 additions & 73 deletions ESPlant-Firmware/frontend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { loadingState } from "./states";
export type Network = {
rssi: number;
ssid: string;
bssid: string;
channel: number;
secure: number;
hidden: boolean;
};

export enum WifiStatus {
Expand All @@ -18,11 +15,43 @@ export enum WifiStatus {
ERROR = 10,
}

export enum ResetFlag {
SENSOR_FLAG = 0,
CONFIGURATION_FLAG = 1,
export interface SensorData {
temperature: number;
humidity: number;
light: number;
moisture: number;
voltage: number;
usb: boolean;
}

export interface HttpCloudConfiguration extends Record<string, string> {
type: "http";
sensorId: string;
url: string;
auth: string;
}

export interface MqttCloudConfiguration extends Record<string, string> {
type: "mqtt";
sensorId: string;
server: string;
port: string;
username: string;
password: string;
topic: string;
clientId: string;
}

export interface BlumyCloudConfiguration extends Record<string, string> {
type: "cloud";
token: string;
}

export type CloudConfiguration =
| HttpCloudConfiguration
| MqttCloudConfiguration
| BlumyCloudConfiguration;

/* fetch loading state indicator */
const fetch = new Proxy(window.fetch, {
apply: async (target, thisArgs, args) => {
Expand All @@ -36,14 +65,20 @@ const fetch = new Proxy(window.fetch, {
},
});

async function postDataToEsp(url: string, params: Record<string, string> | string) {
const body = typeof params === "string" ? params : Object.entries(params).map(([key, value]) => key + "=" + value).join("\n") + "\n";
async function postDataToEsp(
url: string,
params?: Record<string, string> | string
) {
const body = params
? typeof params === "string"
? params
: Object.entries(params)
.map(([key, value]) => key + "=" + value)
.join("\n") + "\n"
: undefined;
return await fetch("/api" + url, {
method: "POST",
body,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
}

Expand All @@ -56,85 +91,58 @@ async function getDataFromEsp(url: string) {
});
}

async function uploadFile(file: File, url: string) {
const formData = new FormData();
formData.append("upload", file);
return await fetch("/api" + url, {
method: "POST",
body: formData,
});
}

export async function setPlantName(name: string) {
return await postDataToEsp("/plantName", name);
export async function connectToNetwork(ssid: string, password: string) {
return await postDataToEsp("/connect", { ssid, password });
}

export async function getPlantName() {
return await getDataFromEsp("/plantName");
export async function resetEsp() {
return await postDataToEsp("/reset");
}

export async function getNetworks(): Promise<Network[]> {
return await getDataFromEsp("/networks");
}

export async function getConnectedNetwork(): Promise<Network> {
return await getDataFromEsp("/connectedNetwork");
}

export async function connectToNetwork(ssid: string, password: string) {
return await postDataToEsp("/connect", {ssid, password});
}

export async function resetEsp(resetFlag: ResetFlag) {
return await postDataToEsp("/reset", String(resetFlag));
}

/**
* @returns WifiStatus
*/
export async function isEspConnected(): Promise<WifiStatus> {
export async function isConnected(): Promise<WifiStatus> {
const res = await getDataFromEsp("/isConnected");
if (res == null) {
return WifiStatus.ERROR;
}
return Number(res);
}

export interface HttpCloudConfiguration extends Record<string, string>{
type: 'http'
sensorId: string,
url: string
auth: string
}

export interface MqttCloudConfiguration extends Record<string, string>{
type: 'mqtt'
sensorId: string,
server: string,
port: string,
username: string,
password: string,
topic: string,
clientId: string
}

export interface BlumyCloudConfiguration extends Record<string, string> {
type: 'cloud'
token: string
export async function getConnectedNetwork(): Promise<{ ssid: string, status: WifiStatus }> {
return await getDataFromEsp("/connectedNetwork");
}

export type CloudConfiguration = HttpCloudConfiguration | MqttCloudConfiguration | BlumyCloudConfiguration;

export async function setCloudCredentials(config: CloudConfiguration) {
if (config.type === 'http') {
if (config.type === "http") {
return await postDataToEsp("/cloudSetup/http", config);
} else if (config.type === 'mqtt') {
} else if (config.type === "mqtt") {
return await postDataToEsp("/cloudSetup/mqtt", config);
} else if (config.type === 'cloud') {
} else if (config.type === "cloud") {
return await postDataToEsp("/cloudSetup/blumy", config);
}
}

export async function getCloudCredentials<T extends CloudConfiguration["type"]>(
type: T
): Promise<
T extends "http"
? HttpCloudConfiguration
: T extends "mqtt"
? MqttCloudConfiguration
: BlumyCloudConfiguration
> {
if (type === "http") {
return await getDataFromEsp("/cloudSetup/http");
} else if (type === "mqtt") {
return await getDataFromEsp("/cloudSetup/mqtt");
} else if (type === "cloud") {
return await getDataFromEsp("/cloudSetup/blumy");
}
}

export async function setSleepTimeout(timeout: number) {
timeout = Math.round(timeout);
return await postDataToEsp("/timeouts/sleep", timeout.toString());
Expand All @@ -144,18 +152,34 @@ export async function getSleepTimeout() {
return Number(await getDataFromEsp("/timeouts/sleep"));
}

export async function setConfigurationModeTimeout(timeout: number) {
timeout = Math.round(timeout);
return await postDataToEsp(
"/timeouts/configurationMode",
timeout.toString()
);
}

export async function getConfigurationModeTimeout() {
return Number(await getDataFromEsp("/timeouts/configurationMode"));
}

export async function getUpdatePercentage() {
return Number(await getDataFromEsp("/update/percentage"));
}

export async function updateFs(file: File) {
return await uploadFile(file, "/update/littlefs");
export async function getSensorData(): Promise<SensorData> {
return await getDataFromEsp("/sensor/data");
}

export async function updateFirmware(file: File) {
return await uploadFile(file, "/update/firmware");
export async function hardResetEsp() {
return await postDataToEsp("/hardReset");
}

export async function getSensorValue() {
return Number(await getDataFromEsp("/sensor/value"));
}
export async function updateFirmware(url: string) {
return await postDataToEsp("/update/firmware", url);
}

export async function getUpdateFirmwareUrl(): Promise<{ url: string }> {
return await getDataFromEsp("/update/firmware");
}
Loading

0 comments on commit da0f00b

Please sign in to comment.