Beispiel #1
0
    public void InitializeSubscriptionSocket(string topic)
    {
        if (!subscriptionSocketForTopic.ContainsKey(topic))
        {
            subscriptionSocketForTopic.Add(topic, new SubscriberSocket(IPHeader + subport));
            subscriptionSocketForTopic[topic].Subscribe(topic);

            //André: Is this necessary??
            //			subscriptionSocketForTopic[topic].Options.SendHighWatermark = PupilSettings.numberOfMessages;// 6;

            subscriptionSocketForTopic[topic].ReceiveReady += (s, a) =>
            {
                int i = 0;

                NetMQMessage m = new NetMQMessage();

                while (a.Socket.TryReceiveMultipartMessage(ref m))
                {
                    // We read all the messages from the socket, but disregard the ones after a certain point
                    //				if ( i > PupilSettings.numberOfMessages ) // 6)
                    //					continue;

                    string msgType = m[0].ConvertToString();
                    mStream = new MemoryStream(m[1].ToByteArray());
                    byte[] thirdFrame = null;
                    if (m.FrameCount >= 3)
                    {
                        thirdFrame = m[2].ToByteArray();
                    }

                    if (PupilSettings.Instance.debug.printMessageType)
                    {
                        Debug.Log(msgType);
                    }

                    if (PupilSettings.Instance.debug.printMessage)
                    {
                        Debug.Log(MessagePackSerializer.ToJson(m[1].ToByteArray()));
                    }

                    if (PupilTools.ReceiveDataIsSet)
                    {
                        PupilTools.ReceiveData(msgType, MessagePackSerializer.Deserialize <Dictionary <string, object> >(mStream), thirdFrame);
                        // Uncomment if this is not the problem for skipping gazepoint
                        //the continue block subscribing to multiple topics
                        // continue;
                    }

                    switch (msgType)
                    {
                    case "notify.calibration.successful":
                        PupilTools.CalibrationFinished();
                        Debug.Log(msgType);
                        break;

                    case "notify.calibration.failed":
                        PupilTools.CalibrationFailed();
                        Debug.Log(msgType);
                        break;

                    case "gaze":
                    case "gaze.2d.0.":
                    case "gaze.2d.1.":
                    case "pupil.0":
                        var dictionary0 = MessagePackSerializer.Deserialize <Dictionary <string, object> >(mStream);
                        confidence0 = PupilTools.FloatFromDictionary(dictionary0, "confidence");
                        if (PupilTools.IsCalibrating)
                        {
                            string eyeID = PupilTools.StringFromDictionary(dictionary0, "id");
                            PupilTools.UpdateCalibrationConfidence(eyeID, confidence0);
                            break;
                        }
                        if ((confidence0 > confidenceThreshold) && msgType.StartsWith("gaze"))
                        {
                            PupilTools.gazeDictionary = dictionary0;
                        }

                        break;

                    case "pupil.1":
                        var dictionary = MessagePackSerializer.Deserialize <Dictionary <string, object> >(mStream);
                        confidence1 = PupilTools.FloatFromDictionary(dictionary, "confidence");
                        if (PupilTools.IsCalibrating)
                        {
                            string eyeID = PupilTools.StringFromDictionary(dictionary, "id");
                            PupilTools.UpdateCalibrationConfidence(eyeID, confidence1);
                            break;
                        }
                        if ((confidence1 > confidenceThreshold) && msgType.StartsWith("gaze"))
                        {
                            PupilTools.gazeDictionary = dictionary;
                        }

                        break;

                    case "frame.eye.0":
                    case "frame.eye.1":
                        break;

                    default:
                        Debug.Log(msgType);
                        break;
                    }

                    i++;
                }
            };
        }
    }