Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Wed Oct 05 16:41:57 2016 +0000
Revision:
185:5ac6ab1ed875
Parent:
183:44f7fea6b208
Child:
195:21df85341cb3
New defined values for relay state changes;

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