首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(7)

使用 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>




返回列表