方案設計
本方案中採用HaaS600進行播報音箱設備端開發,HaaS600是基於移遠EC100Y-CN通信模組的LTE Cat 1開發板,專為M2M 和IoT應用而設計,可應用於共享控制、金融支付、智能語音、泛工業等場景的智能硬體產品開發。
軟體方案設計如下:
應用開發
連接雲平臺
使用JS輕應用的IoT API,傳入三元組信息,即可快速建立和雲端的連接。
示例:
var iot = require('iot');const productkey = '<product-key>';const devicename = '<device-name>';const devicesecret = '<device-secret>';var iotdev = iot.device({ productKey: productkey, deviceName: devicename, deviceSecret: devicesecret, success: function() { console.log('success connect to aliyun iot server'); }, fail: function() { console.log('fail to connect to aliyun iot server'); }});接收雲端播報消息(類型為service),示例:
iotdev.on('service', function(serviceid, request) { console.log('received cloud serviceid is ' + serviceid + '\r\n'); console.log('received cloud request is ' + request + '\r\n');});播報語音 IoT輕應用提供音頻播放組件audioplayer,使用相關API可實現本地和在線音頻文件的播放及控制。示例:var audioplayer = require('audioplayer');var source = "/test.mp3"audioplayer.play(source);var sourceList = ["/test1.mp3", "/test2.mp3", "/test3.mp3"];audioplayer.listPlay(sourceList); 單個文件的播報,例如廣告、TTS合成語音等,可通過audioplayer.play()接口,傳入音頻文件地址(支持本地文件以及http、https網絡音頻) 多個文件拼接組合播報,例如金額的拼接,將需要拼接播放的音頻文件存放在數組中,通過audioplayer.listPlay()接口,將音頻文件進行拼接和播放。按鍵處理
var gpio = require('gpio'); var led_network = gpio.open({ id: 'led_network'}); var key_function = gpio.open({ id: 'key_function'}); var key_volumeup = gpio.open({ id: 'key_volumeup'}); var key_volumedown = gpio.open({ id: 'key_volumedown'}); key_function.onIRQ({ trigger: 'rising', cb: function() { console.log('key function pressed'); }}); key_volumeup.onIRQ({ trigger: 'rising', cb: function() { console.log('key volumeup pressed'); }}); key_volumedown.onIRQ({ trigger: 'rising', cb: function() { console.log('key volumedown pressed'); }});低功耗
當系統空閒時自動進入低功耗狀態。
var pm = require('pm');pm.setAutosleepMode(1)雲端物模型
播報金額物模型:
{ speechs:["alipay","{$100}","yuan"], id:"123", timestamp:"1595765968612"}定義:
id: 消息id,用於判斷是否是重複推送
timestamp:交易時間
speechs:表示需要拼接的內容,有3類
1. {$+數字}:表示按照金額進行播放
2. {N+數字}或{n+數字}:表示按照數字進行播放
3. 其它:表示需要播放的語料的標識 播放
播放音頻連結物模型:
{ url:"http://*********", id:"123",}定義:
url:音頻內容url,設備端收到後通過該url下載並播放
id: 編號,用於判斷是否是重複推送
本地語料更新(SpeechPost){ speechs:[{"id":"test","url":"http://********"}], jobcode:"123" }欄位:
speechs:需要更新的語料列表,每個元素包括id和url,其中id表示語料標識、url是語料下載地址
jobcode:表示語料更新任務id,用於雲端和設備同步語料更新任務執行狀態
功能實現
連接雲平臺var iot = require('iot');var iotdev = iot.device({ productKey: productkey, deviceName: devicename, deviceSecret: devicesecret, success: function() { console.log('success connect to aliyun iot server'); }, fail: function() { console.log('fail to connect to aliyun iot server'); }});執行完成後,和雲端的連接通道建立。
物模型處理
iotdev.on('service', function(serviceid, request) { console.log('received cloud serviceid is ' + serviceid + '\r\n'); console.log('received cloud request is ' + request + '\r\n'); if (serviceid.indexOf("AudioPlayback") != -1) { voiceboxPlayContent(request); } else if (serviceid.indexOf("SpeechBroadcast") != -1) { voiceboxPlayReceipt(request); } else if (serviceid.indexOf("SpeechPost") != -1) { voiceboxResUpdate(request); }});語音拼接
數字拼接:
按照普通數字發音規則進行拼接,例如手機號
按照金額類數字發音規則進行拼接,例如收款金額
本地語音拼接
通過音頻標識查找本地文件
拼接結果,例如:
{ speechs:["alipay","{$100}","yuan"], id:123, timestamp:"1595765968612"}拼接後:
語料更新下載並更新本地語料文件
function voiceboxResUpdate(resource){ var resource = eval('(' + resource + ')'); var speechArray = resource.speechs; for (var speechIndex = 0; speechIndex < speechArray.length; speechIndex++) { var speech = speechArray[speechIndex]; console.log('update local speech id: ' + speech.id + ', url: ' + speech.url); var resourcePath = toneDir + speech.id + tonenameSuffix; http.download({ url: speech.url, filepath: resourcePath, method: 'GET', headers: { 'Accept':'*/*' }, success: function (data) { if(data === defaultMessage) { console.log('http: [success] http.download'); } } }); }}開發者技術支持
如需更多技術支持,可加入釘釘開發者群