123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /**
- * @license
- * Visual Blocks Language
- *
- * Copyright 2012 Google Inc.
- * https://developers.google.com/blockly/
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview Generating Python for colour blocks.
- * @author fraser@google.com (Neil Fraser)
- */
- 'use strict';
- goog.provide('Blockly.Python.voicekit');
- goog.require('Blockly.Python');
- Blockly.Python['voicekit_from'] = function(block) {
- var from_module = block.getFieldValue("FROM")
- var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__'
- var code = 'from '+ from_module + ' '+ input+'\n';
- return code
- };
- Blockly.Python['voicekit_import'] = function(block) {
- var code = "import auth_helpers"
- return [code, Blockly.Python.ORDER_ATOMIC]
- };
- Blockly.Python['voicekit_import_tts'] = function(block) {
- var code = "from aiy.voice import tts"
- return code;
- }
- Blockly.Python['voicekit_tts_say'] = function(block) {
- var text = Blockly.Python.valueToCode(block,"TEXT",Blockly.Python.ORDER_ATOMIC)|| "_";
- var code = "tts.say("+text+", lang='en-US', volume=60, pitch=130, speed=100, device='default')\n"
- return code
- }
- Blockly.Python['voicekit_set_credential'] = function(block) {
- var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__'
- var statement_do = Blockly.Python.statementToCode(block, "STACK");
- var assist = block.getFieldValue("ASSISTANT");
- var code = "with Assistant(" + input + ") as "+assist + ":\n"+
- statement_do+"\n";
- return code;
- }
- Blockly.Python['voicekit_get_credential'] = function(block) {
- var code = "auth_helpers.get_assistant_credentials()";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_import_assistant'] = function(block) {
- var code = "import Assistant";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_assistant_preNext'] = function(block) {
- var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__';
- var operation = block.getFieldValue("OPERATION");
- var code = input+ "."+ operation+"\n";
- return code;
- }
- Blockly.Python['voicekit_assistant_output'] = function(block) {
- var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__';
- var operation = block.getFieldValue("OPERATION");
- var code = input+ "."+ operation;
- return [code,Blockly.Python.ORDER_ATOMIC]
- }
- Blockly.Python['voicekit_EventType_import'] = function(block) {
- var code = "import EventType";
- return [code , Blockly.Python.ORDER_ATOMIC]
- }
- Blockly.Python['voicekit_EventType_assistant'] = function(block) {
- var type = block.getFieldValue("TYPE")
- var code = "EventType."+ type;
- return [code, Blockly.Python.ORDER_ATOMIC]
- }
- Blockly.Python['voicekit_event_of'] = function(block) {
- var input = Blockly.Python.valueToCode(block,"INPUT",Blockly.Python.ORDER_ATOMIC) || '__';
- var code = input + ".type";
- return [code , Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_run_task'] = function(block) {
- var code = "self._run_task";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_process_event'] = function(block) {
- var code = "self._process_event(event)\n";
- return code
- }
- Blockly.Python['voicekit_assistant_start_conversation'] = function(block) {
- var code = "self._assistant.start_conversation()\n";
- return code;
- }
- Blockly.Python['voicekit_send_instructions'] = function(block) {
- var text = Blockly.Python.valueToCode(block,"TEXT", Blockly.Python.ORDER_ATOMIC) || '__';
- var code = 'self.send_instructions('+text+')\n';
- return code;
- }
- Blockly.Python['voicekit_on_button_pressed'] = function(block) {
- var code = "self._on_button_pressed";
- return [code, Blockly.Python.ORDER_ATOMIC]
- }
- /**************************
- * simplify *
- ***************************/
- Blockly.Python['voicekit_import_aiy'] = function(block) {
- var code = "from google.assistant.library.event import EventType\n"+
- "from aiy.assistant import auth_helpers\n"+
- "from aiy.assistant.library import Assistant\n"+
- "from aiy.voice import tts\n";
- return code
- }
- Blockly.Python['voicekit_init'] = function(block) {
- var code = 'def __init__(self):\n'
- for (let i= 0;i < block.itemCount_; i++) {
- var text = Blockly.Python.valueToCode(block,"ADD"+ i, Blockly.Python.ORDER_ATOMIC) || '\n';
- switch (text) {
- case 'button':
- code+= ' self._board.button.when_pressed = self._on_button_pressed\n'
- break;
- case 'board':
- code+= ' self._board = Board()\n'
- break;
- case 'task':
- code+= ' self._task = threading.Thread(target=self._run_task)\n'
- break;
- case 'can_start':
- code+= ' self._can_start_conversation = False\n'
- break;
- case 'assistant':
- code+= ' self._assistant = None\n'
- break;
- default:
- break;
- }
- }
- return code;
- }
- Blockly.Python['voicekit_task'] = function(block) {
- var code = "task";
- return [code, Blockly.Python.ORDER_ATOMIC]
- }
- Blockly.Python['voicekit_can_start'] = function(block) {
- var code = "can_start";
- return [code, Blockly.Python.ORDER_ATOMIC]
- }
- Blockly.Python['voicekit_asssistant'] = function(block) {
- var code = "assistant";
- return [code, Blockly.Python.ORDER_ATOMIC]
- }
- Blockly.Python['voicekit_start_thread'] = function(block) {
- var code = "def start(self):\n"+
- " self._task.start()\n"
- return code;
- }
- Blockly.Python['voicekit_runTask'] = function(block) {
- var code = "def _run_task(self):\n"+
- " credentials = auth_helpers.get_assistant_credentials()\n"+
- " with Assistant(credentials) as assistant:\n"+
- " self._assistant = assistant\n"+
- " for event in assistant.start():\n"+
- " self._process_event(event)\n"
- return code;
- }
- Blockly.Python['voicekit_logging_config'] = function(block) {
- var code = "logging.basicConfig(level=logging.INFO)\n";
- return code;
- }
- Blockly.Python['voicekit_myassistant_start'] = function(block) {
- var input_a = block.getFieldValue("INPUT");
- var code = input_a + "().start()";
- return code;
- }
- Blockly.Python['voicekit_process_event'] = function(block) {
- var statement_input = Blockly.Python.statementToCode(block,"STACK");
- var code = "def _process_event(self, event):\n"+
- " logging.info(event)\n"+ statement_input;
- return code
- }
- Blockly.Python['voicekit_process_eventType'] = function(block) {
- var code = "event.type";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_EventType_select'] = function(block) {
- var type = block.getFieldValue("TYPE")
- var code = "EventType."+ type;
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_can_start_conversation_self'] = function(block) {
- var code = "self._can_start_conversation";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_can_start_conversation'] = function(block) {
- var setting = block.getFieldValue("SETTING");
- var code = "self._can_start_conversation = "+setting+ "\n";
- return code;
- }
- Blockly.Python['voicekit_assistant_conversation'] = function(block) {
- var setting = block.getFieldValue("SETTING");
- var code = "self._assistant."+setting+ "\n";
- return code;
- }
- Blockly.Python['voicekit_event_args'] = function(block) {
- var code = "event.args";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
- Blockly.Python['voicekit_event_args_of'] = function(block) {
- var text = block.getFieldValue("TEXT")
- var code = "event.args['"+text+"']";
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
|