ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:34:48 2017 +0800
Revision:
45:2aa9f933c8d2
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 45:2aa9f933c8d2 1 /*
TMBOY 45:2aa9f933c8d2 2 * Copyright (c) 2017 Baidu, Inc. All Rights Reserved.
TMBOY 45:2aa9f933c8d2 3 *
TMBOY 45:2aa9f933c8d2 4 * Licensed under the Apache License, Version 2.0 (the "License");
TMBOY 45:2aa9f933c8d2 5 * you may not use this file except in compliance with the License.
TMBOY 45:2aa9f933c8d2 6 * You may obtain a copy of the License at
TMBOY 45:2aa9f933c8d2 7 *
TMBOY 45:2aa9f933c8d2 8 * http://www.apache.org/licenses/LICENSE-2.0
TMBOY 45:2aa9f933c8d2 9 *
TMBOY 45:2aa9f933c8d2 10 * Unless required by applicable law or agreed to in writing, software
TMBOY 45:2aa9f933c8d2 11 * distributed under the License is distributed on an "AS IS" BASIS,
TMBOY 45:2aa9f933c8d2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
TMBOY 45:2aa9f933c8d2 13 * See the License for the specific language governing permissions and
TMBOY 45:2aa9f933c8d2 14 * limitations under the License.
TMBOY 45:2aa9f933c8d2 15 */
TMBOY 45:2aa9f933c8d2 16 package com.baidu.duer.dcs.devicemodule.playbackcontroller;
TMBOY 45:2aa9f933c8d2 17
TMBOY 45:2aa9f933c8d2 18 import com.baidu.duer.dcs.devicemodule.alerts.AlertsDeviceModule;
TMBOY 45:2aa9f933c8d2 19 import com.baidu.duer.dcs.devicemodule.system.HandleDirectiveException;
TMBOY 45:2aa9f933c8d2 20 import com.baidu.duer.dcs.framework.BaseDeviceModule;
TMBOY 45:2aa9f933c8d2 21 import com.baidu.duer.dcs.framework.IMessageSender;
TMBOY 45:2aa9f933c8d2 22 import com.baidu.duer.dcs.framework.IResponseListener;
TMBOY 45:2aa9f933c8d2 23 import com.baidu.duer.dcs.framework.message.ClientContext;
TMBOY 45:2aa9f933c8d2 24 import com.baidu.duer.dcs.framework.message.Directive;
TMBOY 45:2aa9f933c8d2 25 import com.baidu.duer.dcs.framework.message.Event;
TMBOY 45:2aa9f933c8d2 26 import com.baidu.duer.dcs.framework.message.Header;
TMBOY 45:2aa9f933c8d2 27 import com.baidu.duer.dcs.framework.message.MessageIdHeader;
TMBOY 45:2aa9f933c8d2 28 import com.baidu.duer.dcs.framework.message.Payload;
TMBOY 45:2aa9f933c8d2 29 import com.baidu.duer.dcs.systeminterface.IPlaybackController;
TMBOY 45:2aa9f933c8d2 30
TMBOY 45:2aa9f933c8d2 31 /**
TMBOY 45:2aa9f933c8d2 32 * 音频播放控制
TMBOY 45:2aa9f933c8d2 33 * <p>
TMBOY 45:2aa9f933c8d2 34 * 用户按了端上的播放/暂停等控制按钮,或者通过端上的GUI进行了此类操作时,上报PlayCommandIssued、PauseCommandIssued等事件
TMBOY 45:2aa9f933c8d2 35 * <p>
TMBOY 45:2aa9f933c8d2 36 * Created by wuruisheng on 2017/5/31.
TMBOY 45:2aa9f933c8d2 37 */
TMBOY 45:2aa9f933c8d2 38 public class PlaybackControllerDeviceModule extends BaseDeviceModule {
TMBOY 45:2aa9f933c8d2 39 private AlertsDeviceModule mAlertsDeviceModule;
TMBOY 45:2aa9f933c8d2 40
TMBOY 45:2aa9f933c8d2 41 public enum PlaybackAction {
TMBOY 45:2aa9f933c8d2 42 PLAY,
TMBOY 45:2aa9f933c8d2 43 PAUSE,
TMBOY 45:2aa9f933c8d2 44 PREVIOUS,
TMBOY 45:2aa9f933c8d2 45 NEXT
TMBOY 45:2aa9f933c8d2 46 }
TMBOY 45:2aa9f933c8d2 47
TMBOY 45:2aa9f933c8d2 48 public PlaybackControllerDeviceModule(IPlaybackController playback, IMessageSender messageSender,
TMBOY 45:2aa9f933c8d2 49 AlertsDeviceModule alertsDeviceModule) {
TMBOY 45:2aa9f933c8d2 50 super(ApiConstants.NAMESPACE, messageSender);
TMBOY 45:2aa9f933c8d2 51 this.mAlertsDeviceModule = alertsDeviceModule;
TMBOY 45:2aa9f933c8d2 52 playback.registerPlaybackListener(new IPlaybackController.IPlaybackListener() {
TMBOY 45:2aa9f933c8d2 53 @Override
TMBOY 45:2aa9f933c8d2 54 public void onPlay(IResponseListener responseListener) {
TMBOY 45:2aa9f933c8d2 55 handlePlaybackAction(PlaybackAction.PLAY, responseListener);
TMBOY 45:2aa9f933c8d2 56 }
TMBOY 45:2aa9f933c8d2 57
TMBOY 45:2aa9f933c8d2 58 @Override
TMBOY 45:2aa9f933c8d2 59 public void onPause(IResponseListener responseListener) {
TMBOY 45:2aa9f933c8d2 60 handlePlaybackAction(PlaybackAction.PAUSE, responseListener);
TMBOY 45:2aa9f933c8d2 61 }
TMBOY 45:2aa9f933c8d2 62
TMBOY 45:2aa9f933c8d2 63 @Override
TMBOY 45:2aa9f933c8d2 64 public void onPrevious(IResponseListener responseListener) {
TMBOY 45:2aa9f933c8d2 65 handlePlaybackAction(PlaybackAction.PREVIOUS, responseListener);
TMBOY 45:2aa9f933c8d2 66 }
TMBOY 45:2aa9f933c8d2 67
TMBOY 45:2aa9f933c8d2 68 @Override
TMBOY 45:2aa9f933c8d2 69 public void onNext(IResponseListener responseListener) {
TMBOY 45:2aa9f933c8d2 70 handlePlaybackAction(PlaybackAction.NEXT, responseListener);
TMBOY 45:2aa9f933c8d2 71 }
TMBOY 45:2aa9f933c8d2 72 });
TMBOY 45:2aa9f933c8d2 73 }
TMBOY 45:2aa9f933c8d2 74
TMBOY 45:2aa9f933c8d2 75 @Override
TMBOY 45:2aa9f933c8d2 76 public ClientContext clientContext() {
TMBOY 45:2aa9f933c8d2 77 return null;
TMBOY 45:2aa9f933c8d2 78 }
TMBOY 45:2aa9f933c8d2 79
TMBOY 45:2aa9f933c8d2 80 @Override
TMBOY 45:2aa9f933c8d2 81 public void handleDirective(Directive directive) throws HandleDirectiveException {
TMBOY 45:2aa9f933c8d2 82 }
TMBOY 45:2aa9f933c8d2 83
TMBOY 45:2aa9f933c8d2 84 @Override
TMBOY 45:2aa9f933c8d2 85 public void release() {
TMBOY 45:2aa9f933c8d2 86 }
TMBOY 45:2aa9f933c8d2 87
TMBOY 45:2aa9f933c8d2 88 private void handlePlaybackAction(PlaybackAction action, IResponseListener responseListener) {
TMBOY 45:2aa9f933c8d2 89 switch (action) {
TMBOY 45:2aa9f933c8d2 90 case PLAY:
TMBOY 45:2aa9f933c8d2 91 if (mAlertsDeviceModule.hasActiveAlerts()) {
TMBOY 45:2aa9f933c8d2 92 mAlertsDeviceModule.stopActiveAlert();
TMBOY 45:2aa9f933c8d2 93 } else {
TMBOY 45:2aa9f933c8d2 94 Event event = createPlaybackControllerEvent(ApiConstants.Events.PlayCommandIssued.NAME);
TMBOY 45:2aa9f933c8d2 95 messageSender.sentEventWithClientContext(event, responseListener);
TMBOY 45:2aa9f933c8d2 96 }
TMBOY 45:2aa9f933c8d2 97 break;
TMBOY 45:2aa9f933c8d2 98 case PAUSE:
TMBOY 45:2aa9f933c8d2 99 if (mAlertsDeviceModule.hasActiveAlerts()) {
TMBOY 45:2aa9f933c8d2 100 mAlertsDeviceModule.stopActiveAlert();
TMBOY 45:2aa9f933c8d2 101 } else {
TMBOY 45:2aa9f933c8d2 102 Event event = createPlaybackControllerEvent(ApiConstants.Events.PauseCommandIssued.NAME);
TMBOY 45:2aa9f933c8d2 103 messageSender.sentEventWithClientContext(event, responseListener);
TMBOY 45:2aa9f933c8d2 104 }
TMBOY 45:2aa9f933c8d2 105 break;
TMBOY 45:2aa9f933c8d2 106 case PREVIOUS:
TMBOY 45:2aa9f933c8d2 107 Event event = createPlaybackControllerEvent(ApiConstants.Events.PreviousCommandIssued.NAME);
TMBOY 45:2aa9f933c8d2 108 messageSender.sentEventWithClientContext(event, responseListener);
TMBOY 45:2aa9f933c8d2 109 break;
TMBOY 45:2aa9f933c8d2 110 case NEXT:
TMBOY 45:2aa9f933c8d2 111 Event eventNext = createPlaybackControllerEvent(ApiConstants.Events.NextCommandIssued.NAME);
TMBOY 45:2aa9f933c8d2 112 messageSender.sentEventWithClientContext(eventNext, responseListener);
TMBOY 45:2aa9f933c8d2 113 break;
TMBOY 45:2aa9f933c8d2 114 default:
TMBOY 45:2aa9f933c8d2 115 break;
TMBOY 45:2aa9f933c8d2 116 }
TMBOY 45:2aa9f933c8d2 117 }
TMBOY 45:2aa9f933c8d2 118
TMBOY 45:2aa9f933c8d2 119 private Event createPlaybackControllerEvent(String name) {
TMBOY 45:2aa9f933c8d2 120 Header header = new MessageIdHeader(ApiConstants.NAMESPACE, name);
TMBOY 45:2aa9f933c8d2 121 return new Event(header, new Payload());
TMBOY 45:2aa9f933c8d2 122 }
TMBOY 45:2aa9f933c8d2 123 }