wificlient.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. 'use strict';
  2. goog.provide('Blockly.Blocks.wificlient');
  3. goog.require('Blockly.Blocks');
  4. goog.require('Blockly.Types');
  5. Blockly.Blocks.wificlient.HUE = "#049d90";
  6. /**wifi client
  7. *setup for httpclient
  8. */
  9. Blockly.Blocks['wifiClient_http_setup'] = {
  10. init: function() {
  11. this.appendDummyInput()
  12. .appendField(new Blockly.FieldImage(
  13. // "http://cocorobo.cn/cocoblockly/blockly/media/cocoblockly_http_block.png", 140, 40, "15"))
  14. "./../blockly/media/cocoblockly_http_block.png", 140, 40, "15"))
  15. this.appendDummyInput()
  16. .appendField(Blockly.Msg.WIFICLIENT_HTTP_SETUP);
  17. this.setColour(Blockly.Blocks.wificlient.HUE);
  18. // this.setPreviousStatement(true, null);
  19. // this.setNextStatement(true, null);
  20. // this.setInputsInline(true);
  21. this.setTooltip("");
  22. this.setHelpUrl("");
  23. },
  24. };
  25. /**wifi client
  26. *http request method to get
  27. */
  28. Blockly.Blocks['wifiClient_http_get'] = {
  29. init: function() {
  30. this.appendDummyInput()
  31. .appendField(new Blockly.FieldImage(
  32. // "http://cocorobo.cn/cocoblockly/blockly/media/cocoblockly_http_block.png", 140, 40, "15"));
  33. "./../blockly/media/cocoblockly_http_block.png", 140, 40, "15"));
  34. this.appendDummyInput()
  35. .appendField(Blockly.Msg.WIFICLIENT_HTTP_GET_URL)
  36. .appendField(new Blockly.FieldTextInput("/"), "URL");
  37. this.appendDummyInput()
  38. .appendField(Blockly.Msg.WIFICLIENT_HTTP_GET_DATA)
  39. .appendField(new Blockly.FieldTextInput("Data_receive"), "VAR")
  40. this.setColour(Blockly.Blocks.wificlient.HUE);
  41. // this.setOutput(true,Blockly.Types.TEXT.output);
  42. this.setPreviousStatement(true, null);
  43. this.setNextStatement(true, null);
  44. this.setTooltip("");
  45. this.setHelpUrl("");
  46. },
  47. };
  48. /**WiFi Client
  49. * receive JSON data from Client,and parse it into List
  50. * @param {variable} var the list name of JSON data parse into
  51. * @param {}
  52. */
  53. Blockly.Blocks['wifiClient_http_get_getValue'] = {
  54. init: function() {
  55. this.appendDummyInput()
  56. .appendField(" " + Blockly.Msg.WIFICLIENT_RECEIVE_GET)
  57. .appendField(new Blockly.FieldTextInput("Data_receive"), "VAR")
  58. .appendField(" " + Blockly.Msg.WIFICLIENT_RECEIVE_GET_LINE2)
  59. .appendField(new Blockly.FieldDropdown([
  60. ["0", "0"],
  61. ["1", "1"],
  62. ["2", "2"],
  63. ["3", "3"],
  64. ["4", "4"],
  65. ["5", "5"],
  66. ["6", "6"],
  67. ["7", "7"],
  68. ["8", "8"],
  69. ["9", "9"],
  70. ["10", "10"],
  71. ["11", "11"],
  72. ]), "INDEX")
  73. .appendField(" " + Blockly.Msg.WIFICLIENT_RECEIVE_GET_LINE3)
  74. .appendField(new Blockly.FieldDropdown([
  75. [Blockly.Msg.WIFISERVER_RECEIVE_GET_INT, "Int"],
  76. [Blockly.Msg.WIFISERVER_RECEIVE_GET_STRING, "String"]
  77. ]), "TYPE")
  78. this.setColour(Blockly.Blocks.wificlient.HUE);
  79. this.setOutput(true)
  80. this.setTooltip("");
  81. this.setHelpUrl("");
  82. },
  83. getBlockType: function() {
  84. return Blockly.Types.NUMBER;
  85. }
  86. }
  87. /**wifi client
  88. *http request method to post
  89. *post content {String,item,Json}
  90. */
  91. Blockly.Blocks['wifiClient_http_post'] = {
  92. init: function() {
  93. this.appendDummyInput()
  94. .appendField(new Blockly.FieldImage(
  95. // "http://cocorobo.cn/cocoblockly/blockly/media/cocoblockly_http_block.png", 140, 40, "15"));
  96. "./../blockly/media/cocoblockly_http_block.png", 140, 40, "15"));
  97. this.appendDummyInput()
  98. .appendField(Blockly.Msg.WIFICLIENT_HTTP_POST_URL)
  99. .appendField(new Blockly.FieldTextInput("/"), "URL");
  100. this.appendDummyInput()
  101. .appendField(Blockly.Msg.WIFICLIENT_HTTP_POST_VAR)
  102. .appendField(new Blockly.FieldTextInput("Data_send"), "VAR")
  103. this.appendValueInput("ADD0")
  104. .appendField(Blockly.Msg.WIFICLIENT_HTTP_POST_DATA + '0');
  105. this.itemCount_ = 1;
  106. this.setMutator(new Blockly.Mutator(['wifiClient_create_with_item']));
  107. this.setColour(Blockly.Blocks.wificlient.HUE);
  108. // this.setOutput(true,Blockly.Types.TEXT.output);
  109. this.setPreviousStatement(true, null);
  110. this.setNextStatement(true, null);
  111. this.setInputsInline(false);
  112. this.setTooltip("");
  113. this.setHelpUrl("");
  114. },
  115. mutationToDom: function() {
  116. var container = document.createElement('mutation');
  117. container.setAttribute('items', this.itemCount_);
  118. return container;
  119. },
  120. /**
  121. * Parse XML to restore the inputs.
  122. * @param {!Element} xmlElement XML storage element.
  123. * @this Blockly.Block
  124. */
  125. domToMutation: function(xmlElement) {
  126. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  127. this.updateShape_();
  128. },
  129. /**
  130. * Populate the mutator's dialog with this block's components.
  131. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  132. * @return {!Blockly.Block} Root block in mutator.
  133. * @this Blockly.Block
  134. */
  135. decompose: function(workspace) {
  136. var containerBlock = workspace.newBlock('wifiClient_create_with_container');
  137. containerBlock.initSvg();
  138. var connection = containerBlock.getInput('STACK').connection;
  139. for (var i = 0; i < this.itemCount_; i++) {
  140. var itemBlock = workspace.newBlock('wifiClient_create_with_item');
  141. itemBlock.initSvg();
  142. connection.connect(itemBlock.previousConnection);
  143. connection = itemBlock.nextConnection;
  144. }
  145. return containerBlock;
  146. },
  147. /**
  148. * Reconfigure this block based on the mutator dialog's components.
  149. * @param {!Blockly.Block} containerBlock Root block in mutator.
  150. * @this Blockly.Block
  151. */
  152. compose: function(containerBlock) {
  153. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  154. // var itemBlock = containerBlock.nextConnection.targetBlock();
  155. // Count number of inputs.
  156. var connections = [];
  157. while (itemBlock) {
  158. connections.push(itemBlock.valueConnection_);
  159. itemBlock = itemBlock.nextConnection &&
  160. itemBlock.nextConnection.targetBlock();
  161. }
  162. // Disconnect any children that don't belong.
  163. // for (var i = 0; i < this.itemCount_; i++) {
  164. // var connection = this.getInput('ADD' + i).connection.targetConnection;
  165. // if (connection && connections.indexOf(connection) == -1) {
  166. // connection.disconnect();
  167. // }
  168. // }
  169. this.itemCount_ = connections.length;
  170. this.updateShape_();
  171. // Reconnect any child blocks.
  172. for (var i = 0; i < this.itemCount_; i++) {
  173. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i)
  174. }
  175. },
  176. /**
  177. * Store pointers to any connected child blocks.
  178. * @param {!Blockly.Block} containerBlock Root block in mutator.
  179. * @this Blockly.Block
  180. */
  181. saveConnections: function(containerBlock) {
  182. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  183. var i = 0;
  184. while (itemBlock) {
  185. var input = this.getInput('ADD' + i);
  186. i++;
  187. itemBlock = itemBlock.nextConnection &&
  188. itemBlock.nextConnection.targetBlock();
  189. }
  190. },
  191. /**
  192. * Modify this block to have the correct number of inputs.
  193. * @private
  194. * @this Blockly.Block
  195. */
  196. updateShape_: function() {
  197. // Add new inputs.
  198. for (var i = 0; i < this.itemCount_; i++) {
  199. if (!this.getInput('ADD' + i)) {
  200. this.appendValueInput("ADD" + i)
  201. .appendField(Blockly.Msg.WIFICLIENT_HTTP_POST_DATA + i)
  202. }
  203. }
  204. // Remove deleted inputs.
  205. while (this.getInput('ADD' + i)) {
  206. this.removeInput('ADD' + i);
  207. i++;
  208. }
  209. }
  210. };
  211. Blockly.Blocks['wifiClient_create_with_container'] = {
  212. /**
  213. * Mutator block for list container.
  214. * @this Blockly.Block
  215. */
  216. init: function() {
  217. this.appendDummyInput()
  218. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  219. this.appendStatementInput('STACK');
  220. this.setColour(Blockly.Blocks.wificlient.HUE);
  221. this.setTooltip('');
  222. this.contextMenu = false;
  223. }
  224. };
  225. Blockly.Blocks['wifiClient_create_with_item'] = {
  226. /**
  227. * Mutator bolck for adding items.
  228. * @this Blockly.Block
  229. */
  230. init: function() {
  231. this.setColour(Blockly.Blocks.wificlient.HUE);
  232. this.appendDummyInput()
  233. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  234. this.setPreviousStatement(true);
  235. this.setNextStatement(true);
  236. this.setTooltip('');
  237. this.contextMenu = false;
  238. }
  239. };
  240. /**wifi client
  241. *http request method to put
  242. *put content {String,item,Json}
  243. */
  244. Blockly.Blocks['wifiClient_http_put'] = {
  245. init: function() {
  246. this.appendDummyInput()
  247. .appendField(new Blockly.FieldImage(
  248. // "http://cocorobo.cn/cocoblockly/blockly/media/cocoblockly_http_block.png", 140, 40, "15"));
  249. "./../blockly/media/cocoblockly_http_block.png", 140, 40, "15"));
  250. this.appendValueInput("DATA")
  251. .appendField(Blockly.Msg.WIFICLIENT_HTTP_PUT_DATA);
  252. this.appendValueInput("URL")
  253. .appendField(Blockly.Msg.WIFICLIENT_HTTP_PUT_URL);
  254. this.appendDummyInput()
  255. .appendField(Blockly.Msg.WIFICLIENT_HTTP_PUT_TYPE)
  256. .appendField(new Blockly.FieldDropdown([
  257. [Blockly.Msg.REQUEST_DATA_STRING, "String"],
  258. [Blockly.Msg.REQUEST_DATA_CLOUD, "Cloud Data"]
  259. ]), "TYPE");
  260. this.setColour(Blockly.Blocks.wificlient.HUE);
  261. // this.setOutput(true,Blockly.Types.TEXT.output);
  262. this.setPreviousStatement(true, null);
  263. this.setNextStatement(true, null);
  264. this.setInputsInline(false);
  265. this.setTooltip("");
  266. this.setHelpUrl("");
  267. },
  268. };
  269. /**wifi Client
  270. *method to get reponse
  271. */
  272. Blockly.Blocks['wifiClient_http_response'] = {
  273. init: function() {
  274. this.appendDummyInput()
  275. .appendField(Blockly.Msg.WIFICLIENT_HTTP_RESPONSE)
  276. this.setColour(Blockly.Blocks.wificlient.HUE);
  277. this.setOutput(true, Blockly.Types.TEXT.output);
  278. // this.setPreviousStatement(true, null);
  279. // this.setNextStatement(true, null);
  280. // this.setInputsInline(true);
  281. this.setTooltip("");
  282. this.setHelpUrl("");
  283. },
  284. };