show device info on Web

This commit is contained in:
proddy
2020-07-26 16:03:06 +02:00
parent 39e5fb1a68
commit 38f00946da
29 changed files with 339 additions and 43 deletions

View File

@@ -5,7 +5,7 @@ import { ENDPOINT_ROOT } from '../api';
import EMSESPDevicesForm from './EMSESPDevicesForm'; import EMSESPDevicesForm from './EMSESPDevicesForm';
import { EMSESPDevices } from './EMSESPtypes'; import { EMSESPDevices } from './EMSESPtypes';
export const EMSESP_DEVICES_ENDPOINT = ENDPOINT_ROOT + "emsespDevices"; export const EMSESP_DEVICES_ENDPOINT = ENDPOINT_ROOT + "allDevices";
type EMSESPDevicesControllerProps = RestControllerProps<EMSESPDevices>; type EMSESPDevicesControllerProps = RestControllerProps<EMSESPDevices>;

View File

@@ -24,10 +24,11 @@ import {
FormButton, FormButton,
} from "../components"; } from "../components";
import { EMSESPDevices, Device } from "./EMSESPtypes"; import { EMSESPDevices, EMSESPDeviceData, Device } from "./EMSESPtypes";
import { ENDPOINT_ROOT } from '../api'; import { ENDPOINT_ROOT } from '../api';
export const SCANDEVICES_ENDPOINT = ENDPOINT_ROOT + "scanDevices"; export const SCANDEVICES_ENDPOINT = ENDPOINT_ROOT + "scanDevices";
export const DEVICE_DATA_ENDPOINT = ENDPOINT_ROOT + "deviceData";
const StyledTableCell = withStyles((theme: Theme) => const StyledTableCell = withStyles((theme: Theme) =>
createStyles({ createStyles({
@@ -54,6 +55,7 @@ function compareDevices(a: Device, b: Device) {
interface EMSESPDevicesFormState { interface EMSESPDevicesFormState {
confirmScanDevices: boolean; confirmScanDevices: boolean;
processing: boolean; processing: boolean;
deviceData?: EMSESPDeviceData;
} }
type EMSESPDevicesFormProps = RestFormProps<EMSESPDevices> & AuthenticatedContextProps & WithWidthProps; type EMSESPDevicesFormProps = RestFormProps<EMSESPDevices> & AuthenticatedContextProps & WithWidthProps;
@@ -65,15 +67,19 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
processing: false processing: false
} }
noData = () => { noDevices = () => {
return (this.props.data.devices.length === 0); return (this.props.data.devices.length === 0);
}; };
noDeviceData = () => {
return (this.state.deviceData?.deviceData.length === 0);
};
createTableItems() { createTableItems() {
const { width, data } = this.props; const { width, data } = this.props;
return ( return (
<TableContainer> <TableContainer>
{!this.noData() && ( {!this.noDevices() && (
<Table size="small" padding={isWidthDown('xs', width!) ? "none" : "default"}> <Table size="small" padding={isWidthDown('xs', width!) ? "none" : "default"}>
<TableHead> <TableHead>
<TableRow> <TableRow>
@@ -87,7 +93,9 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
</TableHead> </TableHead>
<TableBody> <TableBody>
{data.devices.sort(compareDevices).map(device => ( {data.devices.sort(compareDevices).map(device => (
<TableRow hover key={device.type}> <TableRow hover key={device.id}
onClick={() => this.handleRowClick(device.id)}
>
<TableCell component="th" scope="row"> <TableCell component="th" scope="row">
{device.type} {device.type}
</TableCell> </TableCell>
@@ -111,7 +119,7 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
</TableBody> </TableBody>
</Table> </Table>
)} )}
{this.noData() && {this.noDevices() &&
( (
<Box bgcolor="error.main" color="error.contrastText" p={2} mt={2} mb={2}> <Box bgcolor="error.main" color="error.contrastText" p={2} mt={2} mb={2}>
<Typography variant="body1"> <Typography variant="body1">
@@ -156,8 +164,7 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
onScanDevicesConfirmed = () => { onScanDevicesConfirmed = () => {
this.setState({ processing: true }); this.setState({ processing: true });
redirectingAuthorizedFetch(SCANDEVICES_ENDPOINT, { method: 'POST' }) redirectingAuthorizedFetch(SCANDEVICES_ENDPOINT).then(response => {
.then(response => {
if (response.status === 200) { if (response.status === 200) {
this.props.enqueueSnackbar("Device scan is starting...", { variant: 'info' }); this.props.enqueueSnackbar("Device scan is starting...", { variant: 'info' });
this.setState({ processing: false, confirmScanDevices: false }); this.setState({ processing: false, confirmScanDevices: false });
@@ -171,11 +178,91 @@ class EMSESPDevicesForm extends Component<EMSESPDevicesFormProps, EMSESPDevicesF
}); });
} }
handleRowClick = (id: any) => {
this.setState({ deviceData: undefined });
redirectingAuthorizedFetch(DEVICE_DATA_ENDPOINT, {
method: 'POST',
body: JSON.stringify({ id: id }),
headers: {
'Content-Type': 'application/json'
}
}).then(response => {
if (response.status === 200) {
return response.json();
// this.setState({ errorMessage: undefined }, this.props.loadData);
}
throw Error("Unexpected response code: " + response.status);
}).then(json => {
this.setState({ deviceData: json });
}).catch(error => {
this.props.enqueueSnackbar(error.message || "Problem getting device data", { variant: 'error' });
this.setState({ deviceData: undefined });
});
}
renderDeviceData() {
const { deviceData } = this.state;
const { width } = this.props;
if (this.noDevices()) {
return (
<p />
)
}
if (!deviceData) {
return (
<Typography variant="h6">
<p></p>
Click on a device to show it's values
</Typography>
);
}
if ((deviceData.deviceData || []).length === 0) {
return (
<p />
);
}
return (
<Fragment>
<p></p>
<Box bgcolor="info.main" p={2} mt={1} mb={1}>
<Typography variant="body1">
{deviceData.deviceName}
</Typography>
</Box>
<TableContainer>
<Table size="small" padding={isWidthDown('xs', width!) ? "none" : "default"}>
<TableHead>
</TableHead>
<TableBody>
{deviceData.deviceData.map(deviceData => (
<TableRow key={deviceData.name}>
<TableCell component="th" scope="row">
{deviceData.name}
</TableCell>
<TableCell align="left">
{deviceData.value}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</Fragment>
);
}
render() { render() {
return ( return (
<Fragment> <Fragment>
<br></br> <br></br>
{this.createTableItems()} {this.createTableItems()}
{this.renderDeviceData()}
<br></br> <br></br>
<Box display="flex" flexWrap="wrap"> <Box display="flex" flexWrap="wrap">
<Box flexGrow={1} padding={1}> <Box flexGrow={1} padding={1}>

View File

@@ -24,6 +24,7 @@ export interface EMSESPStatus {
} }
export interface Device { export interface Device {
id: number;
type: string; type: string;
brand: string; brand: string;
name: string; name: string;
@@ -36,3 +37,13 @@ export interface EMSESPDevices {
devices: Device[]; devices: Device[];
} }
export interface DeviceData {
name: string;
value: string;
}
export interface EMSESPDeviceData {
deviceName: string;
deviceData: DeviceData[];
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View File

@@ -4,22 +4,37 @@
namespace emsesp { namespace emsesp {
EMSESPDevicesService::EMSESPDevicesService(AsyncWebServer * server, SecurityManager * securityManager) { EMSESPDevicesService::EMSESPDevicesService(AsyncWebServer * server, SecurityManager * securityManager)
: _timeHandler(DEVICE_DATA_SERVICE_PATH,
securityManager->wrapCallback(std::bind(&EMSESPDevicesService::device_data, this, _1, _2), AuthenticationPredicates::IS_AUTHENTICATED)) {
// body
server->on(EMSESP_DEVICES_SERVICE_PATH, server->on(EMSESP_DEVICES_SERVICE_PATH,
HTTP_GET, HTTP_GET,
securityManager->wrapRequest(std::bind(&EMSESPDevicesService::emsespDevicesService, this, std::placeholders::_1), securityManager->wrapRequest(std::bind(&EMSESPDevicesService::all_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED));
AuthenticationPredicates::IS_AUTHENTICATED));
server->on(SCAN_DEVICES_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest(std::bind(&EMSESPDevicesService::scan_devices, this, _1), AuthenticationPredicates::IS_AUTHENTICATED));
_timeHandler.setMethod(HTTP_POST);
_timeHandler.setMaxContentLength(256);
server->addHandler(&_timeHandler);
} }
void EMSESPDevicesService::emsespDevicesService(AsyncWebServerRequest * request) { void EMSESPDevicesService::scan_devices(AsyncWebServerRequest * request) {
EMSESP::send_read_request(EMSdevice::EMS_TYPE_UBADevices, EMSdevice::EMS_DEVICE_ID_BOILER);
request->send(200);
}
void EMSESPDevicesService::all_devices(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_STATUS_SIZE); AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_STATUS_SIZE);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
JsonArray devices = root.createNestedArray("devices"); JsonArray devices = root.createNestedArray("devices");
for (const auto & emsdevice : EMSESP::emsdevices) { for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice) { if (emsdevice) {
JsonObject deviceRoot = devices.createNestedObject(); JsonObject deviceRoot = devices.createNestedObject();
deviceRoot["id"] = emsdevice->unique_id();
deviceRoot["type"] = emsdevice->device_type_name(); deviceRoot["type"] = emsdevice->device_type_name();
deviceRoot["brand"] = emsdevice->brand_to_string(); deviceRoot["brand"] = emsdevice->brand_to_string();
deviceRoot["name"] = emsdevice->name(); deviceRoot["name"] = emsdevice->name();
@@ -28,8 +43,23 @@ void EMSESPDevicesService::emsespDevicesService(AsyncWebServerRequest * request)
deviceRoot["version"] = emsdevice->version(); deviceRoot["version"] = emsdevice->version();
} }
} }
response->setLength(); response->setLength();
request->send(response); request->send(response);
} }
void EMSESPDevicesService::device_data(AsyncWebServerRequest * request, JsonVariant & json) {
if (json.is<JsonObject>()) {
uint8_t id = json["id"]; // get id from selected table row
AsyncJsonResponse * response = new AsyncJsonResponse(false, 1024);
EMSESP::device_info(id, (JsonObject &)response->getRoot());
response->setLength();
request->send(response);
} else {
AsyncWebServerResponse * response = request->beginResponse(200);
request->send(response);
}
}
} // namespace emsesp } // namespace emsesp

View File

@@ -6,23 +6,26 @@
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <SecurityManager.h> #include <SecurityManager.h>
// #include <HttpEndpoint.h>
// #include <MqttPubSub.h>
// #include <WebSocketTxRx.h>
#include "version.h"
#define MAX_EMSESP_STATUS_SIZE 1024 #define MAX_EMSESP_STATUS_SIZE 1024
#define EMSESP_DEVICES_SERVICE_PATH "/rest/emsespDevices"
#define EMSESP_DEVICES_SERVICE_PATH "/rest/allDevices"
#define SCAN_DEVICES_SERVICE_PATH "/rest/scanDevices"
#define DEVICE_DATA_SERVICE_PATH "/rest/deviceData"
namespace emsesp { namespace emsesp {
using namespace std::placeholders; // for `_1`
class EMSESPDevicesService { class EMSESPDevicesService {
public: public:
EMSESPDevicesService(AsyncWebServer * server, SecurityManager * securityManager); EMSESPDevicesService(AsyncWebServer * server, SecurityManager * securityManager);
private: private:
void emsespDevicesService(AsyncWebServerRequest * request); void all_devices(AsyncWebServerRequest * request);
void scan_devices(AsyncWebServerRequest * request);
AsyncCallbackJsonWebHandler _timeHandler;
void device_data(AsyncWebServerRequest * request, JsonVariant & json);
}; };
} // namespace emsesp } // namespace emsesp

View File

@@ -166,6 +166,24 @@ void Boiler::boiler_cmd_wwtemp(const char * message) {
} }
} }
void Boiler::device_info(JsonArray & root) {
JsonObject dataElement;
dataElement = root.createNestedObject();
dataElement["name"] = F("Hot tap water");
dataElement["value"] = tap_water_active_ ? F("running") : F("off");
dataElement = root.createNestedObject();
dataElement["name"] = F("Central heating");
dataElement["value"] = heating_active_ ? F("active") : F("off");
render_value_json(root, "", F("Selected flow temperature"), selFlowTemp_, F_(degrees));
render_value_json(root, "", F("Current flow temperature"), curFlowTemp_, F_(degrees), 10);
render_value_json(root, "", F("Warm Water selected temperature"), wWSelTemp_, F_(degrees));
render_value_json(root, "", F("Warm Water set temperature"), wWSetTmp_, F_(degrees));
render_value_json(root, "", F("Warm Water current temperature (intern)"), wWCurTmp_, F_(degrees), 10);
}
// publish values via MQTT // publish values via MQTT
void Boiler::publish_values() { void Boiler::publish_values() {
const size_t capacity = JSON_OBJECT_SIZE(47); // must recalculate if more objects addded https://arduinojson.org/v6/assistant/ const size_t capacity = JSON_OBJECT_SIZE(47); // must recalculate if more objects addded https://arduinojson.org/v6/assistant/

View File

@@ -40,6 +40,7 @@ class Boiler : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -34,6 +34,9 @@ Connect::Connect(uint8_t device_type, uint8_t device_id, uint8_t product_id, con
// register_mqtt_topic("topic", std::bind(&Connect::cmd, this, _1)); // register_mqtt_topic("topic", std::bind(&Connect::cmd, this, _1));
} }
void Connect::device_info(JsonArray & root) {
}
void Connect::add_context_menu() { void Connect::add_context_menu() {
} }

View File

@@ -37,6 +37,7 @@ class Connect : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -39,6 +39,9 @@ Controller::Controller(uint8_t device_type, uint8_t device_id, uint8_t product_i
void Controller::add_context_menu() { void Controller::add_context_menu() {
} }
void Controller::device_info(JsonArray & root) {
}
// display all values into the shell console // display all values into the shell console
void Controller::show_values(uuid::console::Shell & shell) { void Controller::show_values(uuid::console::Shell & shell) {
EMSdevice::show_values(shell); // always call this to show header EMSdevice::show_values(shell); // always call this to show header

View File

@@ -37,6 +37,7 @@ class Controller : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -39,6 +39,9 @@ Gateway::Gateway(uint8_t device_type, uint8_t device_id, uint8_t product_id, con
void Gateway::add_context_menu() { void Gateway::add_context_menu() {
} }
void Gateway::device_info(JsonArray & root) {
}
// display all values into the shell console // display all values into the shell console
void Gateway::show_values(uuid::console::Shell & shell) { void Gateway::show_values(uuid::console::Shell & shell) {
EMSdevice::show_values(shell); // always call this to show header EMSdevice::show_values(shell); // always call this to show header

View File

@@ -37,6 +37,7 @@ class Gateway : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -50,6 +50,9 @@ Heatpump::Heatpump(uint8_t device_type, uint8_t device_id, uint8_t product_id, c
void Heatpump::add_context_menu() { void Heatpump::add_context_menu() {
} }
void Heatpump::device_info(JsonArray & root) {
}
// display all values into the shell console // display all values into the shell console
void Heatpump::show_values(uuid::console::Shell & shell) { void Heatpump::show_values(uuid::console::Shell & shell) {
EMSdevice::show_values(shell); // always call this to show header EMSdevice::show_values(shell); // always call this to show header

View File

@@ -37,6 +37,7 @@ class Heatpump : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -57,6 +57,9 @@ Mixing::Mixing(uint8_t device_type, uint8_t device_id, uint8_t product_id, const
void Mixing::add_context_menu() { void Mixing::add_context_menu() {
} }
void Mixing::device_info(JsonArray & root) {
}
// check to see if values have been updated // check to see if values have been updated
bool Mixing::updated_values() { bool Mixing::updated_values() {
return false; return false;

View File

@@ -37,6 +37,7 @@ class Mixing : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -58,6 +58,9 @@ Solar::Solar(uint8_t device_type, uint8_t device_id, uint8_t product_id, const s
void Solar::add_context_menu() { void Solar::add_context_menu() {
} }
void Solar::device_info(JsonArray & root) {
}
// display all values into the shell console // display all values into the shell console
void Solar::show_values(uuid::console::Shell & shell) { void Solar::show_values(uuid::console::Shell & shell) {
EMSdevice::show_values(shell); // always call this to show header EMSdevice::show_values(shell); // always call this to show header

View File

@@ -37,6 +37,7 @@ class Solar : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -39,6 +39,9 @@ Switch::Switch(uint8_t device_type, uint8_t device_id, uint8_t product_id, const
void Switch::add_context_menu() { void Switch::add_context_menu() {
} }
void Switch::device_info(JsonArray & root) {
}
// display all values into the shell console // display all values into the shell console
void Switch::show_values(uuid::console::Shell & shell) { void Switch::show_values(uuid::console::Shell & shell) {
EMSdevice::show_values(shell); // always call this to show header EMSdevice::show_values(shell); // always call this to show header

View File

@@ -37,6 +37,7 @@ class Switch : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -169,6 +169,50 @@ void Thermostat::init_mqtt() {
register_mqtt_topic("thermostat_cmd_mode", std::bind(&Thermostat::thermostat_cmd_mode, this, _1)); register_mqtt_topic("thermostat_cmd_mode", std::bind(&Thermostat::thermostat_cmd_mode, this, _1));
} }
// prepare data for Web UI
void Thermostat::device_info(JsonArray & root) {
JsonObject dataElement;
uint8_t flags = (this->flags() & 0x0F); // specific thermostat characteristics, strip the option bits
for (const auto & hc : heating_circuits_) {
if (!Helpers::hasValue(hc->setpoint_roomTemp)) {
break; // skip this HC
}
// different thermostat types store their temperature values differently
uint8_t format_setpoint, format_curr;
switch (flags) {
case EMS_DEVICE_FLAG_EASY:
format_setpoint = 100; // *100
format_curr = 100; // *100
break;
case EMS_DEVICE_FLAG_JUNKERS:
format_setpoint = 10; // *10
format_curr = 10; // *10
break;
default: // RC30, RC35 etc...
format_setpoint = 2; // *2
format_curr = 10; // *10
break;
}
// create prefix with heating circuit number
std::string hc_str(5, '\0');
snprintf_P(&hc_str[0], hc_str.capacity() + 1, PSTR("hc%d: "), hc->hc_num());
render_value_json(root, hc_str, F("Current room temperature"), hc->curr_roomTemp, F_(degrees), format_curr);
render_value_json(root, hc_str, F("Setpoint room temperature"), hc->setpoint_roomTemp, F_(degrees), format_setpoint);
if (Helpers::hasValue(hc->mode)) {
dataElement = root.createNestedObject();
std::string mode_str(15, '\0');
snprintf_P(&mode_str[0], mode_str.capacity() + 1, PSTR("%sMode"), hc_str.c_str());
dataElement["name"] = mode_str;
dataElement["value"] = mode_tostring(hc->get_mode(flags));
}
}
}
// only add the menu for the master thermostat // only add the menu for the master thermostat
void Thermostat::add_context_menu() { void Thermostat::add_context_menu() {
if (device_id() != EMSESP::actual_master_thermostat()) { if (device_id() != EMSESP::actual_master_thermostat()) {
@@ -1024,8 +1068,6 @@ void Thermostat::show_values(uuid::console::Shell & shell) {
} }
} }
// std::sort(heating_circuits_.begin(), heating_circuits_.end()); // sort based on hc number. This has moved to the heating_circuit() function
for (const auto & hc : heating_circuits_) { for (const auto & hc : heating_circuits_) {
if (!Helpers::hasValue(hc->setpoint_roomTemp)) { if (!Helpers::hasValue(hc->setpoint_roomTemp)) {
break; // skip this HC break; // skip this HC

View File

@@ -94,6 +94,7 @@ class Thermostat : public EMSdevice {
virtual void show_values(uuid::console::Shell & shell); virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(); virtual void publish_values();
virtual void device_info(JsonArray & root);
virtual bool updated_values(); virtual bool updated_values();
virtual void add_context_menu(); virtual void add_context_menu();

View File

@@ -127,7 +127,7 @@ uint8_t EMSdevice::decode_brand(uint8_t value) {
} }
} }
// print human friendly description of the EMS device // returns string of a human friendly description of the EMS device
std::string EMSdevice::to_string() const { std::string EMSdevice::to_string() const {
std::string str(160, '\0'); std::string str(160, '\0');
@@ -153,6 +153,17 @@ std::string EMSdevice::to_string() const {
return str; return str;
} }
// returns out brand + device name
std::string EMSdevice::to_string_short() const {
std::string str(160, '\0');
if (brand_ == Brand::NO_BRAND) {
snprintf_P(&str[0], str.capacity() + 1, PSTR("%s: %s"), device_type_name().c_str(), name_.c_str());
} else {
snprintf_P(&str[0], str.capacity() + 1, PSTR("%s: %s %s"), device_type_name().c_str(), brand_to_string().c_str(), name_.c_str());
}
return str;
}
// prints the header for the section // prints the header for the section
void EMSdevice::show_values(uuid::console::Shell & shell) { void EMSdevice::show_values(uuid::console::Shell & shell) {
shell.printfln(F("%s: %s"), device_type_name().c_str(), to_string().c_str()); shell.printfln(F("%s: %s"), device_type_name().c_str(), to_string().c_str());

View File

@@ -102,10 +102,20 @@ class EMSdevice {
return name_; return name_;
} }
inline uint8_t unique_id() const {
return unique_id_;
}
inline void unique_id(uint8_t unique_id) {
unique_id_ = unique_id;
}
std::string brand_to_string() const; std::string brand_to_string() const;
static uint8_t decode_brand(uint8_t value); static uint8_t decode_brand(uint8_t value);
std::string to_string() const; std::string to_string() const;
std::string to_string_short() const;
void show_telegram_handlers(uuid::console::Shell & shell); void show_telegram_handlers(uuid::console::Shell & shell);
void show_mqtt_handlers(uuid::console::Shell & shell); void show_mqtt_handlers(uuid::console::Shell & shell);
@@ -126,6 +136,7 @@ class EMSdevice {
virtual void publish_values() = 0; virtual void publish_values() = 0;
virtual bool updated_values() = 0; virtual bool updated_values() = 0;
virtual void add_context_menu() = 0; virtual void add_context_menu() = 0;
virtual void device_info(JsonArray & root) = 0;
std::string telegram_type_name(std::shared_ptr<const Telegram> telegram); std::string telegram_type_name(std::shared_ptr<const Telegram> telegram);
@@ -165,6 +176,37 @@ class EMSdevice {
} }
} }
// takes a value from an ems device and creates a nested json (name, value)
// which can be passed to the web UI
template <typename Value>
static void render_value_json(JsonArray & json,
const std::string & prefix,
const __FlashStringHelper * name,
Value & value,
const __FlashStringHelper * suffix,
const uint8_t format = 0) {
char buffer[15];
if (Helpers::render_value(buffer, value, format) == nullptr) {
return;
}
JsonObject dataElement = json.createNestedObject();
// copy flash into std::strings to ensure arduinojson can reference them without a copy
if (suffix != nullptr) {
std::string text(20, '\0');
snprintf_P(&text[0], text.capacity() + 1, PSTR("%s%s"), buffer, uuid::read_flash_string(suffix).c_str());
dataElement["value"] = text;
} else {
dataElement["value"] = buffer;
}
std::string text2(100, '\0');
snprintf_P(&text2[0], text2.capacity() + 1, PSTR("%s%s"), prefix.c_str(), uuid::read_flash_string(name).c_str());
dataElement["name"] = text2;
}
static void print_value(uuid::console::Shell & shell, uint8_t padding, const __FlashStringHelper * name, const __FlashStringHelper * value); static void print_value(uuid::console::Shell & shell, uint8_t padding, const __FlashStringHelper * name, const __FlashStringHelper * value);
static void print_value(uuid::console::Shell & shell, uint8_t padding, const __FlashStringHelper * name, const char * value); static void print_value(uuid::console::Shell & shell, uint8_t padding, const __FlashStringHelper * name, const char * value);
@@ -229,6 +271,7 @@ class EMSdevice {
static constexpr uint8_t EMS_DEVICE_FLAG_JUNKERS_2 = (1 << 6); // 6th bit set if older models static constexpr uint8_t EMS_DEVICE_FLAG_JUNKERS_2 = (1 << 6); // 6th bit set if older models
private: private:
uint8_t unique_id_;
uint8_t device_type_ = DeviceType::UNKNOWN; uint8_t device_type_ = DeviceType::UNKNOWN;
uint8_t device_id_ = 0; uint8_t device_id_ = 0;
uint8_t product_id_ = 0; uint8_t product_id_ = 0;

View File

@@ -41,7 +41,6 @@ EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&web
EMSESPStatusService EMSESP::emsespStatusService = EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager()); EMSESPStatusService EMSESP::emsespStatusService = EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager());
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
@@ -63,6 +62,7 @@ uint16_t EMSESP::watch_id_ = WATCH_ID_NONE; /
uint8_t EMSESP::watch_ = 0; // trace off uint8_t EMSESP::watch_ = 0; // trace off
bool EMSESP::tap_water_active_ = false; // for when Boiler states we having running warm water. used in Shower() bool EMSESP::tap_water_active_ = false; // for when Boiler states we having running warm water. used in Shower()
uint32_t EMSESP::last_fetch_ = 0; uint32_t EMSESP::last_fetch_ = 0;
uint8_t EMSESP::unique_id_count_ = 0;
// for a specific EMS device go and request data values // for a specific EMS device go and request data values
// or if device_id is 0 it will fetch from all our known and active devices // or if device_id is 0 it will fetch from all our known and active devices
@@ -474,6 +474,20 @@ bool EMSESP::process_telegram(std::shared_ptr<const Telegram> telegram) {
return found; return found;
} }
// calls the device handler's function to populate a json doc with device info
void EMSESP::device_info(const uint8_t unique_id, JsonObject & root) {
for (const auto & emsdevice : emsdevices) {
if (emsdevice) {
if (emsdevice->unique_id() == unique_id) {
root["deviceName"] = emsdevice->to_string_short(); // can;t use c_str() because of scope
JsonArray data = root.createNestedArray("deviceData");
emsdevice->device_info(data);
return;
}
}
}
}
// return true if we have this device already registered // return true if we have this device already registered
bool EMSESP::device_exists(const uint8_t device_id) { bool EMSESP::device_exists(const uint8_t device_id) {
for (const auto & emsdevice : emsdevices) { for (const auto & emsdevice : emsdevices) {
@@ -513,6 +527,9 @@ void EMSESP::show_devices(uuid::console::Shell & shell) {
// shell.printf(F("[factory ID: %d] "), device_class.first); // shell.printf(F("[factory ID: %d] "), device_class.first);
for (const auto & emsdevice : emsdevices) { for (const auto & emsdevice : emsdevices) {
if ((emsdevice) && (emsdevice->device_type() == device_class.first)) { if ((emsdevice) && (emsdevice->device_type() == device_class.first)) {
#if defined(EMSESP_DEBUG)
shell.printf(F("[id=%d] "), emsdevice->unique_id());
#endif
shell.printf(F("%s: %s"), emsdevice->device_type_name().c_str(), emsdevice->to_string().c_str()); shell.printf(F("%s: %s"), emsdevice->device_type_name().c_str(), emsdevice->to_string().c_str());
if ((emsdevice->device_type() == EMSdevice::DeviceType::THERMOSTAT) && (emsdevice->device_id() == actual_master_thermostat())) { if ((emsdevice->device_type() == EMSdevice::DeviceType::THERMOSTAT) && (emsdevice->device_id() == actual_master_thermostat())) {
shell.printf(F(" ** master device **")); shell.printf(F(" ** master device **"));
@@ -584,6 +601,7 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, std::
} else { } else {
emsdevices.push_back( emsdevices.push_back(
EMSFactory::add(device_p->device_type, device_id, device_p->product_id, version, uuid::read_flash_string(device_p->name), device_p->flags, brand)); EMSFactory::add(device_p->device_type, device_id, device_p->product_id, version, uuid::read_flash_string(device_p->name), device_p->flags, brand));
emsdevices.back()->unique_id(++unique_id_count_);
LOG_DEBUG(F("Adding new device with device ID 0x%02X with product ID %d and version %s"), device_id, product_id, version.c_str()); LOG_DEBUG(F("Adding new device with device ID 0x%02X with product ID %d and version %s"), device_id, product_id, version.c_str());
// go and fetch its data, // go and fetch its data,
fetch_device_values(device_id); fetch_device_values(device_id);
@@ -745,11 +763,11 @@ void EMSESP::loop() {
} }
system_.loop(); // does LED and checks system health, and syslog service system_.loop(); // does LED and checks system health, and syslog service
mqtt_.loop(); // starts mqtt, and sends out anything in the queue
rxservice_.loop(); // process what ever is in the rx queue rxservice_.loop(); // process what ever is in the rx queue
txservice_.loop(); // check that the Tx is all ok txservice_.loop(); // check that the Tx is all ok
shower_.loop(); // check for shower on/off shower_.loop(); // check for shower on/off
sensors_.loop(); // this will also send out via MQTT sensors_.loop(); // this will also send out via MQTT
mqtt_.loop(); // sends out anything in the queue via MQTT
console_.loop(); // telnet/serial console console_.loop(); // telnet/serial console
// force a query on the EMS devices to fetch latest data at a set interval (1 min) // force a query on the EMS devices to fetch latest data at a set interval (1 min)

View File

@@ -37,7 +37,6 @@
#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"
@@ -82,6 +81,8 @@ class EMSESP {
static void send_raw_telegram(const char * data); static void send_raw_telegram(const char * data);
static bool device_exists(const uint8_t device_id); static bool device_exists(const uint8_t device_id);
static void device_info(const uint8_t unique_id, JsonObject & root);
static uint8_t count_devices(const uint8_t device_type); static uint8_t count_devices(const uint8_t device_type);
static uint8_t actual_master_thermostat(); static uint8_t actual_master_thermostat();
@@ -150,7 +151,6 @@ class EMSESP {
static EMSESPSettingsService emsespSettingsService; static EMSESPSettingsService emsespSettingsService;
static EMSESPStatusService emsespStatusService; static EMSESPStatusService emsespStatusService;
static EMSESPDevicesService emsespDevicesService; static EMSESPDevicesService emsespDevicesService;
static EMSESPScanDevicesService emsespScanDevicesService;
static uuid::log::Logger logger() { static uuid::log::Logger logger() {
return logger_; return logger_;
@@ -182,6 +182,8 @@ class EMSESP {
static uint16_t watch_id_; static uint16_t watch_id_;
static uint8_t watch_; static uint8_t watch_;
static bool tap_water_active_; static bool tap_water_active_;
static uint8_t unique_id_count_;
}; };
} // namespace emsesp } // namespace emsesp

View File

@@ -89,6 +89,7 @@ class ConcreteEMSFactory : EMSFactory {
ConcreteEMSFactory(const uint8_t device_type) { ConcreteEMSFactory(const uint8_t device_type) {
EMSFactory::registerFactory(device_type, this); EMSFactory::registerFactory(device_type, this);
} }
auto construct(uint8_t device_type, uint8_t device_id, uint8_t product_id, std::string version, std::string name, uint8_t flags, uint8_t brand) const auto construct(uint8_t device_type, uint8_t device_id, uint8_t product_id, std::string version, std::string name, uint8_t flags, uint8_t brand) const
-> EMSdevice * { -> EMSdevice * {
return new DerivedClass(device_type, device_id, product_id, version, name, flags, brand); return new DerivedClass(device_type, device_id, product_id, version, name, flags, brand);