Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
davidjhoward
Date:
Thu Sep 22 21:30:48 2016 +0000
Revision:
120:539b20bd3816
Parent:
115:1558e01d04c6
Child:
121:650205ffa656
use LoggerApi.h

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 84:7b7cad3ba139 19 static void refreshOutputs(void);
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 84:7b7cad3ba139 112 refreshOutputs();
jmarkel44 84:7b7cad3ba139 113 }
jmarkel44 84:7b7cad3ba139 114 }
jmarkel44 84:7b7cad3ba139 115 }
jmarkel44 84:7b7cad3ba139 116
jmarkel44 115:1558e01d04c6 117 void recordEvent(std::string output, const Control *control)
jmarkel44 114:c24aebb8b473 118 {
jmarkel44 115:1558e01d04c6 119
jmarkel44 115:1558e01d04c6 120 EventReasonStruct_t ev;
jmarkel44 115:1558e01d04c6 121 memset(&ev, 0, sizeof(ev));
jmarkel44 115:1558e01d04c6 122
jmarkel44 115:1558e01d04c6 123 switch ( control->getControlType() ) {
jmarkel44 115:1558e01d04c6 124 case CONTROL_SETPOINT:
jmarkel44 115:1558e01d04c6 125 ModbusValue input_value;
jmarkel44 115:1558e01d04c6 126 ModbusValue output_value;
jmarkel44 115:1558e01d04c6 127 ev.eventReason = EVENT_REASON_AUTO;
jmarkel44 115:1558e01d04c6 128 strncpy(ev.inputTag, control->getInput().c_str(), sizeof(ev.inputTag));
jmarkel44 115:1558e01d04c6 129 strncpy(ev.outputTag, output.c_str(), sizeof(ev.outputTag));
jmarkel44 115:1558e01d04c6 130 ModbusMasterReadRegister(control->getInput(), &input_value);
jmarkel44 115:1558e01d04c6 131 ModbusMasterReadRegister(output, &output_value);
jmarkel44 115:1558e01d04c6 132 ev.inputValue = input_value.value;
jmarkel44 115:1558e01d04c6 133 ev.outputValue = output_value.value;
jmarkel44 115:1558e01d04c6 134 printf("\rEVENT RECORD\n");
jmarkel44 115:1558e01d04c6 135 printf("\rev.eventReason = %d\n", ev.eventReason);
jmarkel44 115:1558e01d04c6 136 printf("\rev.inputTag = %s\n", ev.inputTag);
jmarkel44 115:1558e01d04c6 137 printf("\rev.outputTag = %s\n", ev.outputTag);
jmarkel44 115:1558e01d04c6 138 printf("\rev.inputValue = %.02f\n", ev.inputValue);
jmarkel44 115:1558e01d04c6 139 printf("\rev.outputValue = %.02f\n", ev.outputValue);
jmarkel44 115:1558e01d04c6 140 EventLoggerApi(ev);
jmarkel44 115:1558e01d04c6 141 break;
jmarkel44 115:1558e01d04c6 142 case CONTROL_MANUAL:
jmarkel44 115:1558e01d04c6 143 default:
jmarkel44 115:1558e01d04c6 144 break;
jmarkel44 115:1558e01d04c6 145 }
jmarkel44 114:c24aebb8b473 146 }
jmarkel44 84:7b7cad3ba139 147
jmarkel44 84:7b7cad3ba139 148 /*****************************************************************************
jmarkel44 84:7b7cad3ba139 149 * Function: refreshOutputs
jmarkel44 84:7b7cad3ba139 150 * Description: send a message to the modbus master of who's in control
jmarkel44 84:7b7cad3ba139 151 *
jmarkel44 84:7b7cad3ba139 152 *
jmarkel44 84:7b7cad3ba139 153 * @param args -> not used
jmarkel44 84:7b7cad3ba139 154 * @return none
jmarkel44 84:7b7cad3ba139 155 *****************************************************************************/
jmarkel44 84:7b7cad3ba139 156 static void refreshOutputs(void)
jmarkel44 84:7b7cad3ba139 157 {
jmarkel44 84:7b7cad3ba139 158 //who's in control here?
jmarkel44 84:7b7cad3ba139 159 StringOutputVector_t::iterator pos;
jmarkel44 84:7b7cad3ba139 160
jmarkel44 84:7b7cad3ba139 161 for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) {
jmarkel44 84:7b7cad3ba139 162 if ( pos->second.empty() ) {
jmarkel44 95:42f92b03f1b8 163 // safeguarding
jmarkel44 95:42f92b03f1b8 164 ModbusMasterWriteRegister(pos->first, 0.00);
jmarkel44 84:7b7cad3ba139 165 } else {
jmarkel44 84:7b7cad3ba139 166 // a control is tied to this output
jmarkel44 94:18ae2fd82638 167 ModbusMasterWriteRegister(pos->first, pos->second.begin()->getState());
jmarkel44 115:1558e01d04c6 168 recordEvent(pos->first, (Control*)pos->second.begin());
jmarkel44 114:c24aebb8b473 169 printf("Preparing to send the event log data \n");
jmarkel44 51:66b820f203a5 170 }
jmarkel44 48:1c7861d80d16 171 }
jmarkel44 66:db1425574b58 172 }
jmarkel44 66:db1425574b58 173
jmarkel44 70:7427f4959201 174 /*****************************************************************************
jmarkel44 70:7427f4959201 175 * Function: DisplayOutputs
jmarkel44 70:7427f4959201 176 * Description: Display a list of outputs and its controls
jmarkel44 70:7427f4959201 177 *
jmarkel44 70:7427f4959201 178 * @param args -> not used
jmarkel44 70:7427f4959201 179 * @return none
jmarkel44 70:7427f4959201 180 *****************************************************************************/
jmarkel44 66:db1425574b58 181 void DisplayOutputs(void)
jmarkel44 66:db1425574b58 182 {
jmarkel44 66:db1425574b58 183 StringOutputVector_t::iterator pos;
jmarkel44 66:db1425574b58 184
jmarkel44 66:db1425574b58 185 for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) {
jmarkel44 71:34856d21f2bf 186 if ( pos->second.empty() ) {
jmarkel44 71:34856d21f2bf 187 printf("\r [%s]->[no controls]\n", pos->first.c_str());
jmarkel44 71:34856d21f2bf 188 } else {
jmarkel44 71:34856d21f2bf 189 printf("\r [%s]->", pos->first.c_str());
jmarkel44 71:34856d21f2bf 190 vector<Control>::iterator i;
jmarkel44 71:34856d21f2bf 191 for ( i = pos->second.begin(); i != pos->second.end(); ++i ) {
jmarkel44 71:34856d21f2bf 192 i->display();
jmarkel44 71:34856d21f2bf 193 }
jmarkel44 71:34856d21f2bf 194 printf("\n");
jmarkel44 71:34856d21f2bf 195 }
jmarkel44 66:db1425574b58 196 }
jmarkel44 66:db1425574b58 197 printf("\r\n");
jmarkel44 66:db1425574b58 198 }
jmarkel44 66:db1425574b58 199
jmarkel44 71:34856d21f2bf 200
jmarkel44 71:34856d21f2bf 201 /*****************************************************************************
jmarkel44 72:3754b352f156 202 * Function: createOutput
jmarkel44 72:3754b352f156 203 * Description:
jmarkel44 71:34856d21f2bf 204 *
jmarkel44 77:43e0a3d9e536 205 * @param controlFile -> name of output file
jmarkel44 71:34856d21f2bf 206 * @return none
jmarkel44 71:34856d21f2bf 207 *****************************************************************************/
jmarkel44 77:43e0a3d9e536 208 static int createOutput(const char *outputFile)
jmarkel44 66:db1425574b58 209 {
jmarkel44 66:db1425574b58 210 char dataBuf[1024];
jmarkel44 77:43e0a3d9e536 211 int status = GLOBAL_mdot->readUserFile(outputFile, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 67:49f266601d83 212 if ( status != true ) {
jmarkel44 77:43e0a3d9e536 213 logError("%s failed to read %s", __func__, outputFile);
jmarkel44 66:db1425574b58 214 return -1;
jmarkel44 66:db1425574b58 215 }
jmarkel44 66:db1425574b58 216
jmarkel44 66:db1425574b58 217 MbedJSONValue json_value;
jmarkel44 66:db1425574b58 218 parse(json_value, dataBuf);
jmarkel44 66:db1425574b58 219
jmarkel44 66:db1425574b58 220 // extract the relay information
jmarkel44 66:db1425574b58 221 string id = json_value["id"].get<string>();
jmarkel44 66:db1425574b58 222
jmarkel44 77:43e0a3d9e536 223 // maps don't allow duplicates, and the vector is empty for now
jmarkel44 71:34856d21f2bf 224 vector<Control> v;
jmarkel44 71:34856d21f2bf 225 outputMap[id] = v;
jmarkel44 71:34856d21f2bf 226
jmarkel44 71:34856d21f2bf 227 return 0;
jmarkel44 71:34856d21f2bf 228 }
jmarkel44 71:34856d21f2bf 229
jmarkel44 71:34856d21f2bf 230
jmarkel44 71:34856d21f2bf 231 /*****************************************************************************
jmarkel44 72:3754b352f156 232 * Function: enableOutputReq
jmarkel44 71:34856d21f2bf 233 * Description: Display a list of outputs and its controls
jmarkel44 71:34856d21f2bf 234 *
jmarkel44 71:34856d21f2bf 235 * @param args -> not used
jmarkel44 71:34856d21f2bf 236 * @return none
jmarkel44 71:34856d21f2bf 237 *****************************************************************************/
jmarkel44 115:1558e01d04c6 238 //static int enableOutputReq(const char *id, unsigned int priority, const char *output)
jmarkel44 115:1558e01d04c6 239 static int enableOutputReq(OutputControlMsg_t *msg)
jmarkel44 71:34856d21f2bf 240 {
jmarkel44 71:34856d21f2bf 241 // attempt to find the output in the map
jmarkel44 71:34856d21f2bf 242 StringOutputVector_t::iterator pos;
jmarkel44 71:34856d21f2bf 243
jmarkel44 115:1558e01d04c6 244 pos = outputMap.find(msg->output_tag);
jmarkel44 71:34856d21f2bf 245 if ( pos == outputMap.end() ) {
jmarkel44 71:34856d21f2bf 246 printf("%s: failed to find the designated output %s\n",
jmarkel44 115:1558e01d04c6 247 __func__, msg->output_tag);
jmarkel44 71:34856d21f2bf 248 return -1;
jmarkel44 71:34856d21f2bf 249 }
jmarkel44 71:34856d21f2bf 250
jmarkel44 71:34856d21f2bf 251 if ( pos->second.empty() ) {
jmarkel44 71:34856d21f2bf 252 // this is a new request
jmarkel44 115:1558e01d04c6 253 string cid(msg->id);
jmarkel44 115:1558e01d04c6 254 string input(msg->input_tag);
jmarkel44 115:1558e01d04c6 255 Control c(cid, msg->controlType, input, msg->priority, CONTROL_ON);
jmarkel44 71:34856d21f2bf 256 pos->second.push_back(c);
jmarkel44 71:34856d21f2bf 257 } else {
jmarkel44 71:34856d21f2bf 258 // find this control in the list
jmarkel44 71:34856d21f2bf 259 vector<Control>::iterator v;
jmarkel44 71:34856d21f2bf 260 for ( v = pos->second.begin(); v != pos->second.end(); ++v ) {
jmarkel44 115:1558e01d04c6 261 if ( strcmp(v->getId().c_str(), msg->id) == 0 ) {
jmarkel44 71:34856d21f2bf 262 v->setState(CONTROL_ON);
jmarkel44 77:43e0a3d9e536 263 break;
jmarkel44 71:34856d21f2bf 264 }
jmarkel44 71:34856d21f2bf 265 }
jmarkel44 77:43e0a3d9e536 266 if ( v == pos->second.end() ) {
jmarkel44 77:43e0a3d9e536 267 // this is a new request, so add it and sort the vector
jmarkel44 115:1558e01d04c6 268 string cid(msg->id);
jmarkel44 115:1558e01d04c6 269 string input(msg->input_tag);
jmarkel44 115:1558e01d04c6 270 Control c(cid, msg->controlType, input, msg->priority, CONTROL_ON);
jmarkel44 77:43e0a3d9e536 271 pos->second.push_back(c);
jmarkel44 77:43e0a3d9e536 272 std::sort(pos->second.begin(), pos->second.end());
jmarkel44 77:43e0a3d9e536 273 }
jmarkel44 71:34856d21f2bf 274 }
jmarkel44 71:34856d21f2bf 275
jmarkel44 71:34856d21f2bf 276 return 0;
jmarkel44 71:34856d21f2bf 277 }
jmarkel44 71:34856d21f2bf 278
jmarkel44 71:34856d21f2bf 279 /*****************************************************************************
jmarkel44 72:3754b352f156 280 * Function: disableOutputReq
jmarkel44 72:3754b352f156 281 * Description:
jmarkel44 71:34856d21f2bf 282 *
jmarkel44 71:34856d21f2bf 283 * @param args -> not used
jmarkel44 71:34856d21f2bf 284 * @return none
jmarkel44 71:34856d21f2bf 285 *****************************************************************************/
jmarkel44 115:1558e01d04c6 286 static int disableOutputReq(OutputControlMsg_t *msg)
jmarkel44 71:34856d21f2bf 287 {
jmarkel44 71:34856d21f2bf 288 // attempt to find the output in the map
jmarkel44 71:34856d21f2bf 289 StringOutputVector_t::iterator pos;
jmarkel44 71:34856d21f2bf 290
jmarkel44 115:1558e01d04c6 291 pos = outputMap.find(msg->output_tag);
jmarkel44 71:34856d21f2bf 292 if ( pos == outputMap.end() ) {
jmarkel44 71:34856d21f2bf 293 printf("%s: failed to find the designated output %s\n",
jmarkel44 115:1558e01d04c6 294 __func__, msg->output_tag);
jmarkel44 71:34856d21f2bf 295 return -1;
jmarkel44 71:34856d21f2bf 296 }
jmarkel44 71:34856d21f2bf 297
jmarkel44 80:b12b0adfcdc2 298 // if the control list is empty, push this control on the list
jmarkel44 71:34856d21f2bf 299 if ( pos->second.empty() ) {
jmarkel44 115:1558e01d04c6 300 string cid(msg->id);
jmarkel44 115:1558e01d04c6 301 string input(msg->input_tag);
jmarkel44 115:1558e01d04c6 302 Control c(cid, msg->controlType, input, msg->priority, CONTROL_OFF);
jmarkel44 71:34856d21f2bf 303 pos->second.push_back(c);
jmarkel44 71:34856d21f2bf 304 } else {
jmarkel44 71:34856d21f2bf 305 // find this control in the list
jmarkel44 71:34856d21f2bf 306 vector<Control>::iterator v;
jmarkel44 71:34856d21f2bf 307 for ( v = pos->second.begin(); v != pos->second.end(); ++v ) {
jmarkel44 115:1558e01d04c6 308 if ( strcmp(v->getId().c_str(), msg->id) == 0 ) {
jmarkel44 71:34856d21f2bf 309 v->setState(CONTROL_OFF);
jmarkel44 77:43e0a3d9e536 310 break;
jmarkel44 71:34856d21f2bf 311 }
jmarkel44 71:34856d21f2bf 312 }
jmarkel44 80:b12b0adfcdc2 313
jmarkel44 80:b12b0adfcdc2 314 if ( v == pos->second.end() ) {
jmarkel44 80:b12b0adfcdc2 315 // this is a new request, so add it and sort the vector
jmarkel44 115:1558e01d04c6 316 string cid(msg->id);
jmarkel44 115:1558e01d04c6 317 string input(msg->input_tag);
jmarkel44 115:1558e01d04c6 318 Control c(cid, msg->controlType, input, msg->priority, CONTROL_OFF);
jmarkel44 80:b12b0adfcdc2 319 pos->second.push_back(c);
jmarkel44 80:b12b0adfcdc2 320 std::sort(pos->second.begin(), pos->second.end());
jmarkel44 80:b12b0adfcdc2 321 }
jmarkel44 71:34856d21f2bf 322 }
jmarkel44 66:db1425574b58 323
jmarkel44 66:db1425574b58 324 return 0;
jmarkel44 66:db1425574b58 325 }
jmarkel44 66:db1425574b58 326
jmarkel44 74:03ccf04998b5 327 /*****************************************************************************
jmarkel44 74:03ccf04998b5 328 * Function: unregisterControl
jmarkel44 74:03ccf04998b5 329 * Description:
jmarkel44 74:03ccf04998b5 330 *
jmarkel44 74:03ccf04998b5 331 * @param id -> control identifier
jmarkel44 74:03ccf04998b5 332 * @param pri -> priority
jmarkel44 74:03ccf04998b5 333 * @param output -> output (e.g. "o_rly5)
jmarkel44 74:03ccf04998b5 334 *
jmarkel44 74:03ccf04998b5 335 * @return 0 on success; -1 on error
jmarkel44 74:03ccf04998b5 336 *****************************************************************************/
jmarkel44 72:3754b352f156 337 static int unregisterControl(const char *id, unsigned int pri, const char *output)
jmarkel44 72:3754b352f156 338 {
jmarkel44 72:3754b352f156 339 // attempt to find the output in the map
jmarkel44 72:3754b352f156 340 StringOutputVector_t::iterator pos;
jmarkel44 80:b12b0adfcdc2 341 bool found = false;
jmarkel44 72:3754b352f156 342
jmarkel44 72:3754b352f156 343 pos = outputMap.find(output);
jmarkel44 72:3754b352f156 344 if ( pos == outputMap.end() ) {
jmarkel44 72:3754b352f156 345 printf("%s: failed to find the designated output %s\n",
jmarkel44 72:3754b352f156 346 __func__, output);
jmarkel44 72:3754b352f156 347 return -1;
jmarkel44 72:3754b352f156 348 }
jmarkel44 72:3754b352f156 349
jmarkel44 72:3754b352f156 350 // find the control in the list
jmarkel44 72:3754b352f156 351 vector<Control>::iterator v;
jmarkel44 72:3754b352f156 352 for ( v = pos->second.begin(); v != pos->second.end(); ++v) {
jmarkel44 72:3754b352f156 353 if ( strcmp(v->getId().c_str(), id) == 0 ) {
jmarkel44 72:3754b352f156 354 // delete this entry
jmarkel44 72:3754b352f156 355 pos->second.erase(v);
jmarkel44 80:b12b0adfcdc2 356 found = true;
jmarkel44 72:3754b352f156 357 break;
jmarkel44 72:3754b352f156 358 }
jmarkel44 74:03ccf04998b5 359 }
jmarkel44 80:b12b0adfcdc2 360 if ( !found ) {
jmarkel44 80:b12b0adfcdc2 361 logError("%s: failed to find control %s in list", __func__, id);
jmarkel44 80:b12b0adfcdc2 362 return -1;
jmarkel44 80:b12b0adfcdc2 363 }
jmarkel44 72:3754b352f156 364 return 0;
jmarkel44 72:3754b352f156 365 }
jmarkel44 72:3754b352f156 366
jmarkel44 72:3754b352f156 367 /*****************************************************************************
jmarkel44 72:3754b352f156 368 * Function: loadPersistentOutputs
jmarkel44 72:3754b352f156 369 * Description: pump up the output map based on persistent files
jmarkel44 72:3754b352f156 370 *
jmarkel44 72:3754b352f156 371 * @param args -> not used
jmarkel44 72:3754b352f156 372 * @return none
jmarkel44 72:3754b352f156 373 *****************************************************************************/
jmarkel44 66:db1425574b58 374 static void loadPersistentOutputs(void)
jmarkel44 66:db1425574b58 375 {
jmarkel44 66:db1425574b58 376 bool status;
jmarkel44 66:db1425574b58 377 MbedJSONValue json_value;
jmarkel44 71:34856d21f2bf 378
jmarkel44 77:43e0a3d9e536 379 printf("\rLoading persistent outputs:\n");
jmarkel44 66:db1425574b58 380
jmarkel44 66:db1425574b58 381 std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles();
jmarkel44 70:7427f4959201 382
jmarkel44 66:db1425574b58 383 for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) {
jmarkel44 67:49f266601d83 384 if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) {
jmarkel44 66:db1425574b58 385
jmarkel44 66:db1425574b58 386 logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name);
jmarkel44 66:db1425574b58 387
jmarkel44 66:db1425574b58 388 char scratchBuf[1024];
jmarkel44 66:db1425574b58 389
jmarkel44 66:db1425574b58 390 status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024);
jmarkel44 66:db1425574b58 391 if( status != true ) {
jmarkel44 66:db1425574b58 392 logInfo("(%d)read file failed, status=%d", __LINE__, status);
jmarkel44 66:db1425574b58 393 } else {
jmarkel44 66:db1425574b58 394 logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf );
jmarkel44 66:db1425574b58 395 }
jmarkel44 66:db1425574b58 396
jmarkel44 66:db1425574b58 397 parse( json_value, scratchBuf );
jmarkel44 66:db1425574b58 398
jmarkel44 66:db1425574b58 399 string id = json_value["id"].get<string>();
jmarkel44 77:43e0a3d9e536 400 printf("\r output %s loaded\n", i->name);
jmarkel44 74:03ccf04998b5 401
jmarkel44 80:b12b0adfcdc2 402 // emplace the empty control vector into the output map
jmarkel44 71:34856d21f2bf 403 vector<Control> v;
jmarkel44 71:34856d21f2bf 404 outputMap[id] = v;
jmarkel44 66:db1425574b58 405 }
jmarkel44 66:db1425574b58 406 }
jmarkel44 70:7427f4959201 407 }