本帖最后由 johnnyit 于 2020-11-9 18:11 编辑
通过小爱音箱app获取设备状态以及设备控制指令。通过这种方式有以下优点:
1.不需要破解小爱音箱设备。(不需要shh)
2.不需要在小爱音箱设备内写入脚本。3.音箱可以随意升级,不受设备升级影响。
缺点:
1.需要长时间刷新服务端状态,资源消耗较大。
主要代码内容:
这里讲下代码实现的思路:
1.登录小爱app
2.时时获取设备语音控制内容
3.分析最新语音指令 匹配拦截词,key_arr
4.获取到匹配词发送暂停播放指令,取消执行失败的播放操作。
5.发送匹配词到本地TTS服务器执行相关指令。
6.播放执行指令内容。
发送数据方法:
def sned_to_cmd(self,message_json,path,method):
bodydata ={
"deviceId":"xxxxxxxxxxxxxxxxxxxx",
"message":message_json,
"method":method,
"path":path,
"requestId":'xxxxxxxxxxxxxxxxxxxxxxxxxx',
}
code_url = 'https://api.mina.mi.com/remote/ubus?'+urllib.urlencode(bodydata)
htt_type = 'post'
data =''
try :
app_txt=self.request.get_appdata(code_url,htt_type,self.head_data,data,self.cookie,self.host)
data_reqa = app_txt[1]
except Exception,e:
print e
return data_reqa
获取控制状态代码:
send = send_to_login(ipport,cookie,start_time,end_time)
def play():
name = 'info','播放'
message_json ='{"action":"play","media":"app_ios"}'
path = 'mediaplayer'
method = 'player_play_operation'
send.sned_to_cmd(message_json,path,method)
def pause():
name = 'info','暂停'
message_json ='{"action":"pause","media":"app_ios"}'
path = 'mediaplayer'
method = 'player_play_operation'
send.sned_to_cmd(message_json,path,method)
def play_status():
name = 'info','播放状态'
message_json ='{}'
path = 'mediaplayer'
method = 'player_get_play_status'
return send.sned_to_cmd(message_json,path,method)
def nlp_get_data():
name = 'info','控制记录'
message_json ='{}'
path = 'mibrain'
method = 'nlp_result_get'
return send.sned_to_cmd(message_json,path,method)
def nlp_get_json():
data_reqa = nlp_get_data()
data_reqa = unicode(data_reqa, "utf-8")
id_data = json.loads(data_reqa)
data_s = id_data['data']['info']
data_key = json.loads(data_s)
nlp_txt = data_key['result'][0]['nlp']
nlp_txt = json.loads(nlp_txt)
return [nlp_txt['response']['answer'][0]['intention']['query'],nlp_txt['meta']['timestamp']]
da_tamp_list =[]
while 1:
#da = nlp_txt['response']['answer'][0]['intention']['query']
da = nlp_get_json()
da_key = da[0]
da_tamp = da[1]
#print da_key
#print da_tamp
key_arr = ['关闭窗帘','打开窗帘','关闭']
for key in key_arr:
#print da_key.encode('utf-8')
if key in da_key.encode('utf-8'):
if da_tamp not in da_tamp_list:
pause()
da_tamp_list.append(da_tamp);
print ('你好,'+key)
send.sned_to_message('你好,'+key)
send.sned_to_host_tts(key)
else:
pass
代码作为功能实现的参考。
|