Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sfeakes committed Sep 30, 2019
1 parent a9daea4 commit 074b1ab
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 62 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ CC = gcc
LIBS := -lm

# debug of not
#DBG = -g -D TESTING
#DBG = -g -O0 -fsanitize=address -static-libasan
#DBG = -g
#DBG = -D TESTING
DBG =

Expand Down Expand Up @@ -35,12 +36,14 @@ SRCS = aquapure.c ap_net_services.c ap_config.c aq_serial.c utils.c mongoose.c j
#SRCS = aq_serial.c utils.c mongoose.c json_messages.c config.c aquapured/ap_net_services.c aquapured/ap_config.c aquapured/aquapure.c

OBJS = $(SRCS:.c=.o)

#SL_OBJS = $(SL_SRC:.c=.o)
#AL_OBJS = $(AL_SRC:.c=.o)
#AP_OBJS = $(AP_SRC:.c=.o)

# define the executable file
MAIN = ./release/aquapured

#SLOG = ./release/serial_logger
#AQUAPURELOG = ./release/aquapure_logger
#AQUAPURED = ./release/aquapured
Expand Down
17 changes: 9 additions & 8 deletions ap_net_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static void ws_send(struct mg_connection *nc, char *msg)
{
int size = strlen(msg);

logMessage (LOG_DEBUG, "WS: Sent %d characters '%s'\n",size, msg);

mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, msg, size);

logMessage (LOG_DEBUG, "WS: Sent %d characters '%s'\n",size, msg);
Expand Down Expand Up @@ -112,7 +114,7 @@ void send_mqtt_float_msg(struct mg_connection *nc, char *dev_name, float value)
static char mqtt_pub_topic[250];
static char msg[10];

sprintf(msg, "%f", value);
sprintf(msg, "%0.2f", value);
sprintf(mqtt_pub_topic, "%s/%s", _apconfig_.mqtt_aq_topic, dev_name);
send_mqtt(nc, mqtt_pub_topic, msg);
}
Expand Down Expand Up @@ -361,21 +363,20 @@ void action_websocket_request(struct mg_connection *nc, struct websocket_message
//char message[JSON_LABEL_SIZE*10];
//build_device_JSON(_aqualink_data, _aqualink_config->light_programming_button_pool, _aqualink_config->light_programming_button_spa, message, JSON_LABEL_SIZE*10, false);
int size = build_device_JSON(_ar_prms, data, JSON_STATUS_SIZE, false);
logMessage(LOG_DEBUG, "-->%s<--", data);
ws_send(nc, data);
} else if (strcmp(request.first.key, "command") == 0) {
if (strcmp(request.first.value, SWG_TOPIC) == 0) {
printf("Turn SWG on/off NOT IMPLIMENTED YET!\n");
_ar_prms->changed = true;
}
} else if (strcmp(request.first.key, "parameter") == 0) {
if (strcmp(request.first.value, SWG_TOPIC) == 0) {
//int value = atoi(request.second.value);
if (strcmp(request.first.value, SWG_TOPIC) == 0) { // Should delete this soon
set_swg_percent(request.second.value, false);
//_ar_prms->Percent = atoi(request.second.value);
//printf("SET PERCENT TO %d\n", _ar_prms->Percent);
//broadcast_aquapurestate(nc);
//write_cache(_ar_prms);
//_ar_prms->changed = true;
} else if (strcmp(request.first.value, SWG_PERCENT_TOPIC) == 0) {
set_swg_percent(request.second.value, false);
} else if (strcmp(request.first.value, SWG_PERCENT_F_TOPIC) == 0) {
set_swg_percent(request.second.value, true);
}
}
}
Expand Down
70 changes: 46 additions & 24 deletions aq_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

static struct termios _oldtio;


void send_packet(int fd, unsigned char *packet, int length);


void log_packet(char *init_str, unsigned char* packet, int length)
Expand Down Expand Up @@ -208,6 +208,7 @@ void print_hex(char *pk, int length)
printf("\n");
}

/*
void test_cmd()
{
const int length = 11;
Expand All @@ -223,7 +224,8 @@ void test_cmd()
ackPacket[7] = generate_checksum(ackPacket, length-1);
print_hex((char *)ackPacket, length);
}

*/
/*
void send_test_cmd(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3)
{
const int length = 11;
Expand Down Expand Up @@ -252,42 +254,62 @@ void send_test_cmd(int fd, unsigned char destination, unsigned char b1, unsigned
#endif
log_packet("Sent ", ackPacket, length);
}
*/
void send_1byte_command(int fd, unsigned char destination, unsigned char b1)
{
int length = 9;
unsigned char packet[] = { NUL, DLE, STX, DEV_MASTER, CMD_ACK, 0x13, DLE, ETX, NUL };
packet[3] = destination;
packet[4] = b1;
packet[5] = generate_checksum(packet, length-1);

send_packet(fd, packet, length);
}
void send_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3)

void send_2byte_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2)
{
const int length = 11;
unsigned char ackPacket[] = { NUL, DLE, STX, DEV_MASTER, CMD_ACK, NUL, NUL, 0x13, DLE, ETX, NUL };
//unsigned char ackPacket[] = { NUL, DLE, STX, DEV_MASTER, NUL, NUL, NUL, 0x13, DLE, ETX, NUL };
int length = 10;
unsigned char packet[] = { NUL, DLE, STX, DEV_MASTER, CMD_ACK, NUL, 0x13, DLE, ETX, NUL };
packet[3] = destination;
packet[4] = b1;
packet[5] = b2;
packet[6] = generate_checksum(packet, length-1);

send_packet(fd, packet, length);
}

// Update the packet and checksum if command argument is not NUL.
ackPacket[3] = destination;
ackPacket[4] = b1;
ackPacket[5] = b2;
ackPacket[6] = b3;
ackPacket[7] = generate_checksum(ackPacket, length-1);
void send_3byte_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3)
{
int length = 11;
unsigned char packet[] = { NUL, DLE, STX, DEV_MASTER, CMD_ACK, NUL, NUL, 0x13, DLE, ETX, NUL };
packet[3] = destination;
packet[4] = b1;
packet[5] = b2;
packet[6] = b3;
packet[7] = generate_checksum(packet, length-1);

send_packet(fd, packet, length);
}

void send_packet(int fd, unsigned char *packet, int length)
{

#ifdef BLOCKING_MODE
write(fd, ackPacket, length);
#else
int nwrite, i;
for (i=0; i<length; i += nwrite) {
nwrite = write(fd, ackPacket + i, length - i);
nwrite = write(fd, packet + i, length - i);
if (nwrite < 0)
logMessage(LOG_ERR, "write to serial port failed\n");
}
//logMessage(LOG_DEBUG_SERIAL, "Send %d bytes to serial\n",length);
//tcdrain(fd);
//logMessage(LOG_DEBUG, "Send '0x%02hhx' to '0x%02hhx'\n", command, destination);
#endif

if ( getLogLevel() >= LOG_DEBUG_SERIAL) {
char buf[30];
sprintf(buf, "Sent %8.8s ", get_packet_type(ackPacket+1, length));
log_packet(buf, ackPacket, length);
sprintf(buf, "Sent %8.8s ", get_packet_type(packet+1, length));
log_packet(buf, packet, length);
}
}

/*
void send_probe(int fd, unsigned char destination)
{
const int length = 9;
Expand Down Expand Up @@ -318,7 +340,7 @@ void send_probe(int fd, unsigned char destination)
log_packet(buf, ackPacket, length);
}
}

*/
void send_messaged(int fd, unsigned char destination, char *message)
{
const int length = 24;
Expand Down Expand Up @@ -487,7 +509,7 @@ int get_packet(int fd, unsigned char* packet)

// Break out of the loop if we exceed maximum packet
// length.
if (index >= AQ_MAXPKTLEN) {
if (index >= AQ_MAXPKTLEN-1) {
break;
}
}
Expand Down
10 changes: 7 additions & 3 deletions aq_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ int get_packet(int file_descriptor, unsigned char* packet);
//void process_status(void const * const ptr);
void process_status(unsigned char* ptr);
const char* get_packet_type(unsigned char* packet, int length);
void send_test_cmd(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3);
void send_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3);
//void send_test_cmd(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3);
//void send_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3);
void send_messaged(int fd, unsigned char destination, char *message);
void send_probe(int fd, unsigned char destination);
//void send_probe(int fd, unsigned char destination);

