Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting emoncms port #102

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion gui/readme.md
Expand Up @@ -18,7 +18,7 @@ a real device or the simulator.

You can configure the dev server using [dotenv](https://www.npmjs.com/package/dotenv). An example `.env` file can be found [here](.env.example).

`OPENEVSE_ENDPOINT` - URL of the OpenEVSE to test against.
`EMONESP_ENDPOINT` - URL of the OpenEVSE to test against.

`DEV_HOST` - By default the dev server is only available to localhost. If you want to expose the server to the local network then set this to the IP address or hostname of the external network interface.

Expand Down
1 change: 1 addition & 0 deletions gui/src/ViewModels/ConfigViewModel.js
Expand Up @@ -7,6 +7,7 @@ function ConfigViewModel() {
"pass": "",
"emoncms_enabled": false,
"emoncms_server": "data.openevse.com",
"emoncms_port": "",
"emoncms_path": "/emoncms",
"emoncms_apikey": "",
"emoncms_node": "",
Expand Down
4 changes: 4 additions & 0 deletions gui/src/ViewModels/EmonEspViewModel.js
Expand Up @@ -198,6 +198,7 @@ function EmonEspViewModel(baseHost, basePort, baseProtocol) {
var emoncms = {
enable: self.config.emoncms_enabled(),
server: self.config.emoncms_server(),
port: self.config.emoncms_port(),
path: self.config.emoncms_path(),
apikey: self.config.emoncms_apikey(),
node: self.config.emoncms_node(),
Expand All @@ -206,6 +207,9 @@ function EmonEspViewModel(baseHost, basePort, baseProtocol) {

if (emoncms.server === "" || emoncms.node === "") {
alert("Please enter Emoncms server and node");
} else if (Number(emoncms.port) % 1 !== 0) {
// accepts whitespace or integers
alert("Please enter port number or leave blank");
} else if (emoncms.apikey.length != 32 && !self.emoncmsApiKey.isDummy()) {
alert("Please enter valid Emoncms apikey");
} else if (emoncms.fingerprint !== "" && emoncms.fingerprint.length != 59) {
Expand Down
5 changes: 5 additions & 0 deletions gui/src/home.html
Expand Up @@ -166,6 +166,11 @@ <h2 onclick="toggle('emoncms');"><span>+</span> Emoncms</h2>
'emonpi', or '192.168.1.4'
</span><br />
</p>
<p data-bind="visible: config.emoncms_enabled">
<b>Emoncms Port:</b><br>
<input data-bind="textInput: config.emoncms_port" type="text"><br />
<span class="small-text">e.g 8080, or blank for default</span><br>
</p>
<p data-bind="visible: config.emoncms_enabled">
<b>Emoncms Path:</b><br>
<input type="text" data-bind="textInput: config.emoncms_path"><br />
Expand Down
10 changes: 5 additions & 5 deletions gui/webpack.config.js
Expand Up @@ -31,7 +31,8 @@ module.exports = {
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].js"
filename: "[name].js",
hashFunction: "sha256"
},
devServer: {
host: devHost,
Expand Down Expand Up @@ -153,8 +154,7 @@ module.exports = {
}
};

function uglify(name, code)
{
function uglify(name, code) {
var compiled = babel.transformSync(code, {
presets: ["@babel/preset-env"],
sourceMaps: true
Expand All @@ -163,10 +163,10 @@ function uglify(name, code)
warnings: true,
sourceMap: {
content: compiled.map,
url: name+".map"
url: name + ".map"
}
});
if(ugly.error) {
if (ugly.error) {
console.log(ugly.error);
return code;
}
Expand Down
15 changes: 9 additions & 6 deletions src/app_config.cpp
Expand Up @@ -40,6 +40,7 @@ String www_password = "";

// EMONCMS SERVER strings
String emoncms_server = "";
int emoncms_port = 0;
String emoncms_path = "";
String emoncms_node = "";
String emoncms_apikey = "";
Expand Down Expand Up @@ -88,20 +89,21 @@ ConfigOpt *opts[] =

// EMONCMS SERVER strings, 5
new ConfigOptDefenition<String>(emoncms_server, "emoncms.org", "emoncms_server", "es"),
new ConfigOptDefenition<int>(emoncms_port, 0, "emoncms_port", "ept"),
new ConfigOptDefenition<String>(emoncms_path, "", "emoncms_path", "ep"),
new ConfigOptDefenition<String>(emoncms_node, node_name, "emoncms_node", "en"),
new ConfigOptSecret(emoncms_apikey, "", "emoncms_apikey", "ea"),
new ConfigOptDefenition<String>(emoncms_fingerprint, "", "emoncms_fingerprint", "ef"),

// MQTT Settings, 10
// MQTT Settings, 11
new ConfigOptDefenition<String>(mqtt_server, "emonpi", "mqtt_server", "ms"),
new ConfigOptDefenition<int>(mqtt_port, 1883, "mqtt_port", "mpt"),
new ConfigOptDefenition<String>(mqtt_topic, "emonesp", "mqtt_topic", "mt"),
new ConfigOptDefenition<String>(mqtt_user, "emonpi", "mqtt_user", "mu"),
new ConfigOptSecret(mqtt_pass, "emonpimqtt2016", "mqtt_pass", "mp"),
new ConfigOptDefenition<String>(mqtt_feed_prefix, "", "mqtt_feed_prefix", "mfp"),

// Timer Settings, 16
// Timer Settings, 17
new ConfigOptDefenition<int>(timer_start1, 0, "timer_start1", "tsr1"),
new ConfigOptDefenition<int>(timer_stop1, 0, "timer_stop1", "tsp1"),
new ConfigOptDefenition<int>(timer_start2, 0, "timer_start2", "tsr2"),
Expand All @@ -112,10 +114,10 @@ ConfigOpt *opts[] =

new ConfigOptDefenition<String>(ctrl_mode, "Off", "ctrl_mode", "cm"),

// Flags, 23
// Flags, 24
&flagsOpt,

// Virtual Options, 24
// Virtual Options, 25
new ConfigOptVirtualBool(flagsOpt, CONFIG_SERVICE_EMONCMS, CONFIG_SERVICE_EMONCMS, "emoncms_enabled", "ee"),
new ConfigOptVirtualBool(flagsOpt, CONFIG_SERVICE_MQTT, CONFIG_SERVICE_MQTT, "mqtt_enabled", "me"),
new ConfigOptVirtualBool(flagsOpt, CONFIG_CTRL_UPDATE, CONFIG_CTRL_UPDATE, "ctrl_update", "ce"),
Expand Down Expand Up @@ -226,8 +228,8 @@ void config_set(const char *name, double val)
config.set(name, val);
}

void config_save_emoncms(bool enable, String server, String path, String node, String apikey,
String fingerprint)
void config_save_emoncms(bool enable, String server, int port, String path, String node,
String apikey, String fingerprint)
{
uint32_t newflags = flags & ~CONFIG_SERVICE_EMONCMS;
if (enable)
Expand All @@ -236,6 +238,7 @@ void config_save_emoncms(bool enable, String server, String path, String node, S
}

config.set(F("emoncms_server"), server);
config.set(F("emoncms_port"), port);
config.set(F("emoncms_path"), path);
config.set(F("emoncms_node"), node);
config.set(F("emoncms_apikey"), apikey);
Expand Down
3 changes: 2 additions & 1 deletion src/app_config.h
Expand Up @@ -52,6 +52,7 @@ extern String www_password;

// EMONCMS SERVER strings
extern String emoncms_server;
extern int emoncms_port;
extern String emoncms_path;
extern String emoncms_node;
extern String emoncms_apikey;
Expand Down Expand Up @@ -115,7 +116,7 @@ extern void config_load_v1_settings();
// -------------------------------------------------------------------
// Save the EmonCMS server details
// -------------------------------------------------------------------
extern void config_save_emoncms(bool enable, String server, String path, String node, String apikey, String fingerprint);
extern void config_save_emoncms(bool enable, String server, int port, String path, String node, String apikey, String fingerprint);

// -------------------------------------------------------------------
// Save the MQTT broker details
Expand Down
3 changes: 2 additions & 1 deletion src/autoauth.cpp
Expand Up @@ -51,7 +51,8 @@ void auth_request() {
int stringpart = 0;

// This needs to be done with an encrypted request otherwise credentials are shared as plain text
result = get_http(mqtt_server.c_str(), url);
// TODO: we are going to mqtt host, which port should we be using?
result = get_http(mqtt_server.c_str(), url, 0);
if (result != F("request registered")) {
for (int i=0; i<result.length(); i++) {
char c = result[i];
Expand Down
7 changes: 3 additions & 4 deletions src/emoncms.cpp
Expand Up @@ -98,19 +98,18 @@ void emoncms_publish(JsonDocument &data)

// Send data to Emoncms server
String result = "";
if (emoncms_fingerprint.isEmpty())
if (!emoncms_fingerprint.isEmpty())
{
// HTTPS on port 443 if HTTPS fingerprint is present
DBUGLN(F("HTTPS Enabled"));
result =
get_https(emoncms_fingerprint.c_str(), emoncms_server.c_str(), url,
443);
get_https(emoncms_fingerprint.c_str(), emoncms_server.c_str(), url, emoncms_port);
}
else
{
// Plain HTTP if other emoncms server e.g EmonPi
DBUGLN(F("Plain old HTTP"));
result = get_http(emoncms_server.c_str(), url);
result = get_http(emoncms_server.c_str(), url, emoncms_port);
}

const size_t capacity = JSON_OBJECT_SIZE(2) + result.length();
Expand Down
13 changes: 6 additions & 7 deletions src/http.cpp
Expand Up @@ -37,15 +37,14 @@ static String get_http_internal(WiFiClient &client, const char *host, String &pa

http.begin(client, host, port, path, secure);
int httpCode = http.GET();
String payload = http.getString();
http.end();
if((httpCode > 0) && (httpCode == HTTP_CODE_OK))
{
String payload = http.getString();
DEBUG.println(payload);
http.end();
return(payload);
} else {
http.end();
return(String(F("server error: "))+http.errorToString(httpCode));
return(String(F("server error: "))+http.errorToString(httpCode)+payload);
}
}

Expand All @@ -64,14 +63,14 @@ String get_https(const char* fingerprint, const char* host, String &path, int ht
return F("Invalid fingerprint");
}

return get_http_internal(*client, host, path, httpsPort, true);
return get_http_internal(*client, host, path, httpsPort ? httpsPort : 443, true);
}

// -------------------------------------------------------------------
// HTTP GET Request
// url: N/A
// -------------------------------------------------------------------
String get_http(const char *host, String &path){
String get_http(const char *host, String &path, int httpPort){
WiFiClient client;
return get_http_internal(client, host, path, 80, false);
return get_http_internal(client, host, path, httpPort ? httpPort : 80, false);
} // end http_get
2 changes: 1 addition & 1 deletion src/http.h
Expand Up @@ -42,6 +42,6 @@ extern String get_https(const char* fingerprint, const char* host, String &path,
// HTTP GET Request
// url: N/A
// -------------------------------------------------------------------
extern String get_http(const char* host, String &path);
extern String get_http(const char* host, String &path, int httpPort);

#endif // _EMONESP_HTTP_H
12 changes: 11 additions & 1 deletion src/web_server.cpp
Expand Up @@ -308,16 +308,26 @@ void handleSaveEmoncms(AsyncWebServerRequest *request)
return;
}

int port = 0;
AsyncWebParameter *portParm = request->getParam(F("port"), true);
DBUGVAR((uint32_t)portParm);
if (nullptr != portParm)
{
port = portParm->value().toInt();
}

config_save_emoncms(isPositive(request->arg(F("enable"))),
request->arg(F("server")),
port,
request->arg(F("path")),
request->arg(F("node")),
request->arg(F("apikey")),
request->arg(F("fingerprint")));

char tmpStr[200];
snprintf_P(tmpStr, sizeof(tmpStr), PSTR("Saved: %s %s %s %s %s"),
snprintf_P(tmpStr, sizeof(tmpStr), PSTR("Saved: %s %d %s %s %s %s"),
emoncms_server.c_str(),
port,
emoncms_path.c_str(),
emoncms_node.c_str(),
emoncms_apikey.c_str(),
Expand Down