nbiot.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. 'use strict';
  2. goog.provide('Blockly.Blocks.nbiot');
  3. goog.require('Blockly.Blocks');
  4. Blockly.Blocks.nbiot.HUE="#6da383";
  5. Blockly.Blocks['NBIoT_setup'] = {
  6. init: function() {
  7. this.appendDummyInput()
  8. .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 100, 40, "0"));
  9. this.appendDummyInput()
  10. .appendField(Blockly.Msg.NBIOT_SETUP);
  11. this.setColour(Blockly.Blocks.nbiot.HUE);
  12. this.setTooltip("");
  13. this.setHelpUrl("");
  14. },
  15. }
  16. Blockly.Blocks['NBIoT_http'] = {
  17. init: function() {
  18. this.appendDummyInput()
  19. .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 100, 40, "0"));
  20. this.appendDummyInput()
  21. .appendField(Blockly.Msg.NBIOT_HTTP_TITLE);
  22. this.appendDummyInput()
  23. .appendField(Blockly.Msg.NBIOT_HTTP_APIKEY)
  24. .appendField(new Blockly.FieldTextInput("yourCloudEventAPIKey"),"APIKEY")
  25. this.appendValueInput("ADD"+0)
  26. .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
  27. .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+0)
  28. .setAlign(Blockly.ALIGN_RIGHT);
  29. this.setPreviousStatement(true);
  30. this.setNextStatement(true);
  31. this.itemCount_ = 1;
  32. this.updateShape_();
  33. this.setMutator(new Blockly.Mutator(['NBIoT_create_with_item']));
  34. this.setColour(Blockly.Blocks.nbiot.HUE);
  35. this.setTooltip("");
  36. this.setHelpUrl("");
  37. },
  38. mutationToDom: function() {
  39. var container = document.createElement('mutation');
  40. container.setAttribute('items', this.itemCount_);
  41. return container;
  42. },
  43. domToMutation: function(xmlElement) {
  44. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  45. this.updateShape_();
  46. },
  47. decompose: function(workspace) {
  48. var containerBlock = workspace.newBlock('NBIoT_create_with_container');
  49. containerBlock.initSvg();
  50. var connection = containerBlock.getInput('STACK').connection;
  51. for (var i = 0; i < this.itemCount_; i++) {
  52. var itemBlock = workspace.newBlock('NBIoT_create_with_item');
  53. itemBlock.initSvg();
  54. connection.connect(itemBlock.previousConnection);
  55. connection = itemBlock.nextConnection;
  56. }
  57. return containerBlock;
  58. },
  59. compose: function(containerBlock) {
  60. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  61. var connections = [];
  62. while (itemBlock) {
  63. connections.push(itemBlock.valueConnection_);
  64. itemBlock = itemBlock.nextConnection &&
  65. itemBlock.nextConnection.targetBlock();
  66. }
  67. for (var i = 0; i < this.itemCount_; i++) {
  68. var connection = this.getInput('ADD' + i).connection.targetConnection;
  69. if (connection && connections.indexOf(connection) == -1) {
  70. connection.disconnect();
  71. }
  72. }
  73. this.itemCount_ = connections.length;
  74. this.updateShape_();
  75. for (var i = 0; i < this.itemCount_; i++) {
  76. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  77. }
  78. },
  79. saveConnections: function(containerBlock) {
  80. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  81. var i = 0;
  82. while (itemBlock) {
  83. var input = this.getInput('ADD' + i);
  84. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  85. i++;
  86. itemBlock = itemBlock.nextConnection &&
  87. itemBlock.nextConnection.targetBlock();
  88. }
  89. },
  90. updateShape_: function() {
  91. for (var i = 0; i < this.itemCount_; i++) {
  92. if (!this.getInput('ADD' + i)) {
  93. var input = this.appendValueInput("ADD"+i)
  94. .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
  95. .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+i)
  96. .setAlign(Blockly.ALIGN_RIGHT);
  97. }
  98. }
  99. while (this.getInput('ADD' + i)) {
  100. this.removeInput('ADD' + i);
  101. i++;
  102. }
  103. },
  104. onchange: function() {
  105. var text_content = this.getFieldValue("APIKEY");
  106. if (text_content.length > 50) {
  107. this.setFieldValue(text_content.substring(0,50),"APIKEY")
  108. }
  109. }
  110. };
  111. Blockly.Blocks['NBIoT_create_with_container'] = {
  112. init: function() {
  113. this.setColour(Blockly.Blocks.nbiot.HUE);
  114. this.appendDummyInput()
  115. .appendField(Blockly.Msg.BLYNK_CREATE_WITH_CONTAINER_TITLE_ADD);
  116. this.appendStatementInput('STACK');
  117. this.setTooltip('');
  118. this.contextMenu = false;
  119. }
  120. };
  121. Blockly.Blocks['NBIoT_create_with_item'] = {
  122. init: function() {
  123. this.setColour(Blockly.Blocks.nbiot.HUE);
  124. this.appendDummyInput()
  125. .appendField(Blockly.Msg.BLYNK_CREATE_WITH_ITEM_TITLE);
  126. this.setPreviousStatement(true);
  127. this.setNextStatement(true);
  128. this.setTooltip('');
  129. this.contextMenu = false;
  130. }
  131. };
  132. Blockly.Blocks['NBIoT_enable'] = {
  133. init: function() {
  134. this.appendDummyInput()
  135. .appendField(Blockly.Msg.NBIOT_ENABLE);
  136. this.setPreviousStatement(true);
  137. this.setNextStatement(true);
  138. this.setColour(Blockly.Blocks.nbiot.HUE);
  139. this.setTooltip("");
  140. this.setHelpUrl("");
  141. },
  142. }
  143. Blockly.Blocks['NBIoT_http_thingspeak'] = {
  144. init: function() {
  145. this.appendDummyInput()
  146. .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 100, 40, "0"));
  147. this.appendDummyInput()
  148. .appendField(Blockly.Msg.NBIOT_HTTP_THINGSPEAK_TITLE);
  149. this.appendDummyInput()
  150. .appendField(Blockly.Msg.NBIOT_HTTP_THINGSPEAK_APIKEY)
  151. .appendField(new Blockly.FieldTextInput("yourEventAPIKey"),"APIKEY")
  152. this.appendValueInput("ADD"+0)
  153. .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
  154. // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+0)
  155. .setAlign(Blockly.ALIGN_RIGHT);
  156. this.setPreviousStatement(true);
  157. this.setNextStatement(true);
  158. this.itemCount_ = 1;
  159. this.updateShape_();
  160. this.setMutator(new Blockly.Mutator(['NBIoT_create_with_item']));
  161. this.setColour(Blockly.Blocks.nbiot.HUE);
  162. this.setTooltip("");
  163. this.setHelpUrl("");
  164. },
  165. mutationToDom: function() {
  166. var container = document.createElement('mutation');
  167. container.setAttribute('items', this.itemCount_);
  168. return container;
  169. },
  170. domToMutation: function(xmlElement) {
  171. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  172. this.updateShape_();
  173. },
  174. decompose: function(workspace) {
  175. var containerBlock = workspace.newBlock('NBIoT_create_with_container');
  176. containerBlock.initSvg();
  177. var connection = containerBlock.getInput('STACK').connection;
  178. for (var i = 0; i < this.itemCount_; i++) {
  179. var itemBlock = workspace.newBlock('NBIoT_create_with_item');
  180. itemBlock.initSvg();
  181. connection.connect(itemBlock.previousConnection);
  182. connection = itemBlock.nextConnection;
  183. }
  184. return containerBlock;
  185. },
  186. compose: function(containerBlock) {
  187. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  188. var connections = [];
  189. while (itemBlock) {
  190. connections.push(itemBlock.valueConnection_);
  191. itemBlock = itemBlock.nextConnection &&
  192. itemBlock.nextConnection.targetBlock();
  193. }
  194. for (var i = 0; i < this.itemCount_; i++) {
  195. var connection = this.getInput('ADD' + i).connection.targetConnection;
  196. if (connection && connections.indexOf(connection) == -1) {
  197. connection.disconnect();
  198. }
  199. }
  200. this.itemCount_ = connections.length;
  201. this.updateShape_();
  202. for (var i = 0; i < this.itemCount_; i++) {
  203. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  204. }
  205. },
  206. saveConnections: function(containerBlock) {
  207. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  208. var i = 0;
  209. while (itemBlock) {
  210. var input = this.getInput('ADD' + i);
  211. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  212. i++;
  213. itemBlock = itemBlock.nextConnection &&
  214. itemBlock.nextConnection.targetBlock();
  215. }
  216. },
  217. updateShape_: function() {
  218. for (var i = 0; i < this.itemCount_; i++) {
  219. if (!this.getInput('ADD' + i)) {
  220. var input = this.appendValueInput("ADD"+i)
  221. .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
  222. // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+i)
  223. .setAlign(Blockly.ALIGN_RIGHT);
  224. }
  225. }
  226. while (this.getInput('ADD' + i)) {
  227. this.removeInput('ADD' + i);
  228. i++;
  229. }
  230. },
  231. onchange: function() {
  232. /** set limitation of AUTH input length */
  233. var text_content = this.getFieldValue("APIKEY");
  234. if (text_content.length > 50) {
  235. this.setFieldValue(text_content.substring(0,50),"APIKEY")
  236. }
  237. }
  238. };
  239. Blockly.Blocks['NBIoT_http_IFTTT'] = {
  240. init: function() {
  241. this.appendDummyInput()
  242. .appendField(new Blockly.FieldImage("./../blockly/media/nbiot_header.png", 130, 40, "0"));
  243. this.appendDummyInput()
  244. .appendField(Blockly.Msg.NBIOT_HTTP_IFTTT_TITLE);
  245. this.appendDummyInput()
  246. .appendField(Blockly.Msg.NBIOT_HTTP_IFTTT_APIKEY)
  247. .appendField(new Blockly.FieldTextInput("yourEventAPIKey"),"APIKEY")
  248. this.appendDummyInput()
  249. .appendField(Blockly.Msg.NBIOT_HTTP_IFTTT_EVENT)
  250. .appendField(new Blockly.FieldTextInput("yourEventName"),"EVENT")
  251. this.appendValueInput("ADD"+0)
  252. .appendField(Blockly.Msg.NBIOT_HTTP_DATA+"0")
  253. // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+0)
  254. .setAlign(Blockly.ALIGN_RIGHT);
  255. this.setPreviousStatement(true);
  256. this.setNextStatement(true);
  257. this.itemCount_ = 1;
  258. this.updateShape_();
  259. this.setMutator(new Blockly.Mutator(['NBIoT_create_with_item']));
  260. this.setColour(Blockly.Blocks.nbiot.HUE);
  261. this.setTooltip("");
  262. this.setHelpUrl("");
  263. },
  264. mutationToDom: function() {
  265. var container = document.createElement('mutation');
  266. container.setAttribute('items', this.itemCount_);
  267. return container;
  268. },
  269. domToMutation: function(xmlElement) {
  270. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  271. this.updateShape_();
  272. },
  273. decompose: function(workspace) {
  274. var containerBlock = workspace.newBlock('NBIoT_create_with_container');
  275. containerBlock.initSvg();
  276. var connection = containerBlock.getInput('STACK').connection;
  277. for (var i = 0; i < this.itemCount_; i++) {
  278. var itemBlock = workspace.newBlock('NBIoT_create_with_item');
  279. itemBlock.initSvg();
  280. connection.connect(itemBlock.previousConnection);
  281. connection = itemBlock.nextConnection;
  282. }
  283. return containerBlock;
  284. },
  285. compose: function(containerBlock) {
  286. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  287. // Count number of inputs.
  288. var connections = [];
  289. while (itemBlock) {
  290. connections.push(itemBlock.valueConnection_);
  291. itemBlock = itemBlock.nextConnection &&
  292. itemBlock.nextConnection.targetBlock();
  293. }
  294. // Disconnect any children that don't belong.
  295. for (var i = 0; i < this.itemCount_; i++) {
  296. var connection = this.getInput('ADD' + i).connection.targetConnection;
  297. if (connection && connections.indexOf(connection) == -1) {
  298. connection.disconnect();
  299. }
  300. }
  301. this.itemCount_ = connections.length;
  302. this.updateShape_();
  303. // Reconnect any child blocks.
  304. for (var i = 0; i < this.itemCount_; i++) {
  305. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  306. }
  307. },
  308. saveConnections: function(containerBlock) {
  309. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  310. var i = 0;
  311. while (itemBlock) {
  312. var input = this.getInput('ADD' + i);
  313. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  314. i++;
  315. itemBlock = itemBlock.nextConnection &&
  316. itemBlock.nextConnection.targetBlock();
  317. }
  318. },
  319. updateShape_: function() {
  320. for (var i = 0; i < this.itemCount_; i++) {
  321. if (!this.getInput('ADD' + i)) {
  322. var input = this.appendValueInput("ADD"+i)
  323. .appendField(Blockly.Msg.NBIOT_HTTP_DATA)
  324. // .appendField(new Blockly.FieldTextInput("default"), "DATA_DEFAULT"+i)
  325. .setAlign(Blockly.ALIGN_RIGHT);
  326. }
  327. }
  328. while (this.getInput('ADD' + i)) {
  329. this.removeInput('ADD' + i);
  330. i++;
  331. }
  332. },
  333. onchange: function() {
  334. var text_content = this.getFieldValue("APIKEY");
  335. if (text_content.length > 50) {
  336. this.setFieldValue(text_content.substring(0,50),"APIKEY")
  337. }
  338. }
  339. };
  340. Blockly.Blocks['NBIoT_network_connected'] = {
  341. init: function() {
  342. this.appendDummyInput()
  343. .appendField(Blockly.Msg.NBIOT_NETWORK_CONNECTED)
  344. this.setColour(Blockly.Blocks.nbiot.HUE);
  345. this.setOutput(true);
  346. }
  347. }
  348. Blockly.Blocks['NBIoT_powered_on'] = {
  349. init: function() {
  350. this.appendDummyInput()
  351. .appendField(Blockly.Msg.NBIOT_IS_POWERON)
  352. this.setColour(Blockly.Blocks.nbiot.HUE);
  353. this.setOutput(true);
  354. }
  355. }
  356. Blockly.Blocks['NBIoT_network_IP'] = {
  357. init: function() {
  358. this.appendDummyInput()
  359. .appendField(Blockly.Msg.NBIOT_NETWORK_IP)
  360. this.setColour(Blockly.Blocks.nbiot.HUE);
  361. this.setOutput(true, Blockly.Types.TEXT.output);
  362. },
  363. getBlockType: function() {
  364. return Blockly.Types.TEXT;
  365. }
  366. }
  367. Blockly.Blocks['NBIoT_http_cococloud_get'] = {
  368. init: function() {
  369. this.appendDummyInput()
  370. .appendField(Blockly.Msg.NBIOT_HTTP_CLOUD_GET)
  371. this.appendDummyInput()
  372. .appendField(Blockly.Msg.NBIOT_HTTP_APIKEY)
  373. .appendField(new Blockly.FieldTextInput("yourCloudEventAPIKey"),"APIKEY")
  374. // this.setColour(Blockly.Blocks.nbiot.HUE);
  375. // this.setPreviousStatement(true);
  376. // this.setNextStatement(true);
  377. this.setOutput(true);
  378. }
  379. }