Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/ConfigurationHandler/Controls/TimerControl.cpp@171:fb29030d4eaf, 2016-10-04 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Oct 04 12:22:13 2016 +0000
- Revision:
- 171:fb29030d4eaf
- Parent:
- 157:0d79678ed00f
- Child:
- 177:9ec90c8e3ce1
reduced file size buffers;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 19:9bc8fabeddfa | 1 | /****************************************************************************** |
jmarkel44 | 19:9bc8fabeddfa | 2 | * |
jmarkel44 | 131:a290a3934132 | 3 | * File: TimerControl.cpp |
jmarkel44 | 35:6235ef67faa1 | 4 | * Desciption: ICE Timer Control Class implementation |
jmarkel44 | 19:9bc8fabeddfa | 5 | * |
jmarkel44 | 19:9bc8fabeddfa | 6 | *****************************************************************************/ |
jmarkel44 | 19:9bc8fabeddfa | 7 | #include "TimerControl.h" |
jmarkel44 | 19:9bc8fabeddfa | 8 | #include "mDot.h" |
jmarkel44 | 35:6235ef67faa1 | 9 | #include "MbedJSONValue.h" |
jmarkel44 | 133:c871de2d2b90 | 10 | #include "global.h" |
jmarkel44 | 35:6235ef67faa1 | 11 | #include <string> |
jmarkel44 | 19:9bc8fabeddfa | 12 | |
jmarkel44 | 19:9bc8fabeddfa | 13 | extern mDot *GLOBAL_mdot; |
jmarkel44 | 19:9bc8fabeddfa | 14 | |
jmarkel44 | 122:4db48b933115 | 15 | // |
jmarkel44 | 122:4db48b933115 | 16 | // method: load |
jmarkel44 | 122:4db48b933115 | 17 | // description: load the pertinents from the control file |
jmarkel44 | 128:534bf29132f8 | 18 | // |
jmarkel44 | 122:4db48b933115 | 19 | // @param _controlFile |
jmarkel44 | 122:4db48b933115 | 20 | // @return true if loaded; false otherwise |
jmarkel44 | 122:4db48b933115 | 21 | // |
jmarkel44 | 35:6235ef67faa1 | 22 | bool TimerControl::load(string _controlFile) |
jmarkel44 | 122:4db48b933115 | 23 | { |
jmarkel44 | 122:4db48b933115 | 24 | MbedJSONValue json_value; // json parsing element |
jmarkel44 | 122:4db48b933115 | 25 | |
jmarkel44 | 19:9bc8fabeddfa | 26 | // try to open the control file |
jmarkel44 | 122:4db48b933115 | 27 | mDot::mdot_file file = GLOBAL_mdot->openUserFile(_controlFile.c_str(), mDot::FM_RDONLY); |
jmarkel44 | 122:4db48b933115 | 28 | if ( file.fd < 0 ) { |
jmarkel44 | 35:6235ef67faa1 | 29 | return false; |
jmarkel44 | 122:4db48b933115 | 30 | } |
jmarkel44 | 35:6235ef67faa1 | 31 | |
jmarkel44 | 35:6235ef67faa1 | 32 | // read the data into a buffer |
jmarkel44 | 171:fb29030d4eaf | 33 | char dataBuf[512]; |
jmarkel44 | 35:6235ef67faa1 | 34 | |
jmarkel44 | 35:6235ef67faa1 | 35 | int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 35:6235ef67faa1 | 36 | if ( bytes_read != sizeof(dataBuf) ) { |
jmarkel44 | 35:6235ef67faa1 | 37 | logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str()); |
jmarkel44 | 35:6235ef67faa1 | 38 | return false; |
jmarkel44 | 35:6235ef67faa1 | 39 | } |
jmarkel44 | 122:4db48b933115 | 40 | |
jmarkel44 | 122:4db48b933115 | 41 | // close the file |
jmarkel44 | 35:6235ef67faa1 | 42 | GLOBAL_mdot->closeUserFile(file); |
jmarkel44 | 35:6235ef67faa1 | 43 | |
jmarkel44 | 35:6235ef67faa1 | 44 | parse(json_value, dataBuf); |
jmarkel44 | 35:6235ef67faa1 | 45 | |
jmarkel44 | 132:45821e189dd0 | 46 | // the pertinents |
jmarkel44 | 122:4db48b933115 | 47 | controlFile = _controlFile; |
jmarkel44 | 132:45821e189dd0 | 48 | id = json_value["id"].get<string>(); |
jmarkel44 | 132:45821e189dd0 | 49 | output = json_value["output"].get<string>(); |
jmarkel44 | 156:44f87c5a83ae | 50 | priority = atoi(json_value["priority"].get<string>().c_str()); |
jmarkel44 | 156:44f87c5a83ae | 51 | startTime = atol(json_value["starttime"].get<string>().c_str()); |
jmarkel44 | 156:44f87c5a83ae | 52 | duration = atoi(json_value["duration"].get<string>().c_str()); |
jmarkel44 | 122:4db48b933115 | 53 | |
jmarkel44 | 122:4db48b933115 | 54 | return true; |
jmarkel44 | 122:4db48b933115 | 55 | } |
jmarkel44 | 122:4db48b933115 | 56 | |
jmarkel44 | 132:45821e189dd0 | 57 | // |
jmarkel44 | 132:45821e189dd0 | 58 | // method: start |
jmarkel44 | 132:45821e189dd0 | 59 | // description: initialize the control |
jmarkel44 | 133:c871de2d2b90 | 60 | // |
jmarkel44 | 132:45821e189dd0 | 61 | // @param none |
jmarkel44 | 132:45821e189dd0 | 62 | // @return none |
jmarkel44 | 132:45821e189dd0 | 63 | // |
jmarkel44 | 126:c85ac6a8e9af | 64 | void TimerControl::start(void) |
jmarkel44 | 126:c85ac6a8e9af | 65 | { |
jmarkel44 | 128:534bf29132f8 | 66 | currentState = STATE_OFF; |
jmarkel44 | 128:534bf29132f8 | 67 | } |
jmarkel44 | 128:534bf29132f8 | 68 | |
jmarkel44 | 132:45821e189dd0 | 69 | // |
jmarkel44 | 132:45821e189dd0 | 70 | // method: timerStart |
jmarkel44 | 132:45821e189dd0 | 71 | // description: examine the timestamp to determine if the timer control |
jmarkel44 | 132:45821e189dd0 | 72 | // should be running |
jmarkel44 | 132:45821e189dd0 | 73 | // |
jmarkel44 | 132:45821e189dd0 | 74 | // @param none |
jmarkel44 | 132:45821e189dd0 | 75 | // @return true if timer should be running; false otherwise |
jmarkel44 | 132:45821e189dd0 | 76 | // |
jmarkel44 | 132:45821e189dd0 | 77 | bool TimerControl::timerStart(void) |
jmarkel44 | 128:534bf29132f8 | 78 | { |
jmarkel44 | 156:44f87c5a83ae | 79 | unsigned long currentTime = time(NULL); |
jmarkel44 | 156:44f87c5a83ae | 80 | |
jmarkel44 | 156:44f87c5a83ae | 81 | if ( currentTime < startTime ) |
jmarkel44 | 156:44f87c5a83ae | 82 | return false; |
jmarkel44 | 156:44f87c5a83ae | 83 | |
jmarkel44 | 156:44f87c5a83ae | 84 | if ( currentTime >= startTime && currentTime <= (startTime + duration) ) { |
jmarkel44 | 156:44f87c5a83ae | 85 | return true; |
jmarkel44 | 132:45821e189dd0 | 86 | } |
jmarkel44 | 133:c871de2d2b90 | 87 | return false; |
jmarkel44 | 132:45821e189dd0 | 88 | } |
jmarkel44 | 132:45821e189dd0 | 89 | // |
jmarkel44 | 132:45821e189dd0 | 90 | // method: timerStop |
jmarkel44 | 132:45821e189dd0 | 91 | // description: determines if a running timer should has reached its duration |
jmarkel44 | 132:45821e189dd0 | 92 | // |
jmarkel44 | 132:45821e189dd0 | 93 | // @param none |
jmarkel44 | 132:45821e189dd0 | 94 | // @return true if the timer has expired; false otherwise |
jmarkel44 | 132:45821e189dd0 | 95 | // |
jmarkel44 | 133:c871de2d2b90 | 96 | bool TimerControl::timerStop(void) |
jmarkel44 | 132:45821e189dd0 | 97 | { |
jmarkel44 | 156:44f87c5a83ae | 98 | if ( time(NULL) >= startTime + duration ) { |
jmarkel44 | 132:45821e189dd0 | 99 | return true; |
jmarkel44 | 132:45821e189dd0 | 100 | } |
jmarkel44 | 128:534bf29132f8 | 101 | return false; |
jmarkel44 | 126:c85ac6a8e9af | 102 | } |
jmarkel44 | 126:c85ac6a8e9af | 103 | |
jmarkel44 | 132:45821e189dd0 | 104 | // |
jmarkel44 | 133:c871de2d2b90 | 105 | // method: update |
jmarkel44 | 132:45821e189dd0 | 106 | // description: run the simplified state machine |
jmarkel44 | 132:45821e189dd0 | 107 | // |
jmarkel44 | 132:45821e189dd0 | 108 | // @param none |
jmarkel44 | 132:45821e189dd0 | 109 | // @return none |
jmarkel44 | 132:45821e189dd0 | 110 | // |
jmarkel44 | 157:0d79678ed00f | 111 | TimerError_t TimerControl::update(void) |
jmarkel44 | 126:c85ac6a8e9af | 112 | { |
jmarkel44 | 157:0d79678ed00f | 113 | TimerError_t rc = TIMER_CONTROL_OK; |
jmarkel44 | 157:0d79678ed00f | 114 | |
jmarkel44 | 128:534bf29132f8 | 115 | switch ( this->currentState ) { |
jmarkel44 | 128:534bf29132f8 | 116 | case STATE_OFF: |
jmarkel44 | 132:45821e189dd0 | 117 | if ( this->timerStart() ) { |
jmarkel44 | 128:534bf29132f8 | 118 | currentState = STATE_RUNNING; |
jmarkel44 | 133:c871de2d2b90 | 119 | this->startFeed(); |
jmarkel44 | 128:534bf29132f8 | 120 | } |
jmarkel44 | 128:534bf29132f8 | 121 | break; |
jmarkel44 | 128:534bf29132f8 | 122 | case STATE_RUNNING: |
jmarkel44 | 132:45821e189dd0 | 123 | if ( this->timerStop() ) { |
jmarkel44 | 128:534bf29132f8 | 124 | currentState = STATE_OFF; |
jmarkel44 | 133:c871de2d2b90 | 125 | this->stopFeed(); |
jmarkel44 | 156:44f87c5a83ae | 126 | this->unregisterControl(); |
jmarkel44 | 157:0d79678ed00f | 127 | rc = TIMER_CONTROL_DESTROY; |
jmarkel44 | 128:534bf29132f8 | 128 | } |
jmarkel44 | 128:534bf29132f8 | 129 | break; |
jmarkel44 | 128:534bf29132f8 | 130 | case STATE_DISABLED: |
jmarkel44 | 128:534bf29132f8 | 131 | // not implelmented |
jmarkel44 | 128:534bf29132f8 | 132 | default: |
jmarkel44 | 128:534bf29132f8 | 133 | break; |
jmarkel44 | 128:534bf29132f8 | 134 | } |
jmarkel44 | 157:0d79678ed00f | 135 | return rc; |
jmarkel44 | 157:0d79678ed00f | 136 | |
jmarkel44 | 126:c85ac6a8e9af | 137 | } |
jmarkel44 | 126:c85ac6a8e9af | 138 | |
jmarkel44 | 122:4db48b933115 | 139 | // |
jmarkel44 | 133:c871de2d2b90 | 140 | // method: startFeed |
jmarkel44 | 133:c871de2d2b90 | 141 | // description: signal the output thread to start a feed |
jmarkel44 | 133:c871de2d2b90 | 142 | // |
jmarkel44 | 133:c871de2d2b90 | 143 | // @param none |
jmarkel44 | 133:c871de2d2b90 | 144 | // @return none |
jmarkel44 | 133:c871de2d2b90 | 145 | void TimerControl::startFeed(void) |
jmarkel44 | 133:c871de2d2b90 | 146 | { |
jmarkel44 | 133:c871de2d2b90 | 147 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 133:c871de2d2b90 | 148 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 133:c871de2d2b90 | 149 | |
jmarkel44 | 133:c871de2d2b90 | 150 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 133:c871de2d2b90 | 151 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 133:c871de2d2b90 | 152 | |
jmarkel44 | 133:c871de2d2b90 | 153 | output_mail->action = ACTION_CONTROL_ON; |
jmarkel44 | 133:c871de2d2b90 | 154 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 156:44f87c5a83ae | 155 | output_mail->priority = priority; |
jmarkel44 | 133:c871de2d2b90 | 156 | |
jmarkel44 | 133:c871de2d2b90 | 157 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 133:c871de2d2b90 | 158 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 133:c871de2d2b90 | 159 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 133:c871de2d2b90 | 160 | } |
jmarkel44 | 133:c871de2d2b90 | 161 | |
jmarkel44 | 133:c871de2d2b90 | 162 | // |
jmarkel44 | 133:c871de2d2b90 | 163 | // method: stopFeed |
jmarkel44 | 133:c871de2d2b90 | 164 | // description: signal the output thread to stop a feed |
jmarkel44 | 133:c871de2d2b90 | 165 | // |
jmarkel44 | 133:c871de2d2b90 | 166 | // @param none |
jmarkel44 | 133:c871de2d2b90 | 167 | // @return none |
jmarkel44 | 133:c871de2d2b90 | 168 | void TimerControl::stopFeed(void) |
jmarkel44 | 133:c871de2d2b90 | 169 | { |
jmarkel44 | 133:c871de2d2b90 | 170 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 133:c871de2d2b90 | 171 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 133:c871de2d2b90 | 172 | |
jmarkel44 | 133:c871de2d2b90 | 173 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 133:c871de2d2b90 | 174 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 133:c871de2d2b90 | 175 | |
jmarkel44 | 133:c871de2d2b90 | 176 | output_mail->action = ACTION_CONTROL_OFF; |
jmarkel44 | 133:c871de2d2b90 | 177 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 156:44f87c5a83ae | 178 | output_mail->priority = priority; |
jmarkel44 | 133:c871de2d2b90 | 179 | |
jmarkel44 | 133:c871de2d2b90 | 180 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 133:c871de2d2b90 | 181 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 133:c871de2d2b90 | 182 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 133:c871de2d2b90 | 183 | } |
jmarkel44 | 156:44f87c5a83ae | 184 | |
jmarkel44 | 153:0845c7bf236a | 185 | // |
jmarkel44 | 136:6ad7ba157b70 | 186 | // Method: unregisterControl |
jmarkel44 | 136:6ad7ba157b70 | 187 | // Description: send OFF indication to Output Master for this control's |
jmarkel44 | 136:6ad7ba157b70 | 188 | // relay |
jmarkel44 | 136:6ad7ba157b70 | 189 | // |
jmarkel44 | 136:6ad7ba157b70 | 190 | // @param none |
jmarkel44 | 136:6ad7ba157b70 | 191 | // @return none |
jmarkel44 | 153:0845c7bf236a | 192 | // |
jmarkel44 | 136:6ad7ba157b70 | 193 | void TimerControl::unregisterControl(void) |
jmarkel44 | 136:6ad7ba157b70 | 194 | { |
jmarkel44 | 136:6ad7ba157b70 | 195 | logInfo("%s: %s attempting to unregister %s\n", |
jmarkel44 | 136:6ad7ba157b70 | 196 | __func__, controlFile.c_str()); |
jmarkel44 | 136:6ad7ba157b70 | 197 | |
jmarkel44 | 136:6ad7ba157b70 | 198 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 136:6ad7ba157b70 | 199 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 156:44f87c5a83ae | 200 | |
jmarkel44 | 156:44f87c5a83ae | 201 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
jmarkel44 | 156:44f87c5a83ae | 202 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 156:44f87c5a83ae | 203 | output_mail->priority = priority; |
jmarkel44 | 136:6ad7ba157b70 | 204 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 136:6ad7ba157b70 | 205 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 156:44f87c5a83ae | 206 | |
jmarkel44 | 136:6ad7ba157b70 | 207 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 136:6ad7ba157b70 | 208 | } |
jmarkel44 | 136:6ad7ba157b70 | 209 | |
jmarkel44 | 133:c871de2d2b90 | 210 | // |
jmarkel44 | 122:4db48b933115 | 211 | // methid: display |
jmarkel44 | 122:4db48b933115 | 212 | // description: display the elements of this timer control object |
jmarkel44 | 122:4db48b933115 | 213 | // |
jmarkel44 | 122:4db48b933115 | 214 | // @param none |
jmarkel44 | 132:45821e189dd0 | 215 | // @return none |
jmarkel44 | 132:45821e189dd0 | 216 | // |
jmarkel44 | 122:4db48b933115 | 217 | void TimerControl::display(void) |
jmarkel44 | 122:4db48b933115 | 218 | { |
jmarkel44 | 128:534bf29132f8 | 219 | string mapper[] = { "OFF", |
jmarkel44 | 128:534bf29132f8 | 220 | "RUNNING", |
jmarkel44 | 128:534bf29132f8 | 221 | "DISABLED" |
jmarkel44 | 128:534bf29132f8 | 222 | }; |
jmarkel44 | 133:c871de2d2b90 | 223 | |
jmarkel44 | 156:44f87c5a83ae | 224 | printf("\r controlFile : %s \n", controlFile.c_str()); |
jmarkel44 | 156:44f87c5a83ae | 225 | printf("\r id : %s \n", id.c_str()); |
jmarkel44 | 156:44f87c5a83ae | 226 | printf("\r output : %s \n", output.c_str()); |
jmarkel44 | 156:44f87c5a83ae | 227 | printf("\r priority : %d \n", priority); |
jmarkel44 | 156:44f87c5a83ae | 228 | printf("\r start time : %lu \n", startTime); |
jmarkel44 | 156:44f87c5a83ae | 229 | printf("\r duration : %u \n", duration); |
jmarkel44 | 156:44f87c5a83ae | 230 | printf("\r end time : %lu \n", startTime + duration); |
jmarkel44 | 156:44f87c5a83ae | 231 | printf("\r expires in : %lu sec \n", (startTime + duration) - time(NULL)); |
jmarkel44 | 156:44f87c5a83ae | 232 | printf("\r current State : %s\r\n\r\n", mapper[currentState].c_str()); |
jmarkel44 | 35:6235ef67faa1 | 233 | } |