예제 #1
0
        string jsonFromCues()
        {
            //setting up a new episode
            CHEpisode episode = new CHEpisode();

            episode.episodeNumber = 0;
            episode.label         = "Act 2";
            episode.cues          = new List <CHMessage>();

            //adding all the cues to episode.cues
            soundCues.ForEach(cue => {
                CHMessage soundCue   = new CHMessage();
                CHMessage cueDetails = soundCue.FromSoundCue(cue);
                episode.cues.Add(cueDetails);
            });


            videoCues.ForEach(cue => {
                CHMessage videoCue   = new CHMessage();
                CHMessage cueDetails = videoCue.FromVideoCue(cue);
                episode.cues.Add(cueDetails);
            });

            imageCues.ForEach(cue => {
                CHMessage imageCue   = new CHMessage();
                CHMessage cueDetails = imageCue.FromImageCue(cue);
                episode.cues.Add(cueDetails);
            });
            //server requires an array of episodes
            episodesArray = new List <CHEpisode>();
            episodesArray.Add(episode);

            //convert episode to JSON
            return(JsonMapper.ToJson(episodesArray));
        }
예제 #2
0
        //void OnRemoteNotificationReceived(UnityEngine.iOS.RemoteNotification n10n) {
        //  Debug.Log("received remote notification in CHSession: ");
        //  Debug.Log("    " + n10n.alertTitle + ": " + n10n.alertBody);
        //  if (n10n.userInfo.Contains("cohortMessage")) {
        //    Debug.Log("n10n had a cohortMessage, processing");
        //    Hashtable msgHashtable = (Hashtable)n10n.userInfo["cohortMessage"];
        //    // process int64s
        //    msgHashtable["mediaDomain"] = System.Convert.ToInt32(msgHashtable["mediaDomain"]);
        //    msgHashtable["cueAction"] = System.Convert.ToInt32(msgHashtable["cueAction"]);

        //    ValidateCohortMessage(msgHashtable);
        //  } else {
        //    Debug.Log("notification had no cohortMessage, displaying text");
        //    // minor hack to mirror notification text in the text cue display area
        //    onTextCue(CueAction.play, n10n.alertBody);
        //    if(n10n.soundName != "default.caf") {
        //      soundCues.ForEach(cue => {
        //        if(cue.audioClip.name == n10n.soundName) {
        //          audioPlayer.clip = cue.audioClip;
        //          audioPlayer.Play();
        //          return;
        //        }
        //      });
        //    }
        //  }
        //}

        void ValidateCohortMessage(Hashtable msgHashtable)
        {
            CHMessage msg = new CHMessage();

            msg.cueNumber = System.Convert.ToSingle(msgHashtable["cueNumber"]);

            //Debug.Log("processing tags");
            ArrayList     tagsFromMsgHashtable = (ArrayList)msgHashtable["targetTags"];
            List <string> tags = new List <string>();

            for (int i = 0; i < tagsFromMsgHashtable.Count; i++)
            {
                string oneTag = tagsFromMsgHashtable[i].ToString();
                tags.Add(oneTag);
            }
            Debug.Log(string.Join(", ", tags.ToArray()));
            msg.targetTags = tags;

            msg.mediaDomain = (MediaDomain)msgHashtable["mediaDomain"];

            msg.cueAction = (CueAction)msgHashtable["cueAction"];

            if (msgHashtable.ContainsKey("id"))
            {
                msg.id = (int)msgHashtable["id"];
            }
            else
            {
                msg.id = -1;
            }


            // hacks related to text cues and notifications

            if (System.Math.Abs(msg.cueNumber) < System.Single.Epsilon && msgHashtable.ContainsKey("cueContent"))
            {
                Debug.Log("creating extemporaneous text cue to match cohort message");
                CHTextCue extemporaneousTextCue = new CHTextCue((string)msgHashtable["cueContent"].ToString());
                float     highestTextCueNumber  = 0;
                foreach (CHTextCue cue in textCues)
                {
                    if (cue.cueNumber > highestTextCueNumber)
                    {
                        highestTextCueNumber = cue.cueNumber;
                    }
                }
                Debug.Log("highest text cue number: " + highestTextCueNumber);
                float newTextCueNumber = highestTextCueNumber + 1;
                extemporaneousTextCue.cueNumber = newTextCueNumber;
                msg.cueNumber = newTextCueNumber;
                textCues.Add(extemporaneousTextCue);
            }



            Debug.Log(CHMessage.FormattedMessage(msg));

            OnCohortMessageReceived(msg);
        }
