wifiwebservices.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. 'use strict';
  2. goog.provide('Blockly.Blocks.wifiwebservices');
  3. goog.require('Blockly.Blocks');
  4. goog.require('Blockly.Types');
  5. Blockly.Blocks.wifiwebservices.HUE = "#37a0ce";
  6. /*IFTTT service
  7. *method to post http request with Json
  8. *Json with three data
  9. */
  10. Blockly.Blocks['wifiwebservices_ifttt_simple'] = {
  11. init: function() {
  12. this.appendDummyInput()
  13. .appendField(Blockly.Msg.WIFIWEBSERVICES_IFTTT);
  14. this.appendValueInput("KEY")
  15. .setCheck("Text")
  16. .appendField(Blockly.Msg.WIFIWEBSERVICES_IFTTT_SET_KEY);
  17. this.appendValueInput("EVENT")
  18. .setCheck("Text")
  19. .appendField(Blockly.Msg.WIFIWEBSERVICES_IFTTT_SET_EVENT);
  20. this.appendValueInput("VALUE1")
  21. .appendField(Blockly.Msg.WIFIWEBSERVICES_IFTTT_SET_VALUE1);
  22. this.appendValueInput("VALUE2")
  23. .appendField(Blockly.Msg.WIFIWEBSERVICES_IFTTT_SET_VALUE2);
  24. this.appendValueInput("VALUE3")
  25. .appendField(Blockly.Msg.WIFIWEBSERVICES_IFTTT_SET_VALUE3);
  26. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  27. this.setPreviousStatement(true, null);
  28. this.setNextStatement(true, null);
  29. // this.setInputsInline(true);
  30. this.setTooltip("");
  31. this.setHelpUrl("");
  32. },
  33. };
  34. /*Thinkspeak service
  35. *method to get http request with data in url
  36. *field1, field2, field3
  37. */
  38. Blockly.Blocks['wifiwebservices_thinkspeak_simple'] = {
  39. init: function() {
  40. this.appendDummyInput()
  41. .appendField(Blockly.Msg.WIFIWEBSERVICES_THINKSPEAK);
  42. this.appendValueInput("KEY")
  43. .setCheck("Text")
  44. .appendField(Blockly.Msg.WIFIWEBSERVICES_THINKSPEAK_SET_KEY);
  45. this.itemCount_ = 1;
  46. this.updateShape_();
  47. this.setMutator(new Blockly.Mutator(['thingspeak_create_with_item']));
  48. // this.appendValueInput("FIELD")
  49. // .appendField(Blockly.Msg.WIFIWEBSERVICES_THINKSPEAK_SET_FIELD1);
  50. // this.appendValueInput("VALUE")
  51. // .appendField(Blockly.Msg.WIFIWEBSERVICES_THINKSPEAK_SET_VALUE);
  52. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  53. this.setPreviousStatement(true, null);
  54. this.setNextStatement(true, null);
  55. // this.setInputsInline(true);
  56. this.setTooltip("");
  57. this.setHelpUrl("");
  58. },
  59. mutationToDom: function() {
  60. var container = document.createElement('mutation');
  61. container.setAttribute('items', this.itemCount_);
  62. return container;
  63. },
  64. /**
  65. * Parse XML to restore the inputs.
  66. * @param {!Element} xmlElement XML storage element.
  67. * @this Blockly.Block
  68. */
  69. domToMutation: function(xmlElement) {
  70. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  71. this.updateShape_();
  72. },
  73. /**
  74. * Populate the mutator's dialog with this block's components.
  75. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  76. * @return {!Blockly.Block} Root block in mutator.
  77. * @this Blockly.Block
  78. */
  79. decompose: function(workspace) {
  80. var containerBlock = workspace.newBlock('thingspeak_create_with_container');
  81. containerBlock.initSvg();
  82. var connection = containerBlock.getInput('STACK').connection;
  83. for (var i = 0; i < this.itemCount_; i++) {
  84. var itemBlock = workspace.newBlock('thingspeak_create_with_item');
  85. itemBlock.initSvg();
  86. connection.connect(itemBlock.previousConnection);
  87. connection = itemBlock.nextConnection;
  88. }
  89. return containerBlock;
  90. },
  91. /**
  92. * Reconfigure this block based on the mutator dialog's components.
  93. * @param {!Blockly.Block} containerBlock Root block in mutator.
  94. * @this Blockly.Block
  95. */
  96. compose: function(containerBlock) {
  97. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  98. // Count number of inputs.
  99. var connections = [];
  100. while (itemBlock) {
  101. connections.push(itemBlock.valueConnection_);
  102. itemBlock = itemBlock.nextConnection &&
  103. itemBlock.nextConnection.targetBlock();
  104. }
  105. // Disconnect any children that don't belong.
  106. for (var i = 0; i < this.itemCount_; i++) {
  107. var connection = this.getInput('ADD' + i).connection.targetConnection;
  108. if (connection && connections.indexOf(connection) == -1) {
  109. connection.disconnect();
  110. }
  111. }
  112. this.itemCount_ = connections.length;
  113. this.updateShape_();
  114. // Reconnect any child blocks.
  115. for (var i = 0; i < this.itemCount_; i++) {
  116. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  117. }
  118. },
  119. /**
  120. * Store pointers to any connected child blocks.
  121. * @param {!Blockly.Block} containerBlock Root block in mutator.
  122. * @this Blockly.Block
  123. */
  124. saveConnections: function(containerBlock) {
  125. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  126. var i = 0;
  127. while (itemBlock) {
  128. var input = this.getInput('ADD' + i);
  129. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  130. i++;
  131. itemBlock = itemBlock.nextConnection &&
  132. itemBlock.nextConnection.targetBlock();
  133. }
  134. },
  135. /**
  136. * Modify this block to have the correct number of inputs.
  137. * @private
  138. * @this Blockly.Block
  139. */
  140. updateShape_: function() {
  141. // if (this.itemCount_ && this.getInput('EMPTY')) {
  142. // this.removeInput('EMPTY');
  143. // } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
  144. // this.appendDummyInput('EMPTY')
  145. // .appendField(this.newQuote_(true))
  146. // .appendField(this.newQuote_(false));
  147. // }
  148. // Add new inputs.
  149. for (var i = 0; i < this.itemCount_; i++) {
  150. if (!this.getInput('ADD' + i)) {
  151. var input = this.appendValueInput('ADD' + i);
  152. input.appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_INPUT_WITH)
  153. .appendField(new Blockly.FieldTextInput("field" + (i + 1)), 'FIELD' + i)
  154. }
  155. }
  156. // Remove deleted inputs.
  157. while (this.getInput('ADD' + i)) {
  158. this.removeInput('ADD' + i);
  159. i++;
  160. }
  161. },
  162. };
  163. Blockly.Blocks['thingspeak_create_with_container'] = {
  164. /**
  165. * Mutator block for list container.
  166. * @this Blockly.Block
  167. */
  168. init: function() {
  169. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  170. this.appendDummyInput()
  171. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  172. this.appendStatementInput('STACK');
  173. this.setTooltip('');
  174. this.contextMenu = false;
  175. }
  176. };
  177. Blockly.Blocks['thingspeak_create_with_item'] = {
  178. /**
  179. * Mutator bolck for adding items.
  180. * @this Blockly.Block
  181. */
  182. init: function() {
  183. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  184. this.appendDummyInput()
  185. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  186. this.setPreviousStatement(true);
  187. this.setNextStatement(true);
  188. this.setTooltip('');
  189. this.contextMenu = false;
  190. }
  191. };
  192. /**
  193. *CocoCloud Service
  194. *method POST a http request with a JSON data
  195. *@param url :http://staging.cocorobo.cn/cloud/iot/{events}
  196. *@param event get from URL://cocorobo.cn/cloud/iot/data/apikey/{@param APIKEY}/event
  197. *@param data JSON form - generate from clouddata blocks
  198. */
  199. Blockly.Blocks['wifiwebservices_cococloud_simple_post'] = {
  200. init: function() {
  201. this.appendDummyInput()
  202. .appendField(Blockly.Msg.COCOCLOUD_TITLE);
  203. this.appendValueInput('ADD0')
  204. .appendField(Blockly.Msg.COCOCLOUD_POST)
  205. .appendField(new Blockly.FieldDropdown([
  206. ["event", "event"]
  207. ]), "event0");
  208. this.itemCount = 1;
  209. this.updateShape_();
  210. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  211. this.setPreviousStatement(true, null);
  212. this.setNextStatement(true, null);
  213. this.setTooltip("");
  214. this.setHelpUrl("");
  215. },
  216. /**
  217. * Modify this block to have the correct number of inputs.
  218. * @private
  219. * @this Blockly.Block
  220. */
  221. updateShape_: function() {
  222. // Add new inputs.
  223. var self = this;
  224. var input = this.getInput('ADD0');
  225. var api_key = document.getElementById('api-key').innerHTML || "";
  226. $.ajax({
  227. url: '//api.cocorobo.cn/iot/data/apikey/' + api_key + '/event/',
  228. // url: '//staging.cocorobo.cn/api/iot/data/apikey/' + api_key + '/event/',
  229. xhrFields: {
  230. withCredentials: true
  231. },
  232. type: 'GET',
  233. success: function(data) {
  234. var eventsName = [];
  235. if (data != null) {
  236. $.each(data, function(i, obj) {
  237. var temparray = []
  238. temparray.push(obj.name);
  239. temparray.push(obj.url);
  240. eventsName.push(temparray);
  241. });
  242. self.eventsName = eventsName;
  243. input.removeField("event0");
  244. input.appendField(new Blockly.FieldDropdown(eventsName), "event0");
  245. }
  246. },
  247. error: function(error) {
  248. console.log("error");
  249. }
  250. });
  251. }
  252. };
  253. /**
  254. *CocoCloud Service
  255. *method GET a http request to get a JSON data
  256. *@param url :http://staging.cocorobo.cn/cloud/iot/{events}
  257. *@param event get from URL://cocorobo.cn/cloud/iot/data/apikey/{@param APIKEY}/event
  258. * data JSON form
  259. */
  260. Blockly.Blocks['wifiwebservices_cococloud_simple_get'] = {
  261. init: function() {
  262. this.appendDummyInput()
  263. .appendField(Blockly.Msg.COCOCLOUD_TITLE);
  264. this.appendDummyInput("iuput_id0")
  265. .appendField(Blockly.Msg.COCOCLOUD_GET, "text_field0")
  266. .appendField(new Blockly.FieldDropdown([
  267. ["event", "event"]
  268. ]), "event0")
  269. .appendField(Blockly.Msg.COCOCLOUD_GET1, "text_field1");
  270. this.itemCount = 1;
  271. this.updateShape_();
  272. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  273. this.setPreviousStatement(true, null);
  274. this.setNextStatement(true, null);
  275. this.setTooltip("");
  276. this.setHelpUrl("");
  277. },
  278. /**
  279. * Modify this block to have the correct number of inputs.
  280. * @private
  281. * @this Blockly.Block
  282. */
  283. updateShape_: function() {
  284. // Add new inputs.
  285. var self = this;
  286. var input = this.getInput('iuput_id0');
  287. var api_key = document.getElementById('api-key').innerHTML || "";
  288. $.ajax({
  289. url: '//api.cocorobo.cn/iot/data/apikey/' + api_key + '/event/',
  290. // url: '//staging.cocorobo.cn/api/iot/data/apikey/' + api_key + '/event/',
  291. xhrFields: {
  292. withCredentials: true
  293. },
  294. type: 'GET',
  295. success: function(data) {
  296. var eventsName = [];
  297. if (data != null) {
  298. $.each(data, function(i, obj) {
  299. var temparray = [];
  300. temparray.push(obj.name);
  301. temparray.push(obj.url);
  302. eventsName.push(temparray);
  303. });
  304. self.eventsName = eventsName;
  305. input.removeField("text_field0");
  306. input.removeField("event0");
  307. input.removeField("text_field1");
  308. input.appendField(Blockly.Msg.COCOCLOUD_GET, "text_field0")
  309. .appendField(new Blockly.FieldDropdown(eventsName), "event0")
  310. .appendField(Blockly.Msg.COCOCLOUD_GET1, "text_field1");
  311. }
  312. },
  313. error: function(error) {
  314. console.log("error");
  315. }
  316. });
  317. }
  318. };
  319. /**
  320. *CocoCloud Service
  321. * get JSON data from cococloud-get-block
  322. */
  323. Blockly.Blocks['wifiwebservices_cococloud_simple_getPayLoad'] = {
  324. init: function() {
  325. this.appendDummyInput()
  326. .appendField(Blockly.Msg.COCOCLOUD_TITLE);
  327. this.appendDummyInput()
  328. .appendField(Blockly.Msg.COCOCLOUD_GETPAYLOAD);
  329. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  330. this.setOutput(true);
  331. this.setTooltip("");
  332. this.setHelpUrl("");
  333. },
  334. };
  335. /*************************
  336. * version 2 *
  337. * eaysmode *
  338. *************************/
  339. /*wifi easymode
  340. * IFTTT
  341. * post data {JSON} to Cloud event
  342. */
  343. Blockly.Blocks['wifiServices_IFTTT'] = {
  344. init: function() {
  345. this.appendDummyInput()
  346. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/blockly-easymode_thingspeak.png", 180, 40, "0"));
  347. .appendField(new Blockly.FieldImage("./../blockly/media/blockly-easymode_ifttt.png", 180, 40, "0"));
  348. this.appendDummyInput()
  349. .appendField(" " + Blockly.Msg.WIFIEASYMODE_IFTTT_KEY);
  350. this.appendDummyInput()
  351. .appendField(" ")
  352. .appendField(new Blockly.FieldTextInput("bAssF9YJ_7oZP1k4et_IUx"), "IFTTT_KEY")
  353. this.appendDummyInput()
  354. .appendField(" " + Blockly.Msg.WIFIEASYMODE_IFTTT_EVENT);
  355. this.appendDummyInput()
  356. .appendField(" ")
  357. .appendField(new Blockly.FieldTextInput("cocorobo_webhook"), "IFTTT_EVENT")
  358. this.appendDummyInput()
  359. .appendField(" " + Blockly.Msg.WIFIEASYMODE_IFTTT_TYPE)
  360. .appendField(new Blockly.FieldDropdown([
  361. [Blockly.Msg.WIFIEASYMODE_IFTTT_POST, "Action"],
  362. [Blockly.Msg.WIFIEASYMODE_IFTTT_FETCH, "Trigger"]
  363. ]), "TYPE");
  364. for (let n = 0; n < 3; n++) {
  365. if (!this.getInput("data" + n))
  366. this.appendValueInput("data" + n)
  367. .appendField(" " + Blockly.Msg.WIFIEASYMODE_IFTTT_SENDDATA + n);
  368. }
  369. // this.appendValueInput("dataA")
  370. // .appendField(" " + Blockly.Msg.WIFIEASYMODE_IFTTT_SENDDATA);
  371. this.onChange();
  372. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  373. this.setPreviousStatement(true, null);
  374. this.setNextStatement(true, null);
  375. // this.setInputsInline(true);
  376. this.setTooltip("");
  377. this.setHelpUrl("");
  378. },
  379. getType: function() {
  380. switch (this.getFieldValue("TYPE")) {
  381. case "Action":
  382. return 1;
  383. case "Trigger":
  384. return 2;
  385. default:
  386. return 0;
  387. }
  388. },
  389. onChange: function() {
  390. if (this.getType() == 1) {
  391. for (let n = 0; n < 3; n++) {
  392. if (!this.getInput("data" + n))
  393. this.appendValueInput("data" + n)
  394. .appendField(" " + Blockly.Msg.WIFIEASYMODE_IFTTT_SENDDATA + n);
  395. }
  396. } else if (this.getType() == 2) {
  397. var i = 0;
  398. var substructure
  399. while (this.getInput('data' + i)) {
  400. substructure = this.getInputTargetBlock("data" + i);
  401. if (substructure) {
  402. substructure.dispose(true, true);
  403. }
  404. this.removeInput('data' + i);
  405. i++;
  406. }
  407. }
  408. }
  409. }
  410. /*wifi easymode
  411. *setup for WiFi network
  412. */
  413. Blockly.Blocks['wifiServices_thingspeak'] = {
  414. init: function() {
  415. this.appendDummyInput()
  416. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/blockly-easymode_thingspeak.png", 180, 40, "0"));
  417. .appendField(new Blockly.FieldImage("./../blockly/media/blockly-easymode_thingspeak.png", 180, 40, "0"));
  418. this.appendDummyInput()
  419. .appendField(" " + Blockly.Msg.WIFIEASYMODE_THINGSPEAK_APIKEY)
  420. this.appendDummyInput()
  421. .appendField(" ")
  422. .appendField(new Blockly.FieldTextInput("bAssF9YJ_7oZP1k4et_IUx"), "API_KEY")
  423. this.appendDummyInput()
  424. .appendField(" " + Blockly.Msg.WIFIEASYMODE_THINGSPEAK_SENDDATA)
  425. this.itemCount_ = 1;
  426. this.updateShape_();
  427. this.setMutator(new Blockly.Mutator(['thingspeakEM_create_with_item']));
  428. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  429. this.setPreviousStatement(true, null);
  430. this.setNextStatement(true, null);
  431. // this.setInputsInline(true);
  432. this.setTooltip("");
  433. this.setHelpUrl("");
  434. },
  435. mutationToDom: function() {
  436. var container = document.createElement('mutation');
  437. container.setAttribute('items', this.itemCount_);
  438. return container;
  439. },
  440. /**
  441. * Parse XML to restore the inputs.
  442. * @param {!Element} xmlElement XML storage element.
  443. * @this Blockly.Block
  444. */
  445. domToMutation: function(xmlElement) {
  446. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  447. this.updateShape_();
  448. },
  449. /**
  450. * Populate the mutator's dialog with this block's components.
  451. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  452. * @return {!Blockly.Block} Root block in mutator.
  453. * @this Blockly.Block
  454. */
  455. decompose: function(workspace) {
  456. var containerBlock = workspace.newBlock('thingspeakEM_create_with_container');
  457. containerBlock.initSvg();
  458. var connection = containerBlock.getInput('STACK').connection;
  459. for (var i = 0; i < this.itemCount_; i++) {
  460. var itemBlock = workspace.newBlock('thingspeakEM_create_with_item');
  461. itemBlock.initSvg();
  462. connection.connect(itemBlock.previousConnection);
  463. connection = itemBlock.nextConnection;
  464. }
  465. return containerBlock;
  466. },
  467. /**
  468. * Reconfigure this block based on the mutator dialog's components.
  469. * @param {!Blockly.Block} containerBlock Root block in mutator.
  470. * @this Blockly.Block
  471. */
  472. compose: function(containerBlock) {
  473. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  474. // Count number of inputs.
  475. var connections = [];
  476. while (itemBlock) {
  477. connections.push(itemBlock.valueConnection_);
  478. itemBlock = itemBlock.nextConnection &&
  479. itemBlock.nextConnection.targetBlock();
  480. }
  481. // Disconnect any children that don't belong.
  482. for (var i = 0; i < this.itemCount_; i++) {
  483. var connection = this.getInput('ADD' + i).connection.targetConnection;
  484. if (connection && connections.indexOf(connection) == -1) {
  485. connection.disconnect();
  486. }
  487. }
  488. this.itemCount_ = connections.length;
  489. this.updateShape_();
  490. // Reconnect any child blocks.
  491. for (var i = 0; i < this.itemCount_; i++) {
  492. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  493. }
  494. },
  495. /**
  496. * Store pointers to any connected child blocks.
  497. * @param {!Blockly.Block} containerBlock Root block in mutator.
  498. * @this Blockly.Block
  499. */
  500. saveConnections: function(containerBlock) {
  501. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  502. var i = 0;
  503. while (itemBlock) {
  504. var input = this.getInput('ADD' + i);
  505. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  506. i++;
  507. itemBlock = itemBlock.nextConnection &&
  508. itemBlock.nextConnection.targetBlock();
  509. }
  510. },
  511. /**
  512. * Modify this block to have the correct number of inputs.
  513. * @private
  514. * @this Blockly.Block
  515. */
  516. updateShape_: function() {
  517. // if (this.itemCount_ && this.getInput('EMPTY')) {
  518. // this.removeInput('EMPTY');
  519. // } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
  520. // this.appendDummyInput('EMPTY')
  521. // .appendField(this.newQuote_(true))
  522. // .appendField(this.newQuote_(false));
  523. // }
  524. // Add new inputs.
  525. for (var i = 0; i < this.itemCount_; i++) {
  526. if (!this.getInput('ADD' + i)) {
  527. var input = this.appendValueInput('ADD' + i);
  528. input.appendField(" " + "Field " + (i + 1) + ":");
  529. }
  530. }
  531. // Remove deleted inputs.
  532. while (this.getInput('ADD' + i)) {
  533. this.removeInput('ADD' + i);
  534. i++;
  535. }
  536. },
  537. };
  538. Blockly.Blocks['thingspeakEM_create_with_container'] = {
  539. /**
  540. * Mutator block for list container.
  541. * @this Blockly.Block
  542. */
  543. init: function() {
  544. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  545. this.appendDummyInput()
  546. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  547. this.appendStatementInput('STACK');
  548. this.setTooltip('');
  549. this.contextMenu = false;
  550. }
  551. };
  552. Blockly.Blocks['thingspeakEM_create_with_item'] = {
  553. /**
  554. * Mutator bolck for adding items.
  555. * @this Blockly.Block
  556. */
  557. init: function() {
  558. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  559. this.appendDummyInput()
  560. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  561. this.setPreviousStatement(true);
  562. this.setNextStatement(true);
  563. this.setTooltip('');
  564. this.contextMenu = false;
  565. }
  566. };
  567. /*wifi easymode
  568. * CocoCloud
  569. * post data {JSON} to Cloud event
  570. */
  571. Blockly.Blocks['wifiServices_cococloud_post'] = {
  572. init: function() {
  573. this.appendDummyInput()
  574. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/blockly-easymode_thingspeak.png", 180, 40, "0"));
  575. .appendField(new Blockly.FieldImage("./../blockly/media/cloud-tx.png", 180, 40, "0"));
  576. this.appendDummyInput()
  577. .appendField(" " + Blockly.Msg.WIFIEASYMODE_COCOClOUD_EVENT);
  578. this.appendDummyInput()
  579. .appendField(" ")
  580. // .appendField(new Blockly.FieldDropdown([
  581. // ["Your event", "Your event"]
  582. // ]), "event0");
  583. .appendField(new Blockly.FieldTextInput("d0f98bb70ae070cd89725780e67dfe6b"), "event0")
  584. this.appendDummyInput()
  585. .appendField(" " + Blockly.Msg.WIFIEASYMODE_COCOCLOUD_SENDDATA);
  586. this.appendValueInput("ADD0")
  587. // .appendField(Blockly.Msg.WIFIEASYMODE_COCOClOUD_DATA_NAME)
  588. .appendField(" ")
  589. .appendField(new Blockly.FieldTextInput("EM_data0"), "field0")
  590. this.itemCount_ = 1;
  591. this.updateShape_();
  592. this.setMutator(new Blockly.Mutator(['cococloud_create_with_item']));
  593. // this.updateEvent_();
  594. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  595. this.setPreviousStatement(true, null);
  596. this.setNextStatement(true, null);
  597. // this.setInputsInline(true);
  598. this.setTooltip("");
  599. this.setHelpUrl("");
  600. },
  601. mutationToDom: function() {
  602. var container = document.createElement('mutation');
  603. container.setAttribute('items', this.itemCount_);
  604. return container;
  605. },
  606. /**
  607. * Parse XML to restore the inputs.
  608. * @param {!Element} xmlElement XML storage element.
  609. * @this Blockly.Block
  610. */
  611. domToMutation: function(xmlElement) {
  612. this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10);
  613. this.updateShape_();
  614. },
  615. /**
  616. * Populate the mutator's dialog with this block's components.
  617. * @param {!Blockly.Workspace} workspace Mutator's workspace.
  618. * @return {!Blockly.Block} Root block in mutator.
  619. * @this Blockly.Block
  620. */
  621. decompose: function(workspace) {
  622. var containerBlock = workspace.newBlock('cococloud_create_with_container');
  623. containerBlock.initSvg();
  624. var connection = containerBlock.getInput('STACK').connection;
  625. for (var i = 0; i < this.itemCount_; i++) {
  626. var itemBlock = workspace.newBlock('cococloud_create_with_item');
  627. itemBlock.initSvg();
  628. connection.connect(itemBlock.previousConnection);
  629. connection = itemBlock.nextConnection;
  630. }
  631. return containerBlock;
  632. },
  633. /**
  634. * Reconfigure this block based on the mutator dialog's components.
  635. * @param {!Blockly.Block} containerBlock Root block in mutator.
  636. * @this Blockly.Block
  637. */
  638. compose: function(containerBlock) {
  639. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  640. // Count number of inputs.
  641. var connections = [];
  642. while (itemBlock) {
  643. connections.push(itemBlock.valueConnection_);
  644. itemBlock = itemBlock.nextConnection &&
  645. itemBlock.nextConnection.targetBlock();
  646. }
  647. // Disconnect any children that don't belong.
  648. for (var i = 0; i < this.itemCount_; i++) {
  649. var connection = this.getInput('ADD' + i).connection.targetConnection;
  650. if (connection && connections.indexOf(connection) == -1) {
  651. connection.disconnect();
  652. }
  653. }
  654. this.itemCount_ = connections.length;
  655. this.updateShape_();
  656. // Reconnect any child blocks.
  657. for (var i = 0; i < this.itemCount_; i++) {
  658. Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i);
  659. }
  660. },
  661. /**
  662. * Store pointers to any connected child blocks.
  663. * @param {!Blockly.Block} containerBlock Root block in mutator.
  664. * @this Blockly.Block
  665. */
  666. saveConnections: function(containerBlock) {
  667. var itemBlock = containerBlock.getInputTargetBlock('STACK');
  668. var i = 0;
  669. while (itemBlock) {
  670. var input = this.getInput('ADD' + i);
  671. itemBlock.valueConnection_ = input && input.connection.targetConnection;
  672. i++;
  673. itemBlock = itemBlock.nextConnection &&
  674. itemBlock.nextConnection.targetBlock();
  675. }
  676. },
  677. /**
  678. * Modify this block to have the correct number of inputs.
  679. * @private
  680. * @this Blockly.Block
  681. */
  682. updateShape_: function() {
  683. // if (this.itemCount_ && this.getInput('EMPTY')) {
  684. // this.removeInput('EMPTY');
  685. // } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
  686. // this.appendDummyInput('EMPTY')
  687. // .appendField(this.newQuote_(true))
  688. // .appendField(this.newQuote_(false));
  689. // }
  690. // Add new inputs.
  691. for (var i = 0; i < this.itemCount_; i++) {
  692. if (!this.getInput('ADD' + i)) {
  693. var input = this.appendValueInput('ADD' + i);
  694. input.appendField(" ")
  695. .appendField(new Blockly.FieldTextInput("EM_data" + i), "field" + i);
  696. }
  697. }
  698. // Remove deleted inputs.
  699. while (this.getInput('ADD' + i)) {
  700. this.removeInput('ADD' + i);
  701. i++;
  702. }
  703. },
  704. updateEvent_: function() {
  705. // Add new inputs.
  706. var self = this;
  707. var input = this.getInput('ADD0');
  708. var api_key = document.getElementById('api-key').innerHTML || "";
  709. $.ajax({
  710. url: '//api.cocorobo.cn/iot/data/apikey/' + api_key + '/event/',
  711. // url: '//staging.cocorobo.cn/api/iot/data/apikey/' + api_key + '/event/',
  712. xhrFields: {
  713. withCredentials: true
  714. },
  715. type: 'GET',
  716. success: function(data) {
  717. var eventsName = [];
  718. if (data != null) {
  719. $.each(data, function(i, obj) {
  720. var temparray = []
  721. temparray.push(obj.name);
  722. temparray.push(obj.url);
  723. eventsName.push(temparray);
  724. });
  725. self.eventsName = eventsName;
  726. input.removeField("event0");
  727. input.appendField(new Blockly.FieldDropdown(eventsName), "event0");
  728. }
  729. },
  730. error: function(error) {
  731. console.log("error");
  732. }
  733. });
  734. }
  735. };
  736. Blockly.Blocks['cococloud_create_with_container'] = {
  737. /**
  738. * Mutator block for list container.
  739. * @this Blockly.Block
  740. */
  741. init: function() {
  742. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  743. this.appendDummyInput()
  744. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_CONTAINER_TITLE_ADD);
  745. this.appendStatementInput('STACK');
  746. this.setTooltip('');
  747. this.contextMenu = false;
  748. }
  749. };
  750. Blockly.Blocks['cococloud_create_with_item'] = {
  751. /**
  752. * Mutator bolck for adding items.
  753. * @this Blockly.Block
  754. */
  755. init: function() {
  756. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  757. this.appendDummyInput()
  758. .appendField(Blockly.Msg.THINGSPEAK_CREATE_WITH_ITEM_TITLE);
  759. this.setPreviousStatement(true);
  760. this.setNextStatement(true);
  761. this.setTooltip('');
  762. this.contextMenu = false;
  763. },
  764. }
  765. /*wifi easymode
  766. * CocoCloud
  767. * post data {JSON} to Cloud event
  768. */
  769. Blockly.Blocks['wifiServices_cococloud_get'] = {
  770. init: function() {
  771. this.appendDummyInput()
  772. // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/blockly-easymode_thingspeak.png", 180, 40, "0"));
  773. .appendField(new Blockly.FieldImage("./../blockly/media/cloud-rx.png", 180, 40, "0"));
  774. this.appendDummyInput()
  775. .appendField(" " + Blockly.Msg.WIFIEASYMODE_COCOClOUD_EVENT)
  776. this.appendDummyInput()
  777. .appendField(" ")
  778. .appendField(new Blockly.FieldTextInput("d0f98bb70ae070cd89725780e67dfe6b"), "event0")
  779. this.appendDummyInput()
  780. .appendField(" " + Blockly.Msg.WIFIEASYMODE_COCOCLOUD_GETDATA)
  781. .appendField(new Blockly.FieldDropdown([
  782. ["rxCloud", "rxCloud"]
  783. ]), "VAR")
  784. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  785. this.setPreviousStatement(true, null);
  786. this.setNextStatement(true, null);
  787. this.setTooltip("");
  788. this.setHelpUrl("");
  789. },
  790. };
  791. Blockly.Blocks['wifiServices_cococloud_get_data'] = {
  792. init: function() {
  793. this.appendDummyInput()
  794. .appendField(Blockly.Msg.WIFIEASYMODE_COCOCLOUD_KEY)
  795. .appendField(new Blockly.FieldDropdown([
  796. ["rxCloud", "rxCloud"]
  797. ]), "VAR")
  798. .appendField(Blockly.Msg.WIFIEASYMODE_COCOCLOUD_KEY1)
  799. this.appendDummyInput()
  800. .appendField(Blockly.Msg.WIFIEASYMODE_COCOCLOUD_KEY2)
  801. .appendField(new Blockly.FieldTextInput("EM_data"), "KEY")
  802. .appendField(Blockly.Msg.WIFIEASYMODE_COCOCLOUD_TYPE)
  803. .appendField(new Blockly.FieldDropdown(
  804. [
  805. ["Text", "Text"],
  806. ["Number", "Number"],
  807. // ["Boolean", "Boolean"],
  808. // ["Large Number", "Large Number"],
  809. // ["Decimal", "Decimal"]
  810. ]), 'VAR_TYPE')
  811. .appendField(Blockly.Msg.WIFIEASYMODE_COCOCLOUD_END)
  812. // this.updateEvent_();
  813. this.setColour(Blockly.Blocks.wifiwebservices.HUE);
  814. this.setOutput(true);
  815. // this.setInputsInline(true);
  816. this.setTooltip("");
  817. this.setHelpUrl("");
  818. },
  819. getBlockType: function() {
  820. var blocktype = Blockly.Types.TEXT;
  821. switch (this.getFieldValue("VAR_TYPE")) {
  822. case "Text":
  823. blocktype = Blockly.Types.TEXT;
  824. break;
  825. case "Number":
  826. blocktype = Blockly.Types.NUMBER;
  827. break;
  828. // case "Boolean":
  829. // blocktype = Blockly.Types.BOOLEAN;
  830. // break;
  831. // case "Large Number":
  832. // blocktype = Blockly.Types.LARGE_NUMBER;
  833. // break;
  834. // case "Decimal":
  835. // blocktype = Blockly.Types.DECIMAL;
  836. // break;
  837. }
  838. return blocktype;
  839. }
  840. };