使用Java在树莓派和Alexa Skill之间建立连接

时间:2018-06-29 17:01:28

标签: java python mysql raspberry-pi alexa-skill

我想使用Alexa和Java控制连接到树莓派的LED。我已按照本教程https://www.hackster.io/nishit-patel/controlling-raspberry-pi-using-alexa-33715b成功实现了此目标,但它使用的是python。但是实际上可以访问我的设备的属性,这些属性可在需要Java代码的MySQL数据库中获得。所以我试图将下面的python代码转换为java。我的主要问题是使用Java中的Flask Ask和Ngrok库,以及如何调用我的意图。如果您有任何想法请帮助? 预先感谢。

import logging
import os
import MySQLdb

from flaskext.mysql import MySQL
from flask import Flask, render_template
from flask_ask import Ask, request, session, question, statement
import RPi.GPIO as GPIO

db=MySQLdb.connect(host='xxx.xxx.xxx.x',user='xxxx',passwd='xxxxxx',db='xxxxx')
cursor=db.cursor()

app = Flask(name)
ask = Ask(app, "/")
logging.getLogger('flask_ask').setLevel(logging.DEBUG)

STATUSON = ['on','high']
STATUSOFF = ['off','low']

@ask.launch
def launch():
    speech_text = 'Welcome to Raspberry Pi Automation.'
    return question(speech_text).reprompt(speech_text).simple_card(speech_text)

@ask.intent('GPIOControlIntent', mapping = {'status':'status'})
def Gpio_Intent(status,room):
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17,GPIO.OUT)
    if status in STATUSON:
      GPIO.output(17,GPIO.HIGH)
      cursor.execute("INSERT INTO led (Etat) VALUES ('1')")
      db.commit()
      return statement('turning {} lights'.format(status))

    elif status in STATUSOFF:
      GPIO.output(17,GPIO.LOW)
      cursor.execute("INSERT INTO `led` (`Etat`) VALUES ('0')") 
      db.commit()
      return statement('turning {} lights'.format(status))
    else:
      return statement('Sorry not possible.')
@ask.intent('AMAZON.HelpIntent')
def help():
    speech_text = 'You can say hello to me!'
    return question(speech_text).reprompt(speech_text).simple_card('HelloWorld', 
    speech_text)

@ask.session_ended
def session_ended():
     return "{}", 200

if name == 'main':
     if 'ASK_VERIFY_REQUESTS' in os.environ:
       verify = str(os.environ.get('ASK_VERIFY_REQUESTS', '')).lower()
       if verify == 'false':
         app.config['ASK_VERIFY_REQUESTS'] = False
     app.run(debug=True)

0 个答案:

没有答案