SmartApp_Miun
SmartApp.cpp@0:3cd336aba723, 2017-04-12 (annotated)
- Committer:
- biwa1400
- Date:
- Wed Apr 12 05:28:25 2017 +0000
- Revision:
- 0:3cd336aba723
- Child:
- 1:5b29579a065e
B
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
biwa1400 | 0:3cd336aba723 | 1 | #include "SmartApp.h" |
biwa1400 | 0:3cd336aba723 | 2 | #include "mbed.h" |
biwa1400 | 0:3cd336aba723 | 3 | #include "mDot.h" |
biwa1400 | 0:3cd336aba723 | 4 | #include <map> |
biwa1400 | 0:3cd336aba723 | 5 | #include <string> |
biwa1400 | 0:3cd336aba723 | 6 | #include <iostream> |
biwa1400 | 0:3cd336aba723 | 7 | |
biwa1400 | 0:3cd336aba723 | 8 | const std::string MIUN::SmartApp::emptyPayload = ""; |
biwa1400 | 0:3cd336aba723 | 9 | const int MIUN::SmartApp::defaultPort = 0; |
biwa1400 | 0:3cd336aba723 | 10 | |
biwa1400 | 0:3cd336aba723 | 11 | MIUN::SmartApp::SmartApp() |
biwa1400 | 0:3cd336aba723 | 12 | { |
biwa1400 | 0:3cd336aba723 | 13 | } |
biwa1400 | 0:3cd336aba723 | 14 | |
biwa1400 | 0:3cd336aba723 | 15 | |
biwa1400 | 0:3cd336aba723 | 16 | void MIUN::SmartApp::initializeSettings() |
biwa1400 | 0:3cd336aba723 | 17 | { |
biwa1400 | 0:3cd336aba723 | 18 | std::string network_name = "MiunLora"; |
biwa1400 | 0:3cd336aba723 | 19 | std::string network_passphrase = "MiunLora"; |
biwa1400 | 0:3cd336aba723 | 20 | uint8_t ack = 3; //Times |
biwa1400 | 0:3cd336aba723 | 21 | uint8_t joinDelay = 1; //Seconds |
biwa1400 | 0:3cd336aba723 | 22 | bool ADR = true; |
biwa1400 | 0:3cd336aba723 | 23 | uint8_t sf = 5; //SF |
biwa1400 | 0:3cd336aba723 | 24 | uint32_t defaultSleepTime_s = 10; //s |
biwa1400 | 0:3cd336aba723 | 25 | |
biwa1400 | 0:3cd336aba723 | 26 | // Handle ReConfing Network |
biwa1400 | 0:3cd336aba723 | 27 | lora.networkConfig(network_name, network_passphrase,ack, joinDelay, ADR, sf, defaultSleepTime_s); |
biwa1400 | 0:3cd336aba723 | 28 | } |
biwa1400 | 0:3cd336aba723 | 29 | |
biwa1400 | 0:3cd336aba723 | 30 | /*** Running Thread ***/ |
biwa1400 | 0:3cd336aba723 | 31 | void MIUN::SmartApp::startRunning() |
biwa1400 | 0:3cd336aba723 | 32 | { |
biwa1400 | 0:3cd336aba723 | 33 | while(true) |
biwa1400 | 0:3cd336aba723 | 34 | { |
biwa1400 | 0:3cd336aba723 | 35 | lora.joinNetwork(); |
biwa1400 | 0:3cd336aba723 | 36 | heartBeat(); |
biwa1400 | 0:3cd336aba723 | 37 | triggerApp(); |
biwa1400 | 0:3cd336aba723 | 38 | checkInterrupt(); |
biwa1400 | 0:3cd336aba723 | 39 | lora.setBatteryLevel(measuredBattery()); |
biwa1400 | 0:3cd336aba723 | 40 | lora.sleep(); |
biwa1400 | 0:3cd336aba723 | 41 | } |
biwa1400 | 0:3cd336aba723 | 42 | } |
biwa1400 | 0:3cd336aba723 | 43 | |
biwa1400 | 0:3cd336aba723 | 44 | |
biwa1400 | 0:3cd336aba723 | 45 | void MIUN::SmartApp::addApp(AppUnit& app) |
biwa1400 | 0:3cd336aba723 | 46 | { |
biwa1400 | 0:3cd336aba723 | 47 | appMap.insert ( std::pair<uint8_t,AppUnit&>(app.getAppNumber(),app) ); |
biwa1400 | 0:3cd336aba723 | 48 | } |
biwa1400 | 0:3cd336aba723 | 49 | |
biwa1400 | 0:3cd336aba723 | 50 | void MIUN::SmartApp::showAppMap() |
biwa1400 | 0:3cd336aba723 | 51 | { |
biwa1400 | 0:3cd336aba723 | 52 | logInfo("Show App:"); |
biwa1400 | 0:3cd336aba723 | 53 | for (std::map<uint8_t,AppUnit&>::iterator it=appMap.begin(); it!=appMap.end(); ++it) |
biwa1400 | 0:3cd336aba723 | 54 | { |
biwa1400 | 0:3cd336aba723 | 55 | logInfo("App Number: %u",it->second.getAppNumber()); |
biwa1400 | 0:3cd336aba723 | 56 | } |
biwa1400 | 0:3cd336aba723 | 57 | } |
biwa1400 | 0:3cd336aba723 | 58 | |
biwa1400 | 0:3cd336aba723 | 59 | uint8_t MIUN::SmartApp::measuredBattery() |
biwa1400 | 0:3cd336aba723 | 60 | { |
biwa1400 | 0:3cd336aba723 | 61 | return 255; |
biwa1400 | 0:3cd336aba723 | 62 | } |
biwa1400 | 0:3cd336aba723 | 63 | |
biwa1400 | 0:3cd336aba723 | 64 | /*** Private Functions ***/ |
biwa1400 | 0:3cd336aba723 | 65 | |
biwa1400 | 0:3cd336aba723 | 66 | void MIUN::SmartApp::heartBeat() |
biwa1400 | 0:3cd336aba723 | 67 | { |
biwa1400 | 0:3cd336aba723 | 68 | if(AppUnit::hasSendPayload() == true) |
biwa1400 | 0:3cd336aba723 | 69 | { |
biwa1400 | 0:3cd336aba723 | 70 | //1. get Send Payload |
biwa1400 | 0:3cd336aba723 | 71 | SendPayload sendPayload = AppUnit::getSendPayload(); |
biwa1400 | 0:3cd336aba723 | 72 | //2. send |
biwa1400 | 0:3cd336aba723 | 73 | lora.send(sendPayload.getPayload(),sendPayload.getAppNumber()); |
biwa1400 | 0:3cd336aba723 | 74 | } |
biwa1400 | 0:3cd336aba723 | 75 | else |
biwa1400 | 0:3cd336aba723 | 76 | { |
biwa1400 | 0:3cd336aba723 | 77 | lora.send(emptyPayload,defaultPort); |
biwa1400 | 0:3cd336aba723 | 78 | } |
biwa1400 | 0:3cd336aba723 | 79 | } |
biwa1400 | 0:3cd336aba723 | 80 | |
biwa1400 | 0:3cd336aba723 | 81 | |
biwa1400 | 0:3cd336aba723 | 82 | void MIUN::SmartApp::triggerApp() |
biwa1400 | 0:3cd336aba723 | 83 | { |
biwa1400 | 0:3cd336aba723 | 84 | int port=-1; |
biwa1400 | 0:3cd336aba723 | 85 | std::string payload = lora.receive(&port); |
biwa1400 | 0:3cd336aba723 | 86 | if( payload != emptyPayload && port>defaultPort) |
biwa1400 | 0:3cd336aba723 | 87 | { |
biwa1400 | 0:3cd336aba723 | 88 | logInfo("Enter Trigger App"); |
biwa1400 | 0:3cd336aba723 | 89 | logInfo("Payload: %s",payload.c_str()); |
biwa1400 | 0:3cd336aba723 | 90 | logInfo("Port: %d",port); |
biwa1400 | 0:3cd336aba723 | 91 | for (std::map<uint8_t,AppUnit&>::iterator it=appMap.begin(); it!=appMap.end(); ++it) |
biwa1400 | 0:3cd336aba723 | 92 | { |
biwa1400 | 0:3cd336aba723 | 93 | if(it->second.getAppNumber() == port) |
biwa1400 | 0:3cd336aba723 | 94 | { |
biwa1400 | 0:3cd336aba723 | 95 | it->second.run(payload); |
biwa1400 | 0:3cd336aba723 | 96 | break; |
biwa1400 | 0:3cd336aba723 | 97 | } |
biwa1400 | 0:3cd336aba723 | 98 | logInfo("App Number: %u",it->second.getAppNumber()); |
biwa1400 | 0:3cd336aba723 | 99 | } |
biwa1400 | 0:3cd336aba723 | 100 | } |
biwa1400 | 0:3cd336aba723 | 101 | } |
biwa1400 | 0:3cd336aba723 | 102 | |
biwa1400 | 0:3cd336aba723 | 103 | void MIUN::SmartApp::checkInterrupt() |
biwa1400 | 0:3cd336aba723 | 104 | { |
biwa1400 | 0:3cd336aba723 | 105 | for (std::map<uint8_t,AppUnit&>::iterator it=appMap.begin(); it!=appMap.end(); ++it) |
biwa1400 | 0:3cd336aba723 | 106 | { |
biwa1400 | 0:3cd336aba723 | 107 | it->second.interrupt(); |
biwa1400 | 0:3cd336aba723 | 108 | } |
biwa1400 | 0:3cd336aba723 | 109 | } |
biwa1400 | 0:3cd336aba723 | 110 | |
biwa1400 | 0:3cd336aba723 | 111 | |
biwa1400 | 0:3cd336aba723 | 112 | /********** Test **********/ |
biwa1400 | 0:3cd336aba723 | 113 | void MIUN::SmartApp::testThread() |
biwa1400 | 0:3cd336aba723 | 114 | { |
biwa1400 | 0:3cd336aba723 | 115 | logInfo("--- Test Thread ---"); |
biwa1400 | 0:3cd336aba723 | 116 | //AppUnit app1(1); |
biwa1400 | 0:3cd336aba723 | 117 | //addApp(app1); |
biwa1400 | 0:3cd336aba723 | 118 | showAppMap(); |
biwa1400 | 0:3cd336aba723 | 119 | startRunning(); |
biwa1400 | 0:3cd336aba723 | 120 | } |
biwa1400 | 0:3cd336aba723 | 121 | |
biwa1400 | 0:3cd336aba723 | 122 | void MIUN::SmartApp::testSaveFile() |
biwa1400 | 0:3cd336aba723 | 123 | { |
biwa1400 | 0:3cd336aba723 | 124 | logInfo("--- Test Save File ---"); |
biwa1400 | 0:3cd336aba723 | 125 | //lora.saveSleepTime(235678901); |
biwa1400 | 0:3cd336aba723 | 126 | //logInfo("Read Sleep Time: %u", lora.readSleepTime()); |
biwa1400 | 0:3cd336aba723 | 127 | while(true); |
biwa1400 | 0:3cd336aba723 | 128 | } |
biwa1400 | 0:3cd336aba723 | 129 | |
biwa1400 | 0:3cd336aba723 | 130 | void MIUN::SmartApp::testSendAndReceive() |
biwa1400 | 0:3cd336aba723 | 131 | { |
biwa1400 | 0:3cd336aba723 | 132 | logInfo("--- Test Send And Receive ---"); |
biwa1400 | 0:3cd336aba723 | 133 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 134 | while(true) |
biwa1400 | 0:3cd336aba723 | 135 | { |
biwa1400 | 0:3cd336aba723 | 136 | lora.joinNetwork(); |
biwa1400 | 0:3cd336aba723 | 137 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 138 | if(lora.send((std::string)"dabin,dabin",1)==true) |
biwa1400 | 0:3cd336aba723 | 139 | { |
biwa1400 | 0:3cd336aba723 | 140 | //logInfo("Receive Payload: [%s]", ().c_str(),&i); |
biwa1400 | 0:3cd336aba723 | 141 | } |
biwa1400 | 0:3cd336aba723 | 142 | lora.sleep(); |
biwa1400 | 0:3cd336aba723 | 143 | logInfo("---After Sleep---"); |
biwa1400 | 0:3cd336aba723 | 144 | } |
biwa1400 | 0:3cd336aba723 | 145 | } |
biwa1400 | 0:3cd336aba723 | 146 | |
biwa1400 | 0:3cd336aba723 | 147 | void MIUN::SmartApp::testMacCommand() |
biwa1400 | 0:3cd336aba723 | 148 | { |
biwa1400 | 0:3cd336aba723 | 149 | logInfo("--- Test Mac Command ---"); |
biwa1400 | 0:3cd336aba723 | 150 | while(true) |
biwa1400 | 0:3cd336aba723 | 151 | { |
biwa1400 | 0:3cd336aba723 | 152 | lora.joinNetwork(); |
biwa1400 | 0:3cd336aba723 | 153 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 154 | lora.getHandler().networkLinkCheck(); |
biwa1400 | 0:3cd336aba723 | 155 | sleep(); |
biwa1400 | 0:3cd336aba723 | 156 | logInfo("---After Sleep---"); |
biwa1400 | 0:3cd336aba723 | 157 | } |
biwa1400 | 0:3cd336aba723 | 158 | } |
biwa1400 | 0:3cd336aba723 | 159 | |
biwa1400 | 0:3cd336aba723 | 160 | |
biwa1400 | 0:3cd336aba723 | 161 | void MIUN::SmartApp::unConfirmedTest() |
biwa1400 | 0:3cd336aba723 | 162 | { |
biwa1400 | 0:3cd336aba723 | 163 | logInfo("---unConfirmedTest Test:---"); |
biwa1400 | 0:3cd336aba723 | 164 | lora.changeAck(0); |
biwa1400 | 0:3cd336aba723 | 165 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 166 | while(true) |
biwa1400 | 0:3cd336aba723 | 167 | { |
biwa1400 | 0:3cd336aba723 | 168 | lora.joinNetwork(); |
biwa1400 | 0:3cd336aba723 | 169 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 170 | if(lora.send((std::string)"dabin,dabin",1)==true) |
biwa1400 | 0:3cd336aba723 | 171 | { |
biwa1400 | 0:3cd336aba723 | 172 | //logInfo("Receive***************: [%s]", lora.receive().c_str()); |
biwa1400 | 0:3cd336aba723 | 173 | } |
biwa1400 | 0:3cd336aba723 | 174 | sleep(); |
biwa1400 | 0:3cd336aba723 | 175 | logInfo("---After Sleep---"); |
biwa1400 | 0:3cd336aba723 | 176 | } |
biwa1400 | 0:3cd336aba723 | 177 | } |
biwa1400 | 0:3cd336aba723 | 178 | |
biwa1400 | 0:3cd336aba723 | 179 | |
biwa1400 | 0:3cd336aba723 | 180 | void MIUN::SmartApp::adaptSFTest() |
biwa1400 | 0:3cd336aba723 | 181 | { |
biwa1400 | 0:3cd336aba723 | 182 | logInfo("---adapt SF Test:---"); |
biwa1400 | 0:3cd336aba723 | 183 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 184 | while(true) |
biwa1400 | 0:3cd336aba723 | 185 | { |
biwa1400 | 0:3cd336aba723 | 186 | lora.joinNetwork(); |
biwa1400 | 0:3cd336aba723 | 187 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 188 | //lora.changeAdaptSF(true); |
biwa1400 | 0:3cd336aba723 | 189 | //lora.changeSF(20); |
biwa1400 | 0:3cd336aba723 | 190 | |
biwa1400 | 0:3cd336aba723 | 191 | if(lora.send((std::string)"dabin,dabin",1)==true) |
biwa1400 | 0:3cd336aba723 | 192 | { |
biwa1400 | 0:3cd336aba723 | 193 | // logInfo("Receive***************: [%s]", lora.receive().c_str()); |
biwa1400 | 0:3cd336aba723 | 194 | } |
biwa1400 | 0:3cd336aba723 | 195 | sleep(); |
biwa1400 | 0:3cd336aba723 | 196 | logInfo("---After Sleep---"); |
biwa1400 | 0:3cd336aba723 | 197 | } |
biwa1400 | 0:3cd336aba723 | 198 | } |
biwa1400 | 0:3cd336aba723 | 199 | |
biwa1400 | 0:3cd336aba723 | 200 | void MIUN::SmartApp::staQuality() |
biwa1400 | 0:3cd336aba723 | 201 | { |
biwa1400 | 0:3cd336aba723 | 202 | logInfo("---Start Test:---"); |
biwa1400 | 0:3cd336aba723 | 203 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 204 | int total = 0; |
biwa1400 | 0:3cd336aba723 | 205 | int truely = 0; |
biwa1400 | 0:3cd336aba723 | 206 | while(true) |
biwa1400 | 0:3cd336aba723 | 207 | { |
biwa1400 | 0:3cd336aba723 | 208 | total++; |
biwa1400 | 0:3cd336aba723 | 209 | lora.joinNetwork(); |
biwa1400 | 0:3cd336aba723 | 210 | lora.showInfo(); |
biwa1400 | 0:3cd336aba723 | 211 | if(lora.send((std::string)"dabin,dabin",1)==true) |
biwa1400 | 0:3cd336aba723 | 212 | { |
biwa1400 | 0:3cd336aba723 | 213 | truely++; |
biwa1400 | 0:3cd336aba723 | 214 | } |
biwa1400 | 0:3cd336aba723 | 215 | logInfo("*******Statistics Quality:*********:"); |
biwa1400 | 0:3cd336aba723 | 216 | logInfo("Total send: %d",total); |
biwa1400 | 0:3cd336aba723 | 217 | logInfo("Successful send: %d",truely); |
biwa1400 | 0:3cd336aba723 | 218 | logInfo("Successful Ratio: %f",(float)truely/total*100); |
biwa1400 | 0:3cd336aba723 | 219 | logInfo("***********************************"); |
biwa1400 | 0:3cd336aba723 | 220 | sleep(); |
biwa1400 | 0:3cd336aba723 | 221 | logInfo("---After Sleep---"); |
biwa1400 | 0:3cd336aba723 | 222 | } |
biwa1400 | 0:3cd336aba723 | 223 | } |