feat: add remaining mock calls #41

This commit is contained in:
Proddy
2021-04-01 23:43:50 +02:00
parent 74179ab6e9
commit 88c8cb424b
6 changed files with 1041 additions and 20592 deletions

21500
interface/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -33,7 +33,9 @@
"scripts": { "scripts": {
"start": "react-app-rewired start", "start": "react-app-rewired start",
"build": "react-app-rewired build", "build": "react-app-rewired build",
"eject": "react-scripts eject" "eject": "react-scripts eject",
"mock-api": "node ../mock-api/server.js",
"dev": "run-p start mock-api"
}, },
"eslintConfig": { "eslintConfig": {
"extends": "react-app" "extends": "react-app"
@@ -53,6 +55,7 @@
"devDependencies": { "devDependencies": {
"concurrently": "^4.1.0", "concurrently": "^4.1.0",
"http-proxy-middleware": "^0.19.1", "http-proxy-middleware": "^0.19.1",
"react-app-rewired": "^2.1.8" "npm-run-all": "^4.1.5",
"react-app-rewired": "^2.1.8",
} }
} }

12
mock-api/README.md Normal file
View File

@@ -0,0 +1,12 @@
(https://github.com/emsesp/EMS-ESP32/issues/41)
When developing and testing the web interface, it's handy not to bother with reflashing an ESP32 each time. The idea is to mimic the ESP using a mock/stub server that responds to HTTP POST and GET requests.
Currently a first draft at this is in the `ft_mockapi` branch. To run it do
```sh
% cd interface
% npm run dev
```
The hardcoded data is limited at the moment and can be easily extended by changing the file `/mock-api/server.js`

View File

@@ -1,3 +1,10 @@
const express = require('express');
const path = require('path');
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 3080;
const ENDPOINT_ROOT = "/rest/"; const ENDPOINT_ROOT = "/rest/";
const FEATURES_ENDPOINT = ENDPOINT_ROOT + "features"; const FEATURES_ENDPOINT = ENDPOINT_ROOT + "features";
@@ -26,14 +33,10 @@ const TIME_ENDPOINT = ENDPOINT_ROOT + "time";
// EMS-ESP Project specific // EMS-ESP Project specific
const EMSESP_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "emsespSettings"; const EMSESP_SETTINGS_ENDPOINT = ENDPOINT_ROOT + "emsespSettings";
const EMSESP_ALLDEVICES_ENDPOINT = ENDPOINT_ROOT + "allDevices"; const EMSESP_ALLDEVICES_ENDPOINT = ENDPOINT_ROOT + "allDevices";
const EMSESP_SCANDEVICES_ENDPOINT = ENDPOINT_ROOT + "scanDevices";
const EMSESP_DEVICEDATA_ENDPOINT = ENDPOINT_ROOT + "deviceData"; const EMSESP_DEVICEDATA_ENDPOINT = ENDPOINT_ROOT + "deviceData";
const EMSESP_STATUS_ENDPOINT = ENDPOINT_ROOT + "emsespStatus"; const EMSESP_STATUS_ENDPOINT = ENDPOINT_ROOT + "emsespStatus";
const EMSESP_BOARDPROFILE_ENDPOINT = ENDPOINT_ROOT + "boardProfile";
const express = require('express');
const path = require('path');
const app = express(),
bodyParser = require("body-parser");
const port = process.env.PORT || 3080;
app.use(bodyParser.json()); app.use(bodyParser.json());
@@ -89,6 +92,10 @@ app.get(NETWORK_SETTINGS_ENDPOINT, (req, res) => {
}); });
}); });
app.post(NETWORK_SETTINGS_ENDPOINT, (req, res) => {
res.json({});
});
app.get(LIST_NETWORKS_ENDPOINT, (req, res) => { app.get(LIST_NETWORKS_ENDPOINT, (req, res) => {
res.json({ res.json({
"networks": [ "networks": [
@@ -121,12 +128,20 @@ app.get(AP_STATUS_ENDPOINT, (req, res) => {
}); });
}); });
app.post(AP_SETTINGS_ENDPOINT, (req, res) => {
res.json({});
});
app.get(OTA_SETTINGS_ENDPOINT, (req, res) => { app.get(OTA_SETTINGS_ENDPOINT, (req, res) => {
res.json({ res.json({
"enabled": true, "port": 8266, "password": "ems-esp-neo" "enabled": true, "port": 8266, "password": "ems-esp-neo"
}); });
}); });
app.post(OTA_SETTINGS_ENDPOINT, (req, res) => {
res.json({});
});
app.get(MQTT_SETTINGS_ENDPOINT, (req, res) => { app.get(MQTT_SETTINGS_ENDPOINT, (req, res) => {
res.json({ res.json({
"enabled": true, "host": "192.168.1.4", "port": 1883, "base": "ems-esp32", "username": "", "password": "", "enabled": true, "host": "192.168.1.4", "port": 1883, "base": "ems-esp32", "username": "", "password": "",
@@ -137,6 +152,10 @@ app.get(MQTT_SETTINGS_ENDPOINT, (req, res) => {
}); });
}); });
app.post(MQTT_SETTINGS_ENDPOINT, (req, res) => {
res.json({});
});
app.get(MQTT_STATUS_ENDPOINT, (req, res) => { app.get(MQTT_STATUS_ENDPOINT, (req, res) => {
res.json({ res.json({
"enabled": true, "connected": true, "client_id": "ems-esp32", "disconnect_reason": 0, "mqtt_fails": 0 "enabled": true, "connected": true, "client_id": "ems-esp32", "disconnect_reason": 0, "mqtt_fails": 0
@@ -149,6 +168,10 @@ app.get(NTP_SETTINGS_ENDPOINT, (req, res) => {
}); });
}); });
app.post(NTP_SETTINGS_ENDPOINT, (req, res) => {
res.json({});
});
app.get(NTP_STATUS_ENDPOINT, (req, res) => { app.get(NTP_STATUS_ENDPOINT, (req, res) => {
res.json({ res.json({
"status": 1, "utc_time": "2021-04-01T14:25:42Z", "local_time": "2021-04-01T16:25:42", "server": "time.google.com", "uptime": 856 "status": 1, "utc_time": "2021-04-01T14:25:42Z", "local_time": "2021-04-01T16:25:42", "server": "time.google.com", "uptime": 856
@@ -169,6 +192,12 @@ app.get(SECURITY_SETTINGS_ENDPOINT, (req, res) => {
}); });
}); });
app.post(SECURITY_SETTINGS_ENDPOINT, (req, res) => {
res.json({});
});
// EMS-ESP Project stuff
app.get(EMSESP_SETTINGS_ENDPOINT, (req, res) => { app.get(EMSESP_SETTINGS_ENDPOINT, (req, res) => {
res.json({ res.json({
"tx_mode": 1, "tx_delay": 0, "ems_bus_id": 11, "syslog_enabled": false, "syslog_level": 3, "tx_mode": 1, "tx_delay": 0, "ems_bus_id": 11, "syslog_enabled": false, "syslog_level": 3,
@@ -240,5 +269,76 @@ app.post(EMSESP_DEVICEDATA_ENDPOINT, (req, res) => {
}); });
app.post(EMSESP_BOARDPROFILE_ENDPOINT, (req, res) => {
const board_profile = req.body.code;
const data = {
led_gpio: 1,
dallas_gpio: 2,
rx_gpio: 3,
tx_gpio: 4,
pbutton_gpio: 5
};
if (board_profile == "S32") { // BBQKees Gateway S32
data.led_gpio = 2;
data.dallas_gpio = 3;
data.rx_gpio = 23;
data.tx_gpio = 5;
data.pbutton_gpio = 0;
} else if (board_profile == "E32") { // BBQKees Gateway E32
data.led_gpio = 2;
data.dallas_gpio = 4;
data.rx_gpio = 5;
data.tx_gpio = 17;
data.pbutton_gpio = 33;
} else if (board_profile == "MT-ET") { // MT-ET Live D1 Mini
data.led_gpio = 2;
data.dallas_gpio = 18;
data.rx_gpio = 23;
data.tx_gpio = 5;
data.pbutton_gpio = 0;
} else if (board_profile == "NODEMCU") { // NodeMCU 32S
data.led_gpio = 2;
data.dallas_gpio = 18;
data.rx_gpio = 23;
data.tx_gpio = 5;
data.pbutton_gpio = 0;
} else if (board_profile == "LOLIN") {// Lolin D32
data.led_gpio = 2;
data.dallas_gpio = 18;
data.rx_gpio = 17;
data.tx_gpio = 16;
data.pbutton_gpio = 0;
} else if (board_profile == "OLIMEX") {// Olimex ESP32-EVB (uses U1TXD/U1RXD/BUTTON, no LED or Dallas)
data.led_gpio = 0;
data.dallas_gpio = 0;
data.rx_gpio = 36;
data.tx_gpio = 4;
data.pbutton_gpio = 34;
// data = { 0, 0, 36, 4, 34};
} else if (board_profile == "TLK110") {// Generic Ethernet (TLK110)
data.led_gpio = 2;
data.dallas_gpio = 4;
data.rx_gpio = 5;
data.tx_gpio = 17;
data.pbutton_gpio = 33;
} else if (board_profile == "LAN8720") {// Generic Ethernet (LAN8720)
data.led_gpio = 2;
data.dallas_gpio = 4;
data.rx_gpio = 5;
data.tx_gpio = 17;
data.pbutton_gpio = 33;
}
res.json(data);
});
app.post(EMSESP_SETTINGS_ENDPOINT, (req, res) => {
res.json({});
});
app.listen(port); app.listen(port);
console.log(`Mock Server listening on port ${port}`);
console.log(`Mock API Server is up and running at: http://localhost:${port}`);