Measure system
Dependencies: EthernetNetIf mbed RF12B
server/impl/SimpleHandler.cpp@2:afe5826411e3, 2011-03-27 (annotated)
- Committer:
- benecsj
- Date:
- Sun Mar 27 07:56:59 2011 +0000
- Revision:
- 2:afe5826411e3
- Parent:
- 1:b26ab2467b1a
- Child:
- 3:799d8c61fb03
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 | 2:afe5826411e3 | 62 | } else { |
benecsj | 2:afe5826411e3 | 63 | InvalidCommand(); |
benecsj | 2:afe5826411e3 | 64 | } |
benecsj | 1:b26ab2467b1a | 65 | |
benecsj | 1:b26ab2467b1a | 66 | } |
benecsj | 1:b26ab2467b1a | 67 | |
benecsj | 1:b26ab2467b1a | 68 | |
benecsj | 1:b26ab2467b1a | 69 | printf("2-SIMPLE-Exit SimpleHandler::doGet()\r\n"); |
benecsj | 0:8d62137f7ff4 | 70 | } |
benecsj | 0:8d62137f7ff4 | 71 | |
benecsj | 1:b26ab2467b1a | 72 | void SimpleHandler::doPost() { |
benecsj | 2:afe5826411e3 | 73 | printf("2-SIMPLE-In SimpleHandler::doPost() STARTED\r\n"); |
benecsj | 2:afe5826411e3 | 74 | |
benecsj | 2:afe5826411e3 | 75 | int res=-1; |
benecsj | 2:afe5826411e3 | 76 | char data[dataLen()+1]; |
benecsj | 2:afe5826411e3 | 77 | res = readData(data,dataLen()); |
benecsj | 2:afe5826411e3 | 78 | data[dataLen()] = 0; |
benecsj | 2:afe5826411e3 | 79 | printf("2-SIMPLE-<Data received: %d | %d>\r\n%s\r\n2-SIMPLE-<END>\r\n",dataLen(),res,data); |
benecsj | 2:afe5826411e3 | 80 | if (res == dataLen()) { |
benecsj | 2:afe5826411e3 | 81 | postOK = 1; |
benecsj | 2:afe5826411e3 | 82 | } else { |
benecsj | 2:afe5826411e3 | 83 | postOK = 0; |
benecsj | 2:afe5826411e3 | 84 | } |
benecsj | 1:b26ab2467b1a | 85 | |
benecsj | 1:b26ab2467b1a | 86 | if (path().compare("/start") == 0) { |
benecsj | 2:afe5826411e3 | 87 | MeasureStart(); |
benecsj | 2:afe5826411e3 | 88 | } else if (path().compare("/stop") == 0) { |
benecsj | 2:afe5826411e3 | 89 | printf("CALLING MEASURE STOP\r\n"); |
benecsj | 2:afe5826411e3 | 90 | MeasureStop(); |
benecsj | 2:afe5826411e3 | 91 | } else if (path().compare("/saveconfig") == 0 & postOK==1) { |
benecsj | 2:afe5826411e3 | 92 | printf("CALLING CONFIG SAVER\r\n"); |
benecsj | 2:afe5826411e3 | 93 | ReceiveConfig(data); |
benecsj | 2:afe5826411e3 | 94 | } else if (path().compare("/login") == 0 & postOK==1) { |
benecsj | 2:afe5826411e3 | 95 | printf("CALLING USER CHECK\r\n"); |
benecsj | 2:afe5826411e3 | 96 | Login(data); |
benecsj | 2:afe5826411e3 | 97 | return; |
benecsj | 2:afe5826411e3 | 98 | } else { |
benecsj | 2:afe5826411e3 | 99 | printf ("PATH = %s\r\n ",path().c_str()); |
benecsj | 2:afe5826411e3 | 100 | printf ("INVALID POST COMMAND\r\n"); |
benecsj | 1:b26ab2467b1a | 101 | } |
benecsj | 1:b26ab2467b1a | 102 | |
benecsj | 2:afe5826411e3 | 103 | string htmltext (""); |
benecsj | 2:afe5826411e3 | 104 | htmltext +="<html>\r\n"; |
benecsj | 2:afe5826411e3 | 105 | htmltext +="<head>\r\n"; |
benecsj | 2:afe5826411e3 | 106 | htmltext +="</head>\r\n"; |
benecsj | 2:afe5826411e3 | 107 | htmltext +="<body bgcolor=""#0000FF"" "; |
benecsj | 2:afe5826411e3 | 108 | htmltext += "text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">\r\n"; |
benecsj | 2:afe5826411e3 | 109 | if (postOK ==1) { |
benecsj | 2:afe5826411e3 | 110 | htmltext +="<h2>Post received.</h2>\r\n"; |
benecsj | 2:afe5826411e3 | 111 | } else { |
benecsj | 2:afe5826411e3 | 112 | htmltext +="<h2>Post not received.</h2>\r\n"; |
benecsj | 2:afe5826411e3 | 113 | } |
benecsj | 2:afe5826411e3 | 114 | htmltext +="<script type=""text/javascript"">\r\n"; |
benecsj | 2:afe5826411e3 | 115 | htmltext += "setTimeout(""history.back()"",1500);"; |
benecsj | 2:afe5826411e3 | 116 | htmltext +="</script>\r\n"; |
benecsj | 2:afe5826411e3 | 117 | htmltext +="</body>\r\n"; |
benecsj | 2:afe5826411e3 | 118 | htmltext +="</html>\r\n"; |
benecsj | 1:b26ab2467b1a | 119 | |
benecsj | 2:afe5826411e3 | 120 | printf("2-SIMPLE-In SimpleHandler::doPost() htmltext done\r\n"); |
benecsj | 2:afe5826411e3 | 121 | |
benecsj | 2:afe5826411e3 | 122 | char temp[350]; |
benecsj | 2:afe5826411e3 | 123 | strcpy(temp, htmltext.c_str()); |
benecsj | 2:afe5826411e3 | 124 | |
benecsj | 2:afe5826411e3 | 125 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 126 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 127 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 128 | printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n"); |
benecsj | 2:afe5826411e3 | 129 | } |
benecsj | 2:afe5826411e3 | 130 | |
benecsj | 2:afe5826411e3 | 131 | void SimpleHandler::onReadable() { |
benecsj | 2:afe5826411e3 | 132 | |
benecsj | 1:b26ab2467b1a | 133 | } |
benecsj | 1:b26ab2467b1a | 134 | |
benecsj | 1:b26ab2467b1a | 135 | void SimpleHandler::doHead() { |
benecsj | 0:8d62137f7ff4 | 136 | |
benecsj | 0:8d62137f7ff4 | 137 | } |
benecsj | 0:8d62137f7ff4 | 138 | |
benecsj | 1:b26ab2467b1a | 139 | void SimpleHandler::onWriteable() { //Data has been written & buf is free |
benecsj | 1:b26ab2467b1a | 140 | printf("2-SIMPLE-SimpleHandler::onWriteable() event\r\n"); |
benecsj | 1:b26ab2467b1a | 141 | close(); //Data written, we can close the connection |
benecsj | 0:8d62137f7ff4 | 142 | } |
benecsj | 0:8d62137f7ff4 | 143 | |
benecsj | 1:b26ab2467b1a | 144 | void SimpleHandler::onClose() { //Connection is closing |
benecsj | 1:b26ab2467b1a | 145 | //Nothing to do |
benecsj | 0:8d62137f7ff4 | 146 | } |
benecsj | 2:afe5826411e3 | 147 | |
benecsj | 2:afe5826411e3 | 148 | |
benecsj | 2:afe5826411e3 | 149 | //----------------------COM HANDLERS-------------------------------- |
benecsj | 2:afe5826411e3 | 150 | |
benecsj | 2:afe5826411e3 | 151 | //-----------------------RESET ME----------------------------------- |
benecsj | 2:afe5826411e3 | 152 | void SimpleHandler::ResetMe() { |
benecsj | 2:afe5826411e3 | 153 | reset = true; |
benecsj | 2:afe5826411e3 | 154 | char temp[] = "Reset command accepted."; |
benecsj | 2:afe5826411e3 | 155 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 156 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 157 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 158 | } |
benecsj | 2:afe5826411e3 | 159 | |
benecsj | 2:afe5826411e3 | 160 | //--------------------------CURRENT DATA---------------------------- |
benecsj | 2:afe5826411e3 | 161 | void SimpleHandler::CurrentData() { |
benecsj | 2:afe5826411e3 | 162 | char resp[100] = ""; |
benecsj | 2:afe5826411e3 | 163 | char temp[100] ; |
benecsj | 2:afe5826411e3 | 164 | |
benecsj | 2:afe5826411e3 | 165 | char buf[50]; |
benecsj | 2:afe5826411e3 | 166 | ctTime = time(NULL); |
benecsj | 2:afe5826411e3 | 167 | ctTime += (clockoffset*3600); |
benecsj | 2:afe5826411e3 | 168 | strftime(buf,sizeof(buf), "%H:%M:%S\r\n", localtime(&ctTime)); |
benecsj | 2:afe5826411e3 | 169 | strcat(resp,buf); |
benecsj | 2:afe5826411e3 | 170 | |
benecsj | 2:afe5826411e3 | 171 | probe[0]->convert_temperature(DS1820::all_devices); |
benecsj | 2:afe5826411e3 | 172 | for (int i=0; i<devices_found; i++) { |
benecsj | 2:afe5826411e3 | 173 | sprintf(temp,"%d.)%3.1fC ",(i+1),(probe[i]->temperature('c'))); |
benecsj | 2:afe5826411e3 | 174 | strcat(resp,temp); |
benecsj | 2:afe5826411e3 | 175 | } |
benecsj | 2:afe5826411e3 | 176 | |
benecsj | 2:afe5826411e3 | 177 | setContentLen( strlen(resp) ); |
benecsj | 2:afe5826411e3 | 178 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 179 | writeData(resp, strlen(resp)); |
benecsj | 2:afe5826411e3 | 180 | } |
benecsj | 2:afe5826411e3 | 181 | //--------------------------CURRENT HTML---------------------------- |
benecsj | 2:afe5826411e3 | 182 | void SimpleHandler::CurrentHtml() { |
benecsj | 2:afe5826411e3 | 183 | char temp[1000]; |
benecsj | 2:afe5826411e3 | 184 | printf("2-SIMPLE- Generating html code\r\n"); |
benecsj | 2:afe5826411e3 | 185 | |
benecsj | 2:afe5826411e3 | 186 | string htmltext (""); |
benecsj | 2:afe5826411e3 | 187 | htmltext +="<html>"; |
benecsj | 2:afe5826411e3 | 188 | htmltext +="<head>"; |
benecsj | 2:afe5826411e3 | 189 | htmltext += "<title>mbed Measure System</title>"; |
benecsj | 2:afe5826411e3 | 190 | htmltext +="</head>"; |
benecsj | 2:afe5826411e3 | 191 | htmltext +="<body bgcolor=""#0000FF"" text=""#00FFFF"" link=""#AAFFD4"" vlink=""#FFFFFF"" alink=""#FF0000"">"; |
benecsj | 2:afe5826411e3 | 192 | htmltext +="<div align=""center"">"; |
benecsj | 2:afe5826411e3 | 193 | htmltext +="<h1>Current values</h1>"; |
benecsj | 2:afe5826411e3 | 194 | |
benecsj | 2:afe5826411e3 | 195 | printf("2-SIMPLE- Reading temps\r\n"); |
benecsj | 2:afe5826411e3 | 196 | |
benecsj | 2:afe5826411e3 | 197 | probe[0]->convert_temperature(DS1820::all_devices); |
benecsj | 2:afe5826411e3 | 198 | for (int i=0; i<devices_found; i++) { |
benecsj | 2:afe5826411e3 | 199 | sprintf(temp,"%d.) %3.1f C",(i+1),(probe[i]->temperature('c'))); |
benecsj | 2:afe5826411e3 | 200 | htmltext +="<h1>"; |
benecsj | 2:afe5826411e3 | 201 | htmltext += temp; |
benecsj | 2:afe5826411e3 | 202 | htmltext +="</h1>"; |
benecsj | 2:afe5826411e3 | 203 | } |
benecsj | 2:afe5826411e3 | 204 | |
benecsj | 2:afe5826411e3 | 205 | printf("2-SIMPLE- Generating html code\r\n"); |
benecsj | 2:afe5826411e3 | 206 | |
benecsj | 2:afe5826411e3 | 207 | htmltext +="<h1><a href=""javascript:location.reload(true)"">Refresh</a></h1>"; |
benecsj | 2:afe5826411e3 | 208 | htmltext +="<h1><a href=""javascript:history.back()"">Return</a></h1>"; |
benecsj | 2:afe5826411e3 | 209 | htmltext +="</div>"; |
benecsj | 2:afe5826411e3 | 210 | htmltext +="</body>"; |
benecsj | 2:afe5826411e3 | 211 | htmltext +="</html>"; |
benecsj | 2:afe5826411e3 | 212 | |
benecsj | 2:afe5826411e3 | 213 | strcpy(temp, htmltext.c_str()); |
benecsj | 2:afe5826411e3 | 214 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 215 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 216 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 217 | } |
benecsj | 2:afe5826411e3 | 218 | //--------------------INVALID COMMAND----------------------- |
benecsj | 2:afe5826411e3 | 219 | void SimpleHandler::InvalidCommand() { |
benecsj | 2:afe5826411e3 | 220 | char temp[] = "Invalid com command."; |
benecsj | 2:afe5826411e3 | 221 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 222 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 223 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 224 | } |
benecsj | 2:afe5826411e3 | 225 | //-----------------------READY------------------------------------ |
benecsj | 2:afe5826411e3 | 226 | void SimpleHandler::Response(char *input) { |
benecsj | 2:afe5826411e3 | 227 | setContentLen( strlen(input) ); |
benecsj | 2:afe5826411e3 | 228 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 229 | writeData(input, strlen(input)); |
benecsj | 2:afe5826411e3 | 230 | } |
benecsj | 2:afe5826411e3 | 231 | //-------------------DELETE LOGS------------------------------------- |
benecsj | 2:afe5826411e3 | 232 | void SimpleHandler::DeleteLogs() { |
benecsj | 2:afe5826411e3 | 233 | DIR *d = opendir(DATA_FOLDER); |
benecsj | 2:afe5826411e3 | 234 | string file; |
benecsj | 2:afe5826411e3 | 235 | struct dirent *p; |
benecsj | 2:afe5826411e3 | 236 | printf("\nList of files in the directory %s:\r\n", DATA_FOLDER); |
benecsj | 2:afe5826411e3 | 237 | while ( (p = readdir(d)) != NULL ) { |
benecsj | 2:afe5826411e3 | 238 | |
benecsj | 2:afe5826411e3 | 239 | file = ""; |
benecsj | 2:afe5826411e3 | 240 | file +=DATA_FOLDER; |
benecsj | 2:afe5826411e3 | 241 | file += "/"; |
benecsj | 2:afe5826411e3 | 242 | file += p->d_name; |
benecsj | 2:afe5826411e3 | 243 | printf("Deleting - %s\r\n", file.c_str()); |
benecsj | 2:afe5826411e3 | 244 | if (logging==1) { |
benecsj | 2:afe5826411e3 | 245 | if (file.find(logfile)==0) { |
benecsj | 2:afe5826411e3 | 246 | continue; |
benecsj | 2:afe5826411e3 | 247 | } |
benecsj | 2:afe5826411e3 | 248 | } |
benecsj | 2:afe5826411e3 | 249 | remove( file.c_str() ); |
benecsj | 2:afe5826411e3 | 250 | } |
benecsj | 2:afe5826411e3 | 251 | closedir(d); |
benecsj | 2:afe5826411e3 | 252 | Response("Done"); |
benecsj | 2:afe5826411e3 | 253 | } |
benecsj | 2:afe5826411e3 | 254 | //-------------------------Delete single log file---------------------- |
benecsj | 2:afe5826411e3 | 255 | void SimpleHandler::DeleteLog() { |
benecsj | 2:afe5826411e3 | 256 | string text = path(); |
benecsj | 2:afe5826411e3 | 257 | string text2 = DATA_FOLDER; |
benecsj | 2:afe5826411e3 | 258 | text2 += text.substr (10); |
benecsj | 2:afe5826411e3 | 259 | |
benecsj | 2:afe5826411e3 | 260 | if (logging==1) { |
benecsj | 2:afe5826411e3 | 261 | if (text2.compare(logfile)==0) { |
benecsj | 2:afe5826411e3 | 262 | Response("CantDelete logging file."); |
benecsj | 2:afe5826411e3 | 263 | return; |
benecsj | 2:afe5826411e3 | 264 | } |
benecsj | 2:afe5826411e3 | 265 | } |
benecsj | 2:afe5826411e3 | 266 | |
benecsj | 2:afe5826411e3 | 267 | printf("Deleting log: %s\r\n",text2.c_str()); |
benecsj | 2:afe5826411e3 | 268 | remove (text2.c_str()); |
benecsj | 2:afe5826411e3 | 269 | Response("Done"); |
benecsj | 2:afe5826411e3 | 270 | } |
benecsj | 2:afe5826411e3 | 271 | //-------------------------Delete system log file---------------------- |
benecsj | 2:afe5826411e3 | 272 | void SimpleHandler::DeleteSysLog() { |
benecsj | 2:afe5826411e3 | 273 | remove (LOGGER_FILE); |
benecsj | 2:afe5826411e3 | 274 | LogWrite(""); |
benecsj | 2:afe5826411e3 | 275 | Response("Done"); |
benecsj | 2:afe5826411e3 | 276 | } |
benecsj | 2:afe5826411e3 | 277 | |
benecsj | 2:afe5826411e3 | 278 | |
benecsj | 2:afe5826411e3 | 279 | //----------------------SET INTERVAL------------------------------ |
benecsj | 2:afe5826411e3 | 280 | void SimpleHandler::SetInterval() { |
benecsj | 2:afe5826411e3 | 281 | string text = path(); |
benecsj | 2:afe5826411e3 | 282 | string text2 = text.substr (10); |
benecsj | 2:afe5826411e3 | 283 | printf("Interval value: %s\r\n",text2.c_str()); |
benecsj | 2:afe5826411e3 | 284 | |
benecsj | 2:afe5826411e3 | 285 | if (0!= strlen(text2.c_str())) { |
benecsj | 2:afe5826411e3 | 286 | int i =atoi(text2.c_str()); |
benecsj | 2:afe5826411e3 | 287 | if (i!=0) { |
benecsj | 2:afe5826411e3 | 288 | interval = i; |
benecsj | 2:afe5826411e3 | 289 | SaveConfig(); |
benecsj | 2:afe5826411e3 | 290 | } |
benecsj | 2:afe5826411e3 | 291 | } |
benecsj | 2:afe5826411e3 | 292 | Response("Done"); |
benecsj | 2:afe5826411e3 | 293 | } |
benecsj | 2:afe5826411e3 | 294 | //-----------------------RECEIVE CONFIG----------------------------- |
benecsj | 2:afe5826411e3 | 295 | void SimpleHandler::ReceiveConfig(char* data) { |
benecsj | 2:afe5826411e3 | 296 | //---Save config-------- |
benecsj | 2:afe5826411e3 | 297 | FILE *fs_src; |
benecsj | 2:afe5826411e3 | 298 | fs_src = fopen( CONFIG_FILE, "w" ); |
benecsj | 2:afe5826411e3 | 299 | fprintf(fs_src,"%s",data); |
benecsj | 2:afe5826411e3 | 300 | fclose( fs_src ); |
benecsj | 2:afe5826411e3 | 301 | //---Load new config---- |
benecsj | 2:afe5826411e3 | 302 | LoadConfig(); |
benecsj | 2:afe5826411e3 | 303 | } |
benecsj | 2:afe5826411e3 | 304 | //----------------------LOGIN CHECK--------------------------------- |
benecsj | 2:afe5826411e3 | 305 | void SimpleHandler::Login(char* data) { |
benecsj | 2:afe5826411e3 | 306 | bool ok = false; |
benecsj | 2:afe5826411e3 | 307 | char temp[5]; |
benecsj | 2:afe5826411e3 | 308 | |
benecsj | 2:afe5826411e3 | 309 | if (strlen(data)>7) { |
benecsj | 2:afe5826411e3 | 310 | char buffer[50 ]; |
benecsj | 2:afe5826411e3 | 311 | string str =""; |
benecsj | 2:afe5826411e3 | 312 | FILE *fs_src; |
benecsj | 2:afe5826411e3 | 313 | fs_src = fopen( LOGIN_FILE, "r" ); |
benecsj | 2:afe5826411e3 | 314 | while (fgets(buffer, 100, fs_src)) { |
benecsj | 2:afe5826411e3 | 315 | str = buffer; |
benecsj | 2:afe5826411e3 | 316 | printf("Storedlogin:(%s)\r\n",buffer); |
benecsj | 2:afe5826411e3 | 317 | if (str.find(data)==0) { |
benecsj | 2:afe5826411e3 | 318 | ok = true; |
benecsj | 2:afe5826411e3 | 319 | } |
benecsj | 2:afe5826411e3 | 320 | } |
benecsj | 2:afe5826411e3 | 321 | |
benecsj | 2:afe5826411e3 | 322 | fclose( fs_src ); |
benecsj | 2:afe5826411e3 | 323 | } |
benecsj | 2:afe5826411e3 | 324 | |
benecsj | 2:afe5826411e3 | 325 | if (ok) { |
benecsj | 2:afe5826411e3 | 326 | strcpy(temp, "OK"); |
benecsj | 2:afe5826411e3 | 327 | } else { |
benecsj | 2:afe5826411e3 | 328 | strcpy(temp, "NO"); |
benecsj | 2:afe5826411e3 | 329 | } |
benecsj | 2:afe5826411e3 | 330 | |
benecsj | 2:afe5826411e3 | 331 | setContentLen( strlen(temp) ); |
benecsj | 2:afe5826411e3 | 332 | respHeaders()["Connection"] = "close"; |
benecsj | 2:afe5826411e3 | 333 | writeData(temp, strlen(temp)); |
benecsj | 2:afe5826411e3 | 334 | printf("2-SIMPLE-In SimpleHandler::doPost() DONE\r\n"); |
benecsj | 2:afe5826411e3 | 335 | } |