protected override async Task RunAsync(IWampChannel channel) { await channel.Open().ConfigureAwait(false); await Task.Yield(); IWampSubject heartbeatSubject = channel.RealmProxy.Services.GetSubject("com.myapp.heartbeat"); var topic2Subject = channel.RealmProxy.Services.GetSubject("com.myapp.topic2", new Topic2TupleConverter()); IObservable <int> timer = Observable.Timer(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(1)).Select((x, i) => i); Random random = new Random(); WampEvent emptyEvent = new WampEvent(); using (IDisposable heartbeatDisposable = timer.Select(value => emptyEvent).Subscribe(heartbeatSubject)) { (int number1, int number2, string c, MyClass d) Topic2Selector(int value) { return(number1 : random.Next(0, 100), number2 : 23, c : "Hello", d : new MyClass() { Counter = value, Foo = new int[] { 1, 2, 3 } }); } using (IDisposable topic2Disposable = timer.Select(value => Topic2Selector(value)).Subscribe(topic2Subject)) { Console.WriteLine("Hit enter to stop publishing"); Console.ReadLine(); } } Console.WriteLine("Stopped publishing"); Console.ReadLine(); }
async private void StreamBtn_Click(object sender, EventArgs e) { if (!IsStreamingToWAMP) { //maybe check if the connection before start wamp string CrossbarAddress = CrossbarIPTextBox.Text + "/ws"; string CrossbarRealm = RealmTextbox.Text; //This was the old code, might have to do some kind of hybrid depending on the url written. /*DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory(); * IWampChannel channel = channelFactory.CreateJsonChannel("wss://syn.ife.no/crossbarproxy/ws", CrossbarRealm); //CrossbarAddress * * channel.Open().Wait(); * //Task openTask = channel.Open() * //openTask.Wait(5000);*/ //if (CrossbarAddress.Contains("wss://")){ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //} WampChannelFactory factory = new WampChannelFactory(); IWampChannel channel = factory.ConnectToRealm(CrossbarRealm) .WebSocketTransport(new Uri(CrossbarAddress + "/ws")) .JsonSerialization() .Build(); IWampRealmProxy realmProxy = channel.RealmProxy; channel.RealmProxy.Monitor.ConnectionEstablished += (sender_inner, arg) => { Console.WriteLine("connected session with ID " + arg.SessionId); dynamic details = arg.WelcomeDetails.OriginalValue.Deserialize <dynamic>(); Console.WriteLine("authenticated using method '{0}' and provider '{1}'", details.authmethod, details.authprovider); Console.WriteLine("authenticated with authid '{0}' and authrole '{1}'", details.authid, details.authrole); }; channel.RealmProxy.Monitor.ConnectionBroken += (sender_inner, arg) => { dynamic details = arg.Details.OriginalValue.Deserialize <dynamic>(); Console.WriteLine("disconnected " + arg.Reason + " " + details.reason + details); Console.WriteLine("disconnected " + arg.Reason); }; await channel.Open().ConfigureAwait(false); WAMPSubject = realmProxy.Services.GetSubject("RETDataSample"); SelectedTracker.GazeDataReceived += HandleGazeData; IsStreamingToWAMP = true; WAMPThread = new Thread(() => { while (IsStreamingToWAMP) { try { while (GazeEventQueue.Any()) { GazeDataEventArgs gazeEvent; GazeEventQueue.TryDequeue(out gazeEvent); float gazeX = 0; float gazeY = 0; if (gazeEvent.LeftEye.GazePoint.Validity == Validity.Valid && gazeEvent.RightEye.GazePoint.Validity == Validity.Valid) { gazeX = (gazeEvent.LeftEye.GazePoint.PositionOnDisplayArea.X + gazeEvent.RightEye.GazePoint.PositionOnDisplayArea.X) / 2; gazeY = (gazeEvent.LeftEye.GazePoint.PositionOnDisplayArea.Y + gazeEvent.RightEye.GazePoint.PositionOnDisplayArea.Y) / 2; } else if (gazeEvent.LeftEye.GazePoint.Validity == Validity.Valid) { gazeX = gazeEvent.LeftEye.GazePoint.PositionOnDisplayArea.X; gazeY = gazeEvent.LeftEye.GazePoint.PositionOnDisplayArea.Y; } else if (gazeEvent.RightEye.GazePoint.Validity == Validity.Valid) { gazeX = gazeEvent.RightEye.GazePoint.PositionOnDisplayArea.X; gazeY = gazeEvent.RightEye.GazePoint.PositionOnDisplayArea.Y; } if (gazeEvent.LeftEye.Pupil.PupilDiameter != -1 || gazeEvent.RightEye.Pupil.PupilDiameter != -1) { CurrentPupilDiameters[0] = Double.IsNaN(gazeEvent.LeftEye.Pupil.PupilDiameter) ? -1 : gazeEvent.LeftEye.Pupil.PupilDiameter; CurrentPupilDiameters[1] = Double.IsNaN(gazeEvent.RightEye.Pupil.PupilDiameter) ? -1 : gazeEvent.RightEye.Pupil.PupilDiameter; } WampEvent evt = new WampEvent(); evt.Arguments = new object[] { SelectedTracker.SerialNumber, new object[] { CurrentPupilDiameters[0], gazeEvent.LeftEye.GazePoint.PositionOnDisplayArea.X, gazeEvent.LeftEye.GazePoint.PositionOnDisplayArea.Y, CurrentPupilDiameters[1], gazeEvent.RightEye.GazePoint.PositionOnDisplayArea.X, gazeEvent.RightEye.GazePoint.PositionOnDisplayArea.Y, gazeEvent.LeftEye.GazeOrigin.PositionInUserCoordinates.X, gazeEvent.LeftEye.GazeOrigin.PositionInUserCoordinates.Y, gazeEvent.LeftEye.GazeOrigin.PositionInUserCoordinates.Z, gazeEvent.RightEye.GazeOrigin.PositionInUserCoordinates.X, gazeEvent.RightEye.GazeOrigin.PositionInUserCoordinates.Y, gazeEvent.RightEye.GazeOrigin.PositionInUserCoordinates.Z, gazeX, gazeY } }; WAMPSubject.OnNext(evt); } } catch (Exception exp) { //do nothing, skip Console.Write(exp); } } }); WAMPThread.Start(); if (StreamBtn.InvokeRequired) { StreamBtn.BeginInvoke((MethodInvoker) delegate() { this.StreamBtn.Text = "Stop"; }); } else { StreamBtn.Text = "Stop"; } } else { IsStreamingToWAMP = false; SelectedTracker.GazeDataReceived -= HandleGazeData; if (StreamBtn.InvokeRequired) { StreamBtn.BeginInvoke((MethodInvoker) delegate() { this.StreamBtn.Text = "Stream"; }); } else { StreamBtn.Text = "Stream"; } WAMPThread.Join(); } }
private void CreateWAMPClient() { using (var reader = new System.IO.StreamReader(@"./crossbar_config.csv")) { List <string> listA = new List <string>(); List <string> listB = new List <string>(); while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); WAMPRouterAdress = values[0]; WAMPRealm = values[1]; } } OpenFaceDataSamples = new ConcurrentQueue <OpenFaceDataStruct>(); //EyeDataSamples = new ConcurrentQueue<EyeDataStruct>(); //HeadDataSamples = new ConcurrentQueue<List<float>>(); DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory(); channel = channelFactory.CreateJsonChannel(WAMPRouterAdress, WAMPRealm); counter = 0; if (ConnectToRouter()) { IsBroadcastingToWAMP = true; while (!StopWAMP) { //stream eye data OpenFaceDataStruct Sample; if (OpenFaceDataSamples.TryDequeue(out Sample)) { EyeDataStruct EyeData = Sample.GazeData; object[] gazeArgs; if (EyeData.EyeLandmarks3D != null) { float leftPupilRadius = Distance(EyeData.EyeLandmarks3D[21], EyeData.EyeLandmarks3D[25]) / 2; float rightPupilRadius = Distance(EyeData.EyeLandmarks3D[49], EyeData.EyeLandmarks3D[53]) / 2; Tuple <float, float, float> leftEyeLocation = /*new Tuple<float, float, float>(0, 0, 0);//*/ AverageBetweenTuples(EyeData.EyeLandmarks3D[11], EyeData.EyeLandmarks3D[17]); Tuple <float, float, float> rightEyeLocation = AverageBetweenTuples(EyeData.EyeLandmarks3D[39], EyeData.EyeLandmarks3D[45]); //gazeArgs = new object[] { EyeData.Confident, leftPupilRadius, // leftEyeLocation.Item3/10, leftEyeLocation.Item1/10, -leftEyeLocation.Item2/10, // (EyeData.Gaze.Item1.Item1), EyeData.Gaze.Item1.Item2, EyeData.Gaze.Item1.Item3, // EyeData.Confident, rightPupilRadius, // rightEyeLocation.Item3/10, rightEyeLocation.Item1/10, -rightEyeLocation.Item2/10, // (EyeData.Gaze.Item2.Item1), EyeData.Gaze.Item2.Item2, EyeData.Gaze.Item2.Item3}; gazeArgs = new object[] { EyeData.Confident, leftPupilRadius, 0, 0, 0, (EyeData.Gaze.Item2.Item3), EyeData.Gaze.Item2.Item1, -EyeData.Gaze.Item2.Item2, EyeData.Confident, rightPupilRadius, 0, 0, 0, (EyeData.Gaze.Item1.Item3), EyeData.Gaze.Item1.Item1, -EyeData.Gaze.Item1.Item2 }; } else { gazeArgs = new object[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; } List <float> HeadData = Sample.HeadData; //float[] orientation = ConvertEulerAnglesToQuaternion(HeadData); object[] headArgs = new object[] { "OpenFace", new object[] { HeadData[2] / 10, HeadData[0] / 10, -HeadData[1] / 10 }, new object[] { HeadData[3] + Math.PI, HeadData[4], -(HeadData[5] + Math.PI) }, //new object[] { orientation[0], orientation[1], orientation[2] }, new object[] { } }; WampEvent evt = new WampEvent(); evt.Arguments = new object[] { "OpenFace", gazeArgs, headArgs }; OpenFaceSubject.OnNext(evt); } //stream head data //Thread.Sleep(100); } } }