mirror of https://github.com/sfeakes/AqualinkD.git
More serial logging info
parent
f9f00efd14
commit
816e0ce125
53
aq_serial.c
53
aq_serial.c
|
@ -35,13 +35,14 @@ static struct termios _oldtio;
|
|||
|
||||
|
||||
|
||||
void log_packet(char *init_str, unsigned char* packet, int length)
|
||||
void log_packet(int level, char *init_str, unsigned char* packet, int length)
|
||||
{
|
||||
if ( getLogLevel() < LOG_DEBUG_SERIAL) {
|
||||
|
||||
if ( getLogLevel() <= level) {
|
||||
//logMessage(LOG_INFO, "Send '0x%02hhx'|'0x%02hhx' to controller\n", packet[5] ,packet[6]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int cnt;
|
||||
int i;
|
||||
char buff[MAXLEN];
|
||||
|
@ -53,7 +54,8 @@ void log_packet(char *init_str, unsigned char* packet, int length)
|
|||
cnt += sprintf(buff+cnt, "0x%02hhx|",packet[i]);
|
||||
|
||||
cnt += sprintf(buff+cnt, "\n");
|
||||
logMessage(LOG_DEBUG_SERIAL, buff);
|
||||
logMessage(level, buff);
|
||||
//logMessage(LOG_DEBUG_SERIAL, buff);
|
||||
/*
|
||||
int i;
|
||||
char temp_string[64];
|
||||
|
@ -133,11 +135,6 @@ int init_serial_port(char* tty)
|
|||
|
||||
logMessage(LOG_DEBUG_SERIAL, "Openeded serial port %s\n",tty);
|
||||
|
||||
// Need to change this to flock, but that depends on good exit.
|
||||
if ( ioctl(fd, TIOCEXCL) != 0) {
|
||||
displayLastSystemError("Locking serial port.");
|
||||
}
|
||||
|
||||
#ifdef BLOCKING_MODE
|
||||
fcntl(fd, F_SETFL, 0);
|
||||
newtio.c_cc[VMIN]= 1;
|
||||
|
@ -150,7 +147,30 @@ int init_serial_port(char* tty)
|
|||
newtio.c_cc[VTIME]= 1;
|
||||
logMessage(LOG_DEBUG_SERIAL, "Set serial port %s to non blocking mode\n",tty);
|
||||
#endif
|
||||
/*
|
||||
// Need to change this to flock, but that depends on good exit.
|
||||
if ( ioctl(fd, TIOCEXCL) != 0) {
|
||||
displayLastSystemError("Locking serial port.");
|
||||
}
|
||||
|
||||
struct flock fl;
|
||||
fl.l_type = F_WRLCK;
|
||||
fl.l_whence = SEEK_SET;
|
||||
fl.l_start = 0;
|
||||
fl.l_len = 0;
|
||||
|
||||
if (fcntl(fd, F_SETLK, &fl) == -1)
|
||||
{
|
||||
if (errno == EACCES || errno == EAGAIN)
|
||||
{
|
||||
displayLastSystemError("Locking serial port.");
|
||||
}
|
||||
else
|
||||
{
|
||||
displayLastSystemError("Unknown error Locking serial port.");
|
||||
}
|
||||
}
|
||||
*/
|
||||
tcgetattr(fd, &_oldtio); // save current port settings
|
||||
// set new port settings for canonical input processing
|
||||
newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
|
||||
|
@ -259,7 +279,7 @@ void send_test_cmd(int fd, unsigned char destination, unsigned char b1, unsigned
|
|||
//logMessage(LOG_DEBUG, "Send '0x%02hhx' to '0x%02hhx'\n", command, destination);
|
||||
#endif
|
||||
|
||||
log_packet("Sent ", ackPacket, length);
|
||||
log_packet(LOG_DEBUG_SERIAL, "Sent ", ackPacket, length);
|
||||
|
||||
}
|
||||
void send_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3)
|
||||
|
@ -292,7 +312,7 @@ void send_command(int fd, unsigned char destination, unsigned char b1, unsigned
|
|||
if ( getLogLevel() >= LOG_DEBUG_SERIAL) {
|
||||
char buf[30];
|
||||
sprintf(buf, "Sent %8.8s ", get_packet_type(ackPacket+1 , length));
|
||||
log_packet(buf, ackPacket, length);
|
||||
log_packet(LOG_DEBUG_SERIAL, buf, ackPacket, length);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,7 +345,7 @@ void send_messaged(int fd, unsigned char destination, char *message)
|
|||
//logMessage(LOG_DEBUG, "Send '0x%02hhx' to '0x%02hhx'\n", command, destination);
|
||||
#endif
|
||||
|
||||
log_packet("Sent ", msgPacket, length);
|
||||
log_packet(LOG_DEBUG_SERIAL, "Sent ", msgPacket, length);
|
||||
}
|
||||
|
||||
void _send_ack(int fd, unsigned char ack_type, unsigned char command);
|
||||
|
@ -384,7 +404,7 @@ void _send_ack(int fd, unsigned char ack_type, unsigned char command)
|
|||
//tcdrain(fd);
|
||||
#endif
|
||||
|
||||
log_packet("Sent ", ackPacket, length);
|
||||
log_packet(LOG_DEBUG_SERIAL, "Sent ", ackPacket, length);
|
||||
}
|
||||
|
||||
// Reads the bytes of the next incoming packet, and
|
||||
|
@ -413,7 +433,8 @@ int get_packet(int fd, unsigned char* packet)
|
|||
} else if (bytesRead < 0 && errno == EAGAIN) {
|
||||
// If we are in the middle of reading a packet, keep going
|
||||
if (retry > 20) {
|
||||
logMessage(LOG_WARNING, "Serial read timeout\n");
|
||||
logMessage(LOG_WARNING, "Serial read timeout\n");
|
||||
log_packet(LOG_WARNING, "Bad receive packet ", packet, index);
|
||||
return 0;
|
||||
}
|
||||
retry++;
|
||||
|
@ -489,11 +510,11 @@ int get_packet(int fd, unsigned char* packet)
|
|||
|
||||
if (generate_checksum(packet, index) != packet[index-3]){
|
||||
logMessage(LOG_WARNING, "Serial read bad checksum, ignoring\n");
|
||||
log_packet("Bad packet ", packet, index);
|
||||
log_packet(LOG_WARNING, "Bad receive packet ", packet, index);
|
||||
return 0;
|
||||
} else if (index < AQ_MINPKTLEN) {
|
||||
logMessage(LOG_WARNING, "Serial read too small\n");
|
||||
log_packet("Bad packet ", packet, index);
|
||||
log_packet(LOG_WARNING, "Bad receive packet ", packet, index);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
17
aq_serial.h
17
aq_serial.h
|
@ -116,8 +116,23 @@
|
|||
|
||||
|
||||
/* AQUAPURE SWG */
|
||||
// These are madeup.
|
||||
#define SWG_STATUS_OFF 0xFF
|
||||
#define SWG_STATUS_UNKNOWN -128
|
||||
// These are actual from RS485
|
||||
|
||||
#define SWG_STATUS_ON 0x00
|
||||
#define SWG_STATUS_NO_FLOW 0x01 // no flow 0x01
|
||||
#define SWG_STATUS_LOW_SALT 0x02 // low salt 0x02
|
||||
#define SWG_STATUS_VLOW_SALT 0x04 // very low salt 0x04
|
||||
#define SWG_STATUS_HIGH_CURRENT 0x08 // high current 0x08
|
||||
#define SWG_STATUS_TURNING_OFF 0x09 // turning off 0x09
|
||||
#define SWG_STATUS_CLEAN_CELL 0x10 // clean cell 0x10
|
||||
#define SWG_STATUS_LOW_VOLTS 0x20 // low voltage 0x20
|
||||
#define SWG_STATUS_LOW_TEMP 0x40 // low watertemp 0x40
|
||||
#define SWG_STATUS_CHECK_PCB 0x80 // check PCB 0x80
|
||||
|
||||
|
||||
#define SWG_STATUS_OFF 0xFF
|
||||
|
||||
typedef enum {
|
||||
ON,
|
||||
|
|
|
@ -345,7 +345,7 @@ void mqtt_broadcast_aqualinkstate(struct mg_connection *nc)
|
|||
send_mqtt_string_msg(nc, FREEZE_PROTECT_ENABELED, MQTT_OFF);*/
|
||||
}
|
||||
|
||||
if (_aqualink_data->ar_swg_status == 0x00) { // If the SWG is actually on
|
||||
if (_aqualink_data->ar_swg_status == SWG_STATUS_ON) { // If the SWG is actually on
|
||||
if (_aqualink_data->swg_percent != TEMP_UNKNOWN && (force_update || _aqualink_data->swg_percent != _last_mqtt_aqualinkdata.swg_percent)) {
|
||||
_last_mqtt_aqualinkdata.swg_percent = _aqualink_data->swg_percent;
|
||||
send_mqtt_numeric_msg(nc, SWG_PERCENT_TOPIC, _aqualink_data->swg_percent);
|
||||
|
@ -358,6 +358,7 @@ void mqtt_broadcast_aqualinkstate(struct mg_connection *nc)
|
|||
send_domoticz_mqtt_numeric_msg(nc, _aqualink_config->dzidx_swg_ppm, _aqualink_data->swg_ppm);
|
||||
}
|
||||
}
|
||||
|
||||
if (_aqualink_data->ar_swg_status != _last_mqtt_aqualinkdata.ar_swg_status) {
|
||||
if (_aqualink_data->ar_swg_status == SWG_STATUS_OFF)
|
||||
send_mqtt_int_msg(nc, SWG_ENABELED_TOPIC, SWG_OFF);
|
||||
|
@ -366,61 +367,61 @@ void mqtt_broadcast_aqualinkstate(struct mg_connection *nc)
|
|||
|
||||
switch (_aqualink_data->ar_swg_status) {
|
||||
// Level = (0=gray, 1=green, 2=yellow, 3=orange, 4=red)
|
||||
case 0x00:
|
||||
case SWG_STATUS_ON:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure ON");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 1, "GENERATING CHLORINE");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_ON);
|
||||
break;
|
||||
case 0x01:
|
||||
case SWG_STATUS_NO_FLOW:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure No Flow");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 2, "NO FLOW");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_OFF);
|
||||
break;
|
||||
case 0x02:
|
||||
case SWG_STATUS_LOW_SALT:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure Low Salt");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 2, "LOW SALT");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_ON);
|
||||
break;
|
||||
case 0x04:
|
||||
case SWG_STATUS_VLOW_SALT:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure Very No flow");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 3, "VERY LOW SALT");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_OFF);
|
||||
break;
|
||||
case 0x08:
|
||||
case SWG_STATUS_HIGH_CURRENT:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure High Current");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 4, "HIGH CURRENT");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_ON);
|
||||
break;
|
||||
case 0x09:
|
||||
case SWG_STATUS_TURNING_OFF:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure Turning Off");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 0, "TURNING OFF");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_OFF);
|
||||
break;
|
||||
case 0x10:
|
||||
case SWG_STATUS_CLEAN_CELL:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure Clean Cell");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 2, "CLEAN CELL");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_ON);
|
||||
break;
|
||||
case 0x20:
|
||||
case SWG_STATUS_LOW_VOLTS:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure Low Voltage");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 3, "LOW VOLTAGE");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_ON);
|
||||
break;
|
||||
case 0x40:
|
||||
case SWG_STATUS_LOW_TEMP:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure Water Temp Low");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 2, "WATER TEMP LOW");
|
||||
send_mqtt_int_msg(nc, SWG_TOPIC, SWG_OFF);
|
||||
break;
|
||||
case 0x80:
|
||||
case SWG_STATUS_CHECK_PCB:
|
||||
if (!_aqualink_data->simulate_panel)
|
||||
sprintf(_aqualink_data->last_display_message, "AquaPure Check PCB");
|
||||
send_domoticz_mqtt_status_message(nc, _aqualink_config->dzidx_swg_status, 4, "CHECK PCB");
|
||||
|
@ -1062,6 +1063,7 @@ void start_mqtt(struct mg_mgr *mgr) {
|
|||
for (i=0; i < TOTAL_BUTTONS; i++) {
|
||||
_last_mqtt_aqualinkdata.aqualinkleds[i].state = LED_S_UNKNOWN;
|
||||
}
|
||||
_last_mqtt_aqualinkdata.ar_swg_status = SWG_STATUS_UNKNOWN;
|
||||
_mqtt_exit_flag = false; // set here to stop multiple connects, if it fails truley fails it will get set to false.
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue