Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jmarkel44
Date:
Thu Oct 06 15:36:26 2016 +0000
Revision:
192:052a419837fa
Parent:
177:9ec90c8e3ce1
Child:
195:21df85341cb3
inclusion of .hasMember

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmarkel44 96:807f04bd5256 1 /******************************************************************************
jmarkel44 96:807f04bd5256 2 *
jmarkel44 96:807f04bd5256 3 * File: ManualControl.cpp
jmarkel44 96:807f04bd5256 4 * Desciption: ICE Manual Control Class implementation
jmarkel44 96:807f04bd5256 5 *
jmarkel44 96:807f04bd5256 6 *****************************************************************************/
jmarkel44 96:807f04bd5256 7 #include "ManualControl.h"
jmarkel44 96:807f04bd5256 8 #include "mDot.h"
jmarkel44 96:807f04bd5256 9 #include "MbedJSONValue.h"
jmarkel44 96:807f04bd5256 10 #include "ModbusMasterApi.h"
jmarkel44 96:807f04bd5256 11 #include "global.h"
jmarkel44 96:807f04bd5256 12 #include <string>
jmarkel44 96:807f04bd5256 13
jmarkel44 96:807f04bd5256 14 extern mDot *GLOBAL_mdot;
jmarkel44 96:807f04bd5256 15
jmarkel44 192:052a419837fa 16 // method: load
jmarkel44 192:052a419837fa 17 // description: open the configuration file and assign data to the
jmarkel44 96:807f04bd5256 18 // setpoint control object
jmarkel44 96:807f04bd5256 19 //
jmarkel44 96:807f04bd5256 20 // @param controlFile -> name of the control file
jmarkel44 96:807f04bd5256 21 // @return true if data is assigned; false on error
jmarkel44 96:807f04bd5256 22
jmarkel44 96:807f04bd5256 23 bool ManualControl::load(string _controlFile)
jmarkel44 96:807f04bd5256 24 {
jmarkel44 97:5cf6ab71dcd0 25 MbedJSONValue json_value;
jmarkel44 97:5cf6ab71dcd0 26 controlFile = _controlFile;
jmarkel44 97:5cf6ab71dcd0 27
jmarkel44 97:5cf6ab71dcd0 28 // open and read from the control file
jmarkel44 97:5cf6ab71dcd0 29 mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY);
jmarkel44 97:5cf6ab71dcd0 30 if ( file.fd < 0 )
jmarkel44 97:5cf6ab71dcd0 31 return false;
jmarkel44 97:5cf6ab71dcd0 32
jmarkel44 97:5cf6ab71dcd0 33 // read the data into a buffer
jmarkel44 177:9ec90c8e3ce1 34 char dataBuf[MAX_FILE_SIZE];
jmarkel44 97:5cf6ab71dcd0 35
jmarkel44 97:5cf6ab71dcd0 36 int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
jmarkel44 97:5cf6ab71dcd0 37 if ( bytes_read != sizeof(dataBuf) ) {
jmarkel44 97:5cf6ab71dcd0 38 logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 39 // caller should destroy the object
jmarkel44 97:5cf6ab71dcd0 40 return false;
jmarkel44 97:5cf6ab71dcd0 41 }
jmarkel44 97:5cf6ab71dcd0 42
jmarkel44 97:5cf6ab71dcd0 43 // close the file
jmarkel44 97:5cf6ab71dcd0 44 GLOBAL_mdot->closeUserFile(file);
jmarkel44 97:5cf6ab71dcd0 45
jmarkel44 97:5cf6ab71dcd0 46 // parse the json data
jmarkel44 97:5cf6ab71dcd0 47 parse(json_value, dataBuf);
jmarkel44 97:5cf6ab71dcd0 48
jmarkel44 192:052a419837fa 49 if ( !json_value.hasMember("id") ||
jmarkel44 192:052a419837fa 50 !json_value.hasMember("output") ||
jmarkel44 192:052a419837fa 51 !json_value.hasMember("type") ||
jmarkel44 192:052a419837fa 52 !json_value.hasMember("priority") ||
jmarkel44 192:052a419837fa 53 !json_value.hasMember("duration") ||
jmarkel44 192:052a419837fa 54 !json_value.hasMember("setpoint") ||
jmarkel44 192:052a419837fa 55 !json_value.hasMember("state") ||
jmarkel44 192:052a419837fa 56 !json_value.hasMember("percent") ) {
jmarkel44 192:052a419837fa 57 logError("Manual Control file is missing expected tags");
jmarkel44 192:052a419837fa 58 return false;
jmarkel44 192:052a419837fa 59 }
jmarkel44 192:052a419837fa 60
jmarkel44 98:8eab18d03ac2 61 id = json_value ["id"].get<string>();
jmarkel44 98:8eab18d03ac2 62 output = json_value ["output"].get<string>();
jmarkel44 97:5cf6ab71dcd0 63 type = atoi(json_value["type"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 64 priority = atoi(json_value["priority"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 65 duration = atoi(json_value["duration"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 66 setpoint = atof(json_value["setpoint"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 67 state = atoi(json_value["state"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 68 percent = atoi(json_value["percent"].get<string>().c_str());
jmarkel44 97:5cf6ab71dcd0 69
jmarkel44 96:807f04bd5256 70 return true;
jmarkel44 96:807f04bd5256 71 }
jmarkel44 96:807f04bd5256 72
jmarkel44 192:052a419837fa 73 // method: start
jmarkel44 192:052a419837fa 74 // description: start the manual control
jmarkel44 96:807f04bd5256 75 //
jmarkel44 96:807f04bd5256 76 // @param none
jmarkel44 96:807f04bd5256 77 // @return none
jmarkel44 96:807f04bd5256 78
jmarkel44 97:5cf6ab71dcd0 79 void ManualControl::start(void)
jmarkel44 96:807f04bd5256 80 {
jmarkel44 97:5cf6ab71dcd0 81 currentState = STATE_STARTUP;
jmarkel44 97:5cf6ab71dcd0 82
jmarkel44 96:807f04bd5256 83 }
jmarkel44 96:807f04bd5256 84
jmarkel44 192:052a419837fa 85 // method: update
jmarkel44 192:052a419837fa 86 // description:
jmarkel44 96:807f04bd5256 87 //
jmarkel44 96:807f04bd5256 88 // @param none
jmarkel44 96:807f04bd5256 89 // @return none
jmarkel44 96:807f04bd5256 90
jmarkel44 96:807f04bd5256 91 int ManualControl::update(void)
jmarkel44 96:807f04bd5256 92 {
jmarkel44 97:5cf6ab71dcd0 93 int rc = 0;
jmarkel44 97:5cf6ab71dcd0 94 switch ( this->currentState ) {
jmarkel44 131:a290a3934132 95 case STATE_INIT:
jmarkel44 131:a290a3934132 96 // do nothing
jmarkel44 131:a290a3934132 97 break;
jmarkel44 97:5cf6ab71dcd0 98 case STATE_STARTUP:
jmarkel44 97:5cf6ab71dcd0 99 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 100 this->currentState = STATE_CONTROL_ON;
jmarkel44 97:5cf6ab71dcd0 101 } else {
jmarkel44 97:5cf6ab71dcd0 102 this->currentState = STATE_CONTROL_OFF;
jmarkel44 97:5cf6ab71dcd0 103 }
jmarkel44 164:7cecd731882e 104 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 105 break;
jmarkel44 97:5cf6ab71dcd0 106 case STATE_CONTROL_ON:
jmarkel44 97:5cf6ab71dcd0 107 if ( !state ) {
jmarkel44 97:5cf6ab71dcd0 108 this->currentState = STATE_CONTROL_OFF;
jmarkel44 164:7cecd731882e 109 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 110 }
jmarkel44 97:5cf6ab71dcd0 111 break;
jmarkel44 97:5cf6ab71dcd0 112 case STATE_CONTROL_OFF:
jmarkel44 97:5cf6ab71dcd0 113 if ( state ) {
jmarkel44 97:5cf6ab71dcd0 114 this->currentState = STATE_CONTROL_ON;
jmarkel44 164:7cecd731882e 115 this->powerOutput();
jmarkel44 97:5cf6ab71dcd0 116 }
jmarkel44 97:5cf6ab71dcd0 117 break;
jmarkel44 97:5cf6ab71dcd0 118 default:
jmarkel44 97:5cf6ab71dcd0 119 logError("%s unknown state %d\n", __func__, this->currentState);
jmarkel44 97:5cf6ab71dcd0 120 rc = -1;
jmarkel44 97:5cf6ab71dcd0 121 break;
jmarkel44 97:5cf6ab71dcd0 122 }
jmarkel44 97:5cf6ab71dcd0 123 return rc;
jmarkel44 96:807f04bd5256 124 }
jmarkel44 96:807f04bd5256 125
jmarkel44 192:052a419837fa 126 // method: unregisterControl
jmarkel44 192:052a419837fa 127 // description: unregister this control with the output master
jmarkel44 96:807f04bd5256 128 //
jmarkel44 96:807f04bd5256 129 // @param none
jmarkel44 96:807f04bd5256 130 // @return none
jmarkel44 96:807f04bd5256 131 int ManualControl::unregisterControl(void)
jmarkel44 96:807f04bd5256 132 {
jmarkel44 115:1558e01d04c6 133 logInfo("%s: Attempting to unregister %s\n",
jmarkel44 115:1558e01d04c6 134 __func__, controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 135
jmarkel44 97:5cf6ab71dcd0 136 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 137 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 192:052a419837fa 138
jmarkel44 121:650205ffa656 139 output_mail->controlType = CONTROL_MANUAL;
jmarkel44 121:650205ffa656 140 output_mail->action = ACTION_CONTROL_UNREGISTER;
jmarkel44 121:650205ffa656 141 output_mail->priority = this->priority;
jmarkel44 115:1558e01d04c6 142 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 97:5cf6ab71dcd0 143 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 192:052a419837fa 144
jmarkel44 97:5cf6ab71dcd0 145 OutputMasterMailBox.put(output_mail);
jmarkel44 97:5cf6ab71dcd0 146 return 0;
jmarkel44 97:5cf6ab71dcd0 147 }
jmarkel44 97:5cf6ab71dcd0 148
jmarkel44 192:052a419837fa 149 //
jmarkel44 192:052a419837fa 150 // method: powerOutput
jmarkel44 192:052a419837fa 151 // description: send a message to the output thread to power the relay
jmarkel44 192:052a419837fa 152 //
jmarkel44 192:052a419837fa 153 // @param none
jmarkel44 192:052a419837fa 154 // @return -1 on error; 0 otherwise
jmarkel44 192:052a419837fa 155 //
jmarkel44 164:7cecd731882e 156 int ManualControl::powerOutput(void)
jmarkel44 97:5cf6ab71dcd0 157 {
jmarkel44 97:5cf6ab71dcd0 158 printf("%s: %s attempting to manually turn %s relay %s\n",
jmarkel44 97:5cf6ab71dcd0 159 __func__, controlFile.c_str(), (state) ? "ON" : "OFF", id.c_str());
jmarkel44 97:5cf6ab71dcd0 160
jmarkel44 97:5cf6ab71dcd0 161 OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
jmarkel44 97:5cf6ab71dcd0 162 memset(output_mail, 0, sizeof(OutputControlMsg_t));
jmarkel44 192:052a419837fa 163
jmarkel44 121:650205ffa656 164 output_mail->controlType = CONTROL_MANUAL;
jmarkel44 115:1558e01d04c6 165 output_mail->action = (state) ? ACTION_CONTROL_ON : ACTION_CONTROL_OFF;
jmarkel44 115:1558e01d04c6 166 output_mail->priority = this->priority;
jmarkel44 115:1558e01d04c6 167 strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
jmarkel44 97:5cf6ab71dcd0 168 strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
jmarkel44 192:052a419837fa 169
jmarkel44 97:5cf6ab71dcd0 170 OutputMasterMailBox.put(output_mail);
jmarkel44 96:807f04bd5256 171 return 0;
jmarkel44 96:807f04bd5256 172 }
jmarkel44 96:807f04bd5256 173
jmarkel44 192:052a419837fa 174 //
jmarkel44 192:052a419837fa 175 // method: display
jmarkel44 192:052a419837fa 176 // description: display the pertinents
jmarkel44 192:052a419837fa 177 //
jmarkel44 192:052a419837fa 178 // @param none
jmarkel44 192:052a419837fa 179 // @return none
jmarkel44 192:052a419837fa 180 //
jmarkel44 96:807f04bd5256 181 void ManualControl::display(void)
jmarkel44 96:807f04bd5256 182 {
jmarkel44 131:a290a3934132 183 string mapper[] = { "INIT",
jmarkel44 131:a290a3934132 184 "STARTUP",
jmarkel44 97:5cf6ab71dcd0 185 "CONTROL_ON",
jmarkel44 97:5cf6ab71dcd0 186 "CONTROL_OFF"
jmarkel44 97:5cf6ab71dcd0 187 };
jmarkel44 97:5cf6ab71dcd0 188
jmarkel44 97:5cf6ab71dcd0 189 printf("\r controlFile : %s \n", controlFile.c_str());
jmarkel44 97:5cf6ab71dcd0 190 printf("\r id : %s \n", id.c_str());
jmarkel44 97:5cf6ab71dcd0 191 printf("\r type : %u \n", type);
jmarkel44 97:5cf6ab71dcd0 192 printf("\r priority : %u \n", priority);
jmarkel44 97:5cf6ab71dcd0 193 printf("\r duration : %u \n", duration);
jmarkel44 97:5cf6ab71dcd0 194 printf("\r state : %u \n", state);
jmarkel44 97:5cf6ab71dcd0 195 printf("\r percent : %u \n", percent);
jmarkel44 97:5cf6ab71dcd0 196 printf("\r currentState: %s \n", mapper[currentState].c_str());
jmarkel44 97:5cf6ab71dcd0 197
jmarkel44 97:5cf6ab71dcd0 198 printf("\r\n");
jmarkel44 96:807f04bd5256 199
jmarkel44 96:807f04bd5256 200 }