wifiserver.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. goog.provide('Blockly.Python.wifiserver');
  3. goog.require('Blockly.Python');
  4. // Blockly.Python['wifiServer_setup'] = function (block) {
  5. // var wifi_webserver= "#include <ESP8266WebServer.h>";
  6. // Blockly.Python.addInclude("wifi_webserver",wifi_webserver);
  7. // var port= Blockly.Python.valueToCode(block, 'PORT', Blockly.Python.ORDER_ATOMIC) || 80;
  8. // var code="ESP8266WebServer server("+port+");"
  9. // Blockly.Python.addDeclaration("setupServer_"+port, code);
  10. // return "";
  11. // };
  12. // Blockly.Python['wifiserver_setRouter'] = function (block) {
  13. // var response= Blockly.Python.valueToCode(block, "VAR", Blockly.Python.ORDER_ATOMIC) || "";
  14. // var url= block.getFieldValue("URL") || "/";
  15. // var code= "server.on(\""+url+"\", []() {"+response+"});"
  16. // Blockly.Python.addSetup("setRouter_"+url, code);
  17. // return "";
  18. // };
  19. Blockly.Python['wifiServer_onNotFound'] = function(block) {
  20. var response = Blockly.Python.valueToCode(block, "VAR", Blockly.Python.ORDER_ATOMIC) || "";
  21. var code = "server.onNotFound(" + response + ");";
  22. Blockly.Python.addSetup("setRouter_urlNotFound", code);
  23. return "";
  24. };
  25. Blockly.Python['wifiServer_serverSend'] = function(block) {
  26. var http_code = Blockly.Python.valueToCode(block, "CODE", Blockly.Python.ORDER_ATOMIC) || "";
  27. var type = Blockly.Python.valueToCode(block, "TYPE", Blockly.Python.ORDER_ATOMIC) || "";
  28. var content = Blockly.Python.valueToCode(block, "CONTENT", Blockly.Python.ORDER_ATOMIC) || "";
  29. var code = "server.send(" + http_code + "," + type + "," + content + ");";
  30. return [code, Blockly.Python.ORDER_ATOMIC];
  31. };
  32. Blockly.Python['wifiServer_handleClient'] = function(block) {
  33. var code = "server.handleClient();\n";
  34. return code;
  35. };
  36. Blockly.Python['wifiServer_waitConnection'] = function(block) {
  37. var code = "";
  38. return "";
  39. };
  40. /***********************
  41. * version 2 *
  42. ***********************/
  43. Blockly.Python['wifiServer_setup'] = function(block) {
  44. var wifi_server = "#include <ESP8266WebServer.h>";
  45. Blockly.Python.addInclude("wifi_server", wifi_server);
  46. var json_include = "#include <ArduinoJson.h>";
  47. Blockly.Python.addInclude("json_include", json_include);
  48. var codes = "ESP8266WebServer server(80);"
  49. Blockly.Python.addDeclaration("SetupWiFiServer_80", codes);
  50. var code = "";
  51. return code;
  52. };
  53. Blockly.Python['wifiServer_setRouter'] = function(block) {
  54. var code = "";
  55. for (let i = 0; i < block.itemCount_; i++) {
  56. var item_url = block.getFieldValue("URL" + i) || "/" + i;
  57. var item_code = Blockly.Python.statementToCode(block, 'STATEMENT' + i);
  58. code += "server.on(\"" + item_url + "\",[]() {\n" + item_code + "\n" +
  59. " server.send(200,\"text/plain\",\"200\");\n" +
  60. "});\n"
  61. }
  62. Blockly.Python.addSetup("setRouter", code);
  63. code = " server.onNotFound([]() {\n" +
  64. " server.send(404,\"text/plain\",\"404:Not found\");\n" +
  65. " });";
  66. Blockly.Python.addSetup("setRouter_onNotFound", code);
  67. Blockly.Python.addSetup("ServerBegin", "server.begin();\n")
  68. return "server.handleClient();\n";
  69. };
  70. Blockly.Python['wifiServer_setRootPage'] = function(block) {
  71. var code = "";
  72. var item_code = Blockly.Python.statementToCode(block, 'STATEMENT0');
  73. code += "server.on(\"/\",[]() {\n" + item_code + "\n" +
  74. " server.send(200,\"text/plain\",\"200\");\n" +
  75. "});\n"
  76. Blockly.Python.addSetup("setRootPage", code);
  77. code = " server.onNotFound([]() {\n" +
  78. " server.send(404,\"text/plain\",\"404:Not found\");\n" +
  79. " });";
  80. Blockly.Python.addSetup("setRouter_onNotFound", code);
  81. Blockly.Python.addSetup("ServerBegin", "server.begin();\n")
  82. return "server.handleClient();\n";
  83. };
  84. Blockly.Python['wifiServer_Send'] = function(block) {
  85. var json_include = "#include <ArduinoJson.h>";
  86. Blockly.Python.addInclude("json_include", json_include);
  87. // var precode = "char JSONServerMsgBuffer[240];\n";
  88. // Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  89. var VarName = block.getFieldValue('VAR') || "json";
  90. var code = "StaticJsonBuffer<800> Serverbuffer_" + VarName + ";\n" +
  91. "JsonObject& " + VarName + "_json_ = Serverbuffer_" + VarName + ".createObject();\n" +
  92. "JsonArray& data_json_S = " + VarName + "_json_.createNestedArray(\"data\");\n" +
  93. // "JsonArray& data_type_S = " + VarName + "_json_.createNestedArray(\"type\");\n"+
  94. "";
  95. var size = block.itemCount_;
  96. var item_value = '';
  97. for (var n = 0; n < block.itemCount_; n++) {
  98. item_value = Blockly.Python.valueToCode(block, 'ADD' + n,
  99. Blockly.Python.ORDER_NONE) || '';
  100. code += 'data_json_S.add(' + item_value + ");\n";
  101. // if (block.childBlocks_[n].type == "text") {
  102. // code += "data_type_S.add(0);\n";
  103. // } else if (block.childBlocks_[n].type == "math_number") {
  104. // code += "data_type_S.add(1);\n";
  105. // }
  106. }
  107. code += "String jsonString;\n" +
  108. VarName + "_json_.printTo(jsonString);\n " +
  109. "server.send(200, \"application/json\", jsonString);\n"
  110. return code;
  111. };
  112. Blockly.Python['wifiServer_Receive'] = function(block) {
  113. var json_include = "#include <ArduinoJson.h>";
  114. Blockly.Python.addInclude("json_declaration", json_include);
  115. var varName = block.getFieldValue("VAR") || '';
  116. var length = block.getFieldValue("LENGTH") || '';
  117. var data_dec = "String data_" + varName + "[" + length + "];\n" +
  118. // "int dataType_" + varName + "[" + length + "];\n" +
  119. "";
  120. // for (let i = 0; i < length; i++) {
  121. // data_dec += "0";
  122. // if (i != length - 1) data_dec += ",";
  123. // }
  124. // data_dec += "};\n";
  125. Blockly.Python.addDeclaration("data_dec" + varName, data_dec);
  126. var Server_receiver = "void server_receiver_" + varName + "(String data) {" +
  127. " StaticJsonBuffer<800> ParseBuffer_" + varName + ";\n" +
  128. " JsonObject& parse_" + varName + " = ParseBuffer_" + varName + ".parseObject(data);\n" +
  129. " for (int i = 0; i < " + length + "; i++) {\n" +
  130. " data_" + varName + "[i] = parse_" + varName + "[\"data\"][i].as<String>();\n" +
  131. // " dataType_" + varName + "[i] = parse_" + varName + "[\"type\"][i].as<int>();\n" +
  132. " }\n}\n";
  133. Blockly.Python.addFunction("Server_receiver_" + varName, Server_receiver);
  134. var code = " String data = server.arg(\"plain\");\n" +
  135. "server_receiver_" + varName + "(data);\n" +
  136. "server.send(200,\"text/plain\",data);\n";
  137. return code;
  138. };
  139. Blockly.Python['wifiServer_Receive_getValue'] = function(block) {
  140. var varName = block.getFieldValue("VAR") || '';
  141. var index = block.getFieldValue("INDEX") || '';
  142. var type = block.getFieldValue("TYPE") || '';
  143. // var code = "dataType_" + varName + "[" + index + "] == 1 ? data_" + varName + "[" + index + "].toInt() : data_" + varName + "[" + index + "]";
  144. var code = "data_" + varName + "[" + index + "]"
  145. if (type == "Int") code += ".toInt()";
  146. return [code, Blockly.Python.ORDER_ATOMIC];
  147. }