void send_1byte_command(int fd, unsigned char destination, unsigned char b1);
void send_2byte_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2);
void send_3byte_command(int fd, unsigned char destination, unsigned char b1, unsigned char b2, unsigned char b3);
#endif // AQ_SERIAL_H_
Binary file modified aq_serial.o
Binary file not shown.
31 changes: 25 additions & 6 deletions aquapure.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct apdata _ar_prms;

bool _keepRunning = true;
//int _percentsalt_ = 50;
bool _forceConnection = false;

void main_loop();

Expand Down Expand Up @@ -136,7 +137,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

_ar_prms.PPM = 0;
_ar_prms.PPM = TEMP_UNKNOWN;
_ar_prms.Percent = 50;
_ar_prms.generating = false;
_ar_prms.cache_file = CACHE_FILE;
Expand All @@ -150,6 +151,8 @@ int main(int argc, char *argv[]) {
deamonize = false;
} else if (strcmp(argv[i], "-c") == 0) {
cfgFile = argv[++i];
} else if (strcmp(argv[i], "-f") == 0) {
_forceConnection = true;
}
}

Expand Down Expand Up @@ -197,6 +200,15 @@ void main_loop() {

rs_fd = init_serial_port(_apconfig_.serial_port);

/*
send_1byte_command(rs_fd, AR_ID, CMD_PROBE);
send_2byte_command(rs_fd, AR_ID, CMD_GETID, 0x01);
send_3byte_command(rs_fd, AR_ID, CMD_PERCENT, (unsigned char)_ar_prms.Percent, NUL);
char msg[] = {0x00, 0x01};
printf("Len = %d\n",strlen(msg));
send_messaged(rs_fd, AR_ID, msg);
return;
*/
if (rs_fd < 0) {
logMessage(LOG_ERR, "Can not open serial port '%s'\n",_apconfig_.serial_port);
exit(EXIT_FAILURE);
Expand All @@ -207,7 +219,7 @@ void main_loop() {

mg_mgr_poll(&mgr, 500);

send_probe(rs_fd, AR_ID);
send_1byte_command(rs_fd, AR_ID, CMD_PROBE);

logMessage(LOG_DEBUG_SERIAL,"Send Probe\n");

Expand All @@ -232,7 +244,7 @@ void main_loop() {
logMessage(LOG_DEBUG_SERIAL,"Nothing read\n");
//if (ar_connected == false && no_reply >= PING_POLL) {
//send_command(rs_fd, AR_ID, CMD_PROBE, 0x62, NUL);
send_probe(rs_fd, AR_ID);
send_1byte_command(rs_fd, AR_ID, CMD_PROBE);
//logMessage(LOG_DEBUG_SERIAL,"Send Probe\n");
// no_reply = 0;
//} else {
Expand Down Expand Up @@ -268,14 +280,20 @@ void main_loop() {

switch (packet_buffer[PKT_CMD]) {
case CMD_ACK:
if (_forceConnection == true)
_ar_prms.generating = true;

if (_ar_prms.generating == false) {
// Chlorinator Translator = GetID | HEX: 0x10|0x02|0x50|0x14|0x00|0x76|0x10|0x03|
// AquaLinkRD To 0x50 of type GetID | HEX: 0x10|0x02|0x50|0x14|0x01|0x77|0x10|0x03|
send_command(rs_fd, AR_ID, CMD_GETID, 0x01, 0x77); // Returns AquaPure or aquapure
send_2byte_command(rs_fd, AR_ID, CMD_GETID, 0x01);
//send_2byte_command(rs_fd, AR_ID, CMD_GETID, 0x01);
//send_command(rs_fd, AR_ID, CMD_GETID, 0x01, 0x77); // Returns AquaPure or aquapure
//send_command(rs_fd, AR_ID, CMD_GETID, 0x00, 0x76); // Returns BOOTS
//logMessage(LOG_DEBUG_SERIAL,"Send Get Status/ID\n");
} else {
send_command(rs_fd, AR_ID, CMD_PERCENT, (unsigned char)_ar_prms.Percent, NUL);
send_3byte_command(rs_fd, AR_ID, CMD_PERCENT, (unsigned char)_ar_prms.Percent, NUL);
//send_command(rs_fd, AR_ID, CMD_PERCENT, (unsigned char)_ar_prms.Percent, NUL);
//logMessage(LOG_DEBUG_SERIAL,"Send set percent salt to %d\n",_ar_prms.Percent);
}
break;
Expand Down Expand Up @@ -319,7 +337,8 @@ void main_loop() {
debugStatusPrint();

case CMD_MSG: // Want to fall through
send_command(rs_fd, AR_ID, CMD_PERCENT, (unsigned char)_ar_prms.Percent, NUL);
//send_command(rs_fd, AR_ID, CMD_PERCENT, (unsigned char)_ar_prms.Percent, NUL);
send_3byte_command(rs_fd, AR_ID, CMD_PERCENT, (unsigned char)_ar_prms.Percent, NUL);
break;
// case 0x16:
// break;
Expand Down
Binary file modified config.o
Binary file not shown.
10 changes: 7 additions & 3 deletions json_messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ int build_device_JSON(struct apdata *aqdata, char* buffer, int size, bool homeki
length += sprintf(buffer+length, ", \"devices\": [");

//if ( aqdata->Percent != TEMP_UNKNOWN ) {
length += sprintf(buffer+length, "{\"type\": \"setpoint_swg\", \"id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"status\": \"%s\", \"spvalue\": \"%.*f\", \"value\": \"%.*f\", \"extended_status\": \"%d\" },",
SWG_TOPIC,
length += sprintf(buffer+length, "{\"type\": \"setpoint_swg\", \"id\": \"%s\", \"setpoint_id\": \"%s\", \"name\": \"%s\", \"state\": \"%s\", \"status\": \"%s\", \"spvalue\": \"%.*f\", \"value\": \"%.*f\", \"extended_status\": \"%d\" },",
SWG_TOPIC,
((homekit_f)?SWG_PERCENT_F_TOPIC:SWG_PERCENT_TOPIC),
"Salt Water Generator",
aqdata->status == SWG_STATUS_ON?JSON_ON:JSON_OFF,
aqdata->status == SWG_STATUS_ON?JSON_ON:JSON_OFF,
Expand Down Expand Up @@ -190,10 +191,13 @@ int build_device_JSON(struct apdata *aqdata, char* buffer, int size, bool homeki

length += sprintf(buffer+length, "]}");

logMessage(LOG_DEBUG, "WEB: homebridge used %d of %d", length, size);
logMessage(LOG_DEBUG, "build_device_JSON %d of %d", length, size);


buffer[length] = '\0';

logMessage(LOG_DEBUG, "-->%s<--", buffer);

return strlen(buffer);

}
Expand Down
Binary file modified json_messages.o
Binary file not shown.
Binary file modified mongoose.o
Binary file not shown.
Binary file modified release/aquapured
Binary file not shown.
Binary file added release/aquapured-test
Binary file not shown.
6 changes: 3 additions & 3 deletions release/aquapured.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
# so, NOTICE also prints WARNING & ERROR
# DEBUG_SERIAL would print everything possible

#log_level=DEBUG_SERIAL
log_level=DEBUG
log_level=DEBUG_SERIAL
#log_level=DEBUG
#log_level=INFO
#log_level=NOTICE

#web_directory=/var/www/aquapured/
web_directory=./web/

socket_port=80
socket_port=88

# The serial port the daemon access to read the Aqualink RS8
serial_port=/dev/ttyUSB0
Expand Down
4 changes: 2 additions & 2 deletions release/aquapured.test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# so, NOTICE also prints WARNING & ERROR
# DEBUG_SERIAL would print everything possible

#log_level=DEBUG_SERIAL
log_level=DEBUG
log_level=DEBUG_SERIAL
#log_level=DEBUG
#log_level=INFO
#log_level=NOTICE

Expand Down
2 changes: 1 addition & 1 deletion utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void logMessage(int msg_level, char *format, ...)
va_list args;
va_start(args, format);
strncpy(buffer, " ", 8);
vsprintf (&buffer[8], format, args);
vsnprintf (&buffer[8], 500, format, args);
va_end(args);

//test(msg_level, buffer);
Expand Down
Binary file modified utils.o
Binary file not shown.
Loading

0 comments on commit 074b1ab

Please sign in to comment.