api.ino 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Control API for Robot motion system */
  2. //Require direction (up / down / left / right)
  3. //Requite state (start / stop)
  4. void handleMovements(AsyncWebServerRequest *request) {
  5. String direction = GetPara(request, "direction");
  6. String state = GetPara(request, "state");
  7. //Set current moving direciton
  8. if (direction == "up"){
  9. movingDirection = 0;
  10. }else if (direction == "down"){
  11. movingDirection = 1;
  12. }else if (direction == "left"){
  13. movingDirection = 2;
  14. }else if (direction == "right"){
  15. movingDirection = 3;
  16. }else{
  17. request->send(500, "text/plain", "500 - Invalid direction given");
  18. return;
  19. }
  20. if (state == "start"){
  21. //Start
  22. movingActive = true;
  23. }else{
  24. //Stop
  25. movingActive = false;
  26. }
  27. request->send(200, "text/plain", "ok");
  28. }
  29. //Handle request for moving the pushing arm, require angle (range 0 - 130)
  30. void handlePusher(AsyncWebServerRequest *request){
  31. String angle = GetPara(request, "angle");
  32. //Angle must be within 0 - 130 (+ offset)
  33. int limitedAngle = constrain(angle.toInt(), 0, 130 + SERVO_ALIGNMENT_OFFSET);
  34. servoSwitchPusher.write(limitedAngle);
  35. request->send(200, "text/plain", "ok");
  36. }
  37. //Handle request for moving the cover, require state: {open / close}
  38. void handleCover(AsyncWebServerRequest *request){
  39. String state = GetPara(request, "state");
  40. if (state == "open"){
  41. //Open
  42. servoCoverPusher.write(90);
  43. }else{
  44. //Close
  45. servoCoverPusher.write(0);
  46. }
  47. request->send(200, "text/plain", "ok");
  48. }
  49. //Render emoji to screen
  50. void handleRenderEmoji(AsyncWebServerRequest *request) {
  51. String anicode = GetPara(request, "anicode");
  52. char anicodeChar = anicode.c_str()[0];
  53. setAnimationCode(anicodeChar);
  54. request->send(200, "text/plain", "ok");
  55. }
  56. /* File System API for SD Browser*/
  57. //File delete API
  58. void handleFileDelete(AsyncWebServerRequest *request) {
  59. String path = GetPara(request, "path");
  60. if (path == "") {
  61. request->send(400, "text/plain", "Missing 'path' parameter");
  62. return;
  63. }
  64. Serial.print("Requested delete path: ");
  65. Serial.println(path);
  66. if (SD.exists(path)) {
  67. if (SD.remove(path)) {
  68. request->send(200, "text/plain", "File removed");
  69. } else {
  70. request->send(500, "text/plain", "Failed to delete file");
  71. }
  72. } else {
  73. request->send(404, "text/plain", "File not found");
  74. }
  75. }
  76. //File download API
  77. void handleFileDownload(AsyncWebServerRequest *request) {
  78. String path = GetPara(request, "path");
  79. if (path == "") {
  80. request->send(404, "text/plain", "'path' parameter not given");
  81. return;
  82. }
  83. Serial.print("Requested path: ");
  84. Serial.println(path);
  85. if (SD.exists(path)) {
  86. String contentType = getMime(path);
  87. request->send(SD, path, contentType, false);
  88. } else {
  89. request->send(404, "text/plain", "File not found");
  90. }
  91. }
  92. //List dir API
  93. void handleListDir(AsyncWebServerRequest *request) {
  94. //Get the folder path to be listed
  95. //As ESP8266 dont have enough memory for proper struct to json conv, we are hacking a json string out of a single for-loop
  96. String jsonString = "[";
  97. String folderSubPath = GetPara(request, "dir");
  98. String folderPath = "/" + folderSubPath;
  99. if (SD.exists(folderPath)) {
  100. File root = SD.open(folderPath);
  101. bool firstObject = true;
  102. if (root) {
  103. while (true) {
  104. File entry = root.openNextFile();
  105. if (!entry) {
  106. // No more files
  107. break;
  108. } else {
  109. //There are more lines. Add a , to the end of the previous json object
  110. if (!firstObject) {
  111. jsonString = jsonString + ",";
  112. } else {
  113. firstObject = false;
  114. }
  115. }
  116. String isDirString = "true";
  117. if (!entry.isDirectory()) {
  118. isDirString = "false";
  119. }
  120. jsonString = jsonString + "{\"Filename\":\"" + entry.name() + "\",\"Filesize\":" + String(entry.size()) + ",\"IsDir\":" + isDirString + "}";
  121. entry.close();
  122. }
  123. root.close();
  124. jsonString += "]";
  125. request->send(200, "application/json", jsonString);
  126. } else {
  127. request->send(500, "text/plain", "500 - Path open error");
  128. }
  129. } else {
  130. request->send(404, "text/plain", "404 - Path not found");
  131. }
  132. Serial.println(folderPath);
  133. }
  134. // Function to handle file uploads
  135. void handleFileUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
  136. static File uploadFile;
  137. static String uploadPath = "/";
  138. if (request->hasParam("dir")) {
  139. uploadPath = "/" + request->getParam("dir")->value() + "/";
  140. }
  141. if (index == 0) {
  142. String path = uploadPath + filename;
  143. Serial.printf("Upload Start: %s\n", path.c_str());
  144. uploadFile = SD.open(path, FILE_WRITE);
  145. if (!uploadFile) {
  146. Serial.println("Failed to open file for writing");
  147. return request->send(500, "text/plain", "File Upload Failed");
  148. }
  149. }
  150. // Write the received data to the file
  151. if (uploadFile) {
  152. uploadFile.write(data, len);
  153. }
  154. if (final) {
  155. // Close the file at the final chunk
  156. if (uploadFile) {
  157. uploadFile.close();
  158. Serial.printf("Upload End: %s (%u)\n", filename.c_str(), index + len);
  159. return request->send(200, "text/plain", "File Uploaded");
  160. } else {
  161. return request->send(500, "text/plain", "File Upload Failed");
  162. }
  163. }
  164. }