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/OutputTask/OutputTask.cpp@94:18ae2fd82638, 2016-09-20 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Sep 20 19:47:04 2016 +0000
- Revision:
- 94:18ae2fd82638
- Parent:
- 86:189c125d8878
- Child:
- 95:42f92b03f1b8
write the relay;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 70:7427f4959201 | 1 | /****************************************************************************** |
jmarkel44 | 70:7427f4959201 | 2 | * |
jmarkel44 | 70:7427f4959201 | 3 | * File: OutputTask.cpp |
jmarkel44 | 70:7427f4959201 | 4 | * Desciption: source for the ICE Output task |
jmarkel44 | 70:7427f4959201 | 5 | * |
jmarkel44 | 70:7427f4959201 | 6 | *****************************************************************************/ |
jmarkel44 | 67:49f266601d83 | 7 | #include "OutputTask.h" |
jmarkel44 | 48:1c7861d80d16 | 8 | #include "global.h" |
jmarkel44 | 66:db1425574b58 | 9 | #include "MbedJSONValue.h" |
jmarkel44 | 94:18ae2fd82638 | 10 | #include "ModbusMasterApi.h" |
jmarkel44 | 70:7427f4959201 | 11 | #include <vector> |
jmarkel44 | 77:43e0a3d9e536 | 12 | #include <string> |
jmarkel44 | 77:43e0a3d9e536 | 13 | #include <algorithm> |
jmarkel44 | 48:1c7861d80d16 | 14 | |
jmarkel44 | 84:7b7cad3ba139 | 15 | // local functions |
jmarkel44 | 67:49f266601d83 | 16 | static int createOutput(const char *controlFile); |
jmarkel44 | 66:db1425574b58 | 17 | static void loadPersistentOutputs(void); |
jmarkel44 | 84:7b7cad3ba139 | 18 | static void refreshOutputs(void); |
jmarkel44 | 77:43e0a3d9e536 | 19 | static int enableOutputReq (const char* id, unsigned int pri, const char* output); |
jmarkel44 | 77:43e0a3d9e536 | 20 | static int disableOutputReq (const char *id, unsigned int pri, const char *output); |
jmarkel44 | 72:3754b352f156 | 21 | static int unregisterControl(const char *id, unsigned int pri, const char *output); |
jmarkel44 | 71:34856d21f2bf | 22 | |
jmarkel44 | 84:7b7cad3ba139 | 23 | // The Output Map |
jmarkel44 | 84:7b7cad3ba139 | 24 | // |
jmarkel44 | 84:7b7cad3ba139 | 25 | // this is the main data structure used to distinguish which control has |
jmarkel44 | 84:7b7cad3ba139 | 26 | // priority of an output. the layout is as-follows: |
jmarkel44 | 84:7b7cad3ba139 | 27 | // |
jmarkel44 | 84:7b7cad3ba139 | 28 | // outputMap["o_rly1"]-> Control<"ManCtrl_rly1", 100, ON > |
jmarkel44 | 84:7b7cad3ba139 | 29 | // Control<"SetpointCtrl_rly1", 800, OFF> |
jmarkel44 | 84:7b7cad3ba139 | 30 | // outputMap["o_rly2"]-> Control<"SetpointCtrl_rly2", 800, ON > |
jmarkel44 | 84:7b7cad3ba139 | 31 | // outputMap["o_rly3"]-> Control<"TimerControl_rl3", 500, ON> |
jmarkel44 | 84:7b7cad3ba139 | 32 | // |
jmarkel44 | 84:7b7cad3ba139 | 33 | // The Control Vector (per relay) is always sorted by priority, whereas |
jmarkel44 | 84:7b7cad3ba139 | 34 | // the highest priority control is at the beginning (v.begin()) of the |
jmarkel44 | 84:7b7cad3ba139 | 35 | // list. |
jmarkel44 | 84:7b7cad3ba139 | 36 | |
jmarkel44 | 71:34856d21f2bf | 37 | typedef std::map<string, vector<Control> > StringOutputVector_t; |
jmarkel44 | 77:43e0a3d9e536 | 38 | StringOutputVector_t outputMap; |
jmarkel44 | 66:db1425574b58 | 39 | |
jmarkel44 | 84:7b7cad3ba139 | 40 | // operator for sorting the outputs vectors per output |
jmarkel44 | 80:b12b0adfcdc2 | 41 | bool operator<(const Control &control1, const Control &control2) |
jmarkel44 | 80:b12b0adfcdc2 | 42 | { |
jmarkel44 | 77:43e0a3d9e536 | 43 | return control1.getPriority() < control2.getPriority(); |
jmarkel44 | 77:43e0a3d9e536 | 44 | } |
jmarkel44 | 63:0ded43237b22 | 45 | |
jmarkel44 | 70:7427f4959201 | 46 | /***************************************************************************** |
jmarkel44 | 70:7427f4959201 | 47 | * Function: OutputTask |
jmarkel44 | 70:7427f4959201 | 48 | * Description: Main entry point for the Output Task |
jmarkel44 | 70:7427f4959201 | 49 | * |
jmarkel44 | 70:7427f4959201 | 50 | * @param args -> not used |
jmarkel44 | 70:7427f4959201 | 51 | * @return none |
jmarkel44 | 70:7427f4959201 | 52 | *****************************************************************************/ |
jmarkel44 | 48:1c7861d80d16 | 53 | void OutputTask(void const *args) |
jmarkel44 | 48:1c7861d80d16 | 54 | { |
jmarkel44 | 80:b12b0adfcdc2 | 55 | int rc; |
jmarkel44 | 51:66b820f203a5 | 56 | UNUSED(args); |
jmarkel44 | 70:7427f4959201 | 57 | |
jmarkel44 | 67:49f266601d83 | 58 | printf("\r%s has started...\n", __func__); |
jmarkel44 | 63:0ded43237b22 | 59 | |
jmarkel44 | 66:db1425574b58 | 60 | loadPersistentOutputs(); |
jmarkel44 | 75:96512ccc0443 | 61 | osSignalSet(mainThreadId, sig_output_continue); |
jmarkel44 | 75:96512ccc0443 | 62 | |
jmarkel44 | 48:1c7861d80d16 | 63 | while (true) { |
jmarkel44 | 51:66b820f203a5 | 64 | // wait for an event |
jmarkel44 | 56:225786c56315 | 65 | osEvent evt = OutputMasterMailBox.get(); |
jmarkel44 | 51:66b820f203a5 | 66 | if (evt.status == osEventMail) { |
jmarkel44 | 80:b12b0adfcdc2 | 67 | |
jmarkel44 | 63:0ded43237b22 | 68 | OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p; |
jmarkel44 | 63:0ded43237b22 | 69 | |
jmarkel44 | 63:0ded43237b22 | 70 | switch ( msg->action ) { |
jmarkel44 | 63:0ded43237b22 | 71 | case ACTION_NEW: |
jmarkel44 | 63:0ded43237b22 | 72 | // read the file and and create an output entry |
jmarkel44 | 80:b12b0adfcdc2 | 73 | rc = createOutput(msg->controlFile); |
jmarkel44 | 80:b12b0adfcdc2 | 74 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 75 | logError("%s: failed to create output %s\n", |
jmarkel44 | 80:b12b0adfcdc2 | 76 | __func__, msg->controlFile); |
jmarkel44 | 80:b12b0adfcdc2 | 77 | } |
jmarkel44 | 63:0ded43237b22 | 78 | break; |
jmarkel44 | 71:34856d21f2bf | 79 | case ACTION_CONTROL_ON: |
jmarkel44 | 77:43e0a3d9e536 | 80 | logInfo("%s is requesting ON control of %s", msg->id, msg->output); |
jmarkel44 | 80:b12b0adfcdc2 | 81 | rc = enableOutputReq(msg->id, msg->priority, msg->output); |
jmarkel44 | 80:b12b0adfcdc2 | 82 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 83 | logError("%s: failed to enabled output for %s", |
jmarkel44 | 80:b12b0adfcdc2 | 84 | __func__, msg->id); |
jmarkel44 | 80:b12b0adfcdc2 | 85 | } |
jmarkel44 | 71:34856d21f2bf | 86 | break; |
jmarkel44 | 71:34856d21f2bf | 87 | case ACTION_CONTROL_OFF: |
jmarkel44 | 77:43e0a3d9e536 | 88 | logInfo("%s is requesting OFF control of %s", msg->id, msg->output); |
jmarkel44 | 80:b12b0adfcdc2 | 89 | rc = disableOutputReq(msg->id, msg->priority, msg->output); |
jmarkel44 | 80:b12b0adfcdc2 | 90 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 91 | printf("%s: failed to disabled output for %s", |
jmarkel44 | 80:b12b0adfcdc2 | 92 | __func__, msg->id); |
jmarkel44 | 80:b12b0adfcdc2 | 93 | } |
jmarkel44 | 71:34856d21f2bf | 94 | break; |
jmarkel44 | 72:3754b352f156 | 95 | case ACTION_CONTROL_UNREGISTER: |
jmarkel44 | 77:43e0a3d9e536 | 96 | logInfo("%s is requesting its deletion from %s", msg->id, msg->output); |
jmarkel44 | 80:b12b0adfcdc2 | 97 | rc = unregisterControl(msg->id, msg->priority, msg->output); |
jmarkel44 | 80:b12b0adfcdc2 | 98 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 99 | printf("%s: failed to unregister control %s", |
jmarkel44 | 84:7b7cad3ba139 | 100 | __func__, msg->id); |
jmarkel44 | 80:b12b0adfcdc2 | 101 | } |
jmarkel44 | 72:3754b352f156 | 102 | break; |
jmarkel44 | 63:0ded43237b22 | 103 | default: |
jmarkel44 | 63:0ded43237b22 | 104 | break; |
jmarkel44 | 63:0ded43237b22 | 105 | } |
jmarkel44 | 63:0ded43237b22 | 106 | |
jmarkel44 | 56:225786c56315 | 107 | // free the message |
jmarkel44 | 56:225786c56315 | 108 | OutputMasterMailBox.free(msg); |
jmarkel44 | 86:189c125d8878 | 109 | |
jmarkel44 | 86:189c125d8878 | 110 | // refresh the outputs |
jmarkel44 | 84:7b7cad3ba139 | 111 | refreshOutputs(); |
jmarkel44 | 84:7b7cad3ba139 | 112 | } |
jmarkel44 | 84:7b7cad3ba139 | 113 | } |
jmarkel44 | 84:7b7cad3ba139 | 114 | } |
jmarkel44 | 84:7b7cad3ba139 | 115 | |
jmarkel44 | 84:7b7cad3ba139 | 116 | |
jmarkel44 | 84:7b7cad3ba139 | 117 | /***************************************************************************** |
jmarkel44 | 84:7b7cad3ba139 | 118 | * Function: refreshOutputs |
jmarkel44 | 84:7b7cad3ba139 | 119 | * Description: send a message to the modbus master of who's in control |
jmarkel44 | 84:7b7cad3ba139 | 120 | * |
jmarkel44 | 84:7b7cad3ba139 | 121 | * |
jmarkel44 | 84:7b7cad3ba139 | 122 | * @param args -> not used |
jmarkel44 | 84:7b7cad3ba139 | 123 | * @return none |
jmarkel44 | 84:7b7cad3ba139 | 124 | *****************************************************************************/ |
jmarkel44 | 84:7b7cad3ba139 | 125 | static void refreshOutputs(void) |
jmarkel44 | 84:7b7cad3ba139 | 126 | { |
jmarkel44 | 84:7b7cad3ba139 | 127 | //who's in control here? |
jmarkel44 | 84:7b7cad3ba139 | 128 | StringOutputVector_t::iterator pos; |
jmarkel44 | 84:7b7cad3ba139 | 129 | |
jmarkel44 | 84:7b7cad3ba139 | 130 | for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) { |
jmarkel44 | 84:7b7cad3ba139 | 131 | if ( pos->second.empty() ) { |
jmarkel44 | 84:7b7cad3ba139 | 132 | printf("\r%s: No controls for %s\n", __func__, pos->first.c_str()); |
jmarkel44 | 84:7b7cad3ba139 | 133 | } else { |
jmarkel44 | 84:7b7cad3ba139 | 134 | // a control is tied to this output |
jmarkel44 | 84:7b7cad3ba139 | 135 | printf("\r%s: %s is controlling %s\n", |
jmarkel44 | 84:7b7cad3ba139 | 136 | __func__, pos->second.begin()->getId().c_str(), pos->first.c_str()); |
jmarkel44 | 94:18ae2fd82638 | 137 | ModbusMasterWriteRegister(pos->first, pos->second.begin()->getState()); |
jmarkel44 | 51:66b820f203a5 | 138 | } |
jmarkel44 | 48:1c7861d80d16 | 139 | } |
jmarkel44 | 66:db1425574b58 | 140 | } |
jmarkel44 | 66:db1425574b58 | 141 | |
jmarkel44 | 70:7427f4959201 | 142 | /***************************************************************************** |
jmarkel44 | 70:7427f4959201 | 143 | * Function: DisplayOutputs |
jmarkel44 | 70:7427f4959201 | 144 | * Description: Display a list of outputs and its controls |
jmarkel44 | 70:7427f4959201 | 145 | * |
jmarkel44 | 70:7427f4959201 | 146 | * @param args -> not used |
jmarkel44 | 70:7427f4959201 | 147 | * @return none |
jmarkel44 | 70:7427f4959201 | 148 | *****************************************************************************/ |
jmarkel44 | 66:db1425574b58 | 149 | void DisplayOutputs(void) |
jmarkel44 | 66:db1425574b58 | 150 | { |
jmarkel44 | 66:db1425574b58 | 151 | StringOutputVector_t::iterator pos; |
jmarkel44 | 66:db1425574b58 | 152 | |
jmarkel44 | 66:db1425574b58 | 153 | for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) { |
jmarkel44 | 71:34856d21f2bf | 154 | if ( pos->second.empty() ) { |
jmarkel44 | 71:34856d21f2bf | 155 | printf("\r [%s]->[no controls]\n", pos->first.c_str()); |
jmarkel44 | 71:34856d21f2bf | 156 | } else { |
jmarkel44 | 71:34856d21f2bf | 157 | printf("\r [%s]->", pos->first.c_str()); |
jmarkel44 | 71:34856d21f2bf | 158 | vector<Control>::iterator i; |
jmarkel44 | 71:34856d21f2bf | 159 | for ( i = pos->second.begin(); i != pos->second.end(); ++i ) { |
jmarkel44 | 71:34856d21f2bf | 160 | i->display(); |
jmarkel44 | 71:34856d21f2bf | 161 | } |
jmarkel44 | 71:34856d21f2bf | 162 | printf("\n"); |
jmarkel44 | 71:34856d21f2bf | 163 | } |
jmarkel44 | 66:db1425574b58 | 164 | } |
jmarkel44 | 66:db1425574b58 | 165 | printf("\r\n"); |
jmarkel44 | 66:db1425574b58 | 166 | } |
jmarkel44 | 66:db1425574b58 | 167 | |
jmarkel44 | 71:34856d21f2bf | 168 | |
jmarkel44 | 71:34856d21f2bf | 169 | /***************************************************************************** |
jmarkel44 | 72:3754b352f156 | 170 | * Function: createOutput |
jmarkel44 | 72:3754b352f156 | 171 | * Description: |
jmarkel44 | 71:34856d21f2bf | 172 | * |
jmarkel44 | 77:43e0a3d9e536 | 173 | * @param controlFile -> name of output file |
jmarkel44 | 71:34856d21f2bf | 174 | * @return none |
jmarkel44 | 71:34856d21f2bf | 175 | *****************************************************************************/ |
jmarkel44 | 77:43e0a3d9e536 | 176 | static int createOutput(const char *outputFile) |
jmarkel44 | 66:db1425574b58 | 177 | { |
jmarkel44 | 66:db1425574b58 | 178 | char dataBuf[1024]; |
jmarkel44 | 77:43e0a3d9e536 | 179 | int status = GLOBAL_mdot->readUserFile(outputFile, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 67:49f266601d83 | 180 | if ( status != true ) { |
jmarkel44 | 77:43e0a3d9e536 | 181 | logError("%s failed to read %s", __func__, outputFile); |
jmarkel44 | 66:db1425574b58 | 182 | return -1; |
jmarkel44 | 66:db1425574b58 | 183 | } |
jmarkel44 | 66:db1425574b58 | 184 | |
jmarkel44 | 66:db1425574b58 | 185 | MbedJSONValue json_value; |
jmarkel44 | 66:db1425574b58 | 186 | parse(json_value, dataBuf); |
jmarkel44 | 66:db1425574b58 | 187 | |
jmarkel44 | 66:db1425574b58 | 188 | // extract the relay information |
jmarkel44 | 66:db1425574b58 | 189 | string id = json_value["id"].get<string>(); |
jmarkel44 | 66:db1425574b58 | 190 | |
jmarkel44 | 77:43e0a3d9e536 | 191 | // maps don't allow duplicates, and the vector is empty for now |
jmarkel44 | 71:34856d21f2bf | 192 | vector<Control> v; |
jmarkel44 | 71:34856d21f2bf | 193 | outputMap[id] = v; |
jmarkel44 | 71:34856d21f2bf | 194 | |
jmarkel44 | 71:34856d21f2bf | 195 | return 0; |
jmarkel44 | 71:34856d21f2bf | 196 | } |
jmarkel44 | 71:34856d21f2bf | 197 | |
jmarkel44 | 71:34856d21f2bf | 198 | |
jmarkel44 | 71:34856d21f2bf | 199 | /***************************************************************************** |
jmarkel44 | 72:3754b352f156 | 200 | * Function: enableOutputReq |
jmarkel44 | 71:34856d21f2bf | 201 | * Description: Display a list of outputs and its controls |
jmarkel44 | 71:34856d21f2bf | 202 | * |
jmarkel44 | 71:34856d21f2bf | 203 | * @param args -> not used |
jmarkel44 | 71:34856d21f2bf | 204 | * @return none |
jmarkel44 | 71:34856d21f2bf | 205 | *****************************************************************************/ |
jmarkel44 | 71:34856d21f2bf | 206 | static int enableOutputReq(const char *id, unsigned int priority, const char *output) |
jmarkel44 | 71:34856d21f2bf | 207 | { |
jmarkel44 | 71:34856d21f2bf | 208 | // attempt to find the output in the map |
jmarkel44 | 71:34856d21f2bf | 209 | StringOutputVector_t::iterator pos; |
jmarkel44 | 71:34856d21f2bf | 210 | |
jmarkel44 | 71:34856d21f2bf | 211 | pos = outputMap.find(output); |
jmarkel44 | 71:34856d21f2bf | 212 | if ( pos == outputMap.end() ) { |
jmarkel44 | 71:34856d21f2bf | 213 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 71:34856d21f2bf | 214 | __func__, output); |
jmarkel44 | 71:34856d21f2bf | 215 | return -1; |
jmarkel44 | 71:34856d21f2bf | 216 | } |
jmarkel44 | 71:34856d21f2bf | 217 | |
jmarkel44 | 71:34856d21f2bf | 218 | if ( pos->second.empty() ) { |
jmarkel44 | 71:34856d21f2bf | 219 | // this is a new request |
jmarkel44 | 71:34856d21f2bf | 220 | string cid(id); |
jmarkel44 | 71:34856d21f2bf | 221 | Control c(cid, priority, CONTROL_ON); |
jmarkel44 | 71:34856d21f2bf | 222 | pos->second.push_back(c); |
jmarkel44 | 71:34856d21f2bf | 223 | } else { |
jmarkel44 | 71:34856d21f2bf | 224 | // find this control in the list |
jmarkel44 | 71:34856d21f2bf | 225 | vector<Control>::iterator v; |
jmarkel44 | 71:34856d21f2bf | 226 | for ( v = pos->second.begin(); v != pos->second.end(); ++v ) { |
jmarkel44 | 71:34856d21f2bf | 227 | if ( strcmp(v->getId().c_str(), id) == 0 ) { |
jmarkel44 | 71:34856d21f2bf | 228 | v->setState(CONTROL_ON); |
jmarkel44 | 77:43e0a3d9e536 | 229 | break; |
jmarkel44 | 71:34856d21f2bf | 230 | } |
jmarkel44 | 71:34856d21f2bf | 231 | } |
jmarkel44 | 77:43e0a3d9e536 | 232 | if ( v == pos->second.end() ) { |
jmarkel44 | 77:43e0a3d9e536 | 233 | // this is a new request, so add it and sort the vector |
jmarkel44 | 77:43e0a3d9e536 | 234 | string cid(id); |
jmarkel44 | 77:43e0a3d9e536 | 235 | Control c(cid, priority, CONTROL_ON); |
jmarkel44 | 77:43e0a3d9e536 | 236 | pos->second.push_back(c); |
jmarkel44 | 77:43e0a3d9e536 | 237 | std::sort(pos->second.begin(), pos->second.end()); |
jmarkel44 | 77:43e0a3d9e536 | 238 | } |
jmarkel44 | 71:34856d21f2bf | 239 | } |
jmarkel44 | 71:34856d21f2bf | 240 | |
jmarkel44 | 71:34856d21f2bf | 241 | return 0; |
jmarkel44 | 71:34856d21f2bf | 242 | } |
jmarkel44 | 71:34856d21f2bf | 243 | |
jmarkel44 | 71:34856d21f2bf | 244 | /***************************************************************************** |
jmarkel44 | 72:3754b352f156 | 245 | * Function: disableOutputReq |
jmarkel44 | 72:3754b352f156 | 246 | * Description: |
jmarkel44 | 71:34856d21f2bf | 247 | * |
jmarkel44 | 71:34856d21f2bf | 248 | * @param args -> not used |
jmarkel44 | 71:34856d21f2bf | 249 | * @return none |
jmarkel44 | 71:34856d21f2bf | 250 | *****************************************************************************/ |
jmarkel44 | 71:34856d21f2bf | 251 | static int disableOutputReq(const char *id, unsigned int priority, const char *output) |
jmarkel44 | 71:34856d21f2bf | 252 | { |
jmarkel44 | 71:34856d21f2bf | 253 | // attempt to find the output in the map |
jmarkel44 | 71:34856d21f2bf | 254 | StringOutputVector_t::iterator pos; |
jmarkel44 | 71:34856d21f2bf | 255 | |
jmarkel44 | 71:34856d21f2bf | 256 | pos = outputMap.find(output); |
jmarkel44 | 71:34856d21f2bf | 257 | if ( pos == outputMap.end() ) { |
jmarkel44 | 71:34856d21f2bf | 258 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 71:34856d21f2bf | 259 | __func__, output); |
jmarkel44 | 71:34856d21f2bf | 260 | return -1; |
jmarkel44 | 71:34856d21f2bf | 261 | } |
jmarkel44 | 71:34856d21f2bf | 262 | |
jmarkel44 | 80:b12b0adfcdc2 | 263 | // if the control list is empty, push this control on the list |
jmarkel44 | 71:34856d21f2bf | 264 | if ( pos->second.empty() ) { |
jmarkel44 | 71:34856d21f2bf | 265 | string cid(id); |
jmarkel44 | 71:34856d21f2bf | 266 | Control c(cid, priority, CONTROL_OFF); |
jmarkel44 | 71:34856d21f2bf | 267 | pos->second.push_back(c); |
jmarkel44 | 71:34856d21f2bf | 268 | } else { |
jmarkel44 | 71:34856d21f2bf | 269 | // find this control in the list |
jmarkel44 | 71:34856d21f2bf | 270 | vector<Control>::iterator v; |
jmarkel44 | 71:34856d21f2bf | 271 | for ( v = pos->second.begin(); v != pos->second.end(); ++v ) { |
jmarkel44 | 71:34856d21f2bf | 272 | if ( strcmp(v->getId().c_str(), id) == 0 ) { |
jmarkel44 | 71:34856d21f2bf | 273 | v->setState(CONTROL_OFF); |
jmarkel44 | 77:43e0a3d9e536 | 274 | break; |
jmarkel44 | 71:34856d21f2bf | 275 | } |
jmarkel44 | 71:34856d21f2bf | 276 | } |
jmarkel44 | 80:b12b0adfcdc2 | 277 | |
jmarkel44 | 80:b12b0adfcdc2 | 278 | if ( v == pos->second.end() ) { |
jmarkel44 | 80:b12b0adfcdc2 | 279 | // this is a new request, so add it and sort the vector |
jmarkel44 | 80:b12b0adfcdc2 | 280 | string cid(id); |
jmarkel44 | 80:b12b0adfcdc2 | 281 | Control c(cid, priority, CONTROL_OFF); |
jmarkel44 | 80:b12b0adfcdc2 | 282 | pos->second.push_back(c); |
jmarkel44 | 80:b12b0adfcdc2 | 283 | std::sort(pos->second.begin(), pos->second.end()); |
jmarkel44 | 80:b12b0adfcdc2 | 284 | } |
jmarkel44 | 71:34856d21f2bf | 285 | } |
jmarkel44 | 66:db1425574b58 | 286 | |
jmarkel44 | 66:db1425574b58 | 287 | return 0; |
jmarkel44 | 66:db1425574b58 | 288 | } |
jmarkel44 | 66:db1425574b58 | 289 | |
jmarkel44 | 74:03ccf04998b5 | 290 | /***************************************************************************** |
jmarkel44 | 74:03ccf04998b5 | 291 | * Function: unregisterControl |
jmarkel44 | 74:03ccf04998b5 | 292 | * Description: |
jmarkel44 | 74:03ccf04998b5 | 293 | * |
jmarkel44 | 74:03ccf04998b5 | 294 | * @param id -> control identifier |
jmarkel44 | 74:03ccf04998b5 | 295 | * @param pri -> priority |
jmarkel44 | 74:03ccf04998b5 | 296 | * @param output -> output (e.g. "o_rly5) |
jmarkel44 | 74:03ccf04998b5 | 297 | * |
jmarkel44 | 74:03ccf04998b5 | 298 | * @return 0 on success; -1 on error |
jmarkel44 | 74:03ccf04998b5 | 299 | *****************************************************************************/ |
jmarkel44 | 72:3754b352f156 | 300 | static int unregisterControl(const char *id, unsigned int pri, const char *output) |
jmarkel44 | 72:3754b352f156 | 301 | { |
jmarkel44 | 72:3754b352f156 | 302 | // attempt to find the output in the map |
jmarkel44 | 72:3754b352f156 | 303 | StringOutputVector_t::iterator pos; |
jmarkel44 | 80:b12b0adfcdc2 | 304 | bool found = false; |
jmarkel44 | 72:3754b352f156 | 305 | |
jmarkel44 | 72:3754b352f156 | 306 | pos = outputMap.find(output); |
jmarkel44 | 72:3754b352f156 | 307 | if ( pos == outputMap.end() ) { |
jmarkel44 | 72:3754b352f156 | 308 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 72:3754b352f156 | 309 | __func__, output); |
jmarkel44 | 72:3754b352f156 | 310 | return -1; |
jmarkel44 | 72:3754b352f156 | 311 | } |
jmarkel44 | 72:3754b352f156 | 312 | |
jmarkel44 | 72:3754b352f156 | 313 | // find the control in the list |
jmarkel44 | 72:3754b352f156 | 314 | vector<Control>::iterator v; |
jmarkel44 | 72:3754b352f156 | 315 | for ( v = pos->second.begin(); v != pos->second.end(); ++v) { |
jmarkel44 | 72:3754b352f156 | 316 | if ( strcmp(v->getId().c_str(), id) == 0 ) { |
jmarkel44 | 72:3754b352f156 | 317 | // delete this entry |
jmarkel44 | 72:3754b352f156 | 318 | pos->second.erase(v); |
jmarkel44 | 80:b12b0adfcdc2 | 319 | found = true; |
jmarkel44 | 72:3754b352f156 | 320 | break; |
jmarkel44 | 72:3754b352f156 | 321 | } |
jmarkel44 | 74:03ccf04998b5 | 322 | } |
jmarkel44 | 80:b12b0adfcdc2 | 323 | if ( !found ) { |
jmarkel44 | 80:b12b0adfcdc2 | 324 | logError("%s: failed to find control %s in list", __func__, id); |
jmarkel44 | 80:b12b0adfcdc2 | 325 | return -1; |
jmarkel44 | 80:b12b0adfcdc2 | 326 | } |
jmarkel44 | 72:3754b352f156 | 327 | return 0; |
jmarkel44 | 72:3754b352f156 | 328 | } |
jmarkel44 | 72:3754b352f156 | 329 | |
jmarkel44 | 72:3754b352f156 | 330 | /***************************************************************************** |
jmarkel44 | 72:3754b352f156 | 331 | * Function: loadPersistentOutputs |
jmarkel44 | 72:3754b352f156 | 332 | * Description: pump up the output map based on persistent files |
jmarkel44 | 72:3754b352f156 | 333 | * |
jmarkel44 | 72:3754b352f156 | 334 | * @param args -> not used |
jmarkel44 | 72:3754b352f156 | 335 | * @return none |
jmarkel44 | 72:3754b352f156 | 336 | *****************************************************************************/ |
jmarkel44 | 66:db1425574b58 | 337 | static void loadPersistentOutputs(void) |
jmarkel44 | 66:db1425574b58 | 338 | { |
jmarkel44 | 66:db1425574b58 | 339 | bool status; |
jmarkel44 | 66:db1425574b58 | 340 | MbedJSONValue json_value; |
jmarkel44 | 71:34856d21f2bf | 341 | |
jmarkel44 | 77:43e0a3d9e536 | 342 | printf("\rLoading persistent outputs:\n"); |
jmarkel44 | 66:db1425574b58 | 343 | |
jmarkel44 | 66:db1425574b58 | 344 | std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles(); |
jmarkel44 | 70:7427f4959201 | 345 | |
jmarkel44 | 66:db1425574b58 | 346 | for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) { |
jmarkel44 | 67:49f266601d83 | 347 | if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) { |
jmarkel44 | 66:db1425574b58 | 348 | |
jmarkel44 | 66:db1425574b58 | 349 | logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name); |
jmarkel44 | 66:db1425574b58 | 350 | |
jmarkel44 | 66:db1425574b58 | 351 | char scratchBuf[1024]; |
jmarkel44 | 66:db1425574b58 | 352 | |
jmarkel44 | 66:db1425574b58 | 353 | status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024); |
jmarkel44 | 66:db1425574b58 | 354 | if( status != true ) { |
jmarkel44 | 66:db1425574b58 | 355 | logInfo("(%d)read file failed, status=%d", __LINE__, status); |
jmarkel44 | 66:db1425574b58 | 356 | } else { |
jmarkel44 | 66:db1425574b58 | 357 | logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf ); |
jmarkel44 | 66:db1425574b58 | 358 | } |
jmarkel44 | 66:db1425574b58 | 359 | |
jmarkel44 | 66:db1425574b58 | 360 | parse( json_value, scratchBuf ); |
jmarkel44 | 66:db1425574b58 | 361 | |
jmarkel44 | 66:db1425574b58 | 362 | string id = json_value["id"].get<string>(); |
jmarkel44 | 77:43e0a3d9e536 | 363 | printf("\r output %s loaded\n", i->name); |
jmarkel44 | 74:03ccf04998b5 | 364 | |
jmarkel44 | 80:b12b0adfcdc2 | 365 | // emplace the empty control vector into the output map |
jmarkel44 | 71:34856d21f2bf | 366 | vector<Control> v; |
jmarkel44 | 71:34856d21f2bf | 367 | outputMap[id] = v; |
jmarkel44 | 66:db1425574b58 | 368 | } |
jmarkel44 | 66:db1425574b58 | 369 | } |
jmarkel44 | 70:7427f4959201 | 370 | } |