private async void nextStep(Steps currentStep) { await semaphore.WaitAsync(); try { switch (currentStep) { case Steps.PersonDetected: { if (lastStep != currentStep) { session = Guid.NewGuid(); Log("person detected, getting emotion"); //take a picture var emotion = await GetEmotion(cancellationSource.Token, 0); if (emotion != null) { Log($"emotion detected: {emotion.Emotion} {emotion.Score}"); emotion.SessionId = session; emotion.UserPresent = true; //send a message to the tree: lights on await sendToTree(emotion); } else { return; //do not set lastStep, have a second chance } } break; } case Steps.PersonGone: { if (lastStep != currentStep) { Log("user gone, sending message to tree"); var emotion = new EmotionResult { SessionId = session, Date = DateTime.Now }; await sendToTree(emotion); } break; } case Steps.Lights: { Log("Lights on, measure happiness"); //take a picture var emotion = await GetEmotion(cancellationSource.Token, 1); if (emotion != null) { Log($"emotion detected: {emotion.Emotion} {emotion.Score}"); emotion.SessionId = session; emotion.UserPresent = true; //send a message to the tree: lights on await sendToTree(emotion); } break; } } lastStep = currentStep; } finally { semaphore.Release(); } }
private async Task sendToTree(EmotionResult emotion) { var emotionJson = Newtonsoft.Json.JsonConvert.SerializeObject(emotion); Message m = new Message(Encoding.UTF8.GetBytes(emotionJson)); m.To = Config.Default.PartnerDevice; Log("Sending to tree"); await deviceClient.SendEventAsync(m); Log("Message sent"); }