예제 #3
0
        CHMessage cueToCHMessage(Cue cue)
        {
            CHMessage msg = new CHMessage();

            msg.mediaDomain = cue.mediaDomain;
            msg.cueNumber   = cue.cueNumber;
            msg.cueAction   = cue.cueAction;
            msg.targetTags  = cue.targetTags;

            return(msg);
        }
예제 #4
0
        public static string FormattedMessage(CHMessage msg)
        {
            string tagsForPrint = "";

            msg.targetTags.ForEach(tag => tagsForPrint = tagsForPrint + tag + ", ");

            return("    " + "cohort message: "
                   + "\n      tags:         " + tagsForPrint
                   + "\n      media domain: " + msg.mediaDomain
                   + "\n      cue #:        " + msg.cueNumber
                   + "\n      action:       " + msg.cueAction);
        }
예제 #5
0
        void OnWebSocketMessage(WebSocket cs, string msg)
        {
            Debug.Log(msg);
            JsonData message = JsonMapper.ToObject(msg);

            Debug.Log(message);
            if (message.Keys.Contains("response"))
            {
                CHSocketSuccessResponse res = JsonMapper.ToObject <CHSocketSuccessResponse>(msg);
                Debug.Log(res);
                if (res.response == "success")
                {
                    Debug.Log("opened websocket connnection");
                    string groupingStatus = "";
                    if (grouping != null & grouping != "")
                    {
                        groupingStatus = ", grouping: " + grouping;
                    }
                    onStatusChanged("Connected to Cohort (occasion id:" + occasion + groupingStatus + ")");
                    socketConnectionActive = true;
                    //connectionIndicator.SetActive(true);
                }
            }
            else
            // this is an ugly way to make sure it's a CHMessage, ugh
            if (message.Keys.Contains("mediaDomain") &&
                message.Keys.Contains("cueNumber") &&
                message.Keys.Contains("cueAction"))
            {
                CHMessage cohortMsg = JsonUtility.FromJson <CHMessage>(msg);
                //CHMessage cohortMsg = JsonMapper.ToObject<CHMessage>(msg);
                Debug.Log("received cohort message");
                Debug.Log(cohortMsg.mediaDomain);
                Debug.Log(cohortMsg.cueNumber);
                Debug.Log(cohortMsg.cueAction);
                OnCohortMessageReceived(cohortMsg);
            }
            else
            {
                Debug.Log("Warning: received non-Cohort message, taking no action");
                Debug.Log(message.Keys);
            }
        }
