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@276:851554207c77, 2016-10-26 (annotated)
- Committer:
- jmarkel44
- Date:
- Wed Oct 26 20:31:39 2016 +0000
- Revision:
- 276:851554207c77
- Parent:
- 274:e2caebc7c65e
cJSON frees
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 | 242:3b0086a6d625 | 9 | #include "cJSON.h" |
jmarkel44 | 94:18ae2fd82638 | 10 | #include "ModbusMasterApi.h" |
davidjhoward | 120:539b20bd3816 | 11 | #include "LoggerApi.h" |
jmarkel44 | 185:5ac6ab1ed875 | 12 | #include "Control.h" |
jmarkel44 | 70:7427f4959201 | 13 | #include <vector> |
jmarkel44 | 77:43e0a3d9e536 | 14 | #include <string> |
jmarkel44 | 77:43e0a3d9e536 | 15 | #include <algorithm> |
jmarkel44 | 48:1c7861d80d16 | 16 | |
jmarkel44 | 114:c24aebb8b473 | 17 | // local functions |
jmarkel44 | 67:49f266601d83 | 18 | static int createOutput(const char *controlFile); |
jmarkel44 | 66:db1425574b58 | 19 | static void loadPersistentOutputs(void); |
jmarkel44 | 195:21df85341cb3 | 20 | static void writeOutputs(const std::string, const std::string); |
jmarkel44 | 212:289f63158d2b | 21 | static int enableOutputReqHandler(OutputControlMsg_t *msg); |
jmarkel44 | 212:289f63158d2b | 22 | static int disableOutputReqHandler(OutputControlMsg_t *msg); |
jmarkel44 | 72:3754b352f156 | 23 | static int unregisterControl(const char *id, unsigned int pri, const char *output); |
jmarkel44 | 71:34856d21f2bf | 24 | |
jmarkel44 | 84:7b7cad3ba139 | 25 | // The Output Map |
jmarkel44 | 114:c24aebb8b473 | 26 | // |
jmarkel44 | 84:7b7cad3ba139 | 27 | // this is the main data structure used to distinguish which control has |
jmarkel44 | 84:7b7cad3ba139 | 28 | // priority of an output. the layout is as-follows: |
jmarkel44 | 212:289f63158d2b | 29 | // outputMap["o_rly1"]-> Control<"ManControl_rly1", 100, ON > <<- highest pri control manipulates relay |
jmarkel44 | 212:289f63158d2b | 30 | // Control<"SetpointControl_rly1", 800, OFF> <<- lowest pri (queued up) |
jmarkel44 | 212:289f63158d2b | 31 | // |
jmarkel44 | 212:289f63158d2b | 32 | // outputMap["o_rly2"]-> Control<"SetpointControl_rly2", 800, ON > |
jmarkel44 | 212:289f63158d2b | 33 | // |
jmarkel44 | 217:d5a2ff093319 | 34 | // outputMap["o_rly3"]-> Control<"TimerControl_rly3", 700, ON> |
jmarkel44 | 212:289f63158d2b | 35 | // |
jmarkel44 | 84:7b7cad3ba139 | 36 | // |
jmarkel44 | 84:7b7cad3ba139 | 37 | // The Control Vector (per relay) is always sorted by priority, whereas |
jmarkel44 | 183:44f7fea6b208 | 38 | // the highest priority control is at the front (v.front()) of the |
jmarkel44 | 114:c24aebb8b473 | 39 | // list. |
jmarkel44 | 84:7b7cad3ba139 | 40 | |
jmarkel44 | 195:21df85341cb3 | 41 | typedef std::map<std::string, vector<Control> > StringOutputVector_t; |
jmarkel44 | 77:43e0a3d9e536 | 42 | StringOutputVector_t outputMap; |
jmarkel44 | 66:db1425574b58 | 43 | |
jmarkel44 | 84:7b7cad3ba139 | 44 | // operator for sorting the outputs vectors per output |
jmarkel44 | 80:b12b0adfcdc2 | 45 | bool operator<(const Control &control1, const Control &control2) |
jmarkel44 | 80:b12b0adfcdc2 | 46 | { |
jmarkel44 | 77:43e0a3d9e536 | 47 | return control1.getPriority() < control2.getPriority(); |
jmarkel44 | 77:43e0a3d9e536 | 48 | } |
jmarkel44 | 63:0ded43237b22 | 49 | |
jmarkel44 | 70:7427f4959201 | 50 | /***************************************************************************** |
jmarkel44 | 70:7427f4959201 | 51 | * Function: OutputTask |
jmarkel44 | 70:7427f4959201 | 52 | * Description: Main entry point for the Output Task |
jmarkel44 | 70:7427f4959201 | 53 | * |
jmarkel44 | 70:7427f4959201 | 54 | * @param args -> not used |
jmarkel44 | 70:7427f4959201 | 55 | * @return none |
jmarkel44 | 70:7427f4959201 | 56 | *****************************************************************************/ |
jmarkel44 | 48:1c7861d80d16 | 57 | void OutputTask(void const *args) |
jmarkel44 | 48:1c7861d80d16 | 58 | { |
jmarkel44 | 80:b12b0adfcdc2 | 59 | int rc; |
jmarkel44 | 51:66b820f203a5 | 60 | UNUSED(args); |
jmarkel44 | 70:7427f4959201 | 61 | |
jmarkel44 | 67:49f266601d83 | 62 | printf("\r%s has started...\n", __func__); |
jmarkel44 | 63:0ded43237b22 | 63 | |
jmarkel44 | 66:db1425574b58 | 64 | loadPersistentOutputs(); |
jmarkel44 | 75:96512ccc0443 | 65 | osSignalSet(mainThreadId, sig_output_continue); |
jmarkel44 | 75:96512ccc0443 | 66 | |
jmarkel44 | 48:1c7861d80d16 | 67 | while (true) { |
jmarkel44 | 51:66b820f203a5 | 68 | // wait for an event |
jmarkel44 | 56:225786c56315 | 69 | osEvent evt = OutputMasterMailBox.get(); |
jmarkel44 | 51:66b820f203a5 | 70 | if (evt.status == osEventMail) { |
jmarkel44 | 80:b12b0adfcdc2 | 71 | |
jmarkel44 | 63:0ded43237b22 | 72 | OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p; |
jmarkel44 | 63:0ded43237b22 | 73 | |
jmarkel44 | 63:0ded43237b22 | 74 | switch ( msg->action ) { |
jmarkel44 | 63:0ded43237b22 | 75 | case ACTION_NEW: |
jmarkel44 | 63:0ded43237b22 | 76 | // read the file and and create an output entry |
jmarkel44 | 80:b12b0adfcdc2 | 77 | rc = createOutput(msg->controlFile); |
jmarkel44 | 80:b12b0adfcdc2 | 78 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 79 | logError("%s: failed to create output %s\n", |
jmarkel44 | 80:b12b0adfcdc2 | 80 | __func__, msg->controlFile); |
jmarkel44 | 80:b12b0adfcdc2 | 81 | } |
jmarkel44 | 63:0ded43237b22 | 82 | break; |
jmarkel44 | 71:34856d21f2bf | 83 | case ACTION_CONTROL_ON: |
jmarkel44 | 115:1558e01d04c6 | 84 | logInfo("%s is requesting ON control of %s", msg->id, msg->output_tag); |
jmarkel44 | 212:289f63158d2b | 85 | rc = enableOutputReqHandler(msg); |
jmarkel44 | 80:b12b0adfcdc2 | 86 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 87 | logError("%s: failed to enabled output for %s", |
jmarkel44 | 80:b12b0adfcdc2 | 88 | __func__, msg->id); |
jmarkel44 | 80:b12b0adfcdc2 | 89 | } |
jmarkel44 | 71:34856d21f2bf | 90 | break; |
jmarkel44 | 71:34856d21f2bf | 91 | case ACTION_CONTROL_OFF: |
jmarkel44 | 115:1558e01d04c6 | 92 | logInfo("%s is requesting OFF control of %s", msg->id, msg->output_tag); |
jmarkel44 | 212:289f63158d2b | 93 | rc = disableOutputReqHandler(msg); |
jmarkel44 | 80:b12b0adfcdc2 | 94 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 95 | printf("%s: failed to disabled output for %s", |
jmarkel44 | 80:b12b0adfcdc2 | 96 | __func__, msg->id); |
jmarkel44 | 80:b12b0adfcdc2 | 97 | } |
jmarkel44 | 71:34856d21f2bf | 98 | break; |
jmarkel44 | 72:3754b352f156 | 99 | case ACTION_CONTROL_UNREGISTER: |
jmarkel44 | 115:1558e01d04c6 | 100 | logInfo("%s is requesting its deletion from %s", msg->id, msg->output_tag); |
jmarkel44 | 115:1558e01d04c6 | 101 | rc = unregisterControl(msg->id, msg->priority, msg->output_tag); |
jmarkel44 | 80:b12b0adfcdc2 | 102 | if ( rc != 0 ) { |
jmarkel44 | 80:b12b0adfcdc2 | 103 | printf("%s: failed to unregister control %s", |
jmarkel44 | 84:7b7cad3ba139 | 104 | __func__, msg->id); |
jmarkel44 | 80:b12b0adfcdc2 | 105 | } |
jmarkel44 | 72:3754b352f156 | 106 | break; |
jmarkel44 | 63:0ded43237b22 | 107 | default: |
jmarkel44 | 63:0ded43237b22 | 108 | break; |
jmarkel44 | 63:0ded43237b22 | 109 | } |
jmarkel44 | 63:0ded43237b22 | 110 | |
jmarkel44 | 56:225786c56315 | 111 | // free the message |
jmarkel44 | 56:225786c56315 | 112 | OutputMasterMailBox.free(msg); |
jmarkel44 | 114:c24aebb8b473 | 113 | |
jmarkel44 | 114:c24aebb8b473 | 114 | // refresh the outputs |
jmarkel44 | 195:21df85341cb3 | 115 | writeOutputs(msg->id, msg->output_tag); |
jmarkel44 | 84:7b7cad3ba139 | 116 | } |
jmarkel44 | 84:7b7cad3ba139 | 117 | } |
jmarkel44 | 84:7b7cad3ba139 | 118 | } |
jmarkel44 | 84:7b7cad3ba139 | 119 | |
jmarkel44 | 128:534bf29132f8 | 120 | /***************************************************************************** |
jmarkel44 | 128:534bf29132f8 | 121 | * Function: recordEvent |
jmarkel44 | 133:c871de2d2b90 | 122 | * Description: send an event to the logger |
jmarkel44 | 128:534bf29132f8 | 123 | * |
jmarkel44 | 133:c871de2d2b90 | 124 | * @param output -> the output channel |
jmarkel44 | 133:c871de2d2b90 | 125 | * @param control -> the control, this can be null |
jmarkel44 | 128:534bf29132f8 | 126 | * @return none |
jmarkel44 | 128:534bf29132f8 | 127 | *****************************************************************************/ |
jmarkel44 | 268:50bed2502270 | 128 | void recordEvent(std::string output, const Control* control) |
jmarkel44 | 114:c24aebb8b473 | 129 | { |
jmarkel44 | 121:650205ffa656 | 130 | |
jmarkel44 | 115:1558e01d04c6 | 131 | EventReasonStruct_t ev; |
jmarkel44 | 121:650205ffa656 | 132 | ModbusValue input_value; |
jmarkel44 | 121:650205ffa656 | 133 | ModbusValue output_value; |
jmarkel44 | 115:1558e01d04c6 | 134 | memset(&ev, 0, sizeof(ev)); |
jmarkel44 | 121:650205ffa656 | 135 | |
jmarkel44 | 217:d5a2ff093319 | 136 | // if there's no control, that means the only control that was on |
jmarkel44 | 217:d5a2ff093319 | 137 | // the relay stack has been destroyed, so we'll send a NO CONTROL |
jmarkel44 | 217:d5a2ff093319 | 138 | // code. |
jmarkel44 | 128:534bf29132f8 | 139 | if ( !control ) { |
jmarkel44 | 128:534bf29132f8 | 140 | ev.eventReason = EVENT_REASON_NO_CONTROL; |
jmarkel44 | 128:534bf29132f8 | 141 | ModbusMasterReadRegister(output, &output_value); |
jmarkel44 | 128:534bf29132f8 | 142 | strncpy(ev.outputTag, output.c_str(), sizeof(ev.outputTag)); |
jmarkel44 | 128:534bf29132f8 | 143 | ev.outputValue = output_value.value; |
jmarkel44 | 128:534bf29132f8 | 144 | |
jmarkel44 | 128:534bf29132f8 | 145 | printf("\rEVENT RECORD\n"); |
jmarkel44 | 128:534bf29132f8 | 146 | printf("\rev.eventReason = %d\n", ev.eventReason); |
jmarkel44 | 128:534bf29132f8 | 147 | printf("\rev.outputTag = %s\n", ev.outputTag); |
jmarkel44 | 128:534bf29132f8 | 148 | printf("\rev.outputValue = %.02f\n", ev.outputValue); |
jmarkel44 | 128:534bf29132f8 | 149 | |
jmarkel44 | 128:534bf29132f8 | 150 | EventLoggerApi(ev); |
jmarkel44 | 128:534bf29132f8 | 151 | return; |
jmarkel44 | 128:534bf29132f8 | 152 | } |
jmarkel44 | 128:534bf29132f8 | 153 | |
jmarkel44 | 115:1558e01d04c6 | 154 | switch ( control->getControlType() ) { |
jmarkel44 | 121:650205ffa656 | 155 | case CONTROL_SETPOINT: |
jmarkel44 | 223:1a03451c0870 | 156 | case CONTROL_COMPOSITE: |
jmarkel44 | 115:1558e01d04c6 | 157 | ev.eventReason = EVENT_REASON_AUTO; |
jmarkel44 | 115:1558e01d04c6 | 158 | strncpy(ev.inputTag, control->getInput().c_str(), sizeof(ev.inputTag)); |
jmarkel44 | 115:1558e01d04c6 | 159 | strncpy(ev.outputTag, output.c_str(), sizeof(ev.outputTag)); |
jmarkel44 | 115:1558e01d04c6 | 160 | ModbusMasterReadRegister(control->getInput(), &input_value); |
jmarkel44 | 115:1558e01d04c6 | 161 | ModbusMasterReadRegister(output, &output_value); |
jmarkel44 | 115:1558e01d04c6 | 162 | ev.inputValue = input_value.value; |
jmarkel44 | 115:1558e01d04c6 | 163 | ev.outputValue = output_value.value; |
jmarkel44 | 115:1558e01d04c6 | 164 | printf("\rEVENT RECORD\n"); |
jmarkel44 | 115:1558e01d04c6 | 165 | printf("\rev.eventReason = %d\n", ev.eventReason); |
jmarkel44 | 115:1558e01d04c6 | 166 | printf("\rev.outputTag = %s\n", ev.outputTag); |
jmarkel44 | 115:1558e01d04c6 | 167 | printf("\rev.outputValue = %.02f\n", ev.outputValue); |
jmarkel44 | 115:1558e01d04c6 | 168 | EventLoggerApi(ev); |
jmarkel44 | 115:1558e01d04c6 | 169 | break; |
jmarkel44 | 133:c871de2d2b90 | 170 | |
jmarkel44 | 115:1558e01d04c6 | 171 | case CONTROL_MANUAL: |
jmarkel44 | 121:650205ffa656 | 172 | ev.eventReason = EVENT_REASON_MANUAL; |
jmarkel44 | 121:650205ffa656 | 173 | strncpy(ev.outputTag, output.c_str(), sizeof(ev.outputTag)); |
jmarkel44 | 121:650205ffa656 | 174 | ModbusMasterReadRegister(output, &output_value); |
jmarkel44 | 121:650205ffa656 | 175 | ev.outputValue = output_value.value; |
jmarkel44 | 121:650205ffa656 | 176 | printf("\rEVENT RECORD\n"); |
jmarkel44 | 121:650205ffa656 | 177 | printf("\rev.eventReason = %d\n", ev.eventReason); |
jmarkel44 | 121:650205ffa656 | 178 | printf("\rev.outputTag = %s\n", ev.outputTag); |
jmarkel44 | 121:650205ffa656 | 179 | printf("\rev.outputValue = %.02f\n", ev.outputValue); |
jmarkel44 | 121:650205ffa656 | 180 | EventLoggerApi(ev); |
jmarkel44 | 121:650205ffa656 | 181 | break; |
jmarkel44 | 133:c871de2d2b90 | 182 | |
jmarkel44 | 133:c871de2d2b90 | 183 | case CONTROL_TIMER: |
jmarkel44 | 133:c871de2d2b90 | 184 | ev.eventReason = EVENT_REASON_TIMER; |
jmarkel44 | 133:c871de2d2b90 | 185 | strncpy(ev.outputTag, output.c_str(), sizeof(ev.outputTag)); |
jmarkel44 | 133:c871de2d2b90 | 186 | ModbusMasterReadRegister(output, &output_value); |
jmarkel44 | 133:c871de2d2b90 | 187 | ev.outputValue = output_value.value; |
jmarkel44 | 133:c871de2d2b90 | 188 | printf("\rEVENT RECORD\n"); |
jmarkel44 | 133:c871de2d2b90 | 189 | printf("\rev.eventReason = %d\n", ev.eventReason); |
jmarkel44 | 133:c871de2d2b90 | 190 | printf("\rev.outputTag = %s\n", ev.outputTag); |
jmarkel44 | 133:c871de2d2b90 | 191 | printf("\rev.outputValue = %.02f\n", ev.outputValue); |
jmarkel44 | 133:c871de2d2b90 | 192 | EventLoggerApi(ev); |
jmarkel44 | 133:c871de2d2b90 | 193 | break; |
jmarkel44 | 269:97243a7f56ba | 194 | |
jmarkel44 | 269:97243a7f56ba | 195 | case CONTROL_FAILSAFE: |
jmarkel44 | 269:97243a7f56ba | 196 | ev.eventReason = EVENT_REASON_FAILSAFE; |
jmarkel44 | 269:97243a7f56ba | 197 | strncpy(ev.outputTag, output.c_str(), sizeof(ev.outputTag)); |
jmarkel44 | 269:97243a7f56ba | 198 | ModbusMasterReadRegister(output, &output_value); |
jmarkel44 | 269:97243a7f56ba | 199 | ev.outputValue = output_value.value; |
jmarkel44 | 269:97243a7f56ba | 200 | printf("\rEVENT RECORD\n"); |
jmarkel44 | 269:97243a7f56ba | 201 | printf("\rrev.eventReason = %d\n", ev.eventReason); |
jmarkel44 | 269:97243a7f56ba | 202 | printf("\rev.outputTag = %s\n", ev.outputTag); |
jmarkel44 | 269:97243a7f56ba | 203 | printf("\rev.outputValue = %.02f\n", ev.outputValue); |
jmarkel44 | 269:97243a7f56ba | 204 | EventLoggerApi(ev); |
jmarkel44 | 269:97243a7f56ba | 205 | break; |
jmarkel44 | 115:1558e01d04c6 | 206 | default: |
jmarkel44 | 115:1558e01d04c6 | 207 | break; |
jmarkel44 | 115:1558e01d04c6 | 208 | } |
jmarkel44 | 114:c24aebb8b473 | 209 | } |
jmarkel44 | 84:7b7cad3ba139 | 210 | |
jmarkel44 | 84:7b7cad3ba139 | 211 | /***************************************************************************** |
jmarkel44 | 195:21df85341cb3 | 212 | * Function: writeOutputs |
jmarkel44 | 84:7b7cad3ba139 | 213 | * Description: send a message to the modbus master of who's in control |
jmarkel44 | 84:7b7cad3ba139 | 214 | * |
jmarkel44 | 84:7b7cad3ba139 | 215 | * |
jmarkel44 | 217:d5a2ff093319 | 216 | * @param id -> control identifier |
jmarkel44 | 217:d5a2ff093319 | 217 | * @param output_tag -> the output to write |
jmarkel44 | 84:7b7cad3ba139 | 218 | * @return none |
jmarkel44 | 84:7b7cad3ba139 | 219 | *****************************************************************************/ |
jmarkel44 | 195:21df85341cb3 | 220 | static void writeOutputs(const std::string id, const std::string output_tag) |
jmarkel44 | 84:7b7cad3ba139 | 221 | { |
jmarkel44 | 195:21df85341cb3 | 222 | UNUSED(id); |
jmarkel44 | 195:21df85341cb3 | 223 | |
jmarkel44 | 133:c871de2d2b90 | 224 | if ( output_tag.empty() ) return; |
jmarkel44 | 133:c871de2d2b90 | 225 | |
jmarkel44 | 84:7b7cad3ba139 | 226 | StringOutputVector_t::iterator pos; |
jmarkel44 | 84:7b7cad3ba139 | 227 | |
jmarkel44 | 195:21df85341cb3 | 228 | // find the output |
jmarkel44 | 128:534bf29132f8 | 229 | pos = outputMap.find(output_tag); |
jmarkel44 | 128:534bf29132f8 | 230 | if ( pos != outputMap.end() ) { |
jmarkel44 | 195:21df85341cb3 | 231 | // we found the ouput, but nothing's controlling it... |
jmarkel44 | 128:534bf29132f8 | 232 | if ( pos->second.empty() ) { |
jmarkel44 | 185:5ac6ab1ed875 | 233 | ModbusMasterWriteRegister(pos->first, RELAY_STATUS_NOT_CONTROLLED); |
jmarkel44 | 128:534bf29132f8 | 234 | recordEvent(pos->first, NULL); |
jmarkel44 | 195:21df85341cb3 | 235 | } else { |
jmarkel44 | 185:5ac6ab1ed875 | 236 | ModbusMasterWriteRegister(pos->first, pos->second.begin()->getMappedState()); |
jmarkel44 | 268:50bed2502270 | 237 | recordEvent(pos->first, pos->second.begin()); |
jmarkel44 | 128:534bf29132f8 | 238 | } |
jmarkel44 | 128:534bf29132f8 | 239 | } else { |
jmarkel44 | 128:534bf29132f8 | 240 | logError("%s failed to find the selected output %s", |
jmarkel44 | 128:534bf29132f8 | 241 | __func__, output_tag.c_str()); |
jmarkel44 | 128:534bf29132f8 | 242 | } |
jmarkel44 | 66:db1425574b58 | 243 | } |
jmarkel44 | 66:db1425574b58 | 244 | |
jmarkel44 | 70:7427f4959201 | 245 | /***************************************************************************** |
jmarkel44 | 72:3754b352f156 | 246 | * Function: createOutput |
jmarkel44 | 72:3754b352f156 | 247 | * Description: |
jmarkel44 | 71:34856d21f2bf | 248 | * |
jmarkel44 | 217:d5a2ff093319 | 249 | * @param outputFile -> name of output file |
jmarkel44 | 71:34856d21f2bf | 250 | * @return none |
jmarkel44 | 71:34856d21f2bf | 251 | *****************************************************************************/ |
jmarkel44 | 77:43e0a3d9e536 | 252 | static int createOutput(const char *outputFile) |
jmarkel44 | 66:db1425574b58 | 253 | { |
jmarkel44 | 177:9ec90c8e3ce1 | 254 | char dataBuf[MAX_FILE_SIZE]; |
jmarkel44 | 77:43e0a3d9e536 | 255 | int status = GLOBAL_mdot->readUserFile(outputFile, (void *)dataBuf, sizeof(dataBuf)); |
jmarkel44 | 67:49f266601d83 | 256 | if ( status != true ) { |
jmarkel44 | 77:43e0a3d9e536 | 257 | logError("%s failed to read %s", __func__, outputFile); |
jmarkel44 | 66:db1425574b58 | 258 | return -1; |
jmarkel44 | 66:db1425574b58 | 259 | } |
jmarkel44 | 195:21df85341cb3 | 260 | |
jmarkel44 | 242:3b0086a6d625 | 261 | cJSON * root = cJSON_Parse(dataBuf); |
jmarkel44 | 242:3b0086a6d625 | 262 | std::string id = cJSON_GetObjectItem(root,"id")->valuestring; |
jmarkel44 | 276:851554207c77 | 263 | cJSON_Delete(root); |
jmarkel44 | 242:3b0086a6d625 | 264 | |
jmarkel44 | 71:34856d21f2bf | 265 | vector<Control> v; |
jmarkel44 | 71:34856d21f2bf | 266 | outputMap[id] = v; |
jmarkel44 | 71:34856d21f2bf | 267 | |
jmarkel44 | 71:34856d21f2bf | 268 | return 0; |
jmarkel44 | 71:34856d21f2bf | 269 | } |
jmarkel44 | 71:34856d21f2bf | 270 | |
jmarkel44 | 71:34856d21f2bf | 271 | /***************************************************************************** |
jmarkel44 | 212:289f63158d2b | 272 | * Function: enableOutputReqHandler |
jmarkel44 | 217:d5a2ff093319 | 273 | * Description: handle a request to enable an output |
jmarkel44 | 71:34856d21f2bf | 274 | * |
jmarkel44 | 217:d5a2ff093319 | 275 | * @param msg -> the message request |
jmarkel44 | 217:d5a2ff093319 | 276 | * @return -1 on error |
jmarkel44 | 71:34856d21f2bf | 277 | *****************************************************************************/ |
jmarkel44 | 212:289f63158d2b | 278 | static int enableOutputReqHandler(OutputControlMsg_t *msg) |
jmarkel44 | 71:34856d21f2bf | 279 | { |
jmarkel44 | 71:34856d21f2bf | 280 | // attempt to find the output in the map |
jmarkel44 | 71:34856d21f2bf | 281 | StringOutputVector_t::iterator pos; |
jmarkel44 | 71:34856d21f2bf | 282 | |
jmarkel44 | 115:1558e01d04c6 | 283 | pos = outputMap.find(msg->output_tag); |
jmarkel44 | 71:34856d21f2bf | 284 | if ( pos == outputMap.end() ) { |
jmarkel44 | 71:34856d21f2bf | 285 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 115:1558e01d04c6 | 286 | __func__, msg->output_tag); |
jmarkel44 | 71:34856d21f2bf | 287 | return -1; |
jmarkel44 | 71:34856d21f2bf | 288 | } |
jmarkel44 | 71:34856d21f2bf | 289 | |
jmarkel44 | 71:34856d21f2bf | 290 | if ( pos->second.empty() ) { |
jmarkel44 | 71:34856d21f2bf | 291 | // this is a new request |
jmarkel44 | 195:21df85341cb3 | 292 | std::string cid(msg->id); |
jmarkel44 | 195:21df85341cb3 | 293 | std::string input(msg->input_tag); |
jmarkel44 | 115:1558e01d04c6 | 294 | Control c(cid, msg->controlType, input, msg->priority, CONTROL_ON); |
jmarkel44 | 71:34856d21f2bf | 295 | pos->second.push_back(c); |
jmarkel44 | 71:34856d21f2bf | 296 | } else { |
jmarkel44 | 71:34856d21f2bf | 297 | // find this control in the list |
jmarkel44 | 71:34856d21f2bf | 298 | vector<Control>::iterator v; |
jmarkel44 | 71:34856d21f2bf | 299 | for ( v = pos->second.begin(); v != pos->second.end(); ++v ) { |
jmarkel44 | 115:1558e01d04c6 | 300 | if ( strcmp(v->getId().c_str(), msg->id) == 0 ) { |
jmarkel44 | 71:34856d21f2bf | 301 | v->setState(CONTROL_ON); |
jmarkel44 | 77:43e0a3d9e536 | 302 | break; |
jmarkel44 | 71:34856d21f2bf | 303 | } |
jmarkel44 | 71:34856d21f2bf | 304 | } |
jmarkel44 | 77:43e0a3d9e536 | 305 | if ( v == pos->second.end() ) { |
jmarkel44 | 77:43e0a3d9e536 | 306 | // this is a new request, so add it and sort the vector |
jmarkel44 | 195:21df85341cb3 | 307 | std::string cid(msg->id); |
jmarkel44 | 195:21df85341cb3 | 308 | std::string input(msg->input_tag); |
jmarkel44 | 115:1558e01d04c6 | 309 | Control c(cid, msg->controlType, input, msg->priority, CONTROL_ON); |
jmarkel44 | 77:43e0a3d9e536 | 310 | pos->second.push_back(c); |
jmarkel44 | 77:43e0a3d9e536 | 311 | std::sort(pos->second.begin(), pos->second.end()); |
jmarkel44 | 274:e2caebc7c65e | 312 | printf("\r\r\nDEBUG: vector size = %d\n", pos->second.size()); |
jmarkel44 | 77:43e0a3d9e536 | 313 | } |
jmarkel44 | 71:34856d21f2bf | 314 | } |
jmarkel44 | 71:34856d21f2bf | 315 | |
jmarkel44 | 71:34856d21f2bf | 316 | return 0; |
jmarkel44 | 71:34856d21f2bf | 317 | } |
jmarkel44 | 71:34856d21f2bf | 318 | |
jmarkel44 | 71:34856d21f2bf | 319 | /***************************************************************************** |
jmarkel44 | 212:289f63158d2b | 320 | * Function: disableOutputReqHandler |
jmarkel44 | 217:d5a2ff093319 | 321 | * Description: handle a request to disable an output |
jmarkel44 | 71:34856d21f2bf | 322 | * |
jmarkel44 | 217:d5a2ff093319 | 323 | * @param msg -> the message request |
jmarkel44 | 71:34856d21f2bf | 324 | * @return none |
jmarkel44 | 71:34856d21f2bf | 325 | *****************************************************************************/ |
jmarkel44 | 212:289f63158d2b | 326 | static int disableOutputReqHandler(OutputControlMsg_t *msg) |
jmarkel44 | 71:34856d21f2bf | 327 | { |
jmarkel44 | 71:34856d21f2bf | 328 | // attempt to find the output in the map |
jmarkel44 | 71:34856d21f2bf | 329 | StringOutputVector_t::iterator pos; |
jmarkel44 | 71:34856d21f2bf | 330 | |
jmarkel44 | 115:1558e01d04c6 | 331 | pos = outputMap.find(msg->output_tag); |
jmarkel44 | 71:34856d21f2bf | 332 | if ( pos == outputMap.end() ) { |
jmarkel44 | 71:34856d21f2bf | 333 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 115:1558e01d04c6 | 334 | __func__, msg->output_tag); |
jmarkel44 | 71:34856d21f2bf | 335 | return -1; |
jmarkel44 | 71:34856d21f2bf | 336 | } |
jmarkel44 | 71:34856d21f2bf | 337 | |
jmarkel44 | 80:b12b0adfcdc2 | 338 | // if the control list is empty, push this control on the list |
jmarkel44 | 71:34856d21f2bf | 339 | if ( pos->second.empty() ) { |
jmarkel44 | 195:21df85341cb3 | 340 | std::string cid(msg->id); |
jmarkel44 | 195:21df85341cb3 | 341 | std::string input(msg->input_tag); |
jmarkel44 | 115:1558e01d04c6 | 342 | Control c(cid, msg->controlType, input, msg->priority, CONTROL_OFF); |
jmarkel44 | 71:34856d21f2bf | 343 | pos->second.push_back(c); |
jmarkel44 | 71:34856d21f2bf | 344 | } else { |
jmarkel44 | 71:34856d21f2bf | 345 | // find this control in the list |
jmarkel44 | 71:34856d21f2bf | 346 | vector<Control>::iterator v; |
jmarkel44 | 71:34856d21f2bf | 347 | for ( v = pos->second.begin(); v != pos->second.end(); ++v ) { |
jmarkel44 | 115:1558e01d04c6 | 348 | if ( strcmp(v->getId().c_str(), msg->id) == 0 ) { |
jmarkel44 | 71:34856d21f2bf | 349 | v->setState(CONTROL_OFF); |
jmarkel44 | 77:43e0a3d9e536 | 350 | break; |
jmarkel44 | 71:34856d21f2bf | 351 | } |
jmarkel44 | 71:34856d21f2bf | 352 | } |
jmarkel44 | 80:b12b0adfcdc2 | 353 | |
jmarkel44 | 80:b12b0adfcdc2 | 354 | if ( v == pos->second.end() ) { |
jmarkel44 | 80:b12b0adfcdc2 | 355 | // this is a new request, so add it and sort the vector |
jmarkel44 | 195:21df85341cb3 | 356 | std::string cid(msg->id); |
jmarkel44 | 195:21df85341cb3 | 357 | std::string input(msg->input_tag); |
jmarkel44 | 115:1558e01d04c6 | 358 | Control c(cid, msg->controlType, input, msg->priority, CONTROL_OFF); |
jmarkel44 | 80:b12b0adfcdc2 | 359 | pos->second.push_back(c); |
jmarkel44 | 80:b12b0adfcdc2 | 360 | std::sort(pos->second.begin(), pos->second.end()); |
jmarkel44 | 80:b12b0adfcdc2 | 361 | } |
jmarkel44 | 71:34856d21f2bf | 362 | } |
jmarkel44 | 66:db1425574b58 | 363 | |
jmarkel44 | 66:db1425574b58 | 364 | return 0; |
jmarkel44 | 66:db1425574b58 | 365 | } |
jmarkel44 | 66:db1425574b58 | 366 | |
jmarkel44 | 74:03ccf04998b5 | 367 | /***************************************************************************** |
jmarkel44 | 74:03ccf04998b5 | 368 | * Function: unregisterControl |
jmarkel44 | 74:03ccf04998b5 | 369 | * Description: |
jmarkel44 | 74:03ccf04998b5 | 370 | * |
jmarkel44 | 217:d5a2ff093319 | 371 | * @param id -> control identifier |
jmarkel44 | 217:d5a2ff093319 | 372 | * @param pri -> priority |
jmarkel44 | 217:d5a2ff093319 | 373 | * @param output -> output (e.g. "o_rly05") |
jmarkel44 | 74:03ccf04998b5 | 374 | * |
jmarkel44 | 74:03ccf04998b5 | 375 | * @return 0 on success; -1 on error |
jmarkel44 | 74:03ccf04998b5 | 376 | *****************************************************************************/ |
jmarkel44 | 72:3754b352f156 | 377 | static int unregisterControl(const char *id, unsigned int pri, const char *output) |
jmarkel44 | 72:3754b352f156 | 378 | { |
jmarkel44 | 72:3754b352f156 | 379 | // attempt to find the output in the map |
jmarkel44 | 72:3754b352f156 | 380 | StringOutputVector_t::iterator pos; |
jmarkel44 | 80:b12b0adfcdc2 | 381 | bool found = false; |
jmarkel44 | 72:3754b352f156 | 382 | |
jmarkel44 | 72:3754b352f156 | 383 | pos = outputMap.find(output); |
jmarkel44 | 72:3754b352f156 | 384 | if ( pos == outputMap.end() ) { |
jmarkel44 | 72:3754b352f156 | 385 | printf("%s: failed to find the designated output %s\n", |
jmarkel44 | 72:3754b352f156 | 386 | __func__, output); |
jmarkel44 | 72:3754b352f156 | 387 | return -1; |
jmarkel44 | 72:3754b352f156 | 388 | } |
jmarkel44 | 72:3754b352f156 | 389 | |
jmarkel44 | 72:3754b352f156 | 390 | // find the control in the list |
jmarkel44 | 72:3754b352f156 | 391 | vector<Control>::iterator v; |
jmarkel44 | 72:3754b352f156 | 392 | for ( v = pos->second.begin(); v != pos->second.end(); ++v) { |
jmarkel44 | 72:3754b352f156 | 393 | if ( strcmp(v->getId().c_str(), id) == 0 ) { |
jmarkel44 | 72:3754b352f156 | 394 | // delete this entry |
jmarkel44 | 72:3754b352f156 | 395 | pos->second.erase(v); |
jmarkel44 | 80:b12b0adfcdc2 | 396 | found = true; |
jmarkel44 | 72:3754b352f156 | 397 | break; |
jmarkel44 | 72:3754b352f156 | 398 | } |
jmarkel44 | 74:03ccf04998b5 | 399 | } |
jmarkel44 | 128:534bf29132f8 | 400 | |
jmarkel44 | 80:b12b0adfcdc2 | 401 | if ( !found ) { |
jmarkel44 | 80:b12b0adfcdc2 | 402 | logError("%s: failed to find control %s in list", __func__, id); |
jmarkel44 | 80:b12b0adfcdc2 | 403 | return -1; |
jmarkel44 | 80:b12b0adfcdc2 | 404 | } |
jmarkel44 | 72:3754b352f156 | 405 | return 0; |
jmarkel44 | 72:3754b352f156 | 406 | } |
jmarkel44 | 72:3754b352f156 | 407 | |
jmarkel44 | 72:3754b352f156 | 408 | /***************************************************************************** |
jmarkel44 | 72:3754b352f156 | 409 | * Function: loadPersistentOutputs |
jmarkel44 | 217:d5a2ff093319 | 410 | * Description: build the output map |
jmarkel44 | 72:3754b352f156 | 411 | * |
jmarkel44 | 217:d5a2ff093319 | 412 | * @param none |
jmarkel44 | 72:3754b352f156 | 413 | * @return none |
jmarkel44 | 72:3754b352f156 | 414 | *****************************************************************************/ |
jmarkel44 | 66:db1425574b58 | 415 | static void loadPersistentOutputs(void) |
jmarkel44 | 66:db1425574b58 | 416 | { |
jmarkel44 | 66:db1425574b58 | 417 | bool status; |
jmarkel44 | 242:3b0086a6d625 | 418 | cJSON *root; |
jmarkel44 | 71:34856d21f2bf | 419 | |
jmarkel44 | 77:43e0a3d9e536 | 420 | printf("\rLoading persistent outputs:\n"); |
jmarkel44 | 66:db1425574b58 | 421 | |
jmarkel44 | 66:db1425574b58 | 422 | std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles(); |
jmarkel44 | 70:7427f4959201 | 423 | |
jmarkel44 | 66:db1425574b58 | 424 | for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) { |
jmarkel44 | 217:d5a2ff093319 | 425 | // load in all of the output files |
jmarkel44 | 67:49f266601d83 | 426 | if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) { |
jmarkel44 | 177:9ec90c8e3ce1 | 427 | char scratchBuf[MAX_FILE_SIZE]; |
jmarkel44 | 66:db1425574b58 | 428 | |
jmarkel44 | 177:9ec90c8e3ce1 | 429 | status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, MAX_FILE_SIZE); |
jmarkel44 | 66:db1425574b58 | 430 | if( status != true ) { |
jmarkel44 | 217:d5a2ff093319 | 431 | logError("%s: failed to read %s", __func__, i->name); |
jmarkel44 | 66:db1425574b58 | 432 | } else { |
jmarkel44 | 217:d5a2ff093319 | 433 | logInfo("%s: successfully read %s", __func__, i->name); |
jmarkel44 | 66:db1425574b58 | 434 | } |
jmarkel44 | 242:3b0086a6d625 | 435 | |
jmarkel44 | 242:3b0086a6d625 | 436 | root = cJSON_Parse(scratchBuf); |
jmarkel44 | 242:3b0086a6d625 | 437 | //parse( json_value, scratchBuf ); |
jmarkel44 | 242:3b0086a6d625 | 438 | std::string id = cJSON_GetObjectItem(root,"id")->valuestring; |
jmarkel44 | 77:43e0a3d9e536 | 439 | printf("\r output %s loaded\n", i->name); |
jmarkel44 | 276:851554207c77 | 440 | cJSON_Delete(root); |
jmarkel44 | 74:03ccf04998b5 | 441 | |
jmarkel44 | 80:b12b0adfcdc2 | 442 | // emplace the empty control vector into the output map |
jmarkel44 | 71:34856d21f2bf | 443 | vector<Control> v; |
jmarkel44 | 71:34856d21f2bf | 444 | outputMap[id] = v; |
jmarkel44 | 66:db1425574b58 | 445 | } |
jmarkel44 | 66:db1425574b58 | 446 | } |
jmarkel44 | 185:5ac6ab1ed875 | 447 | } |
jmarkel44 | 217:d5a2ff093319 | 448 | |
jmarkel44 | 217:d5a2ff093319 | 449 | /***************************************************************************** |
jmarkel44 | 217:d5a2ff093319 | 450 | * Function: DisplayOutputs |
jmarkel44 | 217:d5a2ff093319 | 451 | * Description: Display a list of outputs and their controls |
jmarkel44 | 217:d5a2ff093319 | 452 | * |
jmarkel44 | 217:d5a2ff093319 | 453 | * @param none |
jmarkel44 | 217:d5a2ff093319 | 454 | * @return none |
jmarkel44 | 217:d5a2ff093319 | 455 | *****************************************************************************/ |
jmarkel44 | 217:d5a2ff093319 | 456 | void DisplayOutputs(void) |
jmarkel44 | 217:d5a2ff093319 | 457 | { |
jmarkel44 | 217:d5a2ff093319 | 458 | StringOutputVector_t::iterator pos; |
jmarkel44 | 217:d5a2ff093319 | 459 | |
jmarkel44 | 217:d5a2ff093319 | 460 | for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) { |
jmarkel44 | 217:d5a2ff093319 | 461 | if ( pos->second.empty() ) { |
jmarkel44 | 217:d5a2ff093319 | 462 | printf("\r [%s]-> [no controls] \n", pos->first.c_str()); |
jmarkel44 | 217:d5a2ff093319 | 463 | } else { |
jmarkel44 | 217:d5a2ff093319 | 464 | printf("\r [%s]-> ", pos->first.c_str()); |
jmarkel44 | 217:d5a2ff093319 | 465 | vector<Control>::iterator i; |
jmarkel44 | 217:d5a2ff093319 | 466 | for ( i = pos->second.begin(); i != pos->second.end(); ++i ) { |
jmarkel44 | 217:d5a2ff093319 | 467 | i->display(); |
jmarkel44 | 217:d5a2ff093319 | 468 | } |
jmarkel44 | 217:d5a2ff093319 | 469 | printf("\n"); |
jmarkel44 | 217:d5a2ff093319 | 470 | } |
jmarkel44 | 217:d5a2ff093319 | 471 | } |
jmarkel44 | 217:d5a2ff093319 | 472 | printf("\r\n"); |
jmarkel44 | 217:d5a2ff093319 | 473 | } |