Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Mon Oct 10 16:06:10 2016 +0000
Revision:
212:289f63158d2b
Parent:
195:21df85341cb3
Child:
217:d5a2ff093319
general cleanup;

Who changed what in which revision?

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