![](/media/cache/profiles/100d5d9191f185eeb98d6e291756954a.50x50_q85.png)
Measure system
Dependencies: EthernetNetIf mbed RF12B
server/impl/SimpleHandler.cpp@3:799d8c61fb03, 2011-05-17 (annotated)
- Committer:
- benecsj
- Date:
- Tue May 17 16:49:23 2011 +0000
- Revision:
- 3:799d8c61fb03
- Parent:
- 2:afe5826411e3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benecsj | 0:8d62137f7ff4 | 1 | |
benecsj | 0:8d62137f7ff4 | 2 | /* |
benecsj | 0:8d62137f7ff4 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
benecsj | 1:b26ab2467b1a | 4 | |
benecsj | 0:8d62137f7ff4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
benecsj | 0:8d62137f7ff4 | 6 | of this software and associated documentation files (the "Software"), to deal |
benecsj | 0:8d62137f7ff4 | 7 | in the Software without restriction, including without limitation the rights |
benecsj | 0:8d62137f7ff4 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
benecsj | 0:8d62137f7ff4 | 9 | copies of the Software, and to permit persons to whom the Software is |
benecsj | 0:8d62137f7ff4 | 10 | furnished to do so, subject to the following conditions: |
benecsj | 1:b26ab2467b1a | 11 | |
benecsj | 0:8d62137f7ff4 | 12 | The above copyright notice and this permission notice shall be included in |
benecsj | 0:8d62137f7ff4 | 13 | all copies or substantial portions of the Software. |
benecsj | 1:b26ab2467b1a | 14 | |
benecsj | 0:8d62137f7ff4 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
benecsj | 0:8d62137f7ff4 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
benecsj | 0:8d62137f7ff4 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
benecsj | 0:8d62137f7ff4 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
benecsj | 0:8d62137f7ff4 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
benecsj | 0:8d62137f7ff4 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
benecsj | 0:8d62137f7ff4 | 21 | THE SOFTWARE. |
benecsj | 0:8d62137f7ff4 | 22 | */ |
benecsj | 0:8d62137f7ff4 | 23 | |
benecsj | 1:b26ab2467b1a | 24 | #include "MeasureSystem.h" |
benecsj | 0:8d62137f7ff4 | 25 | #include "SimpleHandler.h" |
benecsj | 0:8d62137f7ff4 | 26 | #include "DS1820.h" |
benecsj | 1:b26ab2467b1a | 27 | |
benecsj | 1:b26ab2467b1a | 28 | extern DS1820* probe[2]; |
benecsj | 0:8d62137f7ff4 | 29 | extern int devices_found; |
benecsj | 0:8d62137f7ff4 | 30 | |
benecsj | 2:afe5826411e3 | 31 | #define CHUNK_SIZE 128 |
benecsj | 2:afe5826411e3 | 32 | |
benecsj | 0:8d62137f7ff4 | 33 | //#define __DEBUG |
benecsj | 0:8d62137f7ff4 | 34 | //#include "dbg/dbg.h" |
benecsj | 0:8d62137f7ff4 | 35 | |
benecsj | 1:b26ab2467b1a | 36 | SimpleHandler::SimpleHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket) {} |
benecsj | 0:8d62137f7ff4 | 37 | |
benecsj | 1:b26ab2467b1a | 38 | SimpleHandler::~SimpleHandler() { |
benecsj | 1:b26ab2467b1a | 39 | HandlerActive = false; |
benecsj | 1:b26ab2467b1a | 40 | printf("2-SIMPLE-Handler destroyed\r\n"); |
benecsj | 0:8d62137f7ff4 | 41 | } |
benecsj | 0:8d62137f7ff4 | 42 | |
benecsj | 1:b26ab2467b1a | 43 | void SimpleHandler::doGet() { |
benecsj | 1:b26ab2467b1a | 44 | printf("2-SIMPLE-In SimpleHandler::doGet()\r\n"); |
benecsj | 2:afe5826411e3 | 45 | printf("rootpath: %s path: %s\r\n",rootPath().c_str(),path().c_str()); |
benecsj | 1:b26ab2467b1a | 46 | if (path().compare("/currentdata") == 0) { |
benecsj | 2:afe5826411e3 | 47 | CurrentData(); |
benecsj | 1:b26ab2467b1a | 48 | } else |
benecsj | 1:b26ab2467b1a | 49 | if (path().compare("/currenthtml") == 0) { |
benecsj | 2:afe5826411e3 | 50 | CurrentHtml(); |
benecsj | 2:afe5826411e3 | 51 | } else if (path().compare("/reset") == 0) { |
benecsj | 2:afe5826411e3 | 52 | ResetMe(); |
benecsj | 2:afe5826411e3 | 53 | } else if (path().compare("/deletelogs") == 0) { |
benecsj | 2:afe5826411e3 | 54 | DeleteLogs(); |
benecsj | 2:afe5826411e3 | 55 | } else if (path().compare("/deletesyslog") == 0) { |
benecsj | 2:afe5826411e3 | 56 | DeleteSysLog(); |
benecsj | 2:afe5826411e3 | 57 | } else { |
benecsj | 2:afe5826411e3 | 58 | if (0 ==path().find("/interval/")) { |
benecsj | 2:afe5826411e3 | 59 | SetInterval(); |
benecsj | 2:afe5826411e3 | 60 | } else if (0 ==path().find("/deletelog/")) { |
benecsj | 2:afe5826411e3 | 61 | DeleteLog(); |
benecsj | 3:799d8c61fb03 | 62 | } else if (0 ==path().find("/setdate/")) { |
benecsj | 3:799d8c61fb03 | 63 | SetDate(); |
benecsj | 2:afe5826411e3 | 64 | } else { |
benecsj | 2:afe5826411e3 | 65 | InvalidCommand(); |
benecsj | 2:afe5826411e3 | 66 | } |
benecsj | 1:b26ab2467b1a | 67 | |
benecsj | 1:b26ab2467b1a | 68 | } |
benecsj | 1:b26ab2467b1a | 69 | |
benecsj | 1:b26ab2467b1a | 70 | |
benecsj | 1:b26ab2467b1a | 71 | printf("2-SIMPLE-Exit SimpleHandler::doGet()\r\n"); |
benecsj | 0:8d62137f7ff4 | 72 | } |
benecsj | 0:8d62137f7ff4 | 73 | |
benecsj | 1:b26ab2467b1a | 74 | void SimpleHandler::doPost() { |
benecsj | 2:afe5826411e3 | 75 | printf("2-SIMPLE-In SimpleHandler::doPost() STARTED\r\n"); |
benecsj | 2:afe5826411e3 | 76 | |
benecsj | 2:afe5826411e3 | 77 | int res=-1; |
benecsj | 2:afe5826411e3 | 78 | char data[dataLen()+1]; |
benecsj | 2:afe5826411e3 | 79 | res = readData(data,dataLen()); |
benecsj | 2:afe5826411e3 | 80 | data[dataLen()] = 0; |
benecsj | 2:afe5826411e3 | 81 | printf("2-SIMPLE-<Data received: %d | %d>\r\n%s\r\n2-SIMPLE-<END>\r\n",dataLen(),res,data); |
benecsj | 2:afe5826411e3 | 82 | if (res == dataLen()) { |
benecsj | 2:afe5826411e3 | 83 | postOK = 1; |
benecsj | 2:afe5826411e3 | 84 | } else { |
benecsj | 2:afe5826411e3 | 85 | postOK = 0; |
benecsj | 2:afe5826411e3 | 86 | } |
benecsj | 1:b26ab2467b1a | 87 | |
benecsj | 1:b26ab2467b1a | 88 | if (path().compare("/start") == 0) { |
benecsj | 2:afe5826411e3 | 89 | MeasureStart(); |
benecsj | 2:afe5826411e3 | 90 | } else if (path().compare("/stop") == 0) { |
benecsj | 2:afe5826411e3 | 91 | printf("CALLING MEASURE STOP\r\n"); |
benecsj | 2:afe5826411e3 | 92 | MeasureStop(); |
benecsj | 2:afe5826411e3 | 93 | } else if (path().compare("/saveconfig") == 0 & postOK==1) { |
benecsj | 2:afe5826411e3 | 94 | printf("CALLING CONFIG SAVER\r\n"); |
benecsj | 2:afe5826411e3 | 95 | ReceiveConfig(data); |
benecsj | 2:afe5826411e3 | 96 | } else if (path().compare("/login") == 0 & postOK==1) { |
benecsj | 2:afe5826411e3 | 97 | printf("CALLING USER CHECK\r\n"); |
benecsj | 2:afe5826411e3 | 98 | Login(data); |
benecsj | 2:afe5826411e3 | 99 | return; |
benecsj | 2:afe5826411e3 | 100 | } else { |
benecsj | 2:afe5826411e3 | 101 | printf ("PATH = %s\r\n ",path().c_str()); |
benecsj | 2:afe5826411e3 | 102 | printf ("INVALID POST COMMAND\r\n"); |
benecsj | 1:b26ab2467b1a | 103 | } |
benecsj | 1:b26ab2467b1a | 104 | |
benecsj | 2:afe5826411e3 | 105 | string htmltext (""); |
benecsj | 2:afe5826411e3 | 106 | htmltext +="<html>\r\n"; |
benecsj | 2:afe5826411e3 | 107 | htmltext +="<head>\r\n"; |
benecsj | 2:afe5826411e3 | 108 | htmltext +="</head>\r\n"; |
benecsj | 2:afe5826411e3 | 109 | htmltext +="<body bgcolor=""#0000FF"" "; |
benecsj | 2:afe5826411e3 | 110 | htmltext += "text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">\r\n"; |
benecsj | 2:afe5826411e3 | 111 | if (postOK ==1) { |
benecsj | 2:afe5826411e3 | 112 | htmltext +="<h2>Post received.</h2>\r\n"; |
benecsj | 2:afe5826411e3 | 113 | } else { |
benecsj | 2:afe5826411e3 | 114 | htmltext +="<h2>Post not received.</h2>\r\n"; |
benecsj | 2:afe5826411e3 | 115 | } |
benecsj | 2:afe5826411e3 | 116 | htmltext +="<script type=""text/javascript"">\r\n"; |
benecsj | 2:afe5826411e3 | 117 | htmltext += "setTimeout(""history.back()"",1500);"; |
benecsj | 2:afe5826411e3 | 118 | htmltext +="</script>\r\n"; |
benecsj | 2:afe5826411e3 | 119 | htmltext +="</body>\r\n"; |
benecsj | 2:afe5826411e3 | 120 | htmltext +="</html>\r\n"; |
benecsj | 1:b26ab2467b1a | 121 | |
benecsj | 2:afe5826411e3 | 122 | printf("2-SIMPLE-In SimpleHandler::doPost() htmltext done\r\n"); |
benecsj | 2:afe5826411e3 | 123 | |
benecsj | 2:afe5826411e3 | 124 | char temp[350]; |
benecsj | 2:afe5826411e3 | 125 | strcpy(temp, htmltext.c_str()); |
benecsj | 2:afe5826411e3 | 126 | |
benecsj | 2:afe5826411e3 | 127 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 128 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 129 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 130 | printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n"); |
benecsj | 2:afe5826411e3 | 131 | } |
benecsj | 2:afe5826411e3 | 132 | |
benecsj | 2:afe5826411e3 | 133 | void SimpleHandler::onReadable() { |
benecsj | 2:afe5826411e3 | 134 | |
benecsj | 1:b26ab2467b1a | 135 | } |
benecsj | 1:b26ab2467b1a | 136 | |
benecsj | 1:b26ab2467b1a | 137 | void SimpleHandler::doHead() { |
benecsj | 0:8d62137f7ff4 | 138 | |
benecsj | 0:8d62137f7ff4 | 139 | } |
benecsj | 0:8d62137f7ff4 | 140 | |
benecsj | 1:b26ab2467b1a | 141 | void SimpleHandler::onWriteable() { //Data has been written & buf is free |
benecsj | 1:b26ab2467b1a | 142 | printf("2-SIMPLE-SimpleHandler::onWriteable() event\r\n"); |
benecsj | 1:b26ab2467b1a | 143 | close(); //Data written, we can close the connection |
benecsj | 0:8d62137f7ff4 | 144 | } |
benecsj | 0:8d62137f7ff4 | 145 | |
benecsj | 1:b26ab2467b1a | 146 | void SimpleHandler::onClose() { //Connection is closing |
benecsj | 1:b26ab2467b1a | 147 | //Nothing to do |
benecsj | 0:8d62137f7ff4 | 148 | } |
benecsj | 2:afe5826411e3 | 149 | |
benecsj | 2:afe5826411e3 | 150 | |
benecsj | 2:afe5826411e3 | 151 | //----------------------COM HANDLERS-------------------------------- |
benecsj | 2:afe5826411e3 | 152 | |
benecsj | 2:afe5826411e3 | 153 | //-----------------------RESET ME----------------------------------- |
benecsj | 2:afe5826411e3 | 154 | void SimpleHandler::ResetMe() { |
benecsj | 2:afe5826411e3 | 155 | reset = true; |
benecsj | 2:afe5826411e3 | 156 | char temp[] = "Reset command accepted."; |
benecsj | 2:afe5826411e3 | 157 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 158 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 159 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 160 | } |
benecsj | 2:afe5826411e3 | 161 | |
benecsj | 3:799d8c61fb03 | 162 | void SimpleHandler::SetDate() { |
benecsj | 3:799d8c61fb03 | 163 | string text = path(); |
benecsj | 3:799d8c61fb03 | 164 | string text2 = text.substr (9); |
benecsj | 3:799d8c61fb03 | 165 | printf("-------TIME: %s\r\n", text2.c_str()); |
benecsj | 3:799d8c61fb03 | 166 | long time; |
benecsj | 3:799d8c61fb03 | 167 | sscanf(text2.c_str(),"%10d",&time); |
benecsj | 3:799d8c61fb03 | 168 | time+=clockoffset*3600; |
benecsj | 3:799d8c61fb03 | 169 | set_time(time); |
benecsj | 3:799d8c61fb03 | 170 | |
benecsj | 3:799d8c61fb03 | 171 | printf("-------TIME: %d\r\n", time); |
benecsj | 3:799d8c61fb03 | 172 | char temp[] = "Set date accepted."; |
benecsj | 3:799d8c61fb03 | 173 | setContentLen( strlen(temp) ); |
benecsj | 3:799d8c61fb03 | 174 | respHeaders()["Connection"] = "close"; |
benecsj | 3:799d8c61fb03 | 175 | writeData(temp, strlen(temp)); |
benecsj | 3:799d8c61fb03 | 176 | } |
benecsj | 2:afe5826411e3 | 177 | //--------------------------CURRENT DATA---------------------------- |
benecsj | 2:afe5826411e3 | 178 | void SimpleHandler::CurrentData() { |
benecsj | 2:afe5826411e3 | 179 | char resp[100] = ""; |
benecsj | 2:afe5826411e3 | 180 | char temp[100] ; |
benecsj | 2:afe5826411e3 | 181 | |
benecsj | 2:afe5826411e3 | 182 | char buf[50]; |
benecsj | 2:afe5826411e3 | 183 | ctTime = time(NULL); |
benecsj | 2:afe5826411e3 | 184 | ctTime += (clockoffset*3600); |
benecsj | 2:afe5826411e3 | 185 | strftime(buf,sizeof(buf), "%H:%M:%S\r\n", localtime(&ctTime)); |
benecsj | 2:afe5826411e3 | 186 | strcat(resp,buf); |
benecsj | 2:afe5826411e3 | 187 | |
benecsj | 2:afe5826411e3 | 188 | probe[0]->convert_temperature(DS1820::all_devices); |
benecsj | 2:afe5826411e3 | 189 | for (int i=0; i<devices_found; i++) { |
benecsj | 2:afe5826411e3 | 190 | sprintf(temp,"%d.)%3.1fC ",(i+1),(probe[i]->temperature('c'))); |
benecsj | 2:afe5826411e3 | 191 | strcat(resp,temp); |
benecsj | 2:afe5826411e3 | 192 | } |
benecsj | 2:afe5826411e3 | 193 | |
benecsj | 2:afe5826411e3 | 194 | setContentLen( strlen(resp) ); |
benecsj | 2:afe5826411e3 | 195 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 196 | writeData(resp, strlen(resp)); |
benecsj | 2:afe5826411e3 | 197 | } |
benecsj | 2:afe5826411e3 | 198 | //--------------------------CURRENT HTML---------------------------- |
benecsj | 2:afe5826411e3 | 199 | void SimpleHandler::CurrentHtml() { |
benecsj | 2:afe5826411e3 | 200 | char temp[1000]; |
benecsj | 2:afe5826411e3 | 201 | printf("2-SIMPLE- Generating html code\r\n"); |
benecsj | 2:afe5826411e3 | 202 | |
benecsj | 2:afe5826411e3 | 203 | string htmltext (""); |
benecsj | 2:afe5826411e3 | 204 | htmltext +="<html>"; |
benecsj | 2:afe5826411e3 | 205 | htmltext +="<head>"; |
benecsj | 2:afe5826411e3 | 206 | htmltext += "<title>mbed Measure System</title>"; |
benecsj | 2:afe5826411e3 | 207 | htmltext +="</head>"; |
benecsj | 2:afe5826411e3 | 208 | htmltext +="<body bgcolor=""#0000FF"" text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">"; |
benecsj | 2:afe5826411e3 | 209 | htmltext +="<div align=""center"">"; |
benecsj | 2:afe5826411e3 | 210 | htmltext +="<h1>Current values</h1>"; |
benecsj | 2:afe5826411e3 | 211 | |
benecsj | 2:afe5826411e3 | 212 | printf("2-SIMPLE- Reading temps\r\n"); |
benecsj | 2:afe5826411e3 | 213 | |
benecsj | 2:afe5826411e3 | 214 | probe[0]->convert_temperature(DS1820::all_devices); |
benecsj | 2:afe5826411e3 | 215 | for (int i=0; i<devices_found; i++) { |
benecsj | 2:afe5826411e3 | 216 | sprintf(temp,"%d.) %3.1f C",(i+1),(probe[i]->temperature('c'))); |
benecsj | 2:afe5826411e3 | 217 | htmltext +="<h1>"; |
benecsj | 2:afe5826411e3 | 218 | htmltext += temp; |
benecsj | 2:afe5826411e3 | 219 | htmltext +="</h1>"; |
benecsj | 2:afe5826411e3 | 220 | } |
benecsj | 2:afe5826411e3 | 221 | |
benecsj | 2:afe5826411e3 | 222 | printf("2-SIMPLE- Generating html code\r\n"); |
benecsj | 2:afe5826411e3 | 223 | |
benecsj | 2:afe5826411e3 | 224 | htmltext +="<h1><a href=""javascript:location.reload(true)"">Refresh</a></h1>"; |
benecsj | 2:afe5826411e3 | 225 | htmltext +="<h1><a href=""javascript:history.back()"">Return</a></h1>"; |
benecsj | 2:afe5826411e3 | 226 | htmltext +="</div>"; |
benecsj | 2:afe5826411e3 | 227 | htmltext +="</body>"; |
benecsj | 2:afe5826411e3 | 228 | htmltext +="</html>"; |
benecsj | 2:afe5826411e3 | 229 | |
benecsj | 2:afe5826411e3 | 230 | strcpy(temp, htmltext.c_str()); |
benecsj | 2:afe5826411e3 | 231 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 232 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 233 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 234 | } |
benecsj | 2:afe5826411e3 | 235 | //--------------------INVALID COMMAND----------------------- |
benecsj | 2:afe5826411e3 | 236 | void SimpleHandler::InvalidCommand() { |
benecsj | 2:afe5826411e3 | 237 | char temp[] = "Invalid com command."; |
benecsj | 2:afe5826411e3 | 238 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 239 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 240 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 241 | } |
benecsj | 2:afe5826411e3 | 242 | //-----------------------READY------------------------------------ |
benecsj | 2:afe5826411e3 | 243 | void SimpleHandler::Response(char *input) { |
benecsj | 2:afe5826411e3 | 244 | setContentLen( strlen(input) ); |
benecsj | 2:afe5826411e3 | 245 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 246 | writeData(input, strlen(input)); |
benecsj | 2:afe5826411e3 | 247 | } |
benecsj | 2:afe5826411e3 | 248 | //-------------------DELETE LOGS------------------------------------- |
benecsj | 2:afe5826411e3 | 249 | void SimpleHandler::DeleteLogs() { |
benecsj | 2:afe5826411e3 | 250 | DIR *d = opendir(DATA_FOLDER); |
benecsj | 2:afe5826411e3 | 251 | string file; |
benecsj | 2:afe5826411e3 | 252 | struct dirent *p; |
benecsj | 2:afe5826411e3 | 253 | printf("\nList of files in the directory %s:\r\n", DATA_FOLDER); |
benecsj | 2:afe5826411e3 | 254 | while ( (p = readdir(d)) != NULL ) { |
benecsj | 2:afe5826411e3 | 255 | |
benecsj | 2:afe5826411e3 | 256 | file = ""; |
benecsj | 2:afe5826411e3 | 257 | file +=DATA_FOLDER; |
benecsj | 2:afe5826411e3 | 258 | file += "/"; |
benecsj | 2:afe5826411e3 | 259 | file += p->d_name; |
benecsj | 2:afe5826411e3 | 260 | printf("Deleting - %s\r\n", file.c_str()); |
benecsj | 2:afe5826411e3 | 261 | if (logging==1) { |
benecsj | 2:afe5826411e3 | 262 | if (file.find(logfile)==0) { |
benecsj | 2:afe5826411e3 | 263 | continue; |
benecsj | 2:afe5826411e3 | 264 | } |
benecsj | 2:afe5826411e3 | 265 | } |
benecsj | 2:afe5826411e3 | 266 | remove( file.c_str() ); |
benecsj | 2:afe5826411e3 | 267 | } |
benecsj | 2:afe5826411e3 | 268 | closedir(d); |
benecsj | 2:afe5826411e3 | 269 | Response("Done"); |
benecsj | 2:afe5826411e3 | 270 | } |
benecsj | 2:afe5826411e3 | 271 | //-------------------------Delete single log file---------------------- |
benecsj | 2:afe5826411e3 | 272 | void SimpleHandler::DeleteLog() { |
benecsj | 2:afe5826411e3 | 273 | string text = path(); |
benecsj | 2:afe5826411e3 | 274 | string text2 = DATA_FOLDER; |
benecsj | 2:afe5826411e3 | 275 | text2 += text.substr (10); |
benecsj | 2:afe5826411e3 | 276 | |
benecsj | 2:afe5826411e3 | 277 | if (logging==1) { |
benecsj | 2:afe5826411e3 | 278 | if (text2.compare(logfile)==0) { |
benecsj | 2:afe5826411e3 | 279 | Response("CantDelete logging file."); |
benecsj | 2:afe5826411e3 | 280 | return; |
benecsj | 2:afe5826411e3 | 281 | } |
benecsj | 2:afe5826411e3 | 282 | } |
benecsj | 2:afe5826411e3 | 283 | |
benecsj | 2:afe5826411e3 | 284 | printf("Deleting log: %s\r\n",text2.c_str()); |
benecsj | 2:afe5826411e3 | 285 | remove (text2.c_str()); |
benecsj | 2:afe5826411e3 | 286 | Response("Done"); |
benecsj | 2:afe5826411e3 | 287 | } |
benecsj | 2:afe5826411e3 | 288 | //-------------------------Delete system log file---------------------- |
benecsj | 2:afe5826411e3 | 289 | void SimpleHandler::DeleteSysLog() { |
benecsj | 2:afe5826411e3 | 290 | remove (LOGGER_FILE); |
benecsj | 2:afe5826411e3 | 291 | LogWrite(""); |
benecsj | 2:afe5826411e3 | 292 | Response("Done"); |
benecsj | 2:afe5826411e3 | 293 | } |
benecsj | 2:afe5826411e3 | 294 | |
benecsj | 2:afe5826411e3 | 295 | |
benecsj | 2:afe5826411e3 | 296 | //----------------------SET INTERVAL------------------------------ |
benecsj | 2:afe5826411e3 | 297 | void SimpleHandler::SetInterval() { |
benecsj | 2:afe5826411e3 | 298 | string text = path(); |
benecsj | 2:afe5826411e3 | 299 | string text2 = text.substr (10); |
benecsj | 2:afe5826411e3 | 300 | printf("Interval value: %s\r\n",text2.c_str()); |
benecsj | 2:afe5826411e3 | 301 | |
benecsj | 2:afe5826411e3 | 302 | if (0!= strlen(text2.c_str())) { |
benecsj | 2:afe5826411e3 | 303 | int i =atoi(text2.c_str()); |
benecsj | 2:afe5826411e3 | 304 | if (i!=0) { |
benecsj | 2:afe5826411e3 | 305 | interval = i; |
benecsj | 2:afe5826411e3 | 306 | SaveConfig(); |
benecsj | 2:afe5826411e3 | 307 | } |
benecsj | 2:afe5826411e3 | 308 | } |
benecsj | 2:afe5826411e3 | 309 | Response("Done"); |
benecsj | 2:afe5826411e3 | 310 | } |
benecsj | 2:afe5826411e3 | 311 | //-----------------------RECEIVE CONFIG----------------------------- |
benecsj | 2:afe5826411e3 | 312 | void SimpleHandler::ReceiveConfig(char* data) { |
benecsj | 2:afe5826411e3 | 313 | //---Save config-------- |
benecsj | 2:afe5826411e3 | 314 | FILE *fs_src; |
benecsj | 2:afe5826411e3 | 315 | fs_src = fopen( CONFIG_FILE, "w" ); |
benecsj | 2:afe5826411e3 | 316 | fprintf(fs_src,"%s",data); |
benecsj | 2:afe5826411e3 | 317 | fclose( fs_src ); |
benecsj | 2:afe5826411e3 | 318 | //---Load new config---- |
benecsj | 2:afe5826411e3 | 319 | LoadConfig(); |
benecsj | 2:afe5826411e3 | 320 | } |
benecsj | 2:afe5826411e3 | 321 | //----------------------LOGIN CHECK--------------------------------- |
benecsj | 2:afe5826411e3 | 322 | void SimpleHandler::Login(char* data) { |
benecsj | 2:afe5826411e3 | 323 | bool ok = false; |
benecsj | 2:afe5826411e3 | 324 | char temp[5]; |
benecsj | 2:afe5826411e3 | 325 | |
benecsj | 2:afe5826411e3 | 326 | if (strlen(data)>7) { |
benecsj | 2:afe5826411e3 | 327 | char buffer[50 ]; |
benecsj | 2:afe5826411e3 | 328 | string str =""; |
benecsj | 2:afe5826411e3 | 329 | FILE *fs_src; |
benecsj | 2:afe5826411e3 | 330 | fs_src = fopen( LOGIN_FILE, "r" ); |
benecsj | 2:afe5826411e3 | 331 | while (fgets(buffer, 100, fs_src)) { |
benecsj | 2:afe5826411e3 | 332 | str = buffer; |
benecsj | 2:afe5826411e3 | 333 | printf("Storedlogin:(%s)\r\n",buffer); |
benecsj | 2:afe5826411e3 | 334 | if (str.find(data)==0) { |
benecsj | 2:afe5826411e3 | 335 | ok = true; |
benecsj | 2:afe5826411e3 | 336 | } |
benecsj | 2:afe5826411e3 | 337 | } |
benecsj | 2:afe5826411e3 | 338 | |
benecsj | 2:afe5826411e3 | 339 | fclose( fs_src ); |
benecsj | 2:afe5826411e3 | 340 | } |
benecsj | 2:afe5826411e3 | 341 | |
benecsj | 2:afe5826411e3 | 342 | if (ok) { |
benecsj | 2:afe5826411e3 | 343 | strcpy(temp, "OK"); |
benecsj | 2:afe5826411e3 | 344 | } else { |
benecsj | 2:afe5826411e3 | 345 | strcpy(temp, "NO"); |
benecsj | 2:afe5826411e3 | 346 | } |
benecsj | 2:afe5826411e3 | 347 | |
benecsj | 2:afe5826411e3 | 348 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 349 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 350 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 351 | printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n"); |
benecsj | 2:afe5826411e3 | 352 | } |