Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Fri Sep 16 15:30:47 2016 +0000
Revision:
67:49f266601d83
Parent:
66:db1425574b58
Child:
70:7427f4959201
bug fixes;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmarkel44 67:49f266601d83 1 #include "OutputTask.h"
jmarkel44 48:1c7861d80d16 2 #include "global.h"
jmarkel44 66:db1425574b58 3 #include "MbedJSONValue.h"
jmarkel44 48:1c7861d80d16 4
jmarkel44 67:49f266601d83 5 static int createOutput(const char *controlFile);
jmarkel44 66:db1425574b58 6 static void loadPersistentOutputs(void);
jmarkel44 63:0ded43237b22 7
jmarkel44 66:db1425574b58 8 typedef std::map<string, string> StringOutputVector_t;
jmarkel44 66:db1425574b58 9
jmarkel44 66:db1425574b58 10 StringOutputVector_t outputMap;
jmarkel44 63:0ded43237b22 11
jmarkel44 48:1c7861d80d16 12 void OutputTask(void const *args)
jmarkel44 48:1c7861d80d16 13 {
jmarkel44 51:66b820f203a5 14 UNUSED(args);
jmarkel44 67:49f266601d83 15
jmarkel44 67:49f266601d83 16 printf("\r%s has started...\n", __func__);
jmarkel44 63:0ded43237b22 17
jmarkel44 66:db1425574b58 18 loadPersistentOutputs();
jmarkel44 66:db1425574b58 19
jmarkel44 48:1c7861d80d16 20 while (true) {
jmarkel44 51:66b820f203a5 21 // wait for an event
jmarkel44 56:225786c56315 22 osEvent evt = OutputMasterMailBox.get();
jmarkel44 51:66b820f203a5 23 if (evt.status == osEventMail) {
jmarkel44 63:0ded43237b22 24 OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p;
jmarkel44 56:225786c56315 25 printf("\r%s received message from someone...\n", __func__);
jmarkel44 63:0ded43237b22 26 printf("\rmsg->relay = %s\n", msg->output.c_str());
jmarkel44 56:225786c56315 27 printf("\rmsg->state = %s\n", msg->state == ON ? "ON" : "OFF");
jmarkel44 56:225786c56315 28 printf("\rmsg->priority = %u\n", msg->priority);
jmarkel44 63:0ded43237b22 29
jmarkel44 63:0ded43237b22 30 switch ( msg->action ) {
jmarkel44 63:0ded43237b22 31 case ACTION_NEW:
jmarkel44 63:0ded43237b22 32 // read the file and and create an output entry
jmarkel44 66:db1425574b58 33 (void) createOutput(msg->controlFile);
jmarkel44 63:0ded43237b22 34 break;
jmarkel44 66:db1425574b58 35 case ACTION_CONTROL_REQ:
jmarkel44 63:0ded43237b22 36 default:
jmarkel44 63:0ded43237b22 37 break;
jmarkel44 63:0ded43237b22 38 }
jmarkel44 63:0ded43237b22 39
jmarkel44 56:225786c56315 40 // free the message
jmarkel44 56:225786c56315 41 OutputMasterMailBox.free(msg);
jmarkel44 51:66b820f203a5 42 }
jmarkel44 48:1c7861d80d16 43 }
jmarkel44 66:db1425574b58 44 }
jmarkel44 66:db1425574b58 45
jmarkel44 66:db1425574b58 46 void DisplayOutputs(void)
jmarkel44 66:db1425574b58 47 {
jmarkel44 66:db1425574b58 48 StringOutputVector_t::iterator pos;
jmarkel44 66:db1425574b58 49
jmarkel44 66:db1425574b58 50 for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) {
jmarkel44 66:db1425574b58 51 printf("\r [%s] [ %s]\n", pos->first.c_str(), pos->second.c_str());
jmarkel44 66:db1425574b58 52 }
jmarkel44 66:db1425574b58 53 printf("\r\n");
jmarkel44 66:db1425574b58 54 }
jmarkel44 66:db1425574b58 55
jmarkel44 66:db1425574b58 56 static int createOutput(const char *controlFile)
jmarkel44 66:db1425574b58 57 {
jmarkel44 66:db1425574b58 58 char dataBuf[1024];
jmarkel44 67:49f266601d83 59 int status = GLOBAL_mdot->readUserFile(controlFile, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 67:49f266601d83 60 if ( status != true ) {
jmarkel44 66:db1425574b58 61 logError("%s failed to read %s", __func__, controlFile);
jmarkel44 66:db1425574b58 62 return -1;
jmarkel44 66:db1425574b58 63 }
jmarkel44 66:db1425574b58 64
jmarkel44 66:db1425574b58 65 MbedJSONValue json_value;
jmarkel44 66:db1425574b58 66 parse(json_value, dataBuf);
jmarkel44 66:db1425574b58 67
jmarkel44 66:db1425574b58 68 // extract the relay information
jmarkel44 66:db1425574b58 69 string id = json_value["id"].get<string>();
jmarkel44 66:db1425574b58 70
jmarkel44 66:db1425574b58 71 // maps shouldnt' allow duplicates
jmarkel44 66:db1425574b58 72 outputMap[id] = "Null for now";
jmarkel44 66:db1425574b58 73
jmarkel44 66:db1425574b58 74 return 0;
jmarkel44 66:db1425574b58 75 }
jmarkel44 66:db1425574b58 76
jmarkel44 66:db1425574b58 77 static void loadPersistentOutputs(void)
jmarkel44 66:db1425574b58 78 {
jmarkel44 66:db1425574b58 79 bool status;
jmarkel44 66:db1425574b58 80 MbedJSONValue json_value;
jmarkel44 66:db1425574b58 81
jmarkel44 66:db1425574b58 82 std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles();
jmarkel44 67:49f266601d83 83
jmarkel44 66:db1425574b58 84 for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) {
jmarkel44 67:49f266601d83 85 if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) {
jmarkel44 66:db1425574b58 86
jmarkel44 66:db1425574b58 87 logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name);
jmarkel44 66:db1425574b58 88
jmarkel44 66:db1425574b58 89 char scratchBuf[1024];
jmarkel44 66:db1425574b58 90
jmarkel44 66:db1425574b58 91 status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024);
jmarkel44 66:db1425574b58 92 if( status != true ) {
jmarkel44 66:db1425574b58 93 logInfo("(%d)read file failed, status=%d", __LINE__, status);
jmarkel44 66:db1425574b58 94 } else {
jmarkel44 66:db1425574b58 95 logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf );
jmarkel44 66:db1425574b58 96 }
jmarkel44 66:db1425574b58 97
jmarkel44 66:db1425574b58 98 parse( json_value, scratchBuf );
jmarkel44 66:db1425574b58 99
jmarkel44 66:db1425574b58 100 string id = json_value["id"].get<string>();
jmarkel44 67:49f266601d83 101 printf("\r%s id = %s\n", __func__, id.c_str());
jmarkel44 66:db1425574b58 102 outputMap[id] = "Null for now";
jmarkel44 66:db1425574b58 103 }
jmarkel44 66:db1425574b58 104 }
jmarkel44 66:db1425574b58 105 }