Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
mqtt base, serviceCode ~H
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Jan 11, 2021
1 parent 93aa853 commit 7705b27
Show file tree
Hide file tree
Showing 22 changed files with 99 additions and 66 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
- split `show values` in smaller packages (edited)
- extended length of IP/hostname from 32 to 48 chars (#676)
- check flowsensor for `tap_water_active`
- serviceCode `~H` instead of nonacii 3-dashes
- mqtt `base` instead of hostname
- mqtt max. topic length fixed to 128

### Removed
26 changes: 13 additions & 13 deletions interface/src/mqtt/MqttSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import SaveIcon from '@material-ui/icons/Save';
import MenuItem from '@material-ui/core/MenuItem';

import { RestFormProps, FormActions, FormButton, BlockFormControlLabel, PasswordValidator } from '../components';
import { isIP, isHostname, or } from '../validators';
import { isIP, isHostname, or, isPath } from '../validators';

import { MqttSettings } from './types';

Expand All @@ -16,6 +16,7 @@ class MqttSettingsForm extends React.Component<MqttSettingsFormProps> {

componentDidMount() {
ValidatorForm.addValidationRule('isIPOrHostname', or(isIP, isHostname));
ValidatorForm.addValidationRule('isPath', isPath);
}

render() {
Expand Down Expand Up @@ -43,6 +44,17 @@ class MqttSettingsForm extends React.Component<MqttSettingsFormProps> {
onChange={handleValueChange('host')}
margin="normal"
/>
<TextValidator
validators={['required', 'isPath']}
errorMessages={['Base is required', 'Not a valid path']}
name="base"
label="Base"
fullWidth
variant="outlined"
value={data.base}
onChange={handleValueChange('base')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:0', 'maxNumber:65535']}
errorMessages={['Port is required', "Must be a number", "Must be greater than 0 ", "Max value is 65535"]}
Expand Down Expand Up @@ -94,18 +106,6 @@ class MqttSettingsForm extends React.Component<MqttSettingsFormProps> {
onChange={handleValueChange('keep_alive')}
margin="normal"
/>
<TextValidator
validators={['required', 'isNumber', 'minNumber:1', 'maxNumber:65535']}
errorMessages={['Max topic length is required', "Must be a number", "Must be greater than 0", "Max value is 65535"]}
name="max_topic_length"
label="Max Topic Length"
fullWidth
variant="outlined"
value={data.max_topic_length}
type="number"
onChange={handleValueChange('max_topic_length')}
margin="normal"
/>
<SelectValidator name="mqtt_format"
label="Format"
value={data.mqtt_format}
Expand Down
2 changes: 1 addition & 1 deletion interface/src/mqtt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export interface MqttStatus {
export interface MqttSettings {
enabled: boolean;
host: string;
base: string;
port: number;
username: string;
password: string;
client_id: string;
keep_alive: number;
clean_session: boolean;
max_topic_length: number;
publish_time_boiler: number;
publish_time_thermostat: number;
publish_time_solar: number;
Expand Down
2 changes: 1 addition & 1 deletion interface/src/project/EMSESPSettingsController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function EMSESPSettingsControllerForm(props: EMSESPSettingsControllerFormProps)
value="trace_raw"
/>
}
label="Trace ems-telegrams in raw format"
label="Trace EMS telegrams in raw format"
/>

<br></br>
Expand Down
2 changes: 2 additions & 0 deletions interface/src/system/SystemStatusForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class SystemStatusForm extends Component<SystemStatusFormProps, SystemStatusForm
</ListItemAvatar>
<ListItemText primary="System Uptime" secondary={data.uptime} />
</ListItem>
<Divider variant="inset" component="li" />
<ListItem >
<ListItemAvatar>
<Avatar>
Expand Down Expand Up @@ -94,6 +95,7 @@ class SystemStatusForm extends Component<SystemStatusFormProps, SystemStatusForm
</ListItem>
</Fragment>)
}
<Divider variant="inset" component="li" />
<ListItem>
<ListItemAvatar>
<Avatar>
Expand Down
1 change: 1 addition & 0 deletions interface/src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as isHostname } from './isHostname';
export { default as isIP } from './isIP';
export { default as optional } from './optional';
export { default as or } from './or';
export { default as isPath } from './isPath';
6 changes: 6 additions & 0 deletions interface/src/validators/isPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const pathLengthRegex = /^[^.]{0,108}$/
const pathPatternRegex = /^([a-zA-Z0-9_][a-zA-Z0-9/_-]*[a-zA-Z0-9_])$/

export default function isPath(path: string) {
return pathLengthRegex.test(path) && pathPatternRegex.test(path);
}
11 changes: 5 additions & 6 deletions lib/framework/MqttSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void MqttSettingsService::configureMqtt() {
_mqttClient.setClientId(retainCstr(_state.clientId.c_str(), &_retainedClientId));
_mqttClient.setKeepAlive(_state.keepAlive);
_mqttClient.setCleanSession(_state.cleanSession);
_mqttClient.setMaxTopicLength(_state.maxTopicLength);
_mqttClient.setMaxTopicLength(FACTORY_MQTT_MAX_TOPIC_LENGTH);
_mqttClient.connect();
}

Expand All @@ -173,13 +173,14 @@ void MqttSettingsService::configureMqtt() {
void MqttSettings::read(MqttSettings & settings, JsonObject & root) {
root["enabled"] = settings.enabled;
root["host"] = settings.host;
root["base"] = settings.base;
root["port"] = settings.port;
root["username"] = settings.username;
root["password"] = settings.password;
root["client_id"] = settings.clientId;
root["keep_alive"] = settings.keepAlive;
root["clean_session"] = settings.cleanSession;
root["max_topic_length"] = settings.maxTopicLength;
// root["max_topic_length"] = settings.maxTopicLength;

// added by proddy for EMS-ESP
root["publish_time_boiler"] = settings.publish_time_boiler;
Expand All @@ -198,13 +199,14 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting

newSettings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED;
newSettings.host = root["host"] | FACTORY_MQTT_HOST;
newSettings.base = root["base"] | FACTORY_MQTT_BASE;
newSettings.port = root["port"] | FACTORY_MQTT_PORT;
newSettings.username = root["username"] | FACTORY_MQTT_USERNAME;
newSettings.password = root["password"] | FACTORY_MQTT_PASSWORD;
newSettings.clientId = root["client_id"] | FACTORY_MQTT_CLIENT_ID;
newSettings.keepAlive = root["keep_alive"] | FACTORY_MQTT_KEEP_ALIVE;
newSettings.cleanSession = root["clean_session"] | FACTORY_MQTT_CLEAN_SESSION;
newSettings.maxTopicLength = root["max_topic_length"] | FACTORY_MQTT_MAX_TOPIC_LENGTH;
// newSettings.maxTopicLength = root["max_topic_length"] | FACTORY_MQTT_MAX_TOPIC_LENGTH;

newSettings.publish_time_boiler = root["publish_time_boiler"] | EMSESP_DEFAULT_PUBLISH_TIME;
newSettings.publish_time_thermostat = root["publish_time_thermostat"] | EMSESP_DEFAULT_PUBLISH_TIME;
Expand All @@ -219,15 +221,12 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
if (newSettings.mqtt_qos != settings.mqtt_qos) {
emsesp::EMSESP::mqtt_.set_qos(newSettings.mqtt_qos);
}

if (newSettings.mqtt_format != settings.mqtt_format) {
emsesp::EMSESP::mqtt_.set_format(newSettings.mqtt_format);
}

if (newSettings.mqtt_retain != settings.mqtt_retain) {
emsesp::EMSESP::mqtt_.set_retain(newSettings.mqtt_retain);
}

if (newSettings.publish_time_boiler != settings.publish_time_boiler) {
emsesp::EMSESP::mqtt_.set_publish_time_boiler(newSettings.publish_time_boiler);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/framework/MqttSettingsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#define FACTORY_MQTT_HOST "test.mosquitto.org"
#endif

#ifndef FACTORY_MQTT_BASE
#define FACTORY_MQTT_BASE "ems-esp"
#endif

#ifndef FACTORY_MQTT_PORT
#define FACTORY_MQTT_PORT 1883
#endif
Expand Down Expand Up @@ -70,6 +74,7 @@ class MqttSettings {
// host and port - if enabled
bool enabled;
String host;
String base;
uint16_t port;

// username and password
Expand All @@ -82,7 +87,7 @@ class MqttSettings {
// connection settings
uint16_t keepAlive;
bool cleanSession;
uint16_t maxTopicLength;
// uint16_t maxTopicLength;

// proddy EMS-ESP specific
uint16_t publish_time_boiler;
Expand Down
8 changes: 6 additions & 2 deletions src/dallassensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,18 @@ void DallasSensor::loop() {
sensors_.back().read = true;
changed_ = true;
}
} else {
sensorfails_++;
}
break;

default:
sensorfails_++;
LOG_ERROR(F("Unknown dallas sensor %s"), Sensor(addr).to_string().c_str());
break;
}
} else {
sensorfails_++;
LOG_ERROR(F("Invalid dallas sensor %s"), Sensor(addr).to_string().c_str());
}
} else {
Expand Down Expand Up @@ -344,8 +348,8 @@ void DallasSensor::publish_values(const bool force) {
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> config;
config["dev_cla"] = FJSON("temperature");

char stat_t[50];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/dallassensor_data"), System::hostname().c_str());
char stat_t[128];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/dallassensor_data"), Mqtt::base().c_str());
config["stat_t"] = stat_t;

config["unit_of_meas"] = FJSON("°C");
Expand Down
15 changes: 10 additions & 5 deletions src/dallassensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class DallasSensor {

const std::vector<Sensor> sensors() const;

uint32_t fails() {
return sensorfails_;
}

private:
static constexpr uint8_t MAX_SENSORS = 20;

Expand Down Expand Up @@ -111,11 +115,12 @@ class DallasSensor {

bool registered_ha_[MAX_SENSORS];

int8_t scancnt_ = -3;
uint8_t firstscan_ = 0;
uint8_t dallas_gpio_ = 0;
bool parasite_ = false;
bool changed_ = false;
int8_t scancnt_ = -3;
uint8_t firstscan_ = 0;
uint8_t dallas_gpio_ = 0;
bool parasite_ = false;
bool changed_ = false;
uint32_t sensorfails_ = 0;
};

} // namespace emsesp
Expand Down
10 changes: 7 additions & 3 deletions src/devices/boiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ void Boiler::register_mqtt_ha_config() {
doc["uniq_id"] = FJSON("boiler");
doc["ic"] = FJSON("mdi:home-thermometer-outline");

char stat_t[50];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/boiler_data"), System::hostname().c_str());
char stat_t[128];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/boiler_data"), Mqtt::base().c_str());
doc["stat_t"] = stat_t;

doc["val_tpl"] = FJSON("{{value_json.serviceCode}}");
Expand Down Expand Up @@ -704,7 +704,11 @@ bool Boiler::export_values_main(JsonObject & json, const bool textformat) {

// Service Code & Service Code Number
if (Helpers::hasValue(serviceCodeNumber_)) {
json["serviceCode"] = serviceCode_;
if (serviceCode_[0] == 0xF0) {
json["serviceCode"] = FJSON("~H");
} else {
json["serviceCode"] = serviceCode_;
}
json["serviceCodeNumber"] = serviceCodeNumber_;
}

Expand Down
4 changes: 2 additions & 2 deletions src/devices/heatpump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ void Heatpump::register_mqtt_ha_config() {
doc["uniq_id"] = F_(heatpump);
doc["ic"] = F_(iconheatpump);

char stat_t[50];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/heatpump_data"), System::hostname().c_str());
char stat_t[128];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/heatpump_data"), Mqtt::base().c_str());
doc["stat_t"] = stat_t;

doc["val_tpl"] = FJSON("{{value_json.airHumidity}}");
Expand Down
4 changes: 2 additions & 2 deletions src/devices/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ void Mixer::register_mqtt_ha_config() {

doc["ic"] = FJSON("mdi:home-thermometer-outline");

char stat_t[50];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/mixer_data"), System::hostname().c_str());
char stat_t[128];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/mixer_data"), Mqtt::base().c_str());
doc["stat_t"] = stat_t;

doc["val_tpl"] = FJSON("{{value_json.type}}"); // HA needs a single value. We take the type which is wwc or hc
Expand Down
4 changes: 2 additions & 2 deletions src/devices/solar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ void Solar::register_mqtt_ha_config() {
doc["uniq_id"] = F_(solar);
doc["ic"] = F_(iconthermostat);

char stat_t[50];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/solar_data"), System::hostname().c_str());
char stat_t[128];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/solar_data"), Mqtt::base().c_str());
doc["stat_t"] = stat_t;

doc["val_tpl"] = FJSON("{{value_json.solarPump}}");
Expand Down
4 changes: 2 additions & 2 deletions src/devices/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ void Switch::register_mqtt_ha_config() {

doc["ic"] = FJSON("mdi:home-thermometer-outline");

char stat_t[50];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/switch_data"), System::hostname().c_str());
char stat_t[128];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/switch_data"), Mqtt::base().c_str());
doc["stat_t"] = stat_t;

doc["val_tpl"] = FJSON("{{value_json.type}}"); // HA needs a single value. We take the type which is wwc or hc
Expand Down
6 changes: 3 additions & 3 deletions src/devices/thermostat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,8 @@ void Thermostat::register_mqtt_ha_config() {
doc["uniq_id"] = FJSON("thermostat");
doc["ic"] = FJSON("mdi:home-thermometer-outline");

char stat_t[50];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/thermostat_data"), System::hostname().c_str());
char stat_t[128];
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/thermostat_data"), Mqtt::base().c_str());
doc["stat_t"] = stat_t;

doc["name"] = FJSON("Thermostat Status");
Expand Down Expand Up @@ -912,7 +912,7 @@ void Thermostat::register_mqtt_ha_config(uint8_t hc_num) {
doc["uniq_id"] = str2;
doc["mode_cmd_t"] = str3;
doc["temp_cmd_t"] = str3;
doc["~"] = System::hostname(); // ems-esp
doc["~"] = Mqtt::base(); // ems-esp
doc["mode_stat_t"] = FJSON("~/thermostat_data");
doc["temp_stat_t"] = FJSON("~/thermostat_data");
doc["curr_temp_t"] = FJSON("~/thermostat_data");
Expand Down
4 changes: 4 additions & 0 deletions src/emsesp.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ class EMSESP {
static bool have_sensors() {
return (!(dallassensor_.sensors().empty()));
}
static uint32_t sensor_fails() {
return dallassensor_.fails();
}

enum Watch : uint8_t { WATCH_OFF, WATCH_ON, WATCH_RAW, WATCH_UNKNOWN };
static void watch_id(uint16_t id);
Expand All @@ -139,6 +142,7 @@ class EMSESP {
static uint8_t watch() {
return watch_;
}

static void set_read_id(uint16_t id) {
read_id_ = id;
}
Expand Down
Loading

0 comments on commit 7705b27

Please sign in to comment.