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@136:6ad7ba157b70, 2016-09-27 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Sep 27 18:46:10 2016 +0000
- Revision:
- 136:6ad7ba157b70
- Parent:
- 133:c871de2d2b90
- Child:
- 141:d924caf402c2
unregister timer control
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 | 35:6235ef67faa1 | 33 | char dataBuf[1024]; |
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 | 132:45821e189dd0 | 50 | |
jmarkel44 | 133:c871de2d2b90 | 51 | // create the schedule |
jmarkel44 | 132:45821e189dd0 | 52 | Schedule_t s; |
jmarkel44 | 132:45821e189dd0 | 53 | s.priority = atol(json_value["priority"].get<string>().c_str()); |
jmarkel44 | 132:45821e189dd0 | 54 | s.startTime = atol(json_value["starttime"].get<string>().c_str()); |
jmarkel44 | 132:45821e189dd0 | 55 | s.duration = atol(json_value["duration"].get<string>().c_str()); |
jmarkel44 | 133:c871de2d2b90 | 56 | |
jmarkel44 | 133:c871de2d2b90 | 57 | // push it on the list |
jmarkel44 | 132:45821e189dd0 | 58 | schedule.push_back(s); |
jmarkel44 | 122:4db48b933115 | 59 | |
jmarkel44 | 122:4db48b933115 | 60 | return true; |
jmarkel44 | 122:4db48b933115 | 61 | } |
jmarkel44 | 122:4db48b933115 | 62 | |
jmarkel44 | 132:45821e189dd0 | 63 | // |
jmarkel44 | 132:45821e189dd0 | 64 | // method: start |
jmarkel44 | 132:45821e189dd0 | 65 | // description: initialize the control |
jmarkel44 | 133:c871de2d2b90 | 66 | // |
jmarkel44 | 132:45821e189dd0 | 67 | // @param none |
jmarkel44 | 132:45821e189dd0 | 68 | // @return none |
jmarkel44 | 132:45821e189dd0 | 69 | // |
jmarkel44 | 126:c85ac6a8e9af | 70 | void TimerControl::start(void) |
jmarkel44 | 126:c85ac6a8e9af | 71 | { |
jmarkel44 | 128:534bf29132f8 | 72 | currentState = STATE_OFF; |
jmarkel44 | 136:6ad7ba157b70 | 73 | vector<Schedule_t>::iterator pos; |
jmarkel44 | 136:6ad7ba157b70 | 74 | |
jmarkel44 | 136:6ad7ba157b70 | 75 | if ( schedule.empty() ) return; |
jmarkel44 | 136:6ad7ba157b70 | 76 | #if 0 |
jmarkel44 | 136:6ad7ba157b70 | 77 | for ( pos = schedule.begin(); pos != schedule.end(); ++pos ) { |
jmarkel44 | 136:6ad7ba157b70 | 78 | if ( (pos->startTime + pos->duration) < time(NULL) ) { |
jmarkel44 | 136:6ad7ba157b70 | 79 | schedule.erase(pos); |
jmarkel44 | 136:6ad7ba157b70 | 80 | } |
jmarkel44 | 136:6ad7ba157b70 | 81 | } |
jmarkel44 | 136:6ad7ba157b70 | 82 | #endif |
jmarkel44 | 128:534bf29132f8 | 83 | } |
jmarkel44 | 128:534bf29132f8 | 84 | |
jmarkel44 | 132:45821e189dd0 | 85 | // |
jmarkel44 | 132:45821e189dd0 | 86 | // method: timerStart |
jmarkel44 | 132:45821e189dd0 | 87 | // description: examine the timestamp to determine if the timer control |
jmarkel44 | 132:45821e189dd0 | 88 | // should be running |
jmarkel44 | 132:45821e189dd0 | 89 | // |
jmarkel44 | 132:45821e189dd0 | 90 | // @param none |
jmarkel44 | 132:45821e189dd0 | 91 | // @return true if timer should be running; false otherwise |
jmarkel44 | 132:45821e189dd0 | 92 | // |
jmarkel44 | 132:45821e189dd0 | 93 | bool TimerControl::timerStart(void) |
jmarkel44 | 128:534bf29132f8 | 94 | { |
jmarkel44 | 132:45821e189dd0 | 95 | // schedules should be sorted in order, so always check the first |
jmarkel44 | 132:45821e189dd0 | 96 | if ( !schedule.empty() ) { |
jmarkel44 | 132:45821e189dd0 | 97 | unsigned long currentTime = time(NULL); |
jmarkel44 | 133:c871de2d2b90 | 98 | // does it fit? |
jmarkel44 | 132:45821e189dd0 | 99 | if ( currentTime < schedule.front().startTime ) { |
jmarkel44 | 132:45821e189dd0 | 100 | return false; |
jmarkel44 | 132:45821e189dd0 | 101 | } |
jmarkel44 | 132:45821e189dd0 | 102 | if ( currentTime >= schedule.front().startTime && |
jmarkel44 | 133:c871de2d2b90 | 103 | currentTime < (schedule.front().startTime + schedule.front().duration) ) { |
jmarkel44 | 133:c871de2d2b90 | 104 | logInfo("%s signals feed start", __func__); |
jmarkel44 | 132:45821e189dd0 | 105 | return true; |
jmarkel44 | 132:45821e189dd0 | 106 | } else { |
jmarkel44 | 136:6ad7ba157b70 | 107 | logInfo("%s: schedule for %s has expired", __func__, id.c_str()); |
jmarkel44 | 132:45821e189dd0 | 108 | schedule.erase(schedule.begin()); |
jmarkel44 | 136:6ad7ba157b70 | 109 | this->unregisterControl(); |
jmarkel44 | 132:45821e189dd0 | 110 | } |
jmarkel44 | 132:45821e189dd0 | 111 | } |
jmarkel44 | 133:c871de2d2b90 | 112 | return false; |
jmarkel44 | 132:45821e189dd0 | 113 | } |
jmarkel44 | 132:45821e189dd0 | 114 | |
jmarkel44 | 132:45821e189dd0 | 115 | // |
jmarkel44 | 132:45821e189dd0 | 116 | // method: timerStop |
jmarkel44 | 132:45821e189dd0 | 117 | // description: determines if a running timer should has reached its duration |
jmarkel44 | 132:45821e189dd0 | 118 | // |
jmarkel44 | 132:45821e189dd0 | 119 | // @param none |
jmarkel44 | 132:45821e189dd0 | 120 | // @return true if the timer has expired; false otherwise |
jmarkel44 | 132:45821e189dd0 | 121 | // |
jmarkel44 | 133:c871de2d2b90 | 122 | bool TimerControl::timerStop(void) |
jmarkel44 | 132:45821e189dd0 | 123 | { |
jmarkel44 | 133:c871de2d2b90 | 124 | // if current time is greater than start time + feed duration... |
jmarkel44 | 133:c871de2d2b90 | 125 | if ( time(NULL) >= (schedule.front().startTime + schedule.front().duration) ) { |
jmarkel44 | 133:c871de2d2b90 | 126 | logInfo("%s signals a feed stop", __func__); |
jmarkel44 | 132:45821e189dd0 | 127 | return true; |
jmarkel44 | 132:45821e189dd0 | 128 | } |
jmarkel44 | 128:534bf29132f8 | 129 | return false; |
jmarkel44 | 126:c85ac6a8e9af | 130 | } |
jmarkel44 | 126:c85ac6a8e9af | 131 | |
jmarkel44 | 132:45821e189dd0 | 132 | // |
jmarkel44 | 133:c871de2d2b90 | 133 | // method: update |
jmarkel44 | 132:45821e189dd0 | 134 | // description: run the simplified state machine |
jmarkel44 | 132:45821e189dd0 | 135 | // |
jmarkel44 | 132:45821e189dd0 | 136 | // @param none |
jmarkel44 | 132:45821e189dd0 | 137 | // @return none |
jmarkel44 | 132:45821e189dd0 | 138 | // |
jmarkel44 | 126:c85ac6a8e9af | 139 | void TimerControl::update(void) |
jmarkel44 | 126:c85ac6a8e9af | 140 | { |
jmarkel44 | 128:534bf29132f8 | 141 | switch ( this->currentState ) { |
jmarkel44 | 128:534bf29132f8 | 142 | case STATE_OFF: |
jmarkel44 | 132:45821e189dd0 | 143 | if ( this->timerStart() ) { |
jmarkel44 | 128:534bf29132f8 | 144 | currentState = STATE_RUNNING; |
jmarkel44 | 133:c871de2d2b90 | 145 | this->startFeed(); |
jmarkel44 | 128:534bf29132f8 | 146 | } |
jmarkel44 | 128:534bf29132f8 | 147 | break; |
jmarkel44 | 128:534bf29132f8 | 148 | case STATE_RUNNING: |
jmarkel44 | 132:45821e189dd0 | 149 | if ( this->timerStop() ) { |
jmarkel44 | 128:534bf29132f8 | 150 | currentState = STATE_OFF; |
jmarkel44 | 133:c871de2d2b90 | 151 | this->stopFeed(); |
jmarkel44 | 128:534bf29132f8 | 152 | } |
jmarkel44 | 128:534bf29132f8 | 153 | break; |
jmarkel44 | 128:534bf29132f8 | 154 | case STATE_DISABLED: |
jmarkel44 | 128:534bf29132f8 | 155 | // not implelmented |
jmarkel44 | 128:534bf29132f8 | 156 | default: |
jmarkel44 | 128:534bf29132f8 | 157 | break; |
jmarkel44 | 128:534bf29132f8 | 158 | } |
jmarkel44 | 126:c85ac6a8e9af | 159 | } |
jmarkel44 | 126:c85ac6a8e9af | 160 | |
jmarkel44 | 122:4db48b933115 | 161 | // |
jmarkel44 | 133:c871de2d2b90 | 162 | // method: startFeed |
jmarkel44 | 133:c871de2d2b90 | 163 | // description: signal the output thread to start a feed |
jmarkel44 | 133:c871de2d2b90 | 164 | // |
jmarkel44 | 133:c871de2d2b90 | 165 | // @param none |
jmarkel44 | 133:c871de2d2b90 | 166 | // @return none |
jmarkel44 | 133:c871de2d2b90 | 167 | void TimerControl::startFeed(void) |
jmarkel44 | 133:c871de2d2b90 | 168 | { |
jmarkel44 | 133:c871de2d2b90 | 169 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 133:c871de2d2b90 | 170 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 133:c871de2d2b90 | 171 | |
jmarkel44 | 133:c871de2d2b90 | 172 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 133:c871de2d2b90 | 173 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 133:c871de2d2b90 | 174 | |
jmarkel44 | 133:c871de2d2b90 | 175 | output_mail->action = ACTION_CONTROL_ON; |
jmarkel44 | 133:c871de2d2b90 | 176 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 133:c871de2d2b90 | 177 | output_mail->priority = this->schedule.front().priority; |
jmarkel44 | 133:c871de2d2b90 | 178 | |
jmarkel44 | 133:c871de2d2b90 | 179 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 133:c871de2d2b90 | 180 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 133:c871de2d2b90 | 181 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 133:c871de2d2b90 | 182 | } |
jmarkel44 | 133:c871de2d2b90 | 183 | |
jmarkel44 | 133:c871de2d2b90 | 184 | // |
jmarkel44 | 133:c871de2d2b90 | 185 | // method: stopFeed |
jmarkel44 | 133:c871de2d2b90 | 186 | // description: signal the output thread to stop a feed |
jmarkel44 | 133:c871de2d2b90 | 187 | // |
jmarkel44 | 133:c871de2d2b90 | 188 | // @param none |
jmarkel44 | 133:c871de2d2b90 | 189 | // @return none |
jmarkel44 | 133:c871de2d2b90 | 190 | void TimerControl::stopFeed(void) |
jmarkel44 | 133:c871de2d2b90 | 191 | { |
jmarkel44 | 133:c871de2d2b90 | 192 | logInfo("%s: %s attempting to start feed on relay %s\n", |
jmarkel44 | 133:c871de2d2b90 | 193 | __func__, controlFile.c_str(), output.c_str()); |
jmarkel44 | 133:c871de2d2b90 | 194 | |
jmarkel44 | 133:c871de2d2b90 | 195 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 133:c871de2d2b90 | 196 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 133:c871de2d2b90 | 197 | |
jmarkel44 | 133:c871de2d2b90 | 198 | output_mail->action = ACTION_CONTROL_OFF; |
jmarkel44 | 133:c871de2d2b90 | 199 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 133:c871de2d2b90 | 200 | output_mail->priority = this->schedule.front().priority; |
jmarkel44 | 133:c871de2d2b90 | 201 | |
jmarkel44 | 133:c871de2d2b90 | 202 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 133:c871de2d2b90 | 203 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 133:c871de2d2b90 | 204 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 133:c871de2d2b90 | 205 | } |
jmarkel44 | 133:c871de2d2b90 | 206 | |
jmarkel44 | 136:6ad7ba157b70 | 207 | // Method: unregisterControl |
jmarkel44 | 136:6ad7ba157b70 | 208 | // Description: send OFF indication to Output Master for this control's |
jmarkel44 | 136:6ad7ba157b70 | 209 | // relay |
jmarkel44 | 136:6ad7ba157b70 | 210 | // |
jmarkel44 | 136:6ad7ba157b70 | 211 | // @param none |
jmarkel44 | 136:6ad7ba157b70 | 212 | // @return none |
jmarkel44 | 136:6ad7ba157b70 | 213 | void TimerControl::unregisterControl(void) |
jmarkel44 | 136:6ad7ba157b70 | 214 | { |
jmarkel44 | 136:6ad7ba157b70 | 215 | logInfo("%s: %s attempting to unregister %s\n", |
jmarkel44 | 136:6ad7ba157b70 | 216 | __func__, controlFile.c_str()); |
jmarkel44 | 136:6ad7ba157b70 | 217 | |
jmarkel44 | 136:6ad7ba157b70 | 218 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 136:6ad7ba157b70 | 219 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
jmarkel44 | 136:6ad7ba157b70 | 220 | |
jmarkel44 | 136:6ad7ba157b70 | 221 | output_mail->action = ACTION_CONTROL_UNREGISTER; |
jmarkel44 | 136:6ad7ba157b70 | 222 | output_mail->controlType = CONTROL_TIMER; |
jmarkel44 | 136:6ad7ba157b70 | 223 | output_mail->priority = this->schedule.front().priority; |
jmarkel44 | 136:6ad7ba157b70 | 224 | strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1); |
jmarkel44 | 136:6ad7ba157b70 | 225 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
jmarkel44 | 136:6ad7ba157b70 | 226 | |
jmarkel44 | 136:6ad7ba157b70 | 227 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 136:6ad7ba157b70 | 228 | } |
jmarkel44 | 136:6ad7ba157b70 | 229 | |
jmarkel44 | 133:c871de2d2b90 | 230 | // |
jmarkel44 | 122:4db48b933115 | 231 | // methid: display |
jmarkel44 | 122:4db48b933115 | 232 | // description: display the elements of this timer control object |
jmarkel44 | 122:4db48b933115 | 233 | // |
jmarkel44 | 122:4db48b933115 | 234 | // @param none |
jmarkel44 | 132:45821e189dd0 | 235 | // @return none |
jmarkel44 | 132:45821e189dd0 | 236 | // |
jmarkel44 | 122:4db48b933115 | 237 | void TimerControl::display(void) |
jmarkel44 | 122:4db48b933115 | 238 | { |
jmarkel44 | 128:534bf29132f8 | 239 | string mapper[] = { "OFF", |
jmarkel44 | 128:534bf29132f8 | 240 | "RUNNING", |
jmarkel44 | 128:534bf29132f8 | 241 | "DISABLED" |
jmarkel44 | 128:534bf29132f8 | 242 | }; |
jmarkel44 | 133:c871de2d2b90 | 243 | |
jmarkel44 | 122:4db48b933115 | 244 | printf("\r controlFile : %s \n", controlFile.c_str()); |
jmarkel44 | 122:4db48b933115 | 245 | printf("\r id : %s \n", id.c_str()); |
jmarkel44 | 122:4db48b933115 | 246 | printf("\r output : %s \n", output.c_str()); |
jmarkel44 | 132:45821e189dd0 | 247 | |
jmarkel44 | 132:45821e189dd0 | 248 | std::vector<Schedule_t>::iterator pos; |
jmarkel44 | 132:45821e189dd0 | 249 | |
jmarkel44 | 132:45821e189dd0 | 250 | int counter = 0; |
jmarkel44 | 136:6ad7ba157b70 | 251 | if ( schedule.empty() ) { |
jmarkel44 | 136:6ad7ba157b70 | 252 | printf("\rNo scheduled timers\r\n"); |
jmarkel44 | 136:6ad7ba157b70 | 253 | return; |
jmarkel44 | 136:6ad7ba157b70 | 254 | } else { |
jmarkel44 | 136:6ad7ba157b70 | 255 | for ( pos = schedule.begin(); pos != schedule.end(); ++pos ) { |
jmarkel44 | 136:6ad7ba157b70 | 256 | printf("\r Schedule %d\n", ++counter); |
jmarkel44 | 136:6ad7ba157b70 | 257 | printf("\r priority : %u \n", pos->priority); |
jmarkel44 | 136:6ad7ba157b70 | 258 | printf("\r start time : %lu\n", pos->startTime); |
jmarkel44 | 136:6ad7ba157b70 | 259 | printf("\r duration : %u sec\n", pos->duration); |
jmarkel44 | 136:6ad7ba157b70 | 260 | printf("\r end time : %lu\n", pos->startTime + pos->duration); |
jmarkel44 | 136:6ad7ba157b70 | 261 | printf("\r current time : %lu\n", time(NULL)); |
jmarkel44 | 136:6ad7ba157b70 | 262 | printf("\r expires in : %lu sec\n", (pos->startTime + pos->duration) - time(NULL)); |
jmarkel44 | 136:6ad7ba157b70 | 263 | } |
jmarkel44 | 132:45821e189dd0 | 264 | } |
jmarkel44 | 133:c871de2d2b90 | 265 | printf("\r current State : %s\n", mapper[currentState].c_str()); |
jmarkel44 | 35:6235ef67faa1 | 266 | } |