From 7cdf85a64bc788f9099f5c4eeee8d019bc7d336d Mon Sep 17 00:00:00 2001 From: Jamie Nuttall Date: Tue, 2 Jun 2020 16:54:48 +0100 Subject: [PATCH] Added aircon mode and time setting to commands --- include/phevargs.h | 10 +++++-- main.c | 6 ++++ src/phevargs.c | 68 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/include/phevargs.h b/include/phevargs.h index 1845af1..bd96a5d 100644 --- a/include/phevargs.h +++ b/include/phevargs.h @@ -13,14 +13,18 @@ #define HEADLIGHTS "headlights" #define BATTERY "battery" #define AIRCON "aircon" +#define AIRCON_MODE "acmode" #define REGISTER "register" #define MONITOR "monitor" #define GET "get" #define ON "on" #define OFF "off" +#define HEAT "heat" +#define COOL "cool" +#define WINDSCREEN "windscreen" #define WAIT_FOR_REG_MAX 40 -typedef enum phev_args_commands_t { CMD_UNSET, CMD_INVALID, CMD_STATUS, CMD_REGISTER, CMD_HEADLIGHTS, CMD_BATTERY, CMD_AIRCON, CMD_GET_REG_VAL, CMD_DISPLAY_REG } phev_args_commands_t; +typedef enum phev_args_commands_t { CMD_UNSET, CMD_INVALID, CMD_STATUS, CMD_REGISTER, CMD_HEADLIGHTS, CMD_BATTERY, CMD_AIRCON, CMD_AIRCON_MODE, CMD_GET_REG_VAL, CMD_DISPLAY_REG } phev_args_commands_t; typedef struct phev_args_opts_t { bool init; @@ -32,6 +36,8 @@ typedef struct phev_args_opts_t { char * command_topic; bool verbose; bool operand_on; + uint8_t operand_mode; + uint8_t operand_time; uint8_t reg_operand; bool error; char * error_message; @@ -44,7 +50,7 @@ static uint8_t PHEV_ARGS_DEFAULT_MAC[] = {0,0,0,0,0,0}; static const char * phev_args_argp_program_version = "Version\t" VERSION; static const char * phev_args_argp_program_bug_address = "jamie@wattu.com"; static char phev_args_doc[] = "\n\nProgram to control the car via the remote WiFi interface. Requires this device to be connected to the REMOTE**** access point with a valid IP address, which is on the 192.168.8.x subnet.\n\nTHIS PROGRAM COMES WITH NO WARRANTY ANY DAMAGE TO THE CAR OR ANY OTHER EQUIPMENT IS AT THE USERS OWN RISK."; -static char phev_args_args_doc[] = "register\nbattery\naircon [on|off]\nheadlights [on|off]\nmonitor\nget "; +static char phev_args_args_doc[] = "register\nbattery\naircon [on|off]\nacmode [heat|cool|windscreen] [10|20|30]\nheadlights [on|off]\nmonitor\nget "; static struct argp_option phev_args_options[] = { { "mac", 'm', "",0, "MAC address."}, { "host", 'h', "",OPTION_HIDDEN, "IP address of car - defaults to 192.168.8.46."}, diff --git a/main.c b/main.c index f1ba8e8..7ffc428 100644 --- a/main.c +++ b/main.c @@ -147,6 +147,12 @@ static int main_eventHandler(phevEvent_t *event) phev_airCon(event->ctx, opts->operand_on, operationCallback); break; } + case CMD_AIRCON_MODE: + { + printf("Switching air conditioning mode to %d for %d mins\n", opts->operand_mode, opts->operand_time); + phev_airConMode(event->ctx,opts->operand_mode,opts->operand_time,operationCallback); + break; + } } } return 0; diff --git a/src/phevargs.c b/src/phevargs.c index 9c6cee6..178824a 100644 --- a/src/phevargs.c +++ b/src/phevargs.c @@ -15,6 +15,14 @@ int phev_args_validate(int arg_num,phev_args_opts_t * opts) } break; } + case CMD_AIRCON_MODE: + { + if(arg_num == 3) + { + return 0; + } + break; + } case CMD_REGISTER: { if(arg_num == 1) @@ -40,6 +48,8 @@ int phev_args_validate(int arg_num,phev_args_opts_t * opts) break; } } + opts->error_message = "Incorrect number of operands"; + opts->error = true; return 1; } int phev_args_process_operands(char * arg, int arg_num, phev_args_opts_t * opts) @@ -74,11 +84,61 @@ int phev_args_process_operands(char * arg, int arg_num, phev_args_opts_t * opts) opts->error_message = "Too many operands"; break; } + case CMD_AIRCON_MODE: + { + if(arg_num == 1) + { + if(strcmp(arg,HEAT) == 0) + { + opts->operand_mode = 2; + break; + } + if(strcmp(arg,COOL) == 0) + { + opts->operand_mode = 1; + break; + } + if(strcmp(arg,WINDSCREEN) == 0) + { + opts->operand_mode = 3; + break; + } + opts->error = true; + opts->error_message = "Unrecognised operand"; + break; + } + + if(arg_num == 2) + { + if(strlen(arg) == 2 && isdigit(arg[0]) && isdigit(arg[1])) + { + opts->operand_time = atoi(arg); + + if(opts->operand_time != 10 && + opts->operand_time != 20 && + opts->operand_time != 30 + ) + { + opts->error = true; + opts->error_message = "Unrecognised operand"; + break; + } + } + else + { + opts->error = true; + opts->error_message = "Unrecognised operand"; + } + } + break; + } case CMD_GET_REG_VAL: { if(strlen(arg) == 2 && isdigit(arg[0]) && isdigit(arg[1]) && arg_num == 1) { opts->reg_operand = atoi(arg); - } else { + } + else + { opts->error = true; opts->error_message = "Not a number"; } @@ -121,6 +181,10 @@ int phev_args_process_command(char * arg, int arg_num, phev_args_opts_t * opts) { opts->command = CMD_DISPLAY_REG; } + if(strcmp(arg,AIRCON_MODE) == 0 && arg_num == 0) + { + opts->command = CMD_AIRCON_MODE; + } return 0; } static error_t phev_args_parse_opt(int key, char *arg, struct argp_state *state) { @@ -193,11 +257,13 @@ static error_t phev_args_parse_opt(int key, char *arg, struct argp_state *state) if(opts->error) { opts->command = CMD_INVALID; + printf("\nERROR : %s\n",opts->error_message); argp_usage(state); } if(phev_args_validate(state->arg_num,opts)) { opts->command = CMD_INVALID; + printf("\nERROR : %s\n",opts->error_message); argp_usage(state); } break;