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.
ICE-Application/src/ConfigurationHandler/Controls/CompositeControl.cpp@0:61364762ee0e, 2017-01-24 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Jan 24 19:05:33 2017 +0000
- Revision:
- 0:61364762ee0e
Port from IAR to Nucleo-F412 board
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jmarkel44 | 0:61364762ee0e | 1 | /****************************************************************************** |
| jmarkel44 | 0:61364762ee0e | 2 | * |
| jmarkel44 | 0:61364762ee0e | 3 | * File: CompositeControl.cpp |
| jmarkel44 | 0:61364762ee0e | 4 | * Desciption: ICE Composite Control Class implementation |
| jmarkel44 | 0:61364762ee0e | 5 | * |
| jmarkel44 | 0:61364762ee0e | 6 | *****************************************************************************/ |
| jmarkel44 | 0:61364762ee0e | 7 | #include "CompositeControl.h" |
| jmarkel44 | 0:61364762ee0e | 8 | #include "ConfigurationHandler.h" |
| jmarkel44 | 0:61364762ee0e | 9 | #include "cJSON.h" |
| jmarkel44 | 0:61364762ee0e | 10 | #include "ModbusMasterApi.h" |
| jmarkel44 | 0:61364762ee0e | 11 | #include "global.h" |
| jmarkel44 | 0:61364762ee0e | 12 | #include "ICELog.h" |
| jmarkel44 | 0:61364762ee0e | 13 | #include <string> |
| jmarkel44 | 0:61364762ee0e | 14 | #include <iostream> |
| jmarkel44 | 0:61364762ee0e | 15 | #include <iomanip> |
| jmarkel44 | 0:61364762ee0e | 16 | |
| jmarkel44 | 0:61364762ee0e | 17 | #ifdef MDOT_ICE |
| jmarkel44 | 0:61364762ee0e | 18 | extern mDot *GLOBAL_mdot; |
| jmarkel44 | 0:61364762ee0e | 19 | #endif |
| jmarkel44 | 0:61364762ee0e | 20 | |
| jmarkel44 | 0:61364762ee0e | 21 | using namespace std; |
| jmarkel44 | 0:61364762ee0e | 22 | |
| jmarkel44 | 0:61364762ee0e | 23 | |
| jmarkel44 | 0:61364762ee0e | 24 | // |
| jmarkel44 | 0:61364762ee0e | 25 | // method: load |
| jmarkel44 | 0:61364762ee0e | 26 | // description: load a composite control |
| jmarkel44 | 0:61364762ee0e | 27 | // |
| jmarkel44 | 0:61364762ee0e | 28 | // @param[in] _controlFile -> the file containing the JSON data |
| jmarkel44 | 0:61364762ee0e | 29 | // @return false if an error occurs; true otherwise |
| jmarkel44 | 0:61364762ee0e | 30 | // |
| jmarkel44 | 0:61364762ee0e | 31 | |
| jmarkel44 | 0:61364762ee0e | 32 | bool CompositeControl::load(std::string _controlFile) |
| jmarkel44 | 0:61364762ee0e | 33 | { |
| jmarkel44 | 0:61364762ee0e | 34 | controlFile = _controlFile; |
| jmarkel44 | 0:61364762ee0e | 35 | |
| jmarkel44 | 0:61364762ee0e | 36 | |
| jmarkel44 | 0:61364762ee0e | 37 | char dataBuf[MAX_FILE_SIZE]; |
| jmarkel44 | 0:61364762ee0e | 38 | |
| jmarkel44 | 0:61364762ee0e | 39 | // read the control data |
| jmarkel44 | 0:61364762ee0e | 40 | bool rc = GLOBAL_mdot->readUserFile(controlFile.c_str(), (void *)dataBuf, sizeof(dataBuf)); |
| jmarkel44 | 0:61364762ee0e | 41 | if ( rc != true ) { |
| jmarkel44 | 0:61364762ee0e | 42 | logError("%s: failed to read %s", __func__, controlFile.c_str()); |
| jmarkel44 | 0:61364762ee0e | 43 | // caller should destroy the object |
| jmarkel44 | 0:61364762ee0e | 44 | return false; |
| jmarkel44 | 0:61364762ee0e | 45 | } |
| jmarkel44 | 0:61364762ee0e | 46 | |
| jmarkel44 | 0:61364762ee0e | 47 | // validate control data |
| jmarkel44 | 0:61364762ee0e | 48 | if ( !validateControlData(dataBuf) ) { |
| jmarkel44 | 0:61364762ee0e | 49 | logError("%s: failed to validate control data", __func__); |
| jmarkel44 | 0:61364762ee0e | 50 | return false; |
| jmarkel44 | 0:61364762ee0e | 51 | } |
| jmarkel44 | 0:61364762ee0e | 52 | |
| jmarkel44 | 0:61364762ee0e | 53 | // copy control data |
| jmarkel44 | 0:61364762ee0e | 54 | copyControlData(dataBuf); |
| jmarkel44 | 0:61364762ee0e | 55 | |
| jmarkel44 | 0:61364762ee0e | 56 | // TODO: validate the list fo outputs |
| jmarkel44 | 0:61364762ee0e | 57 | |
| jmarkel44 | 0:61364762ee0e | 58 | return true; |
| jmarkel44 | 0:61364762ee0e | 59 | } |
| jmarkel44 | 0:61364762ee0e | 60 | |
| jmarkel44 | 0:61364762ee0e | 61 | // |
| jmarkel44 | 0:61364762ee0e | 62 | // method: validateControlData |
| jmarkel44 | 0:61364762ee0e | 63 | // description: validate JSON formatted control data |
| jmarkel44 | 0:61364762ee0e | 64 | // |
| jmarkel44 | 0:61364762ee0e | 65 | // @param[in] buf -> JSON formatted string |
| jmarkel44 | 0:61364762ee0e | 66 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 67 | // @return true if valid; false otherwise |
| jmarkel44 | 0:61364762ee0e | 68 | // |
| jmarkel44 | 0:61364762ee0e | 69 | |
| jmarkel44 | 0:61364762ee0e | 70 | bool CompositeControl::validateControlData(const char *buf) |
| jmarkel44 | 0:61364762ee0e | 71 | { |
| jmarkel44 | 0:61364762ee0e | 72 | bool rc = true; |
| jmarkel44 | 0:61364762ee0e | 73 | cJSON * root = cJSON_Parse(buf); |
| jmarkel44 | 0:61364762ee0e | 74 | |
| jmarkel44 | 0:61364762ee0e | 75 | if ( !cJSON_HasObjectItem(root, "id") || |
| jmarkel44 | 0:61364762ee0e | 76 | !cJSON_HasObjectItem(root, "tag") || |
| jmarkel44 | 0:61364762ee0e | 77 | !cJSON_HasObjectItem(root, "priority") || |
| jmarkel44 | 0:61364762ee0e | 78 | !cJSON_HasObjectItem(root, "ca") || |
| jmarkel44 | 0:61364762ee0e | 79 | !cJSON_HasObjectItem(root, "outputs") ) { |
| jmarkel44 | 0:61364762ee0e | 80 | logError("%s: control file missing expected tags", __func__); |
| jmarkel44 | 0:61364762ee0e | 81 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 82 | return false; |
| jmarkel44 | 0:61364762ee0e | 83 | } |
| jmarkel44 | 0:61364762ee0e | 84 | |
| jmarkel44 | 0:61364762ee0e | 85 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 86 | return rc; |
| jmarkel44 | 0:61364762ee0e | 87 | } |
| jmarkel44 | 0:61364762ee0e | 88 | |
| jmarkel44 | 0:61364762ee0e | 89 | // |
| jmarkel44 | 0:61364762ee0e | 90 | // method: copyControlData |
| jmarkel44 | 0:61364762ee0e | 91 | // description: copy the JSON formatted data |
| jmarkel44 | 0:61364762ee0e | 92 | // |
| jmarkel44 | 0:61364762ee0e | 93 | // @param[in] buf -> JSON formatted string |
| jmarkel44 | 0:61364762ee0e | 94 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 95 | // @return none |
| jmarkel44 | 0:61364762ee0e | 96 | // |
| jmarkel44 | 0:61364762ee0e | 97 | void CompositeControl::copyControlData(const char *buf) |
| jmarkel44 | 0:61364762ee0e | 98 | { |
| jmarkel44 | 0:61364762ee0e | 99 | cJSON * root = cJSON_Parse(buf); |
| jmarkel44 | 0:61364762ee0e | 100 | |
| jmarkel44 | 0:61364762ee0e | 101 | id = cJSON_GetObjectItem(root,"id")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 102 | tag = cJSON_GetObjectItem(root, "tag")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 103 | priority = atoi(cJSON_GetObjectItem(root, "priority")->valuestring); |
| jmarkel44 | 0:61364762ee0e | 104 | ca = cJSON_GetObjectItem(root, "ca")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 105 | cJSON *array = cJSON_GetObjectItem(root, "outputs"); |
| jmarkel44 | 0:61364762ee0e | 106 | for ( int i = 0; i < cJSON_GetArraySize(array); ++i ) { |
| jmarkel44 | 0:61364762ee0e | 107 | cJSON *subitem = cJSON_GetArrayItem(array, i); |
| jmarkel44 | 0:61364762ee0e | 108 | std::string tag = cJSON_GetObjectItem(subitem, "tag")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 109 | std::string response = cJSON_GetObjectItem(subitem, "responseA")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 110 | OutputElement x = { tag, response }; |
| jmarkel44 | 0:61364762ee0e | 111 | outputs.push_back(x); |
| jmarkel44 | 0:61364762ee0e | 112 | } |
| jmarkel44 | 0:61364762ee0e | 113 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 114 | } |
| jmarkel44 | 0:61364762ee0e | 115 | |
| jmarkel44 | 0:61364762ee0e | 116 | // |
| jmarkel44 | 0:61364762ee0e | 117 | // method: start |
| jmarkel44 | 0:61364762ee0e | 118 | // description: start the composite control |
| jmarkel44 | 0:61364762ee0e | 119 | // |
| jmarkel44 | 0:61364762ee0e | 120 | // @param none |
| jmarkel44 | 0:61364762ee0e | 121 | // @return none |
| jmarkel44 | 0:61364762ee0e | 122 | // |
| jmarkel44 | 0:61364762ee0e | 123 | void CompositeControl::start(void) |
| jmarkel44 | 0:61364762ee0e | 124 | { |
| jmarkel44 | 0:61364762ee0e | 125 | currentState = STATE_START; |
| jmarkel44 | 0:61364762ee0e | 126 | } |
| jmarkel44 | 0:61364762ee0e | 127 | |
| jmarkel44 | 0:61364762ee0e | 128 | // |
| jmarkel44 | 0:61364762ee0e | 129 | // method: update |
| jmarkel44 | 0:61364762ee0e | 130 | // description: updater for the composite control |
| jmarkel44 | 0:61364762ee0e | 131 | // |
| jmarkel44 | 0:61364762ee0e | 132 | // @param none |
| jmarkel44 | 0:61364762ee0e | 133 | // @return none |
| jmarkel44 | 0:61364762ee0e | 134 | // |
| jmarkel44 | 0:61364762ee0e | 135 | CompositeControlError_t CompositeControl::update(void) |
| jmarkel44 | 0:61364762ee0e | 136 | { |
| jmarkel44 | 0:61364762ee0e | 137 | CompositeControlError_t rc = COMPOSITE_CONTROL_OK; |
| jmarkel44 | 0:61364762ee0e | 138 | std::string function; |
| jmarkel44 | 0:61364762ee0e | 139 | |
| jmarkel44 | 0:61364762ee0e | 140 | switch ( currentState ) { |
| jmarkel44 | 0:61364762ee0e | 141 | case STATE_INIT: |
| jmarkel44 | 0:61364762ee0e | 142 | // do nothing |
| jmarkel44 | 0:61364762ee0e | 143 | break; |
| jmarkel44 | 0:61364762ee0e | 144 | case STATE_START: |
| jmarkel44 | 0:61364762ee0e | 145 | function = executeCommand(); |
| jmarkel44 | 0:61364762ee0e | 146 | if ( function == "responseA" ) { |
| jmarkel44 | 0:61364762ee0e | 147 | currentState = STATE_CONTROL_ON; |
| jmarkel44 | 0:61364762ee0e | 148 | triggerOutputs(function); |
| jmarkel44 | 0:61364762ee0e | 149 | } else if ( function == "nothing" ) { |
| jmarkel44 | 0:61364762ee0e | 150 | currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 0:61364762ee0e | 151 | } |
| jmarkel44 | 0:61364762ee0e | 152 | break; |
| jmarkel44 | 0:61364762ee0e | 153 | case STATE_CONTROL_ON: |
| jmarkel44 | 0:61364762ee0e | 154 | function = executeCommand(); |
| jmarkel44 | 0:61364762ee0e | 155 | if ( function == "nothing" ) { |
| jmarkel44 | 0:61364762ee0e | 156 | currentState = STATE_CONTROL_OFF; |
| jmarkel44 | 0:61364762ee0e | 157 | unregisterControls(); |
| jmarkel44 | 0:61364762ee0e | 158 | } else { |
| jmarkel44 | 0:61364762ee0e | 159 | // do nothing |
| jmarkel44 | 0:61364762ee0e | 160 | } |
| jmarkel44 | 0:61364762ee0e | 161 | break; |
| jmarkel44 | 0:61364762ee0e | 162 | case STATE_CONTROL_OFF: |
| jmarkel44 | 0:61364762ee0e | 163 | function = executeCommand(); |
| jmarkel44 | 0:61364762ee0e | 164 | if ( function == "responseA" ) { |
| jmarkel44 | 0:61364762ee0e | 165 | currentState = STATE_CONTROL_ON; |
| jmarkel44 | 0:61364762ee0e | 166 | triggerOutputs(function); |
| jmarkel44 | 0:61364762ee0e | 167 | } else { |
| jmarkel44 | 0:61364762ee0e | 168 | // do nothing |
| jmarkel44 | 0:61364762ee0e | 169 | } |
| jmarkel44 | 0:61364762ee0e | 170 | break; |
| jmarkel44 | 0:61364762ee0e | 171 | default: |
| jmarkel44 | 0:61364762ee0e | 172 | logError("%s: unknown state %d", __func__, this->currentState); |
| jmarkel44 | 0:61364762ee0e | 173 | rc = COMPOSITE_CONTROL_UNK_STATE; |
| jmarkel44 | 0:61364762ee0e | 174 | break; |
| jmarkel44 | 0:61364762ee0e | 175 | } |
| jmarkel44 | 0:61364762ee0e | 176 | return rc; |
| jmarkel44 | 0:61364762ee0e | 177 | } |
| jmarkel44 | 0:61364762ee0e | 178 | |
| jmarkel44 | 0:61364762ee0e | 179 | // |
| jmarkel44 | 0:61364762ee0e | 180 | // method: executeCommand |
| jmarkel44 | 0:61364762ee0e | 181 | // description: execute the command specified in the control algorithm |
| jmarkel44 | 0:61364762ee0e | 182 | // |
| jmarkel44 | 0:61364762ee0e | 183 | // @param none |
| jmarkel44 | 0:61364762ee0e | 184 | // @return none |
| jmarkel44 | 0:61364762ee0e | 185 | // |
| jmarkel44 | 0:61364762ee0e | 186 | std::string CompositeControl::executeCommand(void) |
| jmarkel44 | 0:61364762ee0e | 187 | { |
| jmarkel44 | 0:61364762ee0e | 188 | // look up the algorithm |
| jmarkel44 | 0:61364762ee0e | 189 | StringAlgorithmMap::const_iterator pos; |
| jmarkel44 | 0:61364762ee0e | 190 | pos = algorithmTable.find(this->ca); |
| jmarkel44 | 0:61364762ee0e | 191 | if ( pos != algorithmTable.end() ) { |
| jmarkel44 | 0:61364762ee0e | 192 | // we found the control algorithm |
| jmarkel44 | 0:61364762ee0e | 193 | return this->executeOperation(pos->second); |
| jmarkel44 | 0:61364762ee0e | 194 | } |
| jmarkel44 | 0:61364762ee0e | 195 | return "nothing"; |
| jmarkel44 | 0:61364762ee0e | 196 | } |
| jmarkel44 | 0:61364762ee0e | 197 | |
| jmarkel44 | 0:61364762ee0e | 198 | // |
| jmarkel44 | 0:61364762ee0e | 199 | // method: executeOperation |
| jmarkel44 | 0:61364762ee0e | 200 | // description: execute an operations from the control equation |
| jmarkel44 | 0:61364762ee0e | 201 | // |
| jmarkel44 | 0:61364762ee0e | 202 | // @param[in] ca -> composite control algorithm |
| jmarkel44 | 0:61364762ee0e | 203 | // @return string to the result |
| jmarkel44 | 0:61364762ee0e | 204 | // |
| jmarkel44 | 0:61364762ee0e | 205 | std::string CompositeControl::executeOperation(const CompositeAlgorithm *ca) |
| jmarkel44 | 0:61364762ee0e | 206 | { |
| jmarkel44 | 0:61364762ee0e | 207 | // (this->tag) <op> <opr> = <result> |
| jmarkel44 | 0:61364762ee0e | 208 | // |
| jmarkel44 | 0:61364762ee0e | 209 | // example: |
| jmarkel44 | 0:61364762ee0e | 210 | // this->tag = "i_flowswitch" |
| jmarkel44 | 0:61364762ee0e | 211 | // opr = "1" |
| jmarkel44 | 0:61364762ee0e | 212 | // op = "==" |
| jmarkel44 | 0:61364762ee0e | 213 | // if true return "responseA" else return "nothing" |
| jmarkel44 | 0:61364762ee0e | 214 | |
| jmarkel44 | 0:61364762ee0e | 215 | ModbusValue value; |
| jmarkel44 | 0:61364762ee0e | 216 | bool rc = ModbusMasterReadRegister(tag,&value); |
| jmarkel44 | 0:61364762ee0e | 217 | if ( rc != true ) { |
| jmarkel44 | 0:61364762ee0e | 218 | logError("%s cannot find tag", __func__); |
| jmarkel44 | 0:61364762ee0e | 219 | return "nothing"; |
| jmarkel44 | 0:61364762ee0e | 220 | } |
| jmarkel44 | 0:61364762ee0e | 221 | |
| jmarkel44 | 0:61364762ee0e | 222 | // equal to operator |
| jmarkel44 | 0:61364762ee0e | 223 | if ( ca->getOp() == "==" ) { |
| jmarkel44 | 0:61364762ee0e | 224 | // perform the equality operation |
| jmarkel44 | 0:61364762ee0e | 225 | if ( value.value == atof(ca->getOpr().c_str()) ) { |
| jmarkel44 | 0:61364762ee0e | 226 | return ca->getResultTrue(); |
| jmarkel44 | 0:61364762ee0e | 227 | } else { |
| jmarkel44 | 0:61364762ee0e | 228 | return ca->getResultFalse(); |
| jmarkel44 | 0:61364762ee0e | 229 | } |
| jmarkel44 | 0:61364762ee0e | 230 | } |
| jmarkel44 | 0:61364762ee0e | 231 | if ( ca->getOp() == ">=" ) { |
| jmarkel44 | 0:61364762ee0e | 232 | if ( value.value >= atof(ca->getOpr().c_str()) ) { |
| jmarkel44 | 0:61364762ee0e | 233 | return ca->getResultTrue(); |
| jmarkel44 | 0:61364762ee0e | 234 | } else { |
| jmarkel44 | 0:61364762ee0e | 235 | return ca->getResultFalse(); |
| jmarkel44 | 0:61364762ee0e | 236 | } |
| jmarkel44 | 0:61364762ee0e | 237 | } |
| jmarkel44 | 0:61364762ee0e | 238 | |
| jmarkel44 | 0:61364762ee0e | 239 | if ( ca->getOp() == "&" ) { |
| jmarkel44 | 0:61364762ee0e | 240 | if ( (int)value.value & (int)atoi(ca->getOpr().c_str()) ) { |
| jmarkel44 | 0:61364762ee0e | 241 | return ca->getResultTrue(); |
| jmarkel44 | 0:61364762ee0e | 242 | } else { |
| jmarkel44 | 0:61364762ee0e | 243 | return ca->getResultFalse(); |
| jmarkel44 | 0:61364762ee0e | 244 | } |
| jmarkel44 | 0:61364762ee0e | 245 | } |
| jmarkel44 | 0:61364762ee0e | 246 | |
| jmarkel44 | 0:61364762ee0e | 247 | // addition operator |
| jmarkel44 | 0:61364762ee0e | 248 | if ( ca->getOp() == "+" ) { |
| jmarkel44 | 0:61364762ee0e | 249 | // TODO |
| jmarkel44 | 0:61364762ee0e | 250 | } |
| jmarkel44 | 0:61364762ee0e | 251 | // multiply operator |
| jmarkel44 | 0:61364762ee0e | 252 | if ( ca->getOp() == "*" ) { |
| jmarkel44 | 0:61364762ee0e | 253 | // TODO: |
| jmarkel44 | 0:61364762ee0e | 254 | } |
| jmarkel44 | 0:61364762ee0e | 255 | // subtraction operator |
| jmarkel44 | 0:61364762ee0e | 256 | if ( ca->getOp() == "-" ) { |
| jmarkel44 | 0:61364762ee0e | 257 | // TODO: |
| jmarkel44 | 0:61364762ee0e | 258 | } |
| jmarkel44 | 0:61364762ee0e | 259 | |
| jmarkel44 | 0:61364762ee0e | 260 | return "nothing"; |
| jmarkel44 | 0:61364762ee0e | 261 | } |
| jmarkel44 | 0:61364762ee0e | 262 | |
| jmarkel44 | 0:61364762ee0e | 263 | // |
| jmarkel44 | 0:61364762ee0e | 264 | // method: triggerOutputs |
| jmarkel44 | 0:61364762ee0e | 265 | // description: trigger the output(s) to do something |
| jmarkel44 | 0:61364762ee0e | 266 | // |
| jmarkel44 | 0:61364762ee0e | 267 | // @param[in] result -> the result of the operation |
| jmarkel44 | 0:61364762ee0e | 268 | // @return none |
| jmarkel44 | 0:61364762ee0e | 269 | // |
| jmarkel44 | 0:61364762ee0e | 270 | void CompositeControl::triggerOutputs(std::string result) |
| jmarkel44 | 0:61364762ee0e | 271 | { |
| jmarkel44 | 0:61364762ee0e | 272 | |
| jmarkel44 | 0:61364762ee0e | 273 | // loop through the list |
| jmarkel44 | 0:61364762ee0e | 274 | StringAlgorithmMap::const_iterator pos; |
| jmarkel44 | 0:61364762ee0e | 275 | pos = algorithmTable.find(this->ca); |
| jmarkel44 | 0:61364762ee0e | 276 | if ( pos != algorithmTable.end() ) { |
| jmarkel44 | 0:61364762ee0e | 277 | std::vector<OutputElement>::const_iterator it; |
| jmarkel44 | 0:61364762ee0e | 278 | for ( it = outputs.begin(); it != outputs.end(); ++it ) { |
| jmarkel44 | 0:61364762ee0e | 279 | if ( it->response == "fixed off" ) { |
| jmarkel44 | 0:61364762ee0e | 280 | printf("\rSending an OFF control for %s\n", it->tag.c_str()); |
| jmarkel44 | 0:61364762ee0e | 281 | sendMail(it->tag, ACTION_CONTROL_OFF); |
| jmarkel44 | 0:61364762ee0e | 282 | } else if ( it->response == "fixed on" ) { |
| jmarkel44 | 0:61364762ee0e | 283 | printf("\rSending an ON request for %s\n", it->tag.c_str()); |
| jmarkel44 | 0:61364762ee0e | 284 | sendMail(it->tag, ACTION_CONTROL_ON); |
| jmarkel44 | 0:61364762ee0e | 285 | } |
| jmarkel44 | 0:61364762ee0e | 286 | } |
| jmarkel44 | 0:61364762ee0e | 287 | } else { |
| jmarkel44 | 0:61364762ee0e | 288 | logError("%s: failed to find the control algorithm %s\n", __func__, this->ca.c_str()); |
| jmarkel44 | 0:61364762ee0e | 289 | } |
| jmarkel44 | 0:61364762ee0e | 290 | } |
| jmarkel44 | 0:61364762ee0e | 291 | |
| jmarkel44 | 0:61364762ee0e | 292 | // |
| jmarkel44 | 0:61364762ee0e | 293 | // method: sendMail |
| jmarkel44 | 0:61364762ee0e | 294 | // description: send mail to the output task |
| jmarkel44 | 0:61364762ee0e | 295 | // |
| jmarkel44 | 0:61364762ee0e | 296 | // @param[in] io_tag -> input/output tag |
| jmarkel44 | 0:61364762ee0e | 297 | // @param[in] action -> ON, OFF, UNREGISTER |
| jmarkel44 | 0:61364762ee0e | 298 | // @return none |
| jmarkel44 | 0:61364762ee0e | 299 | // |
| jmarkel44 | 0:61364762ee0e | 300 | void CompositeControl::sendMail(const std::string io_tag, OutputAction action) |
| jmarkel44 | 0:61364762ee0e | 301 | { |
| jmarkel44 | 0:61364762ee0e | 302 | logInfo("%s: composite control attempting to send action %d\n", |
| jmarkel44 | 0:61364762ee0e | 303 | __func__, action); |
| jmarkel44 | 0:61364762ee0e | 304 | |
| jmarkel44 | 0:61364762ee0e | 305 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
| jmarkel44 | 0:61364762ee0e | 306 | memset(output_mail, 0, sizeof(OutputControlMsg_t)); |
| jmarkel44 | 0:61364762ee0e | 307 | |
| jmarkel44 | 0:61364762ee0e | 308 | output_mail->action = action; |
| jmarkel44 | 0:61364762ee0e | 309 | output_mail->controlType = CONTROL_COMPOSITE; |
| jmarkel44 | 0:61364762ee0e | 310 | output_mail->priority = this->priority; |
| jmarkel44 | 0:61364762ee0e | 311 | |
| jmarkel44 | 0:61364762ee0e | 312 | strncpy(output_mail->input_tag, this->tag.c_str(), sizeof(output_mail->input_tag)-1); |
| jmarkel44 | 0:61364762ee0e | 313 | strncpy(output_mail->output_tag, io_tag.c_str(), sizeof(output_mail->output_tag)-1); |
| jmarkel44 | 0:61364762ee0e | 314 | strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1); |
| jmarkel44 | 0:61364762ee0e | 315 | |
| jmarkel44 | 0:61364762ee0e | 316 | OutputMasterMailBox.put(output_mail); |
| jmarkel44 | 0:61364762ee0e | 317 | } |
| jmarkel44 | 0:61364762ee0e | 318 | |
| jmarkel44 | 0:61364762ee0e | 319 | // |
| jmarkel44 | 0:61364762ee0e | 320 | // method: unregisterControls |
| jmarkel44 | 0:61364762ee0e | 321 | // description: unregister the control with the output task |
| jmarkel44 | 0:61364762ee0e | 322 | // |
| jmarkel44 | 0:61364762ee0e | 323 | // @param none |
| jmarkel44 | 0:61364762ee0e | 324 | // @return none |
| jmarkel44 | 0:61364762ee0e | 325 | // |
| jmarkel44 | 0:61364762ee0e | 326 | void CompositeControl::unregisterControls(void) |
| jmarkel44 | 0:61364762ee0e | 327 | { |
| jmarkel44 | 0:61364762ee0e | 328 | // loop through the list |
| jmarkel44 | 0:61364762ee0e | 329 | StringAlgorithmMap::const_iterator pos; |
| jmarkel44 | 0:61364762ee0e | 330 | pos = algorithmTable.find(this->ca); |
| jmarkel44 | 0:61364762ee0e | 331 | if ( pos != algorithmTable.end() ) { |
| jmarkel44 | 0:61364762ee0e | 332 | std::vector<OutputElement>::const_iterator it; |
| jmarkel44 | 0:61364762ee0e | 333 | for ( it = outputs.begin(); it != outputs.end(); ++it ) { |
| jmarkel44 | 0:61364762ee0e | 334 | sendMail(it->tag, ACTION_CONTROL_UNREGISTER); |
| jmarkel44 | 0:61364762ee0e | 335 | } |
| jmarkel44 | 0:61364762ee0e | 336 | } else { |
| jmarkel44 | 0:61364762ee0e | 337 | logError("%s: failed to find the control algorithm %s\n", __func__, this->ca.c_str()); |
| jmarkel44 | 0:61364762ee0e | 338 | } |
| jmarkel44 | 0:61364762ee0e | 339 | } |
| jmarkel44 | 0:61364762ee0e | 340 | |
| jmarkel44 | 0:61364762ee0e | 341 | // |
| jmarkel44 | 0:61364762ee0e | 342 | // method: display |
| jmarkel44 | 0:61364762ee0e | 343 | // description: display the pertinents |
| jmarkel44 | 0:61364762ee0e | 344 | // |
| jmarkel44 | 0:61364762ee0e | 345 | // @param none |
| jmarkel44 | 0:61364762ee0e | 346 | // @return none |
| jmarkel44 | 0:61364762ee0e | 347 | // |
| jmarkel44 | 0:61364762ee0e | 348 | void CompositeControl::display(void) |
| jmarkel44 | 0:61364762ee0e | 349 | { |
| jmarkel44 | 0:61364762ee0e | 350 | const char *mapper[] = { "INIT", |
| jmarkel44 | 0:61364762ee0e | 351 | "START", |
| jmarkel44 | 0:61364762ee0e | 352 | "CONTROL_OFF", |
| jmarkel44 | 0:61364762ee0e | 353 | "CONTROL_ON" |
| jmarkel44 | 0:61364762ee0e | 354 | }; |
| jmarkel44 | 0:61364762ee0e | 355 | |
| jmarkel44 | 0:61364762ee0e | 356 | printf("\r\n"); |
| jmarkel44 | 0:61364762ee0e | 357 | std::cout << left << setw(10) << setfill(' ') << "composite: "; |
| jmarkel44 | 0:61364762ee0e | 358 | std::cout << left << setw(40) << setfill(' ') << controlFile; |
| jmarkel44 | 0:61364762ee0e | 359 | std::cout << left << setw(20) << setfill(' ') << id; |
| jmarkel44 | 0:61364762ee0e | 360 | std::cout << left << setw(20) << setfill(' ') << tag; |
| jmarkel44 | 0:61364762ee0e | 361 | std::cout << left << setw(6) << setfill(' ') << priority; |
| jmarkel44 | 0:61364762ee0e | 362 | std::cout << left << setw(20) << setfill(' ') << ca; |
| jmarkel44 | 0:61364762ee0e | 363 | std::cout << left << setw(16) << setfill(' ') << mapper[currentState]; |
| jmarkel44 | 0:61364762ee0e | 364 | |
| jmarkel44 | 0:61364762ee0e | 365 | vector<OutputElement>::const_iterator it; |
| jmarkel44 | 0:61364762ee0e | 366 | for ( it = outputs.begin(); it != outputs.end(); ++it ) { |
| jmarkel44 | 0:61364762ee0e | 367 | std::cout << left << (*it).tag << ":" << (*it).response << " "; |
| jmarkel44 | 0:61364762ee0e | 368 | } |
| jmarkel44 | 0:61364762ee0e | 369 | |
| jmarkel44 | 0:61364762ee0e | 370 | std::cout.flush(); |
| jmarkel44 | 0:61364762ee0e | 371 | |
| jmarkel44 | 0:61364762ee0e | 372 | } |