Skip to content

Commit

Permalink
update for gpio monitor/control from anywhere with mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
suraji committed Aug 1, 2020
1 parent 5e42aef commit 59050a4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=esp8266-framework
version=1.1.2
version=1.1.3
author=Suraj I.
maintainer=Suraj I. <[email protected]>
sentence=esp8266 framework stack for easy configurable applications
Expand Down
3 changes: 2 additions & 1 deletion src/service_provider/GpioServiceProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ void GpioServiceProvider::applyGpioJsonPayload( char* _payload, uint16_t _payloa
uint16_t _value_limit = _mode == ANALOG_WRITE ? ANALOG_GPIO_RESOLUTION : 2;
this->gpio_config_copy.gpio_mode[_pin] = _mode < ANALOG_READ ? _mode : this->gpio_config_copy.gpio_mode[_pin];
this->gpio_config_copy.gpio_readings[_pin] = _value < _value_limit ? _value : this->gpio_config_copy.gpio_readings[_pin];
this->enable_update_gpio_table_from_copy();
__database_service.set_gpio_config_table(&this->gpio_config_copy);
this->handleGpioModes();
}
}
}
Expand Down
94 changes: 47 additions & 47 deletions src/webserver/controllers/GPIOController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GpioController : public Controller {
/**
* @var gpio_config_table gpio_configs
*/
gpio_config_table gpio_configs;
// gpio_config_table gpio_configs;

/**
* @var last_gpio_monitor_point _last_monitor_point
Expand All @@ -52,7 +52,7 @@ class GpioController : public Controller {
*/
void boot( void ){

this->gpio_configs = this->web_resource->db_conn->get_gpio_config_table();
// this->gpio_configs = this->web_resource->db_conn->get_gpio_config_table();
this->route_handler->register_route( EW_SERVER_GPIO_MANAGE_CONFIG_ROUTE, [&]() { this->handleGpioManageRoute(); }, AUTH_MIDDLEWARE );
this->route_handler->register_route( EW_SERVER_GPIO_SERVER_CONFIG_ROUTE, [&]() { this->handleGpioServerConfigRoute(); }, AUTH_MIDDLEWARE );
this->route_handler->register_route( EW_SERVER_GPIO_MODE_CONFIG_ROUTE, [&]() { this->handleGpioModeConfigRoute(); }, AUTH_MIDDLEWARE );
Expand Down Expand Up @@ -202,10 +202,10 @@ class GpioController : public Controller {
strcat_P( _page, EW_SERVER_GPIO_SERVER_PAGE_TOP );

char _port[10],_freq[10];memset( _port, 0, 10 );memset( _freq, 0, 10 );
__appendUintToBuff( _port, "%d", this->gpio_configs.gpio_port, 8 );
__appendUintToBuff( _freq, "%d", this->gpio_configs.gpio_post_frequency, 8 );
__appendUintToBuff( _port, "%d", __gpio_service.gpio_config_copy.gpio_port, 8 );
__appendUintToBuff( _freq, "%d", __gpio_service.gpio_config_copy.gpio_post_frequency, 8 );

concat_tr_input_html_tags( _page, PSTR("Host Address:"), PSTR("hst"), this->gpio_configs.gpio_host, GPIO_HOST_BUF_SIZE-1 );
concat_tr_input_html_tags( _page, PSTR("Host Address:"), PSTR("hst"), __gpio_service.gpio_config_copy.gpio_host, GPIO_HOST_BUF_SIZE-1 );
concat_tr_input_html_tags( _page, PSTR("Host Port:"), PSTR("prt"), _port );
concat_tr_input_html_tags( _page, PSTR("Post Frequency:"), PSTR("frq"), _freq );

Expand Down Expand Up @@ -241,10 +241,10 @@ class GpioController : public Controller {
Logln();
#endif

_gpio_host.toCharArray( this->gpio_configs.gpio_host, _gpio_host.length()+1 );
this->gpio_configs.gpio_port = (int)_gpio_port.toInt();
this->gpio_configs.gpio_post_frequency = (int)_post_freq.toInt() < 0 ? GPIO_DATA_POST_FREQ : (int)_post_freq.toInt();
this->web_resource->db_conn->set_gpio_config_table( &this->gpio_configs );
_gpio_host.toCharArray( __gpio_service.gpio_config_copy.gpio_host, _gpio_host.length()+1 );
__gpio_service.gpio_config_copy.gpio_port = (int)_gpio_port.toInt();
__gpio_service.gpio_config_copy.gpio_post_frequency = (int)_post_freq.toInt() < 0 ? GPIO_DATA_POST_FREQ : (int)_post_freq.toInt();
this->web_resource->db_conn->set_gpio_config_table( &__gpio_service.gpio_config_copy );

_is_posted = true;
}
Expand Down Expand Up @@ -282,9 +282,9 @@ class GpioController : public Controller {
_name[1] = (0x30 + _pin );_label[1] = (0x30 + _pin );
_exception = _pin == 0 ? 3:-1;
if( !__gpio_service.is_exceptional_gpio_pin(_pin) )
concat_tr_select_html_tags( _page, _name, _label, _gpio_mode_general_options, 4, (int)this->gpio_configs.gpio_mode[_pin], _exception );
concat_tr_select_html_tags( _page, _name, _label, _gpio_mode_general_options, 4, (int)__gpio_service.gpio_config_copy.gpio_mode[_pin], _exception );
}
concat_tr_select_html_tags( _page, (char*)"A0:", (char*)"a0", _gpio_mode_analog_options, 5, (int)this->gpio_configs.gpio_mode[MAX_NO_OF_GPIO_PINS] );
concat_tr_select_html_tags( _page, (char*)"A0:", (char*)"a0", _gpio_mode_analog_options, 5, (int)__gpio_service.gpio_config_copy.gpio_mode[MAX_NO_OF_GPIO_PINS] );

strcat_P( _page, EW_SERVER_WIFI_CONFIG_PAGE_BOTTOM );
if( _enable_flash )
Expand All @@ -310,29 +310,29 @@ class GpioController : public Controller {
char _label[6]; memset(_label, 0, 6); strcpy( _label, "d0" );
for (uint8_t _pin = 0; _pin < MAX_NO_OF_GPIO_PINS; _pin++) {
_label[1] = (0x30 + _pin );
this->gpio_configs.gpio_mode[_pin] = !__gpio_service.is_exceptional_gpio_pin(_pin) ? (int)this->web_resource->server->arg(_label).toInt():0;
__gpio_service.gpio_config_copy.gpio_mode[_pin] = !__gpio_service.is_exceptional_gpio_pin(_pin) ? (int)this->web_resource->server->arg(_label).toInt():0;
#ifdef EW_SERIAL_LOG
Log(F("Pin ")); Log( _pin ); Log(F(" : ")); Logln( (int)this->web_resource->server->arg(_label).toInt() );
#endif
if( this->gpio_configs.gpio_mode[_pin] == OFF || this->gpio_configs.gpio_mode[_pin] == DIGITAL_WRITE || this->gpio_configs.gpio_mode[_pin] == ANALOG_WRITE ){
this->gpio_configs.gpio_alert_comparator[_pin] = EQUAL;
this->gpio_configs.gpio_alert_channel[_pin] = NO_ALERT;
this->gpio_configs.gpio_alert_values[_pin] = OFF;
if( __gpio_service.gpio_config_copy.gpio_mode[_pin] == OFF || __gpio_service.gpio_config_copy.gpio_mode[_pin] == DIGITAL_WRITE || __gpio_service.gpio_config_copy.gpio_mode[_pin] == ANALOG_WRITE ){
__gpio_service.gpio_config_copy.gpio_alert_comparator[_pin] = EQUAL;
__gpio_service.gpio_config_copy.gpio_alert_channel[_pin] = NO_ALERT;
__gpio_service.gpio_config_copy.gpio_alert_values[_pin] = OFF;
}
}
this->gpio_configs.gpio_mode[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("a0").toInt();
if( this->gpio_configs.gpio_mode[MAX_NO_OF_GPIO_PINS] == OFF ){
this->gpio_configs.gpio_alert_comparator[MAX_NO_OF_GPIO_PINS] = EQUAL;
this->gpio_configs.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] = NO_ALERT;
this->gpio_configs.gpio_alert_values[MAX_NO_OF_GPIO_PINS] = OFF;
__gpio_service.gpio_config_copy.gpio_mode[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("a0").toInt();
if( __gpio_service.gpio_config_copy.gpio_mode[MAX_NO_OF_GPIO_PINS] == OFF ){
__gpio_service.gpio_config_copy.gpio_alert_comparator[MAX_NO_OF_GPIO_PINS] = EQUAL;
__gpio_service.gpio_config_copy.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] = NO_ALERT;
__gpio_service.gpio_config_copy.gpio_alert_values[MAX_NO_OF_GPIO_PINS] = OFF;
}

#ifdef EW_SERIAL_LOG
Log(F("Pin A: ")); Logln( this->gpio_configs.gpio_mode[MAX_NO_OF_GPIO_PINS] );
Log(F("Pin A: ")); Logln( __gpio_service.gpio_config_copy.gpio_mode[MAX_NO_OF_GPIO_PINS] );
Logln();
#endif

this->web_resource->db_conn->set_gpio_config_table( &this->gpio_configs );
this->web_resource->db_conn->set_gpio_config_table( &__gpio_service.gpio_config_copy );

_is_posted = true;
}
Expand Down Expand Up @@ -370,14 +370,14 @@ class GpioController : public Controller {

if( !__gpio_service.is_exceptional_gpio_pin(_pin) ){

if( this->gpio_configs.gpio_mode[_pin] == DIGITAL_WRITE ){
if( __gpio_service.gpio_config_copy.gpio_mode[_pin] == DIGITAL_WRITE ){
_added_options = true;
concat_tr_select_html_tags( _page, _name, _label, _gpio_digital_write_options, 2, (int)this->gpio_configs.gpio_readings[_pin] );
concat_tr_select_html_tags( _page, _name, _label, _gpio_digital_write_options, 2, (int)__gpio_service.gpio_config_copy.gpio_readings[_pin] );
}
if( this->gpio_configs.gpio_mode[_pin] == ANALOG_WRITE ){
if( __gpio_service.gpio_config_copy.gpio_mode[_pin] == ANALOG_WRITE ){
_added_options = true;
memset( _analog_value, 0, 10 );
__appendUintToBuff( _analog_value, "%d", this->gpio_configs.gpio_readings[_pin], 8 );
__appendUintToBuff( _analog_value, "%d", __gpio_service.gpio_config_copy.gpio_readings[_pin], 8 );
concat_tr_input_html_tags( _page, _name, _label, _analog_value, 0, HTML_INPUT_RANGE_TAG_TYPE, false, false );
}
}
Expand Down Expand Up @@ -412,7 +412,7 @@ class GpioController : public Controller {
for (uint8_t _pin = 0; _pin < MAX_NO_OF_GPIO_PINS; _pin++) {
_label[1] = (0x30 + _pin );
if( this->web_resource->server->hasArg(_label) ){
this->gpio_configs.gpio_readings[_pin] = __gpio_service.is_exceptional_gpio_pin(_pin) ? 0 : this->gpio_configs.gpio_mode[_pin] == DIGITAL_WRITE ?
__gpio_service.gpio_config_copy.gpio_readings[_pin] = __gpio_service.is_exceptional_gpio_pin(_pin) ? 0 : __gpio_service.gpio_config_copy.gpio_mode[_pin] == DIGITAL_WRITE ?
(int)this->web_resource->server->arg(_label).toInt() : (int)this->web_resource->server->arg(_label).toInt();
#ifdef EW_SERIAL_LOG
Log(F("Pin ")); Log( _pin ); Log(F(" : ")); Logln( (int)this->web_resource->server->arg(_label).toInt() );
Expand All @@ -422,7 +422,7 @@ class GpioController : public Controller {
}

if( _is_posted )
this->web_resource->db_conn->set_gpio_config_table( &this->gpio_configs );
this->web_resource->db_conn->set_gpio_config_table( &__gpio_service.gpio_config_copy );
}

char* _page = new char[EW_HTML_MAX_SIZE];
Expand Down Expand Up @@ -465,21 +465,21 @@ class GpioController : public Controller {
for (uint8_t _pin = 0; _pin < MAX_NO_OF_GPIO_PINS; _pin++) {
_name[1] = (0x30 + _pin );_label[1] = (0x30 + _pin );_alert_label[2] = (0x30 + _pin );

if( !__gpio_service.is_exceptional_gpio_pin(_pin) && this->gpio_configs.gpio_mode[_pin] == DIGITAL_READ ){
if( !__gpio_service.is_exceptional_gpio_pin(_pin) && __gpio_service.gpio_config_copy.gpio_mode[_pin] == DIGITAL_READ ){

_added_options = true;
strcat_P( _page, HTML_TR_OPEN_TAG );
strcat_P( _page, HTML_TAG_CLOSE_BRACKET );
concat_td_select_html_tags( _page, _name, _label, _gpio_digital_alert_options, 2, (int)this->gpio_configs.gpio_alert_values[_pin] );
concat_td_select_html_tags( _page, (char*)" ? ", _alert_label, _gpio_alert_channels, 2, (int)this->gpio_configs.gpio_alert_channel[_pin] );
concat_td_select_html_tags( _page, _name, _label, _gpio_digital_alert_options, 2, (int)__gpio_service.gpio_config_copy.gpio_alert_values[_pin] );
concat_td_select_html_tags( _page, (char*)" ? ", _alert_label, _gpio_alert_channels, 2, (int)__gpio_service.gpio_config_copy.gpio_alert_channel[_pin] );
strcat_P( _page, HTML_TR_CLOSE_TAG );
}
}

if( this->gpio_configs.gpio_mode[MAX_NO_OF_GPIO_PINS] == ANALOG_READ ){
if( __gpio_service.gpio_config_copy.gpio_mode[MAX_NO_OF_GPIO_PINS] == ANALOG_READ ){
_added_options = true;
memset( _analog_value, 0, 10 );
__appendUintToBuff( _analog_value, "%d", this->gpio_configs.gpio_alert_values[MAX_NO_OF_GPIO_PINS], 8 );
__appendUintToBuff( _analog_value, "%d", __gpio_service.gpio_config_copy.gpio_alert_values[MAX_NO_OF_GPIO_PINS], 8 );
strcat_P( _page, HTML_TR_OPEN_TAG );
strcat_P( _page, HTML_TAG_CLOSE_BRACKET );
strcat_P( _page, HTML_TD_OPEN_TAG );
Expand All @@ -490,10 +490,10 @@ class GpioController : public Controller {
strcat_P( _page, HTML_STYLE_ATTR );
strcat( _page, (char*)"'display:flex;'" );
strcat_P( _page, HTML_TAG_CLOSE_BRACKET );
concat_select_html_tag( _page, PSTR("a0"), _gpio_analog_alert_comparators, 3, (int)this->gpio_configs.gpio_alert_comparator[MAX_NO_OF_GPIO_PINS] );
concat_select_html_tag( _page, PSTR("a0"), _gpio_analog_alert_comparators, 3, (int)__gpio_service.gpio_config_copy.gpio_alert_comparator[MAX_NO_OF_GPIO_PINS] );
concat_input_html_tag( _page, PSTR("aval"), _analog_value );
strcat_P( _page, HTML_TD_CLOSE_TAG );
concat_td_select_html_tags( _page, (char*)" ? ", (char*)"anlt0", _gpio_alert_channels, 2, (int)this->gpio_configs.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] );
concat_td_select_html_tags( _page, (char*)" ? ", (char*)"anlt0", _gpio_alert_channels, 2, (int)__gpio_service.gpio_config_copy.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] );
strcat_P( _page, HTML_TR_CLOSE_TAG );
}

Expand Down Expand Up @@ -525,33 +525,33 @@ class GpioController : public Controller {
_label[1] = (0x30 + _pin ); _alert_label[2] = (0x30 + _pin );
if( this->web_resource->server->hasArg(_label) && this->web_resource->server->hasArg(_alert_label) ){

this->gpio_configs.gpio_alert_comparator[_pin] = EQUAL;
this->gpio_configs.gpio_alert_values[_pin] = (int)this->web_resource->server->arg(_label).toInt();
this->gpio_configs.gpio_alert_channel[_pin] = (int)this->web_resource->server->arg(_alert_label).toInt();
__gpio_service.gpio_config_copy.gpio_alert_comparator[_pin] = EQUAL;
__gpio_service.gpio_config_copy.gpio_alert_values[_pin] = (int)this->web_resource->server->arg(_label).toInt();
__gpio_service.gpio_config_copy.gpio_alert_channel[_pin] = (int)this->web_resource->server->arg(_alert_label).toInt();
#ifdef EW_SERIAL_LOG
Log(F("Pin ")); Log( _pin );
Log(F(" : ")); Log( this->gpio_configs.gpio_alert_values[_pin] );
Log(F(" : ")); Logln( this->gpio_configs.gpio_alert_channel[_pin] );
Log(F(" : ")); Log( __gpio_service.gpio_config_copy.gpio_alert_values[_pin] );
Log(F(" : ")); Logln( __gpio_service.gpio_config_copy.gpio_alert_channel[_pin] );
#endif
_is_posted = true;
}
}

if( this->web_resource->server->hasArg("a0") && this->web_resource->server->hasArg("aval") && this->web_resource->server->hasArg("anlt0") ){

this->gpio_configs.gpio_alert_comparator[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("a0").toInt();
this->gpio_configs.gpio_alert_values[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("aval").toInt();
this->gpio_configs.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("anlt0").toInt();
__gpio_service.gpio_config_copy.gpio_alert_comparator[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("a0").toInt();
__gpio_service.gpio_config_copy.gpio_alert_values[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("aval").toInt();
__gpio_service.gpio_config_copy.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] = (int)this->web_resource->server->arg("anlt0").toInt();
#ifdef EW_SERIAL_LOG
Log(F("Pin A0"));
Log(F(" : ")); Log( this->gpio_configs.gpio_alert_values[MAX_NO_OF_GPIO_PINS] );
Log(F(" : ")); Logln( this->gpio_configs.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] );
Log(F(" : ")); Log( __gpio_service.gpio_config_copy.gpio_alert_values[MAX_NO_OF_GPIO_PINS] );
Log(F(" : ")); Logln( __gpio_service.gpio_config_copy.gpio_alert_channel[MAX_NO_OF_GPIO_PINS] );
#endif
_is_posted = true;
}

if( _is_posted )
this->web_resource->db_conn->set_gpio_config_table( &this->gpio_configs );
this->web_resource->db_conn->set_gpio_config_table( &__gpio_service.gpio_config_copy );
}

char* _page = new char[EW_HTML_MAX_SIZE];
Expand Down

0 comments on commit 59050a4

Please sign in to comment.