예제 #6
0
        /*
         *   Cohort handlers
         */

        void OnCohortMessageReceived(CHMessage msg)
        {
            if (msg.id > 0)
            {
                PlayerPrefs.SetInt("lastReceivedCohortMessageId", msg.id);
            }

            Debug.Log("current grouping: " + grouping);
            if (!msg.targetTags.Contains(grouping) && !msg.targetTags.Contains("all"))
            {
                Debug.Log("cohort message is for another grouping (not " + grouping + "), not processing it");
                return;
            }

            // DO STUFF
            switch (msg.mediaDomain)
            {
            case MediaDomain.sound:
                CHSoundCue soundCue = soundCues.Find((CHSoundCue matchingCue) => System.Math.Abs(matchingCue.cueNumber - msg.cueNumber) < 0.00001);
                if (soundCue != null)
                {
                    audioPlayer.clip = soundCue.audioClip;
                }

                // looped cues
                //if(Math.Abs(msg.cueNumber - 4) < Mathf.Epsilon) {
                //  if(msg.cueAction == CueAction.play) {
                //    audioPlayer.loop = true;
                //  } else {
                //    audioPlayer.loop = false;
                //  }
                //}

                switch (msg.cueAction)
                {
                case CueAction.play:
                    audioPlayer.Play();
                    break;

                case CueAction.pause:
                    audioPlayer.Pause();
                    break;

                case CueAction.restart:
                    audioPlayer.Pause();
                    audioPlayer.time = 0;
                    audioPlayer.Play();
                    break;

                case CueAction.stop:
                    audioPlayer.Stop();
                    audioPlayer.clip = null;
                    break;
                }
                break;

            case MediaDomain.video:
                CHVideoCue videoCue = videoCues.Find((CHVideoCue matchingCue) => System.Math.Abs(matchingCue.cueNumber - msg.cueNumber) < 0.00001);
                if (videoCue != null)
                {
                    if (videoPlayer.clip != videoCue.videoClip)
                    {
                        videoPlayer.Pause();
                        videoPlayer.clip = videoCue.videoClip;
                    }

                    switch (msg.cueAction)
                    {
                    case CueAction.play:
                        videoPlayer.Play();
                        break;

                    case CueAction.pause:
                        videoPlayer.Pause();
                        break;

                    case CueAction.restart:
                        videoPlayer.Pause();
                        videoPlayer.time = 0;
                        videoPlayer.Play();
                        break;

                    case CueAction.stop:
                        videoPlayer.Stop();
                        videoPlayer.clip = nullVideo;
                        videoPlayer.Play();
                        break;

                    default:
                        Debug.Log("Error: cue action not implemented");
                        break;
                    }
                    break;
                }
                else
                {
                    Debug.Log("Error: cue number not valid");
                }
                break;

            case MediaDomain.image:
                CHImageCue imageCue = imageCues.Find((CHImageCue matchingCue) => System.Math.Abs(matchingCue.cueNumber - msg.cueNumber) < 0.00001);
                if (imageCue != null)
                {
                    switch (msg.cueAction)
                    {
                    case CueAction.play:
                        Debug.Log("got image cue");
                        imageCueSurface.sprite = imageCue.image;
                        imageCueSurface.gameObject.SetActive(true);
                        break;

                    case CueAction.stop:
                        imageCueSurface.gameObject.SetActive(false);
                        imageCueSurface.sprite = null;
                        break;

                    default:
                        Debug.Log("Error: cue action not implemented");
                        break;
                    }
                }
                else
                {
                    Debug.Log("Error: cue number not valid");
                }
                break;

            case MediaDomain.text:
                CHTextCue textCue = textCues.Find((CHTextCue matchingCue) => System.Math.Abs(matchingCue.cueNumber - msg.cueNumber) < 0.00001);
                if (textCue == null && msg.cueContent == null)
                {
                    return;
                }
                string textOfCue;
                if (msg.cueContent != null)
                {
                    textOfCue = msg.cueContent;
                }
                else if (textCue.text != null)
                {
                    textOfCue = textCue.text;
                }
                else
                {
                    Debug.Log("Error: Failed to find text for text cue in onboard text cues or in remote cue");
                    return;
                }

                switch (msg.cueAction)
                {
                case CueAction.play:
                    UnityEngine.Handheld.Vibrate();
                    onTextCue(CueAction.play, textOfCue);
                    break;

                case CueAction.pause:
                    Debug.Log("action 'pause' is not defined for text cues");
                    break;

                case CueAction.restart:
                    Debug.Log("action 'restart' is not defined for text cues");
                    break;

                case CueAction.stop:
                    onTextCue(CueAction.stop, textOfCue);
                    break;
                }
                break;

            //case MediaDomain.light:
            //  switch(msg.cueAction) {
            //    case CueAction.play:
            //      flashlightController.TurnOn();
            //      break;
            //    case CueAction.stop:
            //      flashlightController.TurnOff();
            //      break;
            //    case CueAction.pause:
            //      flashlightController.TurnOff();
            //      break;
            //    case CueAction.restart:
            //      Debug.Log("action 'restart' is not defined for light cues");
            //      break;
            //  }
            //  break;

            case MediaDomain.haptic:
                UnityEngine.Handheld.Vibrate();
                break;
            }
        }