/// <summary> /// Handler for receiving data from the MQTT server. This is where the main functionality is placed. /// It simplay grabs an MQTT message, tries to parse it into a HEUCOD BasicEvent. /// If sucecsful the data is stored in the datastore, providing it with the current time stamp. /// Please note, this could cause expections if many services try to create events simulatanously. /// If this happens, a random back-off and wait algorithm needs to be implented. /// As an alternative GUID could be used as identifier. However, this has twice the length of the /// current long - and it does not provide the same sequence which is supplied by using DateTime.UtcNow.Ticks /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { LastEvent = DateTime.Now; // newTask.Start(() => try { var message = (Encoding.UTF8.GetString(e.Message)); //Check whether this contains a string with patientid - if not - it is not properly formatted - and worthless if (message.ToLower().Contains("patientid")) { var newevent = BasicEvent.FromJson(message); if (newevent.PatientId == "2") { newevent.Id = DateTime.UtcNow.Ticks.ToString(); if (newevent.SensorType.Contains("ToothBrush")) { Console.WriteLine("Just detected a toothbrushing event which lasted " + newevent.Value + " seconds - of type: " + newevent.SensorType); } if (newevent.SensorType.Contains("Bath") && newevent.Value == 1) { Console.WriteLine("Just detected bathroom movement of type: " + newevent.SensorType); } } } } catch (Exception ex) { //TODO: Add logging in case of errors - and also implement a back-off and wait and retry - in case //the ID is already used by the database. Console.WriteLine("Error in service: " + ex); Log.Logger.Error("Error while reciving event: " + ex); } }
private BasicEvent ParseEvent(byte[] message) { return(BasicEvent.FromJson(Encoding.UTF8.GetString(message))); }