changeset 8:c32b5792683a

Rework code to allow multiple relays to be set per job.
author Daniel O'Connor <darius@dons.net.au>
date Sun, 15 Feb 2015 17:47:55 +1030
parents 78983502a4e9
children e591c74304a2
files water.c
diffstat 1 files changed, 109 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/water.c	Sun Feb 15 17:47:25 2015 +1030
+++ b/water.c	Sun Feb 15 17:47:55 2015 +1030
@@ -65,7 +65,7 @@
     uint8_t	valid;
     uint32_t	start;
     uint16_t	length;
-    uint8_t	region;
+    uint8_t	relays;
 } job_t;
 
 typedef struct {
@@ -84,7 +84,8 @@
 static void		water_default_settings(void);
 static void		water_write_settings(void);
 static void		water_load_or_init_settings(void);
-static void		water_addjob(int region, uint16_t delay, uint16_t time);
+static void		water_addjob(uint8_t relays, uint16_t delay, uint16_t time);
+static void		water_deljob(int job);
 static void		water_printjobs(void);
 
 void
@@ -128,59 +129,104 @@
 void
 water_cmd(int argc, char **argv) {
     char	*e;
-    uint16_t	delay;
-    uint16_t	time;
+    uint16_t	delay, time;
+    uint8_t	relays;
     long	tmp;
-    int		i;
+    int		region;
 
-    if (!strcmp_P(argv[0], PSTR("all"))) {
-	    if (argc != 3) {
-		    puts_P(PSTR("Wrong number of arguments, usage: wa all delay time\r\n"));
-		    return;
-	    }
+    if (!strcasecmp_P(argv[0], PSTR("add"))) {
+	if (argc != 4) {
+	    puts_P(PSTR("Wrong number of arguments, usage: wa add region delay time\r\n"));
+	    return;
+	}
 
-	    tmp = strtol(argv[1], &e, 10);
-	    if (e == argv[1]) {
-		    puts_P(PSTR("Unable to parse delay\r\n"));
-		    return;
+	relays = 0;
+	if (!strcasecmp_P(argv[1], PSTR("all"))) {
+	    /* all => Or everything together */
+	    for (region = 0; region < sizeof(regions) / sizeof(regions[0]); region++)
+		relays |= 1 << regions[region].relay;
+	} else {
+	    /* find if it's a valid region */
+	    for (region = 0; region < sizeof(regions) / sizeof(regions[0]); region++)
+		if (!strcasecmp(argv[1], regions[region].name))
+		    relays |= 1 << regions[region].relay;
+	    if (relays == 0) {
+		puts_P(PSTR("Unknown region\r\n"));
+		return;
 	    }
-	    if (tmp < 0 || tmp > 65535) {
-		    puts_P(PSTR("delay out of range, must be 0 - 65535\r\n"));
-		    return;
-	    }
-	    delay = tmp;
+	}
+	tmp = strtol(argv[2], &e, 10);
+	if (e == argv[2]) {
+	    puts_P(PSTR("Unable to parse delay\r\n"));
+	    return;
+	}
+	if (tmp < 0 || tmp > 65535) {
+	    puts_P(PSTR("delay out of range, must be 0 - 65535\r\n"));
+	    return;
+	}
+	delay = tmp;
 
-	    tmp = strtol(argv[2], &e, 10);
-	    if (e == argv[2]) {
-		    puts_P(PSTR("Unable to parse time\r\n"));
-		    return;
-	    }
-	    if (tmp < 1 || tmp > 65535) {
-		    puts_P(PSTR("time out of range, must be 1 - 65535\r\n"));
-		    return;
-	    }
-	    time = tmp;
+	tmp = strtol(argv[3], &e, 10);
+	if (e == argv[3]) {
+	    puts_P(PSTR("Unable to parse time\r\n"));
+	    return;
+	}
+	if (tmp < 1 || tmp > 65535) {
+	    puts_P(PSTR("time out of range, must be 1 - 65535\r\n"));
+	    return;
+	}
+	time = tmp;
+
+	water_addjob(relays, delay, time);
+
+	water_printjobs();
+    } else if (!strcasecmp_P(argv[0], PSTR("del"))) {
+	if (argc != 2) {
+	    puts_P(PSTR("Wrong number of arguments, usage: wa del job\r\n"));
+	    return;
+	}
 
-	    water_printjobs();
-
-	    /* add for each region one after the other */
-	    for (i = 0; i < sizeof(regions) / sizeof(regions[0]); i++) {
-		    water_addjob(i, delay + time * i, time);
-	    }
-
-	    water_printjobs();
+	tmp = strtol(argv[1], &e, 10);
+	if (e == argv[1]) {
+	    puts_P(PSTR("Unable to parse job\r\n"));
+	    return;
+	}
+	water_deljob(tmp);
+    } else if (!strcasecmp_P(argv[0], PSTR("pr"))) {
+	if (argc != 1) {
+	    puts_P(PSTR("Wrong number of arguments, usage: wa pr\r\n"));
+	    return;
+	}
+	water_printjobs();
+    } else if (!strcasecmp_P(argv[0], PSTR("regions"))) {
+	int i;
+	if (argc != 1) {
+	    puts_P(PSTR("Wrong number of arguments, usage: wa regions\r\n"));
+	    return;
+	}
+	printf_P(PSTR("%-20S %S\r\n"), PSTR("Region"), PSTR("Relay"));
+	for (i = 0; i < sizeof(regions) / sizeof(regions[0]); i++)
+	    printf_P(PSTR("%-20s %d\r\n"), regions[i].name, regions[i].relay);
+    } else if (!strcasecmp_P(argv[0], PSTR("help"))) {
+        puts_P(PSTR(
+	      "wa help                   This help\r\n"
+	      "wa add region dly time    Water region (or all) for time minutes after dly minutes\r\n"
+	      "wa del job                Delete job\r\n"
+	      "wa pr                     Print list of jobs\r\n"
+	      "wa regions                Print list of regions\r\n"
+	  ));
     } else {
-	    puts_P(PSTR("Unknown 'wa' sub-command\r\n"));
+	    puts_P(PSTR("Unknown 'wa' sub-command. 'wa help' for help\r\n"));
 	    return;
     }
 }
 
 static void
-water_addjob(int region, uint16_t delay, uint16_t time) {
+water_addjob(uint8_t relays, uint16_t delay, uint16_t time) {
     int		i;
     time_t	now;
 
-    printf_P(PSTR("adding region %d, delay %d, time %d\r\n"), region, delay, time);
+    printf_P(PSTR("adding delays 0x%02x, delay %d, time %d\r\n"), relays, delay, time);
 
     now = ds1307_time();
     if (now == -1) {
@@ -201,20 +247,38 @@
 
     settings.jobs[i].start = now + delay * 60;
     settings.jobs[i].length = time * 60;
-    settings.jobs[i].region = region;
+    settings.jobs[i].relays = relays;
     settings.jobs[i].valid = 1;
 
     water_write_settings();
 }
 
 static void
+water_deljob(int job) {
+    if (job < 0 || job >= NJOBS) {
+	    puts_P(PSTR("Invalid job number\r\n"));
+	    return;
+    }
+
+    if (settings.jobs[job].valid == 0) {
+	    puts_P(PSTR("Job already deleted\r\n"));
+	    return;
+    }
+
+    settings.jobs[job].valid = 0;
+    printf_P(PSTR("Removed job %d\r\n"), job);
+
+    water_write_settings();
+}
+
+static void
 water_printjobs(void) {
     int		i;
 
     for (i = 0; i < NJOBS; i++) {
 	printf_P(PSTR("%d: "), i);
 	if (settings.jobs[i].valid) {
-	    printf_P(PSTR("region %d (%s) start %ld length %d\r\n"), settings.jobs[i].region, regions[settings.jobs[i].region].name,
+	    printf_P(PSTR("relays 0x%02x start %ld length %d\r\n"), settings.jobs[i].relays,
 		     settings.jobs[i].start, settings.jobs[i].length);
 	} else {
 	    printf_P(PSTR("invalid\r\n"));
@@ -248,7 +312,7 @@
     for (i = 0; i < NJOBS; i++) {
 	if (settings.jobs[i].valid) {
 	    if (now > settings.jobs[i].start + settings.jobs[i].length) {
-		printf_P(PSTR("Marking job %d (%s) done\r\n"), i, regions[settings.jobs[i].region].name);
+		printf_P(PSTR("Marking job %d (relays 0x%02x) done\r\n"), i, settings.jobs[i].relays);
 		settings.jobs[i].valid = 0;
 		logged[i] = 0;
 		continue;
@@ -256,10 +320,10 @@
 
 	    if (now > settings.jobs[i].start) {
 		if (logged[i] == 0) {
-		    printf_P(PSTR("Job %d (%s) running\r\n"), i, regions[settings.jobs[i].region].name);
+		    printf_P(PSTR("Job %d (relays 0x%02x) running\r\n"), i, settings.jobs[i].relays);
 		    logged[i] = 1;
 		}
-		relays |= 1 << regions[settings.jobs[i].region].relay;
+		relays |= 1 << settings.jobs[i].relays;
 		continue;
 	    }
 	}