added scan devices button to web - (v2) New Web UI #421

This commit is contained in:
proddy
2020-07-06 22:35:57 +02:00
parent d4dd75214c
commit 76e4a32ef2
7 changed files with 137 additions and 28 deletions

View File

@@ -9,23 +9,25 @@ import {
List, List,
withWidth, withWidth,
WithWidthProps, WithWidthProps,
isWidthDown isWidthDown,
Button,
DialogTitle, DialogContent, DialogActions, Box, Dialog, Typography
} from "@material-ui/core"; } from "@material-ui/core";
import { Box, Typography } from '@material-ui/core';
import RefreshIcon from "@material-ui/icons/Refresh"; import RefreshIcon from "@material-ui/icons/Refresh";
import { withAuthenticatedContext, AuthenticatedContextProps } from '../authentication'; import { redirectingAuthorizedFetch, withAuthenticatedContext, AuthenticatedContextProps } from '../authentication';
import { import {
RestFormProps, RestFormProps,
FormActions,
FormButton, FormButton,
} from "../components"; } from "../components";
import { EMSESPDevices, Device } from "./types"; import { EMSESPDevices, Device } from "./types";
import { ENDPOINT_ROOT } from '../api';
export const SCANDEVICES_ENDPOINT = ENDPOINT_ROOT + "scanDevices";
function compareDevices(a: Device, b: Device) { function compareDevices(a: Device, b: Device) {
if (a.type < b.type) { if (a.type < b.type) {
return -1; return -1;
@@ -36,9 +38,19 @@ function compareDevices(a: Device, b: Device) {
return 0; return 0;
} }
interface EMSESPDevicesFormState {
confirmScanDevices: boolean;
processing: boolean;
}
type EMSESPDevicesFormProps = RestFormProps<EMSESPDevices> & AuthenticatedContextProps & WithWidthProps; type EMSESPDevicesFormProps = RestFormProps<EMSESPDevices> & AuthenticatedContextProps & WithWidthProps;
class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps> { class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesFormState> {
state: EMSESPDevicesFormState = {
confirmScanDevices: false,
processing: false
}
noData = () => { noData = () => {
return (this.props.data.devices.length === 0); 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() { render() {
return ( return (
<Fragment> <Fragment>
<List>{this.createListItems()}</List> <List>{this.createListItems()}</List>
<FormActions>
<FormButton <Box display="flex" flexWrap="wrap">
startIcon={<RefreshIcon />} <Box flexGrow={1} padding={1}>
variant="contained" <FormButton startIcon={<RefreshIcon />} variant="contained" color="secondary" onClick={this.props.loadData}>
color="secondary"
onClick={this.props.loadData}
>
Refresh Refresh
</FormButton> </FormButton>
</FormActions> </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> </Fragment>
); );
} }
} }
export default withAuthenticatedContext(withWidth()(EMSESPDevicesForm)); export default withAuthenticatedContext(withWidth()(EMSESPDevicesForm));
// export default withTheme(EMSESPDevicesForm);

View File

@@ -6,11 +6,10 @@
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <SecurityManager.h> #include <SecurityManager.h>
#include <HttpEndpoint.h> // #include <HttpEndpoint.h>
#include <MqttPubSub.h> // #include <MqttPubSub.h>
#include <WebSocketTxRx.h> // #include <WebSocketTxRx.h>
#include "EMSESPSettingsService.h"
#include "version.h" #include "version.h"
#define MAX_EMSESP_STATUS_SIZE 1024 #define MAX_EMSESP_STATUS_SIZE 1024

View 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

View 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

View File

@@ -196,7 +196,7 @@ void EMSESPShell::add_console_commands() {
if (arguments.size() == 0) { if (arguments.size() == 0) {
EMSESP::send_read_request(EMSdevice::EMS_TYPE_UBADevices, EMSdevice::EMS_DEVICE_ID_BOILER); EMSESP::send_read_request(EMSdevice::EMS_TYPE_UBADevices, EMSdevice::EMS_DEVICE_ID_BOILER);
} else { } 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; std::vector<uint8_t> Device_Ids;
Device_Ids.push_back(0x08); // Boilers - 0x08 Device_Ids.push_back(0x08); // Boilers - 0x08

View File

@@ -40,6 +40,8 @@ EMSESPStatusService EMSESP::emsespStatusService =
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager()); 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<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 std::vector<emsesp::EMSESP::Device_record> EMSESP::device_library_; // libary of all our known EMS devices so far

View File

@@ -37,6 +37,7 @@
#include "EMSESPStatusService.h" #include "EMSESPStatusService.h"
#include "EMSESPDevicesService.h" #include "EMSESPDevicesService.h"
#include "EMSESPSettingsService.h" #include "EMSESPSettingsService.h"
#include "EMSESPScanDevicesService.h"
#include "emsdevice.h" #include "emsdevice.h"
#include "emsfactory.h" #include "emsfactory.h"
@@ -136,6 +137,7 @@ class EMSESP {
static std::vector<std::unique_ptr<EMSdevice>> emsdevices; static std::vector<std::unique_ptr<EMSdevice>> emsdevices;
// services
static Mqtt mqtt_; static Mqtt mqtt_;
static System system_; static System system_;
static Sensors sensors_; static Sensors sensors_;
@@ -144,10 +146,12 @@ class EMSESP {
static RxService rxservice_; static RxService rxservice_;
static TxService txservice_; static TxService txservice_;
// web controllers
static ESP8266React esp8266React; static ESP8266React esp8266React;
static EMSESPSettingsService emsespSettingsService; static EMSESPSettingsService emsespSettingsService;
static EMSESPStatusService emsespStatusService; static EMSESPStatusService emsespStatusService;
static EMSESPDevicesService emsespDevicesService; static EMSESPDevicesService emsespDevicesService;
static EMSESPScanDevicesService emsespScanDevicesService;
private: private:
EMSESP() = delete; EMSESP() = delete;