Exemple #1
0
        public void OnGazeApiResponse(String response)
        {
            var reply = JsonConvert.DeserializeObject <ReplyBase>(response);

            if (reply.StatusCode == (int)HttpStatusCode.OK)
            {
                switch (reply.Category)
                {
                case Protocol.CATEGORY_TRACKER:

                    if (reply.Request.Equals(Protocol.TRACKER_REQUEST_GET))
                    {
                        var jsreader = new JsonTextReader(new StringReader(response));
                        var json     = (JObject) new JsonSerializer().Deserialize(jsreader);

                        JObject values = json[Protocol.KEY_VALUES].ToObject <JObject>();
                        JToken  value;

                        if (null != values)
                        {
                            if (values.TryGetValue(Protocol.TRACKER_VERSION, out value))
                            {
                                version = value.ToObject <ApiVersion>();
                            }

                            if (values.TryGetValue(Protocol.TRACKER_MODE_PUSH, out value))
                            {
                                if ((bool)value)
                                {
                                    clientMode = ClientMode.Push;
                                }
                                else
                                {
                                    clientMode = ClientMode.Pull;
                                }
                            }

                            if (values.TryGetValue(Protocol.TRACKER_HEARTBEATINTERVAL, out value))
                            {
                                HeartbeatMillis = (int)value;
                            }

                            if (values.TryGetValue(Protocol.TRACKER_FRAMERATE, out value))
                            {
                                Framerate = value.ToObject <FrameRate>();
                            }

                            if (values.TryGetValue(Protocol.TRACKER_TRACKERSTATE, out value))
                            {
                                //if tracker state changed, notify listeners
                                if ((int)value != (int)Trackerstate.GetTypeCode())
                                {
                                    Trackerstate = (TrackerState)(int)value;

                                    lock (((ICollection)trackerStateListeners).SyncRoot)
                                    {
                                        foreach (ITrackerStateListener listener in trackerStateListeners)
                                        {
                                            try
                                            {
                                                listener.OnTrackerStateChanged(Trackerstate);
                                            }
                                            catch (Exception e)
                                            {
                                                Debug.WriteLine("Exception while calling ITrackerStateListener.OnTrackerStateChanged() on listener " + listener + ": " + e.StackTrace);
                                            }
                                        }
                                    }
                                }
                            }

                            if (values.TryGetValue(Protocol.TRACKER_CALIBRATIONRESULT, out value))
                            {
                                LastCalibrationResult = value.ToObject <CalibrationResult>();
                            }

                            if (values.TryGetValue(Protocol.TRACKER_ISCALIBRATING, out value))
                            {
                                IsCalibrating = (bool)value;
                            }

                            if (values.TryGetValue(Protocol.TRACKER_ISCALIBRATED, out value))
                            {
                                //if calibration state changed, notify listeners
                                if ((bool)value != IsCalibrated)
                                {
                                    IsCalibrated = (bool)value;

                                    lock (((ICollection)calibrationStateListeners).SyncRoot)
                                    {
                                        foreach (ICalibrationResultListener listener in calibrationStateListeners)
                                        {
                                            try
                                            {
                                                listener.OnCalibrationChanged(IsCalibrated, LastCalibrationResult);
                                            }
                                            catch (Exception e)
                                            {
                                                Debug.WriteLine("Exception while calling ICalibrationResultListener.OnCalibrationChanged() on listener " + listener + ": " + e.StackTrace);
                                            }
                                        }
                                    }
                                }
                            }

                            if (values.TryGetValue(Protocol.TRACKER_SCREEN_RESOLUTION_WIDTH, out value))
                            {
                                ScreenResolutionWidth = (int)value;
                            }

                            if (values.TryGetValue(Protocol.TRACKER_SCREEN_RESOLUTION_HEIGHT, out value))
                            {
                                ScreenResolutionHeight = (int)value;
                            }

                            if (values.TryGetValue(Protocol.TRACKER_SCREEN_PHYSICAL_WIDTH, out value))
                            {
                                ScreenPhysicalWidth = (int)value;
                            }

                            if (values.TryGetValue(Protocol.TRACKER_SCREEN_PHYSICAL_HEIGHT, out value))
                            {
                                ScreenPhysicalHeight = (int)value;
                            }

                            if (values.TryGetValue(Protocol.TRACKER_SCREEN_INDEX, out value))
                            {
                                //if screen index changed, notify listeners
                                if ((int)value != ScreenIndex)
                                {
                                    ScreenIndex = (int)value;

                                    lock (((ICollection)trackerStateListeners).SyncRoot)
                                    {
                                        foreach (ITrackerStateListener listener in trackerStateListeners)
                                        {
                                            try
                                            {
                                                listener.OnScreenStatesChanged(ScreenIndex, ScreenResolutionWidth, ScreenResolutionHeight, ScreenPhysicalWidth, ScreenPhysicalHeight);
                                            }
                                            catch (Exception e)
                                            {
                                                Debug.WriteLine("Exception while calling ITrackerStateListener.OnScreenStatesChanged() on listener " + listener + ": " + e.StackTrace);
                                            }
                                        }
                                    }
                                }
                            }

                            if (values.TryGetValue(Protocol.TRACKER_FRAME, out value) && null != gazeBroadcaster)
                            {
                                //Add gaze update to high frequency broadcasting queue
                                lock (((ICollection)queueGazeData).SyncRoot)
                                {
                                    queueGazeData.Enqueue(value.ToObject <GazeData>());
                                }

                                events.GetUpdateHandle().Set();
                            }
                        }

                        //Handle initialization
                        if (initializationLock != null)
                        {
                            lock (initializationLock)
                            {
                                Monitor.Pulse(initializationLock);
                                initializationLock = null;
                            }
                        }
                    }
                    else if (reply.Request.Equals(Protocol.TRACKER_REQUEST_SET))
                    {
                        //Do nothing
                    }
                    break;

                case Protocol.CATEGORY_CALIBRATION:

                    switch (reply.Request)
                    {
                    case Protocol.CALIBRATION_REQUEST_START:

                        IsCalibrating = true;

                        if (null != calibrationListener)
                        {
                            //Notify calibration listener that a new calibration process was successfully started
                            try
                            {
                                calibrationListener.OnCalibrationStarted();
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine("Exception while calling ICalibrationProcessHandler.OnCalibrationStarted() on listener " + calibrationListener + ": " + e.StackTrace);
                            }
                        }

                        break;

                    case Protocol.CALIBRATION_REQUEST_POINTSTART:
                        break;

                    case Protocol.CALIBRATION_REQUEST_POINTEND:

                        ++sampledCalibrationPoints;

                        if (null != calibrationListener)
                        {
                            //Notify calibration listener that a new calibration point has been sampled
                            try
                            {
                                calibrationListener.OnCalibrationProgress(sampledCalibrationPoints / totalCalibrationPoints);
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine("Exception while calling ICalibrationProcessHandler.OnCalibrationProgress() on listener " + calibrationListener + ": " + e.StackTrace);
                            }


                            if (sampledCalibrationPoints == totalCalibrationPoints)
                            {
                                //Notify calibration listener that all calibration points have been sampled and the analysis of the calirbation results has begun
                                try
                                {
                                    calibrationListener.OnCalibrationProcessing();
                                }
                                catch (Exception e)
                                {
                                    Debug.WriteLine("Exception while calling ICalibrationProcessHandler.OnCalibrationProcessing() on listener " + calibrationListener + ": " + e.StackTrace);
                                }
                            }
                        }

                        var cper = JsonConvert.DeserializeObject <CalibrationPointEndReply>(response);

                        if (cper == null || cper.Values.CalibrationResult == null)
                        {
                            break;         // not done with calibration yet
                        }
                        //if calibration state changed, notify listeners
                        if (cper.Values.CalibrationResult.Result != IsCalibrated)
                        {
                            lock (((ICollection)calibrationStateListeners).SyncRoot)
                            {
                                foreach (ICalibrationResultListener listener in calibrationStateListeners)
                                {
                                    try
                                    {
                                        listener.OnCalibrationChanged(cper.Values.CalibrationResult.Result, cper.Values.CalibrationResult);
                                    }
                                    catch (Exception e)
                                    {
                                        Debug.WriteLine("Exception while calling ICalibrationStateListener.OnCalibrationChanged() on listener " + listener + ": " + e.StackTrace);
                                    }
                                }
                            }
                        }

                        IsCalibrated          = cper.Values.CalibrationResult.Result;
                        IsCalibrating         = !cper.Values.CalibrationResult.Result;
                        LastCalibrationResult = cper.Values.CalibrationResult;

                        if (null != calibrationListener)
                        {
                            //Notify calibration listener that calibration results are ready for evaluation
                            try
                            {
                                calibrationListener.OnCalibrationResult(cper.Values.CalibrationResult);
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine("Exception while calling ICalibrationProcessHandler.OnCalibrationResult() on listener " + calibrationListener + ": " + e.StackTrace);
                            }
                        }
                        break;

                    case Protocol.CALIBRATION_REQUEST_ABORT:
                        IsCalibrating = false;

                        //restore states of last calibration if any
                        apiManager.RequestCalibrationStates();
                        break;

                    case Protocol.CALIBRATION_REQUEST_CLEAR:
                        IsCalibrated          = false;
                        IsCalibrating         = false;
                        LastCalibrationResult = null;
                        break;
                    }
                    break;     // end calibration switch

                case Protocol.CATEGORY_HEARTBEAT:
                    //do nothing
                    break;

                default:
                    var rf = JsonConvert.DeserializeObject <ReplyFailed>(response);
                    Debug.WriteLine("Request FAILED");
                    Debug.WriteLine("Category: " + rf.Category);
                    Debug.WriteLine("Request: " + rf.Request);
                    Debug.WriteLine("StatusCode: " + rf.StatusCode);
                    Debug.WriteLine("StatusMessage: " + rf.Values.StatusMessage);
                    break;
                }
            }
            else
            {
                var rf = JsonConvert.DeserializeObject <ReplyFailed>(response);

                /*
                 * JSON Message status code is different from HttpStatusCode.OK. Check if special TET
                 * specific statuscode before handling error
                 */

                switch (rf.StatusCode)
                {
                case Protocol.STATUSCODE_CALIBRATION_UPDATE:
                    //The calibration state has changed, clients should update themselves
                    apiManager.RequestCalibrationStates();
                    break;

                case Protocol.STATUSCODE_SCREEN_UPDATE:
                    //The primary screen index has changed, clients should update themselves
                    apiManager.RequestScreenStates();
                    break;

                case Protocol.STATUSCODE_TRACKER_UPDATE:
                    //The connected Tracker Device has changed state, clients should update themselves
                    apiManager.RequestTrackerState();
                    break;

                default:
                    Debug.WriteLine("Request FAILED");
                    Debug.WriteLine("Category: " + rf.Category);
                    Debug.WriteLine("Request: " + rf.Request);
                    Debug.WriteLine("StatusCode: " + rf.StatusCode);
                    Debug.WriteLine("StatusMessage: " + rf.Values.StatusMessage);
                    break;
                }
            }
        }