Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions data/inverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ var paramsCache = {
var inverter = {

firmwareVersion: 0,
uiHooks: {
onCommunicationError: function(_show) {},
onCanMappingError: function(_message, _status) {},
onSetParamProgress: function(_key, _value) {},
onSetParamReply: function(_reply, _key, _value) {},
onSetParamScroll: function() {},
onRequestError: function(_requestName) {}
},

setUiHooks: function(hooks)
{
inverter.uiHooks = Object.assign(inverter.uiHooks, hooks || {});
},

updateCommunicationState: function(status)
{
if (status != 200) {
paramsCache.failedFetchCount += 1;
}
else {
paramsCache.failedFetchCount = 0;
}

inverter.uiHooks.onCommunicationError(paramsCache.failedFetchCount >= 2);
},

/** @brief send a command to the inverter */
sendCmd: function(cmd, replyFunc, repeat)
Expand All @@ -80,18 +105,7 @@ var inverter = {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
console.log(req + ": " + xmlhttp.status);
if (xmlhttp.status != 200) {
paramsCache.failedFetchCount += 1;
if ( paramsCache.failedFetchCount >= 2 && typeof ui !== 'undefined'){
ui.showCommunicationErrorBar();
}
}
else {
paramsCache.failedFetchCount = 0;
}
if ( paramsCache.failedFetchCount < 2 && typeof ui !== 'undefined') {
ui.hideCommunicationErrorBar();
}
inverter.updateCommunicationState(xmlhttp.status);
}
}

Expand Down Expand Up @@ -135,10 +149,7 @@ var inverter = {
xmlhttp.onload = function()
{
if (xmlhttp.status != 200) {
var messageBox = document.getElementById("message");
if (messageBox) {
messageBox.innerHTML = this.responseText;
}
inverter.uiHooks.onCanMappingError(this.responseText, xmlhttp.status);
return;
}

Expand All @@ -148,18 +159,7 @@ var inverter = {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
console.log(req + ": " + xmlhttp.status);
if (xmlhttp.status != 200) {
paramsCache.failedFetchCount += 1;
if ( paramsCache.failedFetchCount >= 2 && typeof ui !== 'undefined'){
ui.showCommunicationErrorBar();
}
}
else {
paramsCache.failedFetchCount = 0;
}
if ( paramsCache.failedFetchCount < 2 && typeof ui !== 'undefined') {
ui.hideCommunicationErrorBar();
}
inverter.updateCommunicationState(xmlhttp.status);
}
}

Expand Down Expand Up @@ -252,11 +252,10 @@ var inverter = {
if (index < keys.length)
{
var key = keys[index];
modal.appendToModal('large', "Setting " + key + " to " + params[key] + "<br>");
inverter.uiHooks.onSetParamProgress(key, params[key]);
inverter.sendCmd("set " + key + " " + params[key], function(reply) {
modal.appendToModal('large', reply + "<br>");
// auto-scroll text in modal as it is added
modal.largeModalScrollToBottom();
inverter.uiHooks.onSetParamReply(reply, key, params[key]);
inverter.uiHooks.onSetParamScroll();
inverter.setParam(params, index + 1);
});
}
Expand All @@ -275,7 +274,7 @@ var inverter = {
}
filesRequest.onerror = function()
{
alert("error");
inverter.uiHooks.onRequestError("getFiles");
}
filesRequest.open("GET", "/list", true);
filesRequest.send();
Expand All @@ -292,7 +291,7 @@ var inverter = {
}
deleteFileRequest.onerror = function()
{
alert("error");
inverter.uiHooks.onRequestError("deleteFile");
}
deleteFileRequest.open("DELETE", "/edit?f=" + filename, true);
deleteFileRequest.send();
Expand Down
35 changes: 35 additions & 0 deletions data/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,39 @@ var ui = {
}
},

bindInverterUiHooks: function()
{
inverter.setUiHooks({
onCommunicationError: function(show) {
if (show) {
ui.showCommunicationErrorBar();
}
else {
ui.hideCommunicationErrorBar();
}
},
onCanMappingError: function(message, _status) {
var messageBox = document.getElementById("message");
if (messageBox) {
messageBox.innerHTML = message;
}
},
onSetParamProgress: function(key, value) {
modal.appendToModal('large', "Setting " + key + " to " + value + "<br>");
},
onSetParamReply: function(reply, _key, _value) {
modal.appendToModal('large', reply + "<br>");
},
onSetParamScroll: function() {
// auto-scroll text in modal as it is added
modal.largeModalScrollToBottom();
},
onRequestError: function(_requestName) {
alert("error");
}
});
},

/** @brief switch to a different page tab */
openPage: function(pageName, elmnt, color)
{
Expand Down Expand Up @@ -157,6 +190,8 @@ var ui = {
/** @brief excutes when page finished loading. Creates tables and chart */
onLoad: function()
{
ui.bindInverterUiHooks();

// Set up listener to execute commands when enter is pressed (dashboard, command box)
var commandinput = document.getElementById('commandinput');
commandinput.addEventListener("keyup", function(event)
Expand Down
22 changes: 19 additions & 3 deletions esp32-web-interface.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,21 @@
#include "src/oi_can.h"
#include "src/config.h"

#ifdef FOCCCI_LOGGING
#define DBG_OUTPUT_PORT Serial2
#define INVERTER_PORT UART_NUM_0
#define INVERTER_RX 3
#define INVERTER_TX 1
#define DBG_BAUD 921600
#define INVERTER_BAUD 921600
#else
#define DBG_OUTPUT_PORT Serial
#define INVERTER_PORT UART_NUM_2
#define INVERTER_RX 16
#define INVERTER_TX 17
#define DBG_BAUD 115200
#define INVERTER_BAUD 115200
#endif
#define UART_TIMEOUT (100 / portTICK_PERIOD_MS)
#define UART_MESSBUF_SIZE 100
#ifndef LED_BUILTIN
Expand Down Expand Up @@ -747,12 +758,12 @@ void staCheck(){
}

void setup(void){
DBG_OUTPUT_PORT.begin(921600);
DBG_OUTPUT_PORT.begin(DBG_BAUD);
//Inverter.setRxBufferSize(50000);
//Inverter.begin(115200, SERIAL_8N1, INVERTER_RX, INVERTER_TX);
//Need to use low level Espressif IDF API instead of Serial to get high enough data rates
uart_config_t uart_config = {
.baud_rate = 921600,
.baud_rate = INVERTER_BAUD,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
Expand Down Expand Up @@ -884,8 +895,11 @@ void binaryLoggingStart()
{
if(createNextSDFile())
{
#ifdef FOCCCI_LOGGING
//In FOCCCI mode data is received directly on INVERTER_PORT; no inverter commands needed
fastLoggingActive = true;
return;
#endif
sendCommand(""); //flush out buffer in case just had power up
delay(10);
sendCommand("binarylogging 1"); //send start logging command to inverter
Expand Down Expand Up @@ -914,10 +928,11 @@ void binaryLoggingStart()

void binaryLoggingStop()
{
#ifdef FOCCCI_LOGGING
dataFile.flush(); //make sure up to date
dataFile.close();
fastLoggingActive = false;
return;
#else
uart_write_bytes(INVERTER_PORT, "\n", 1);
delay(1);
uart_write_bytes(INVERTER_PORT, "binarylogging 0", strlen("binarylogging 0"));
Expand All @@ -943,6 +958,7 @@ void binaryLoggingStop()
}
delay(10);
uart_flush(INVERTER_PORT);
#endif
}

void loop(void){
Expand Down
14 changes: 14 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ lib_deps =
bblanchon/ArduinoJson@^7.1.0
bblanchon/StreamUtils@^1.7.3
SPI

[env:foccci_logging]
build_flags =
${env.build_flags}
-D RELEASE
-D FOCCCI_LOGGING
-Wall -Werror
build_type = release
lib_deps =
fbiego/ESP32Time@^2.0.0
adafruit/RTClib@^2.1.1
bblanchon/ArduinoJson@^7.1.0
bblanchon/StreamUtils@^1.7.3
SPI