mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
added scan devices button to web - (v2) New Web UI #421
This commit is contained in:
@@ -9,23 +9,25 @@ import {
|
||||
List,
|
||||
withWidth,
|
||||
WithWidthProps,
|
||||
isWidthDown
|
||||
isWidthDown,
|
||||
Button,
|
||||
DialogTitle, DialogContent, DialogActions, Box, Dialog, Typography
|
||||
} from "@material-ui/core";
|
||||
|
||||
import { Box, Typography } from '@material-ui/core';
|
||||
|
||||
import RefreshIcon from "@material-ui/icons/Refresh";
|
||||
|
||||
import { withAuthenticatedContext, AuthenticatedContextProps } from '../authentication';
|
||||
import { redirectingAuthorizedFetch, withAuthenticatedContext, AuthenticatedContextProps } from '../authentication';
|
||||
|
||||
import {
|
||||
RestFormProps,
|
||||
FormActions,
|
||||
FormButton,
|
||||
} from "../components";
|
||||
|
||||
import { EMSESPDevices, Device } from "./types";
|
||||
|
||||
import { ENDPOINT_ROOT } from '../api';
|
||||
export const SCANDEVICES_ENDPOINT = ENDPOINT_ROOT + "scanDevices";
|
||||
|
||||
function compareDevices(a: Device, b: Device) {
|
||||
if (a.type < b.type) {
|
||||
return -1;
|
||||
@@ -36,9 +38,19 @@ function compareDevices(a: Device, b: Device) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
interface EMSESPDevicesFormState {
|
||||
confirmScanDevices: boolean;
|
||||
processing: boolean;
|
||||
}
|
||||
|
||||
type EMSESPDevicesFormProps = RestFormProps<EMSESPDevices> & AuthenticatedContextProps & WithWidthProps;
|
||||
|
||||
class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps> {
|
||||
class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesFormState> {
|
||||
|
||||
state: EMSESPDevicesFormState = {
|
||||
confirmScanDevices: false,
|
||||
processing: false
|
||||
}
|
||||
|
||||
noData = () => {
|
||||
return (this.props.data.devices.length === 0);
|
||||
@@ -97,25 +109,76 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps> {
|
||||
);
|
||||
}
|
||||
|
||||
renderScanDevicesDialog() {
|
||||
return (
|
||||
<Dialog
|
||||
open={this.state.confirmScanDevices}
|
||||
onClose={this.onScanDevicesRejected}
|
||||
>
|
||||
<DialogTitle>Confirm Scan Devices</DialogTitle>
|
||||
<DialogContent dividers>
|
||||
Are you sure you want to scan the EMS bus for all new devices?
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button variant="contained" onClick={this.onScanDevicesRejected} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button startIcon={<RefreshIcon />} variant="contained" onClick={this.onScanDevicesConfirmed} disabled={this.state.processing} color="primary" autoFocus>
|
||||
Start Scan
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
onScanDevices = () => {
|
||||
this.setState({ confirmScanDevices: true });
|
||||
}
|
||||
|
||||
onScanDevicesRejected = () => {
|
||||
this.setState({ confirmScanDevices: false });
|
||||
}
|
||||
|
||||
onScanDevicesConfirmed = () => {
|
||||
this.setState({ processing: true });
|
||||
redirectingAuthorizedFetch(SCANDEVICES_ENDPOINT, { method: 'POST' })
|
||||
.then(response => {
|
||||
if (response.status === 200) {
|
||||
this.props.enqueueSnackbar("Device scan is starting...", { variant: 'info' });
|
||||
this.setState({ processing: false, confirmScanDevices: false });
|
||||
} else {
|
||||
throw Error("Invalid status code: " + response.status);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
this.props.enqueueSnackbar(error.message || "Problem with scan", { variant: 'error' });
|
||||
this.setState({ processing: false, confirmScanDevices: false });
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<Fragment>
|
||||
<List>{this.createListItems()}</List>
|
||||
<FormActions>
|
||||
<FormButton
|
||||
startIcon={<RefreshIcon />}
|
||||
variant="contained"
|
||||
color="secondary"
|
||||
onClick={this.props.loadData}
|
||||
>
|
||||
Refresh
|
||||
</FormButton>
|
||||
</FormActions>
|
||||
|
||||
<Box display="flex" flexWrap="wrap">
|
||||
<Box flexGrow={1} padding={1}>
|
||||
<FormButton startIcon={<RefreshIcon />} variant="contained" color="secondary" onClick={this.props.loadData}>
|
||||
Refresh
|
||||
</FormButton>
|
||||
</Box>
|
||||
|
||||
<Box flexWrap="none" padding={1} whiteSpace="nowrap">
|
||||
<FormButton startIcon={<RefreshIcon />} variant="contained" color="primary" onClick={this.onScanDevices}>
|
||||
Scan Devices
|
||||
</FormButton>
|
||||
</Box>
|
||||
</Box>
|
||||
{this.renderScanDevicesDialog()}
|
||||
</Fragment>
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default withAuthenticatedContext(withWidth()(EMSESPDevicesForm));
|
||||
|
||||
// export default withTheme(EMSESPDevicesForm);
|
||||
|
||||
@@ -6,11 +6,10 @@
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SecurityManager.h>
|
||||
|
||||
#include <HttpEndpoint.h>
|
||||
#include <MqttPubSub.h>
|
||||
#include <WebSocketTxRx.h>
|
||||
// #include <HttpEndpoint.h>
|
||||
// #include <MqttPubSub.h>
|
||||
// #include <WebSocketTxRx.h>
|
||||
|
||||
#include "EMSESPSettingsService.h"
|
||||
#include "version.h"
|
||||
|
||||
#define MAX_EMSESP_STATUS_SIZE 1024
|
||||
|
||||
20
src/EMSESPScanDevicesService.cpp
Normal file
20
src/EMSESPScanDevicesService.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <EMSESPScanDevicesService.h>
|
||||
|
||||
#include "emsesp.h"
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
EMSESPScanDevicesService::EMSESPScanDevicesService(AsyncWebServer * server, SecurityManager * securityManager) {
|
||||
server->on(SCAN_DEVICES_SERVICE_PATH,
|
||||
HTTP_POST,
|
||||
securityManager->wrapRequest(std::bind(&EMSESPScanDevicesService::scan_devices, this, std::placeholders::_1), AuthenticationPredicates::IS_ADMIN));
|
||||
}
|
||||
|
||||
void EMSESPScanDevicesService::scan_devices(AsyncWebServerRequest * request) {
|
||||
request->onDisconnect([]() {
|
||||
EMSESP::send_read_request(EMSdevice::EMS_TYPE_UBADevices, EMSdevice::EMS_DEVICE_ID_BOILER);
|
||||
});
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
21
src/EMSESPScanDevicesService.h
Normal file
21
src/EMSESPScanDevicesService.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef EMSESPScanDevicesService_h
|
||||
#define EMSESPScanDevicesService_h
|
||||
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SecurityManager.h>
|
||||
|
||||
#define SCAN_DEVICES_SERVICE_PATH "/rest/scanDevices"
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
class EMSESPScanDevicesService {
|
||||
public:
|
||||
EMSESPScanDevicesService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||
|
||||
private:
|
||||
void scan_devices(AsyncWebServerRequest * request);
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
#endif
|
||||
@@ -196,7 +196,7 @@ void EMSESPShell::add_console_commands() {
|
||||
if (arguments.size() == 0) {
|
||||
EMSESP::send_read_request(EMSdevice::EMS_TYPE_UBADevices, EMSdevice::EMS_DEVICE_ID_BOILER);
|
||||
} else {
|
||||
shell.printfln(F("Performing a deep scan by pinging our device library..."));
|
||||
shell.printfln(F("Performing a deep scan..."));
|
||||
std::vector<uint8_t> Device_Ids;
|
||||
|
||||
Device_Ids.push_back(0x08); // Boilers - 0x08
|
||||
|
||||
@@ -40,6 +40,8 @@ EMSESPStatusService EMSESP::emsespStatusService =
|
||||
|
||||
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
EMSESPScanDevicesService EMSESP::emsespScanDevicesService = EMSESPScanDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
std::vector<std::unique_ptr<EMSdevice>> EMSESP::emsdevices; // array of all the detected EMS devices
|
||||
std::vector<emsesp::EMSESP::Device_record> EMSESP::device_library_; // libary of all our known EMS devices so far
|
||||
|
||||
|
||||
14
src/emsesp.h
14
src/emsesp.h
@@ -37,6 +37,7 @@
|
||||
#include "EMSESPStatusService.h"
|
||||
#include "EMSESPDevicesService.h"
|
||||
#include "EMSESPSettingsService.h"
|
||||
#include "EMSESPScanDevicesService.h"
|
||||
|
||||
#include "emsdevice.h"
|
||||
#include "emsfactory.h"
|
||||
@@ -136,6 +137,7 @@ class EMSESP {
|
||||
|
||||
static std::vector<std::unique_ptr<EMSdevice>> emsdevices;
|
||||
|
||||
// services
|
||||
static Mqtt mqtt_;
|
||||
static System system_;
|
||||
static Sensors sensors_;
|
||||
@@ -144,10 +146,12 @@ class EMSESP {
|
||||
static RxService rxservice_;
|
||||
static TxService txservice_;
|
||||
|
||||
static ESP8266React esp8266React;
|
||||
static EMSESPSettingsService emsespSettingsService;
|
||||
static EMSESPStatusService emsespStatusService;
|
||||
static EMSESPDevicesService emsespDevicesService;
|
||||
// web controllers
|
||||
static ESP8266React esp8266React;
|
||||
static EMSESPSettingsService emsespSettingsService;
|
||||
static EMSESPStatusService emsespStatusService;
|
||||
static EMSESPDevicesService emsespDevicesService;
|
||||
static EMSESPScanDevicesService emsespScanDevicesService;
|
||||
|
||||
private:
|
||||
EMSESP() = delete;
|
||||
@@ -169,7 +173,7 @@ class EMSESP {
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
static std::vector<Device_record> device_library_;
|
||||
static std::vector<Device_record> device_library_;
|
||||
|
||||
static uint8_t actual_master_thermostat_;
|
||||
static uint16_t watch_id_;
|
||||
|
||||
Reference in New Issue
Block a user