jsontool.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // 'use strict';
  2. // goog.provide('Blockly.Python.jsontool');
  3. // goog.require('Blockly.Python');
  4. // /****************************************
  5. // * * * * * * * * *
  6. // * Generate JSON *
  7. // * * * * * * * * *
  8. // ****************************************/
  9. // Blockly.Python['jsontool_generator_setup'] = function (block) {
  10. // var jsontool_declaration= "#include <ArduinoJson.h>";
  11. // Blockly.Python.addDeclaration("jsontool_declaration",jsontool_declaration);
  12. // var jsonname= block.getFieldValue('JSONNAME');
  13. // var code= "StaticJsonBuffer<200> JSONbuffer;\n" +
  14. // "JsonObject& " + jsonname + " = JSONbuffer.createObject();\n";
  15. // return code;
  16. // };
  17. // Blockly.Python['jsontool_generator_keyvalue'] = function (block) {
  18. // var jsonname= block.getFieldValue('JSONNAME');
  19. // var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
  20. // var value= Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC) || "";
  21. // var code= ""+jsonname+"["+key+ "] = "+ value+";\n";
  22. // return code;
  23. // };
  24. // Blockly.Python['jsontool_generator_main_block'] = function (block) {
  25. // var jsontool_declaration= "#include <ArduinoJson.h>";
  26. // Blockly.Python.addDeclaration("jsontool_declaration",jsontool_declaration);
  27. // var jsonType = block.getFieldValue('jsontype') || "Object";
  28. // var jsonName = block.getFieldValue('NAME') || "jsonVar";
  29. // var code= "StaticJsonBuffer<240> JSONbuffer_"+jsonName+";\n";
  30. // if (jsonType == "Object") {
  31. // code += "JsonObject& "+jsonName+" = JSONbuffer_"+jsonName+".createObject();\n";
  32. // } else if (jsonType == "Array"){
  33. // code += "JsonArray& "+jsonName+" = JSONbuffer_"+jsonName+".createArray();\n";
  34. // }
  35. // var branch = Blockly.Python.statementToCode(block, 'STACK');
  36. // // console.log("jsontool.js-40: branch :\n"+ branch);
  37. // /* spilt the branch String into servral string according to tag "$"
  38. // * each string means a statement
  39. // * */
  40. // var statements_arr = branch.split("$");
  41. // for (var i in statements_arr) {
  42. // /* spilt each statement string into two part according to tag "|"
  43. // * the first part is ["field"];
  44. // * the second part is real string of statement
  45. // */
  46. // if (statements_arr[i] == "")
  47. // break;
  48. // var item_arr = statements_arr[i].split("|");
  49. // if(item_arr[0] == "OBJECT") {
  50. // code += jsonName + item_arr[1]+ ";\n";
  51. // } else if (item_arr[0] == "ARRAY") {
  52. // code += jsonName + ".add("+ item_arr[1] + ");\n";
  53. // } else if (item_arr[0] == "NESTEDOBJECT") {
  54. // var item = item_arr[1].split("@");
  55. // var nestedJsonName = item[0];
  56. // if (jsonType == "Object")
  57. // code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject(\""+nestedJsonName+"\");\n"
  58. // + item[1];
  59. // else
  60. // code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject();\n"
  61. // + item[1];
  62. // } else if (item_arr[0] == "NESTEDARRAY") {
  63. // var item = item_arr[1].split("@");
  64. // var nestedJsonName = item[0];
  65. // if (jsonType == "Object")
  66. // code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray(\""+nestedJsonName+"\");\n"
  67. // + item[1]+ "\n";
  68. // else
  69. // code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray();\n"
  70. // + item[1];
  71. // }
  72. // }
  73. // return code;
  74. // };
  75. // Blockly.Python['jsontool_generator_nested_block'] = function (block) {
  76. // var jsonType = block.getFieldValue('jsontype') || "Object";
  77. // var jsonName = block.getFieldValue('NAME') || "jsonVar";
  78. // var code;
  79. // if (jsonType == "Object") {
  80. // code = "$NESTEDOBJECT|"+jsonName+"@";
  81. // } else if (jsonType == "Array"){
  82. // code = "$NESTEDARRAY|"+jsonName+"@";
  83. // }
  84. // var branch = Blockly.Python.statementToCode(block, 'STACK1');
  85. // /* spilt the branch String into servral string according to tag "$"
  86. // * each string means a statement
  87. // * */
  88. // var statements_arr = branch.split("$");
  89. // for (var i in statements_arr) {
  90. // /* spilt each statement string into two part according to tag "|"
  91. // * the first part is ["field"];
  92. // * the second part is real string of statement
  93. // */
  94. // if (statements_arr[i] == "")
  95. // break;
  96. // var item_arr = statements_arr[i].split("|");
  97. // if(item_arr[0] == "OBJECT") {
  98. // code += jsonName + item_arr[1]+ ";\n";
  99. // } else if (item_arr[0] == "ARRAY") {
  100. // code += jsonName + ".add("+ item_arr[1] + ");\n";
  101. // } else if (item_arr[0] == "NESTEDOBJECT") {
  102. // var item = item_arr[1].split("@");
  103. // var nestedJsonName = item[0];
  104. // if (jsonType == "Object")
  105. // code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject(\""+nestedJsonName+"\");\n"
  106. // + item[1];
  107. // else
  108. // code += "JsonObject&" +nestedJsonName + " = "+jsonName +".createNestedObject();\n"
  109. // + item[1];
  110. // } else if (item_arr[0] == "NESTEDARRAY") {
  111. // var item = item_arr[1].split("@");
  112. // var nestedJsonName = item[0];
  113. // if (jsonType == "Object")
  114. // code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray(\""+nestedJsonName+"\");\n"
  115. // + item[1];
  116. // else
  117. // code += "JsonArray&" +nestedJsonName + " = "+jsonName +".createNestedArray();\n"
  118. // + item[1];
  119. // }
  120. // }
  121. // // console.log("nestedJson code"+ code);
  122. // return code;
  123. // };
  124. // Blockly.Python['jsontool_generator_object_keyValue'] = function (block) {
  125. // var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
  126. // var value= Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC) || "";
  127. // var code = "$OBJECT|["+key+"]=" + value;
  128. // return code;
  129. // }
  130. // Blockly.Python['jsontool_generator_array_value'] = function (block) {
  131. // var value= Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC) || "";
  132. // var code = "$ARRAY|" + value;
  133. // return code;
  134. // }
  135. // /****************************************
  136. // * * * * * * * * *
  137. // * Parse JSON *
  138. // * * * * * * * * *
  139. // ****************************************/
  140. // Blockly.Python['jsontool_parse_setup'] = function (block) {
  141. // var jsontool_declaration= "#include <ArduinoJson.h>";
  142. // Blockly.Python.addInclude("jsontool_declaration",jsontool_declaration);
  143. // var object_size= Blockly.Python.valueToCode(block, 'OBJECT_SIZE', Blockly.Python.ORDER_ATOMIC) || "";
  144. // var array_size= Blockly.Python.valueToCode(block, 'ARRAY_SIZE', Blockly.Python.ORDER_ATOMIC) || "";
  145. // var response= Blockly.Python.valueToCode(block, 'RESPONSE', Blockly.Python.ORDER_ATOMIC) || "";
  146. // var jsonname= block.getFieldValue('JSONNAME');
  147. // var jsonParseCode= "const size_t capacity_"+jsonname +" = JSON_OBJECT_SIZE("+ object_size+
  148. // ") + JSON_ARRAY_SIZE("+array_size+") + 60;\n" +
  149. // "DynamicJsonBuffer jsonBuffer_"+jsonname +"(capacity_"+jsonname +");\n" +
  150. // "JsonObject& "+jsonname+" = jsonBuffer_"+jsonname +".parseObject("+response+");\n";
  151. // return jsonParseCode;
  152. // };
  153. // Blockly.Python['jsontool_parse_begin'] = function(block) {
  154. // var jsonname = block.getFieldValue('JSONNAME');
  155. // var parseVar = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
  156. // Blockly.Variables.NAME_TYPE);
  157. // var varType = block.getFieldValue('JSONVAR_TYPE');
  158. // if (varType == "Text") {
  159. // varType = "String";
  160. // } else if (varType == "Number") {
  161. // varType = "int";
  162. // } else if (varType == "Boolean") {
  163. // varType = "boolean";
  164. // } else if (varType == "Large Number") {
  165. // varType = "long";
  166. // }else if (varType == "Decimal") {
  167. // varType = "float";
  168. // }
  169. // var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
  170. // var code = parseVar+"=("+varType+")"+ jsonname+""+key+";\n";
  171. // return code;
  172. // };
  173. // Blockly.Python['jsontool_parse_key'] = function(block) {
  174. // var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
  175. // var code="["+key+"]";
  176. // return [code, Blockly.Python.ORDER_ATOMIC];
  177. // };
  178. // Blockly.Python['jsontool_parse_arr_inobject'] = function(block) {
  179. // var array= Blockly.Python.valueToCode(block, 'VAR', Blockly.Python.ORDER_ATOMIC) || "";
  180. // var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
  181. // var code="["+array+"]["+index+"]";
  182. // return [code, Blockly.Python.ORDER_ATOMIC];
  183. // };
  184. // Blockly.Python['jsontool_parse_arr_inarray'] = function(block) {
  185. // var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
  186. // var code="["+index+"]";
  187. // return [code, Blockly.Python.ORDER_ATOMIC];
  188. // };
  189. // Blockly.Python['jsontool_parse_key_nest'] = function(block) {
  190. // var key= Blockly.Python.valueToCode(block, 'KEY', Blockly.Python.ORDER_ATOMIC) || "";
  191. // var nest= Blockly.Python.valueToCode(block, 'NEST', Blockly.Python.ORDER_ATOMIC) || "";
  192. // var code="["+key+"]"+ nest;
  193. // return [code, Blockly.Python.ORDER_ATOMIC];
  194. // };
  195. // Blockly.Python['jsontool_parse_arr_inobject_nest'] = function(block) {
  196. // var array= Blockly.Python.valueToCode(block, 'VAR', Blockly.Python.ORDER_ATOMIC) || "";
  197. // var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
  198. // var nest= Blockly.Python.valueToCode(block, 'NEST', Blockly.Python.ORDER_ATOMIC) || "";
  199. // var code="["+array+"]["+index+"]"+nest;
  200. // return [code, Blockly.Python.ORDER_ATOMIC];
  201. // };
  202. // Blockly.Python['jsontool_parse_arr_inarray_nest'] = function(block) {
  203. // var index= Blockly.Python.valueToCode(block, 'INDEX', Blockly.Python.ORDER_ATOMIC) || "";
  204. // var nest= Blockly.Python.valueToCode(block, 'NEST', Blockly.Python.ORDER_ATOMIC) || "";
  205. // var code="["+index+"]"+nest;
  206. // return [code, Blockly.Python.ORDER_ATOMIC];
  207. // };
  208. // Blockly.Python['jsontool_parse_prettyPrint'] = function(block) {
  209. // var jsonname= block.getFieldValue('JSONNAME');
  210. // var precode = "char JSONmessageBuffer[240];\n";
  211. // Blockly.Python.addDeclaration("jsontool_prettyPrint",precode);
  212. // var code = ""+ jsonname+ ".prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));\n";
  213. // return code;
  214. // };
  215. // Blockly.Python['jsontool_parse_prettyPrint_display'] = function(block) {
  216. // var code = "JSONmessageBuffer";
  217. // return [code, Blockly.Python.ORDER_ATOMIC];
  218. // };
  219. // Blockly.Python['jsontool_parse_isSuccess'] = function(block) {
  220. // var jsonname= block.getFieldValue('JSONNAME');
  221. // var code = ""+jsonname+".success()";
  222. // return [code, Blockly.Python.ORDER_ATOMIC];
  223. // };