首页
|
新闻
|
新品
|
文库
|
方案
|
视频
|
下载
|
商城
|
开发板
|
数据中心
|
座谈新版
|
培训
|
工具
|
博客
|
论坛
|
百科
|
GEC
|
活动
|
主题月
|
电子展
注册
登录
论坛
博客
搜索
帮助
导航
默认风格
uchome
discuz6
GreenM
»
MCU 单片机技术
»
PowerPC
» 使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(7)
返回列表
回复
发帖
发新话题
发布投票
发布悬赏
发布辩论
发布活动
发布视频
发布商品
使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(7)
发短消息
加为好友
look_w
当前离线
UID
1066743
帖子
8283
精华
0
积分
4142
阅读权限
90
在线时间
233 小时
注册时间
2017-6-23
最后登录
2019-5-18
论坛元老
UID
1066743
1
#
打印
字体大小:
t
T
look_w
发表于 2018-1-13 19:08
|
只看该作者
使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(7)
实用程序函数最后,同样在 UIExt.swift 文件中,添加以下函数:
send() 函数播放一段声音,将发送的消息附加到消息数组中,调用 finishSendingMessage() 函数,然后调用 conversationRequestResponse() 函数来处理与 Watson Conversation 服务的交互。
1
2
3
4
5
6
7
8
func send(_ text: String) {
setAudioPortToSpeaker()
JSQSystemSoundPlayer.jsq_playMessageSentSound()
let message = JSQMessage(senderId: self.senderId, senderDisplayName: self.senderDisplayName, date: Date(), text: text)
self.messages.append(message!)
self.finishSendingMessage(animated: true)
self.conversationRequestResponse(text)
}
firstMessage() 函数返回第一条不是来自同一个发送者的消息,以便在文本气泡顶部指示发送者。
1
2
3
4
5
6
7
8
9
10
11
12
13
func firstMessage(at: IndexPath) -> JSQMessage! {
let message = self.messages[at.item]
if message.senderId == self.senderId {
return nil
}
if at.item - 1 > 0 {
let previousMessage = self.messages[at.item-1]
if previousMessage.senderId == message.senderId {
return nil
}
}
return message
}
didReceiveConversationResponse() 函数在 Watson Conversation 返回响应时调用。该函数播放一段声音,将消息附加到消息数组,并将响应分派给 Watson Text to Speech 服务来合成句子。最后,它会调用 finishReceiveMessage() 函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func didReceiveConversationResponse(_ response: [String]) {
let sentence = re,sponse.joined(separator: " ")
if sentence == "" { return }
setAudioPortToSpeaker()
JSQSystemSoundPlayer.jsq_playMessageReceivedSound()
let message = JSQMessage(senderId: "Home Assistant", senderDisplayName: "Home Assistant", date: Date(), text: sentence)
self.messages.append(message!)
DispatchQueue.main.async {
// text-to-speech synthesize
self.ttsSynthesize(sentence)
self.reloadMessagesView()
self.finishReceivingMessage(animated: true)
}
}
参阅 文件查看完整的实现(包含在 “获取代码” 部分中的文件中)。
连接 Watson 服务现在是时候实现与 Watson 服务的连接了。
准备 Watson SDK
安装 。必须使用 Carthage 完成此任务,而安装 Carthage 的一种方法是使用 (MacOS 的一个包管理器)。
安装 Homebrew。
1
/usr/bin/ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install
)"
安装 Carthage。
1
2
brew update
brew install carthage
在项目文件夹中创建一个 Cartfile 并添加下面这行代码。
1
github "https://github.com/watson-developer-cloud/swift-sdk"
安装 Swift SDK 框架。
1
carthage update --platform iOS
如果获得类似下面这样的错误:
1
Module compiled with Swift 3.1 cannot be imported in Swift 3.0.2
则使用此命令重新编译二进制文件:
1
carthage update --platform iOS --no-use-binaries
导航到
General > Linked Frameworks > Libraries
,以便将 Swift SDK 框架添加到项目中。单击 + 图标。
单击
Add Other
并导航到 Carthage/Build/iOS 文件夹。选择以下框架:TextToSpeechV1.framework、SpeechToTextV1.framework、ConversationV1.framework 和 RestKit.framework。
点击查看大图
完成后,您应该已添加以下框架。
将这些框架复制到您的应用程序中,以便可在运行时访问它们。转到
Build Phases
选项卡,单击 + 图标,然后选择
New Run Script Phase
。
在 Run Script 中,指定:
1
/usr/local/bin/carthage copy-frameworks
单击 + 图标指定以下输入文件。
1
2
3
4
$(SRCROOT)/Carthage/Build/iOS/TextToSpeechV1.framework
$(SRCROOT)/Carthage/Build/iOS/SpeechToTextV1.framework
$(SRCROOT)/Carthage/Build/iOS/ConversationV1.framework
$(SRCROOT)/Carthage/Build/iOS/RestKit.framew
最后,在 Info.plist 文件中输入以下行,作为连接到 Watson IoT Platform 的源代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>watsonplatform.net</key>
<dict>
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
<false/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
</dict>
</dict>
</dict>
收藏
分享
评分
回复
引用
订阅
TOP
返回列表
MCU 单片机技术
电商论坛
Pine A64
资料下载
方案分享
FAQ
行业应用
消费电子
便携式设备
医疗电子
汽车电子
工业控制
热门技术
智能可穿戴
3D打印
智能家居
综合设计
示波器技术
存储器
电子制造
计算机和外设
软件开发
分立器件
传感器技术
无源元件
资料共享
PCB综合技术
综合技术交流
EDA
MCU 单片机技术
ST MCU
Freescale MCU
NXP MCU
新唐 MCU
MIPS
X86
ARM
PowerPC
DSP技术
嵌入式技术
FPGA/CPLD可编程逻辑
模拟电路
数字电路
富士通半导体FRAM 铁电存储器“免费样片”使用心得
电源与功率管理
LED技术
测试测量
通信技术
3G
无线技术
微波在线
综合交流区
职场驿站
活动专区
在线座谈交流区
紧缺人才培训课程交流区
意见和建议