voicekit.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating Python for colour blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Python.voicekit');
  26. goog.require('Blockly.Python');
  27. Blockly.Python['voicekit_from'] = function(block) {
  28. var from_module = block.getFieldValue("FROM")
  29. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__'
  30. var code = 'from '+ from_module + ' '+ input+'\n';
  31. return code
  32. };
  33. Blockly.Python['voicekit_import'] = function(block) {
  34. var code = "import auth_helpers"
  35. return [code, Blockly.Python.ORDER_ATOMIC]
  36. };
  37. Blockly.Python['voicekit_import_tts'] = function(block) {
  38. var code = "from aiy.voice import tts"
  39. return code;
  40. }
  41. Blockly.Python['voicekit_tts_say'] = function(block) {
  42. var text = Blockly.Python.valueToCode(block,"TEXT",Blockly.Python.ORDER_ATOMIC)|| "_";
  43. var code = "tts.say("+text+", lang='en-US', volume=60, pitch=130, speed=100, device='default')\n"
  44. return code
  45. }
  46. Blockly.Python['voicekit_set_credential'] = function(block) {
  47. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__'
  48. var statement_do = Blockly.Python.statementToCode(block, "STACK");
  49. var assist = block.getFieldValue("ASSISTANT");
  50. var code = "with Assistant(" + input + ") as "+assist + ":\n"+
  51. statement_do+"\n";
  52. return code;
  53. }
  54. Blockly.Python['voicekit_get_credential'] = function(block) {
  55. var code = "auth_helpers.get_assistant_credentials()";
  56. return [code, Blockly.Python.ORDER_ATOMIC];
  57. }
  58. Blockly.Python['voicekit_import_assistant'] = function(block) {
  59. var code = "import Assistant";
  60. return [code, Blockly.Python.ORDER_ATOMIC];
  61. }
  62. Blockly.Python['voicekit_assistant_preNext'] = function(block) {
  63. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__';
  64. var operation = block.getFieldValue("OPERATION");
  65. var code = input+ "."+ operation+"\n";
  66. return code;
  67. }
  68. Blockly.Python['voicekit_assistant_output'] = function(block) {
  69. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__';
  70. var operation = block.getFieldValue("OPERATION");
  71. var code = input+ "."+ operation;
  72. return [code,Blockly.Python.ORDER_ATOMIC]
  73. }
  74. Blockly.Python['voicekit_EventType_import'] = function(block) {
  75. var code = "import EventType";
  76. return [code , Blockly.Python.ORDER_ATOMIC]
  77. }
  78. Blockly.Python['voicekit_EventType_assistant'] = function(block) {
  79. var type = block.getFieldValue("TYPE")
  80. var code = "EventType."+ type;
  81. return [code, Blockly.Python.ORDER_ATOMIC]
  82. }
  83. Blockly.Python['voicekit_event_of'] = function(block) {
  84. var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__';
  85. var code = input + ".type";
  86. return [code , Blockly.Python.ORDER_ATOMIC];
  87. }
  88. Blockly.Python['voicekit_run_task'] = function(block) {
  89. var code = "self._run_task";
  90. return [code, Blockly.Python.ORDER_ATOMIC];
  91. }
  92. Blockly.Python['voicekit_process_event'] = function(block) {
  93. var code = "self._process_event(event)\n";
  94. return code
  95. }
  96. Blockly.Python['voicekit_assistant_start_conversation'] = function(block) {
  97. var code = "self._assistant.start_conversation()\n";
  98. return code;
  99. }
  100. Blockly.Python['voicekit_send_instructions'] = function(block) {
  101. var text = Blockly.Python.valueToCode(block,"TEXT", Blockly.Python.ORDER_ATOMIC) || '__';
  102. var code = 'self.send_instructions('+text+')\n';
  103. return code;
  104. }
  105. Blockly.Python['voicekit_on_button_pressed'] = function(block) {
  106. var code = "self._on_button_pressed";
  107. return [code, Blockly.Python.ORDER_ATOMIC]
  108. }
  109. /**************************
  110. * simplify *
  111. ***************************/
  112. Blockly.Python['voicekit_import_aiy'] = function(block) {
  113. var code = "from google.assistant.library.event import EventType\n"+
  114. "from aiy.assistant import auth_helpers\n"+
  115. "from aiy.assistant.library import Assistant\n"+
  116. "from aiy.voice import tts\n";
  117. return code
  118. }
  119. Blockly.Python['voicekit_init'] = function(block) {
  120. var code = 'def __init__(self):\n'
  121. for (let i= 0;i < block.itemCount_; i++) {
  122. var text = Blockly.Python.valueToCode(block,"ADD"+ i, Blockly.Python.ORDER_ATOMIC) || '\n';
  123. switch (text) {
  124. case 'button':
  125. code+= ' self._board.button.when_pressed = self._on_button_pressed\n'
  126. break;
  127. case 'board':
  128. code+= ' self._board = Board()\n'
  129. break;
  130. case 'task':
  131. code+= ' self._task = threading.Thread(target=self._run_task)\n'
  132. break;
  133. case 'can_start':
  134. code+= ' self._can_start_conversation = False\n'
  135. break;
  136. case 'assistant':
  137. code+= ' self._assistant = None\n'
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. return code;
  144. }
  145. Blockly.Python['voicekit_task'] = function(block) {
  146. var code = "task";
  147. return [code, Blockly.Python.ORDER_ATOMIC]
  148. }
  149. Blockly.Python['voicekit_can_start'] = function(block) {
  150. var code = "can_start";
  151. return [code, Blockly.Python.ORDER_ATOMIC]
  152. }
  153. Blockly.Python['voicekit_asssistant'] = function(block) {
  154. var code = "assistant";
  155. return [code, Blockly.Python.ORDER_ATOMIC]
  156. }
  157. Blockly.Python['voicekit_start_thread'] = function(block) {
  158. var code = "def start(self):\n"+
  159. " self._task.start()\n"
  160. return code;
  161. }
  162. Blockly.Python['voicekit_runTask'] = function(block) {
  163. var code = "def _run_task(self):\n"+
  164. " credentials = auth_helpers.get_assistant_credentials()\n"+
  165. " with Assistant(credentials) as assistant:\n"+
  166. " self._assistant = assistant\n"+
  167. " for event in assistant.start():\n"+
  168. " self._process_event(event)\n"
  169. return code;
  170. }
  171. Blockly.Python['voicekit_logging_config'] = function(block) {
  172. var code = "logging.basicConfig(level=logging.INFO)\n";
  173. return code;
  174. }
  175. Blockly.Python['voicekit_myassistant_start'] = function(block) {
  176. var input_a = block.getFieldValue("INPUT");
  177. var code = input_a + "().start()";
  178. return code;
  179. }
  180. Blockly.Python['voicekit_process_event'] = function(block) {
  181. var statement_input = Blockly.Python.statementToCode(block,"STACK");
  182. var code = "def _process_event(self, event):\n"+
  183. " logging.info(event)\n"+ statement_input;
  184. return code
  185. }
  186. Blockly.Python['voicekit_process_eventType'] = function(block) {
  187. var code = "event.type";
  188. return [code, Blockly.Python.ORDER_ATOMIC];
  189. }
  190. Blockly.Python['voicekit_EventType_select'] = function(block) {
  191. var type = block.getFieldValue("TYPE")
  192. var code = "EventType."+ type;
  193. return [code, Blockly.Python.ORDER_ATOMIC];
  194. }
  195. Blockly.Python['voicekit_can_start_conversation_self'] = function(block) {
  196. var code = "self._can_start_conversation";
  197. return [code, Blockly.Python.ORDER_ATOMIC];
  198. }
  199. Blockly.Python['voicekit_can_start_conversation'] = function(block) {
  200. var setting = block.getFieldValue("SETTING");
  201. var code = "self._can_start_conversation = "+setting+ "\n";
  202. return code;
  203. }
  204. Blockly.Python['voicekit_assistant_conversation'] = function(block) {
  205. var setting = block.getFieldValue("SETTING");
  206. var code = "self._assistant."+setting+ "\n";
  207. return code;
  208. }
  209. Blockly.Python['voicekit_event_args'] = function(block) {
  210. var code = "event.args";
  211. return [code, Blockly.Python.ORDER_ATOMIC];
  212. }
  213. Blockly.Python['voicekit_event_args_of'] = function(block) {
  214. var text = block.getFieldValue("TEXT")
  215. var code = "event.args['"+text+"']";
  216. return [code, Blockly.Python.ORDER_ATOMIC];
  217. }