clouddata.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // 'use strict';
  2. // goog.provide('Blockly.Python.clouddata');
  3. // goog.require('Blockly.Python');
  4. // /****************************************
  5. // * * * * * * * * *
  6. // * Simple version for CocoCloud *
  7. // * Generate JSON *
  8. // * * * * * * * * *
  9. // ****************************************/
  10. // Blockly.Python['clouddata_json_generator'] = function(block) {
  11. // var json_include = "#include <ArduinoJson.h>";
  12. // Blockly.Python.addInclude("json_declaration", json_include);
  13. // var precode = "char JSONmessageBuffer[240];\n";
  14. // Blockly.Python.addDeclaration("jsontool_prettyPrint", precode);
  15. // var VarName = block.getFieldValue('VAR') || "json";
  16. // var code = "StaticJsonBuffer<240> JSONbuffer_" + VarName + ";\n" +
  17. // "JsonObject& " + VarName + " = JSONbuffer_" + VarName + ".createObject();\n";
  18. // var size = block.itemCount_;
  19. // var item_key = '',
  20. // item_value = '';
  21. // for (var n = 0; n < this.itemCount_; n++) {
  22. // item_key = block.getFieldValue('KEY' + n);
  23. // item_value = Blockly.Python.valueToCode(this, 'ADD' + n,
  24. // Blockly.Python.ORDER_NONE) || '';
  25. // code += VarName + '[\"' + item_key + '\"]=' + item_value + ";\n";
  26. // }
  27. // code += "" + VarName + ".prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));\n";
  28. // return code;
  29. // }
  30. // Blockly.Python['clouddata_parse_prettyPrint_display'] = function(block) {
  31. // var code = "JSONmessageBuffer";
  32. // return [code, Blockly.Python.ORDER_ATOMIC];
  33. // };
  34. // /****************************************
  35. // * * * * * * * * *
  36. // * Simple version for CocoCloud *
  37. // * Parse JSON *
  38. // * * * * * * * * *
  39. // ****************************************/
  40. // Blockly.Python['clouddata_json_parser_setup'] = function(block) {
  41. // var json_include = "#include <ArduinoJson.h>";
  42. // Blockly.Python.addInclude("json_declaration", json_include);
  43. // var payload = Blockly.Python.valueToCode(this, 'PAYLOAD', Blockly.Python.ORDER_NONE) || '';
  44. // var data = block.getFieldValue('DATA') || '';
  45. // var code = "StaticJsonBuffer<240> jsonBuffer_" + data + ";\n" +
  46. // "JsonObject& cloud_" + data + " = jsonBuffer_" + data + ".parseObject(" + payload + ");\n";
  47. // return code;
  48. // }
  49. // Blockly.Python['clouddata_json_parser'] = function(block) {
  50. // var data = block.getFieldValue('DATA') || '';
  51. // var key = block.getFieldValue('KEY') || '';
  52. // var varType = block.getFieldValue('VAR_TYPE');
  53. // if (varType == "Text") {
  54. // varType = "char*";
  55. // } else if (varType == "Number") {
  56. // varType = "int";
  57. // } else if (varType == "Boolean") {
  58. // varType = "boolean";
  59. // } else if (varType == "Large Number") {
  60. // varType = "long";
  61. // } else if (varType == "Decimal") {
  62. // varType = "float";
  63. // }
  64. // var code = "cloud_" + data + "[\"data\"][0][\"" + key + "\"].as<" + varType + ">()";
  65. // return [code, Blockly.Python.ORDER_ATOMIC];
  66. // }
  67. // Blockly.Python['cococloud_simple_post'] = function(block) {
  68. // var size = block.itemCount_;
  69. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  70. // Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  71. // var HTTPResponse = "String http_response;";
  72. // Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  73. // var HTTPCode = "int httpCode;";
  74. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  75. // let serial_setup = "Serial.begin(9600);";
  76. // Blockly.Python.addSetup('serial_serial', serial_setup);
  77. // var httpclient = "HTTPClient http;";
  78. // Blockly.Python.addDeclaration("wifi_httpclient_setup", httpclient);
  79. // var code = "";
  80. // // var item_url = "http://cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0");
  81. // var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
  82. // // console.log(block.eventTag);
  83. // block.eventTag = item_url;
  84. // var item_data = Blockly.Python.valueToCode(this, 'ADD0',
  85. // Blockly.Python.ORDER_NONE) || '';
  86. // code += "http.begin(\"" + item_url + "\");\n" +
  87. // " http.addHeader(\"Content-Type\", \"application/json\");\n" +
  88. // " int httpCode = http.POST(" + item_data + ");\n" +
  89. // " String payload = http.getString();\n" +
  90. // " Serial.println(httpCode);\n" +
  91. // " Serial.println(payload);\n" +
  92. // " http.end();\n";
  93. // return code;
  94. // }
  95. // Blockly.Python['cococloud_simple_get'] = function(block) {
  96. // var size = block.itemCount_;
  97. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  98. // Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  99. // var HTTPResponse = "String http_response;";
  100. // Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  101. // var HTTPCode = "int httpCode;";
  102. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  103. // let serial_setup = "Serial.begin(9600);";
  104. // Blockly.Python.addSetup('serial_serial', serial_setup);
  105. // var httpclient = "HTTPClient http;";
  106. // Blockly.Python.addDeclaration("wifi_httpclient_setup", httpclient);
  107. // var cococloud_Response = "String cococloud_response;";
  108. // Blockly.Python.addDeclaration("cococloud_Response", cococloud_Response);
  109. // var code = "";
  110. // // var item_url = "http://cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0");
  111. // var item_url = "http://staging.cocorobo.cn/api/iot/data/eventAPIKey/" + block.getFieldValue("event0"); //staging
  112. // code += "http.begin(\"" + item_url + "\");\n" +
  113. // " http.addHeader(\"Content-Type\", \"application/json\");\n" +
  114. // " int httpCode = http.GET();\n" +
  115. // " String payload = http.getString();\n" +
  116. // " cococloud_response = payload;\n" +
  117. // " Serial.println(httpCode);\n" +
  118. // " Serial.println(payload);\n" +
  119. // " http.end();\n";
  120. // return code;
  121. // }
  122. // Blockly.Python['cococloud_simple_getPayLoad'] = function(block) {
  123. // var code = "cococloud_response";
  124. // return [code, Blockly.Python.ORDER_ATOMIC];
  125. // }
  126. // /*
  127. // * World time
  128. // * setup
  129. // */
  130. // Blockly.Python['cloud_time_setup'] = function(block) {
  131. // Blockly.Python.addInclude('wire', "#include <Wire.h>");
  132. // Blockly.Python.addInclude('RTClib', "#include \"RTClib.h\"");
  133. // var wifi_httpclient = "#include <ESP8266HTTPClient.h>";
  134. // Blockly.Python.addInclude('wifi_httpclient', wifi_httpclient);
  135. // var json_include = "#include <ArduinoJson.h>";
  136. // Blockly.Python.addInclude("json_declaration", json_include);
  137. // var HTTPResponse = "String http_response;";
  138. // Blockly.Python.addDeclaration('HTTP_Response', HTTPResponse);
  139. // var HTTPCode = "int httpCode;";
  140. // Blockly.Python.addDeclaration('HTTP_code', HTTPCode);
  141. // var HTTPClient = "HTTPClient http;\n";
  142. // Blockly.Python.addDeclaration("wifi_httpclient_setup", HTTPClient);
  143. // var cloud_time = "DateTime worldtime;\n";
  144. // Blockly.Python.addDeclaration("cloud_time", cloud_time);
  145. // let serial_setup = "Serial.begin(9600);";
  146. // Blockly.Python.addSetup('serial_serial', serial_setup);
  147. // var code = "http.begin(\"http://api.cocorobo.cn/iot/time\");\n" +
  148. // " httpCode = http.GET();\n" +
  149. // " Serial.print(\"HTTP code: \");\n" +
  150. // " Serial.println(httpCode);\n" +
  151. // " http_response = http.getString();\n" +
  152. // " http.end();\n" +
  153. // " delay(300);\n" +
  154. // " StaticJsonBuffer<240> jsonBuffer_Data\n;" +
  155. // " JsonObject& cloud_Data = jsonBuffer_Data.parseObject(http_response);\n" +
  156. // " String temptime = cloud_Data[\"time\"].as<char*>();\n" +
  157. // " String temp_time = temptime.substring(0,temptime.length()-3);\n" +
  158. // " DateTime world_time(atol(temp_time.c_str())+ 3600*8);\n";
  159. // return code;
  160. // }
  161. // /*
  162. // * World time
  163. // * get Date
  164. // */
  165. // Blockly.Python['cloud_time_getDate'] = function(block) {
  166. // var detail = block.getFieldValue("date_detail");
  167. // var code = "";
  168. // switch (detail) {
  169. // case "year":
  170. // code = "world_time.year()";
  171. // break;
  172. // case "month":
  173. // code = "world_time.month()";
  174. // break;
  175. // case "day":
  176. // code = "world_time.day()";
  177. // break;
  178. // default:
  179. // break;
  180. // }
  181. // return [code, Blockly.Python.ORDER_ATOMIC];
  182. // }
  183. // /*
  184. // * get Time
  185. // */
  186. // Blockly.Python['cloud_time_getTime'] = function(block) {
  187. // var detail = block.getFieldValue("time_detail");
  188. // var code = "";
  189. // switch (detail) {
  190. // case "hour":
  191. // code = "world_time.hour()";
  192. // break;
  193. // case "minute":
  194. // code = "world_time.minute()";
  195. // break;
  196. // case "second":
  197. // code = "world_time.second()";
  198. // break;
  199. // default:
  200. // break;
  201. // }
  202. // return [code, Blockly.Python.ORDER_ATOMIC];
  203. // }