Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Tue Sep 27 15:28:59 2016 +0000
Revision:
133:c871de2d2b90
Parent:
130:c932494f05b8
Child:
152:c03ebcda54ef
Child:
164:7cecd731882e
timer controls working

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