Skip to content

Commit

Permalink
#4: co-exist with PDA
Browse files Browse the repository at this point in the history
Introducing last active timer.  This will release control 5 seconds
after thread terminates until 30 seconds have expired and it's time to
do another status update.
  • Loading branch information
Lee_Ballard authored and ballle98 committed Jun 12, 2019
1 parent 9aa1aa7 commit 4665496
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
1 change: 1 addition & 0 deletions aq_programmer.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ void cleanAndTerminateThread(struct programmingThreadCtrl *threadCtrl)
delay(500);
threadCtrl->aq_data->active_thread.thread_id = 0;
threadCtrl->aq_data->active_thread.ptype = AQP_NULL;
time(&threadCtrl->aq_data->last_active_time);
threadCtrl->thread_id = 0;
free(threadCtrl);
pthread_exit(0);
Expand Down
1 change: 1 addition & 0 deletions aqualink.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct aqualinkdata
int open_websockets;
//bool last_msg_was_status;
//bool ar_swg_connected;
time_t last_active_time;
};


Expand Down
71 changes: 63 additions & 8 deletions aqualinkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ int main(int argc, char *argv[])
exit(EXIT_SUCCESS);
}

/*

void debugPacketPrint(unsigned char ID, unsigned char *packet_buffer, int packet_length)
{
char buff[1000];
Expand All @@ -824,9 +824,9 @@ void debugPacketPrint(unsigned char ID, unsigned char *packet_buffer, int packet
cnt += sprintf(buff + cnt, "\n");

//logMessage(LOG_NOTICE, "- AQUA SWG - \n%s", buff);
if (_config_parameters.debug_RSProtocol_packets)
writePacketLog(buff);
else
//if (_config_parameters.debug_RSProtocol_packets)
// writePacketLog(buff);
//else
logMessage(LOG_NOTICE, "%s", buff);
}

Expand All @@ -846,6 +846,7 @@ void debugPacket(unsigned char *packet_buffer, int packet_length)
lastID = packet_buffer[PKT_DEST];
}

/*
void logPacket(unsigned char *packet_buffer, int packet_length)
{
static unsigned char last_packet_buffer[AQ_MAXPKTLEN];
Expand Down Expand Up @@ -921,6 +922,56 @@ void caculate_ack_packet(int rs_fd, unsigned char *packet_buffer) {
send_ack(rs_fd, NUL);
else
send_ack(rs_fd, pop_aq_cmd(&_aqualink_data));
// :TODO: enable last active
#if 0
bool bPDA_in_standby = false;
if (_aqualink_data.active_thread.thread_id == 0)
{
time_t now;
time (&now);
if (difftime (now, _aqualink_data.last_active_time) > 65)
{
aq_programmer (AQ_PDA_DEVICE_STATUS, NULL,
&_aqualink_data);
}
else if (difftime (now, _aqualink_data.last_active_time) > 5)
{
bPDA_in_standby = true;
pda_m_clear ();
}
}
// Can only send command to status message on PDA.
if (_config_parameters.pda_mode == true
&& packet_buffer[PKT_CMD] == CMD_STATUS)
{
if ((_aqualink_data.active_thread.thread_id == 0)
&& bPDA_in_standby)
{
logMessage (LOG_DEBUG, "NO STATUS ACK\n");
}
else
{
send_ack (rs_fd, pop_aq_cmd (&_aqualink_data));
}
}
else if (_config_parameters.pda_mode == true
&& packet_buffer[PKT_CMD] == CMD_PROBE)
{
if ((_aqualink_data.active_thread.thread_id == 0)
&& bPDA_in_standby)
{
logMessage (LOG_DEBUG, "NO PROBE ACK\n");
}
else
{
send_ack (rs_fd, NUL);
}
}
else
{
send_ack(rs_fd, NUL);
}
#endif
}
} else { // We are in simlator mode, ack get's complicated now.
// If have a command to send, send a normal ack.
Expand Down Expand Up @@ -1004,6 +1055,7 @@ void main_loop()
_aqualink_data.service_mode_state = OFF;
_aqualink_data.battery = OK;
_aqualink_data.open_websockets = 0;
_aqualink_data.last_active_time = 0;

for (i=0; i < MAX_PUMPS; i++) {
_aqualink_data.pumps[i].rpm = TEMP_UNKNOWN;
Expand Down Expand Up @@ -1082,17 +1134,20 @@ void main_loop()
else if (packet_length > 0)
{
blank_read = 0;

if ((getLogLevel() >= LOG_DEBUG) && (packet_length > 0))
{
debugPacketPrint(packet_buffer[PKT_DEST], packet_buffer, packet_length);
}
if (_config_parameters.debug_RSProtocol_packets || getLogLevel() >= LOG_DEBUG_SERIAL)
logPacket(packet_buffer, packet_length);

if (packet_length > 0 && packet_buffer[PKT_DEST] == _config_parameters.device_id)
{

if (getLogLevel() >= LOG_DEBUG)
logMessage(LOG_DEBUG, "RS received packet of type %s length %d\n", get_packet_type(packet_buffer, packet_length), packet_length);
{
//logMessage(LOG_DEBUG, "RS received packet of type %s length %d\n", get_packet_type(packet_buffer, packet_length), packet_length);
}

//logMessage(LOG_DEBUG, "RS received packet of type %s length %d\n", get_packet_type(packet_buffer, packet_length), packet_length);
//debugPacketPrint(0x00, packet_buffer, packet_length);
//unsigned char ID, unsigned char *packet_buffer, int packet_length)

Expand Down
15 changes: 12 additions & 3 deletions pda_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ bool process_pda_menu_packet(unsigned char* packet, int length)
bool rtn = true;
switch (packet[PKT_CMD]) {
case CMD_PDA_CLEAR:
_hlightindex = -1;
memset(_menu, 0, PDA_LINES * (AQ_MSGLEN+1));
rtn = pda_m_clear();
break;
case CMD_MSG_LONG:
if (packet[PKT_DATA] < 10) {
Expand Down Expand Up @@ -258,4 +257,14 @@ bool NEW_process_pda_menu_packet_NEW(unsigned char* packet, int length)

return rtn;
}
#endif
#endif

/*
clear the menu
*/
bool pda_m_clear()
{
_hlightindex = -1;
memset(_menu, 0, PDA_LINES * (AQ_MSGLEN+1));
return true;
}
1 change: 1 addition & 0 deletions pda_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ pda_menu_type pda_m_type();
int pda_find_m_index(char *text);
int pda_find_m_index_case(char *text, int limit);
//int pda_find_m_index_swcase(char *text, int limit);
bool pda_m_clear();

#endif
10 changes: 8 additions & 2 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include <sys/time.h>

#ifndef _UTILS_C_
#define _UTILS_C_
Expand Down Expand Up @@ -340,7 +341,12 @@ void logMessage(int msg_level, char *format, ...)
if (msg_level == LOG_ERR) {
fprintf(stderr, "%s", buffer);
} else {
printf("%s", buffer);
struct timespec tspec;
struct tm localtm;
clock_gettime(CLOCK_REALTIME, &tspec);
char timeStr[TIMESTAMP_LENGTH];
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", localtime_r(&tspec.tv_sec, &localtm));
printf("%s.%03ld %s", timeStr, tspec.tv_nsec / 1000000L, buffer);
}
}
}
Expand Down Expand Up @@ -547,4 +553,4 @@ void writePacketLog(char *buffer) {
}
void closePacketLog() {
fclose(_packetLogFile);
}
}

0 comments on commit 4665496

Please sign in to comment.