Example #1
0
        public IActionResult UpdateObservation([FromBody] ObservationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var accountId = int.Parse(User.Claims.FirstOrDefault(c => c.Type == "id").Value);

            var observation = new Observation
            {
                Id          = model.Id,
                Description = model.Description,
                Latitude    = model.Latitude,
                Longitude   = model.Longitude,
                SpeciesId   = model.SpeciesId,
                Date        = model.Date,
                AccountId   = model.AccountId
            };

            _context.Entry(observation).State = EntityState.Modified;
            _context.SaveChanges();

            return(Ok());
        }
Example #2
0
 private ValueClass getCorrectValueType(ObservationModel observationModel)
 {
     if (observationModel.valueQuantity != null)
     {
         return(observationModel.valueQuantity);
     }
     if (observationModel.valueBoolean != null)
     {
         return(observationModel.valueBoolean);
     }
     if (observationModel.valueInteger != null)
     {
         return(observationModel.valueInteger);
     }
     if (observationModel.valueString != null)
     {
         return(observationModel.valueString);
     }
     if (observationModel.valueRange != null)
     {
         return(observationModel.valueRange);
     }
     return(new ValueClass {
         Value = 0,
         Unit = "N/A",
         Code = "Error: Missing type of value from FHIR. Please update parser to include this new type",
         System = new Uri("N/A")
     });
 }
Example #3
0
        public IActionResult AddObservation([FromBody] ObservationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (model.Longitude > 180)
            {
                model.Longitude = (model.Longitude + 180) % 360 - 180;
            }
            else if (model.Longitude < -180)
            {
                model.Longitude = (model.Longitude - 180) % 360 + 180;
            }

            var accountId = int.Parse(User.Claims.FirstOrDefault(c => c.Type == "id").Value);

            var observation = new Observation
            {
                Description = model.Description,
                Latitude    = model.Latitude,
                Longitude   = model.Longitude,
                SpeciesId   = model.SpeciesId,
                Date        = model.Date,
                AccountId   = accountId
            };

            _context.Observations.Add(observation);

            _context.SaveChanges();

            return(Ok());
        }
        public void Put(string patientid, string id, [FromBody] ObservationModel model)
        {
            var filter = Builders <PatientModel> .Filter.And(
                Builders <PatientModel> .Filter.Eq("Id", patientid));

            var update = Builders <PatientModel> .Update.PopFirst(x => x.Observations.Any(y => y.ObservationId.Equals(id)))
                         .Push(x => x.Observations, model);
        }
Example #5
0
        private static bool Validate(ObservationModel model)
        {
            var errorList = new List <string>();

            using (var repo = _baseRepository.Get <ISequence>())
            {
                var id = repo.SequenceExist(model.Sequence);
                if (id == null)
                {
                    errorList.Add("The sequence isn't found");
                }

                var start = repo.GetStartNumber(model.Sequence);
                if (start == 0 && model.Color == Colors.red)
                {
                    errorList.Add("There isn't enough data");
                }

                var finished = repo.IsFinished(model.Sequence);
                if (finished != null)
                {
                    errorList.Add("The red observation should be the last");
                }

                if (model.Color != Colors.green && model.Color != Colors.red)
                {
                    errorList.Add("Invalid color");
                }

                if (model.Color == Colors.green)
                {
                    if (model.Numbers == null || model.Numbers.Length == 0 || model.Numbers.Length != 2)
                    {
                        errorList.Add("Invalid count of numbers");
                    }
                    else if (!Regex.IsMatch(model.Numbers[0], "^[0-1]{7}$") || !Regex.IsMatch(model.Numbers[1], "^[0-1]{7}$"))
                    {
                        errorList.Add("Invalid numbers format");
                    }
                }
            }

            if (errorList.Count != 0)
            {
                throw new DTLException(errorList);
            }

            return(true);
        }
Example #6
0
        //Helper method used to recursively find observations from bundles
        private JObject recursiveObservationSearch(List <JObject> observations, JObject sortedObservations,
                                                   Dictionary <string, int> dateMappings)
        {
            for (int i = 0; i < observations.Count; i++)
            {
                if ((string)observations[i].SelectToken("resourceType") == "Bundle")
                {
                    List <JObject> observationSubsearch = new List <JObject>();
                    int            count = observations[i].SelectToken("entry").Count();

                    for (int j = 0; j < count; j++)
                    {
                        observationSubsearch.Add((JObject)observations[i].SelectToken("entry")[j]
                                                 .SelectToken("resource"));
                    }
                    recursiveObservationSearch(observationSubsearch, sortedObservations, dateMappings);
                }
                else if ((string)observations[i].SelectToken("resourceType") == "Observation")
                {
                    ObservationModel observationModel = observations[i].ToObject <ObservationModel>();
                    string           date             = observationModel.EffectiveDateTime.ToString("yyyyMMdd");
                    int id;
                    if (dateMappings.TryGetValue(date, out id))
                    {
                        JArray items = (JArray)sortedObservations.SelectToken(date).SelectToken("items");
                        valueContainer(items, observationModel);
                    }
                    else
                    {
                        int    newKey = sortedObservations.Count;
                        JArray items  = new JArray();
                        valueContainer(items, observationModel);

                        JProperty entry = new JProperty(date,
                                                        new JObject
                        {
                            new JProperty("items", items)
                        }
                                                        );
                        sortedObservations.Add(entry);
                        dateMappings.Add(date, newKey);
                    }
                }
            }
            return(sortedObservations);
        }
Example #7
0
        private ObservationModel GetMappedObservation(Observation observation)
        {
            var resultObservation = new ObservationModel
            {
                Id = observation.Id
            };

            resultObservation.VersionId  = observation.VersionId ?? "";
            resultObservation.Text       = observation.Code.Text;
            resultObservation.SubjectId  = observation.Subject.Reference.Split('/')[1];
            resultObservation.IssuedDate = observation.Issued;
            if (observation.Value is Quantity value)
            {
                resultObservation.Value = value.Value ?? 0;
                resultObservation.Unit  = value.Unit;
            }
            return(resultObservation);
        }
        public ObservationModule() : base("/api")
        {
            Get["/observation"] = _ =>
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                var om = new ObservationModel
                {
                    UniqueClients = Observation.GetUnique(DateTime.Now.AddHours(-1), DateTime.Now, 30),
                    ActiveZones =  Observation.GetFloors(DateTime.Now.AddHours(-1), DateTime.Now, 30)
                };

                sw.Stop();
                Console.WriteLine(sw.ElapsedMilliseconds.ToString());
                //return Response.AsJson(o);
                return NetJSON.NetJSON.Serialize(om);
            };


            //Get["/observations/stats"] = _ =>
            //{
            //    var o = Observation.ToList(DateTime.Today, DateTime.Now, 25);
            //    var unique = o.Select(x => x.ClientMac).Distinct();

            //    foreach (var uq in unique)
            //    {
            //        var ob = Observation.ClientMacList(DateTime.Today, DateTime.Now, uq, 25);
            //        var dwell = new TimeSpan();
            //        foreach (Observation obi in ob)
            //        {
                         
            //        }
            //    }

            //    var stats = new Statistics
            //    {
            //        UniqueClients = o.Select(x => x.ClientMac).Distinct().Count(),
            //        //AvgDwell = o.
            //    };

            //    return NetJSON.NetJSON.Serialize(null);
            //}
        }
        public IHttpActionResult Post(string patientid, [FromBody] ObservationModel obs)
        {
            if (ModelState.IsValid)
            {
                var filter = Builders <PatientModel> .Filter.Eq("Id", patientid);

                var update = Builders <PatientModel> .Update.Push(x => x.Observations, obs);

                var modified = _context.Patients.UpdateOne(filter, update).ModifiedCount;
                var test     = _context.Patients.Find(filter).FirstOrDefault();
                _factory = new VMFactory(new UrlHelper(Request));
                var viewModel = _factory.Create(test);
                return(Created(viewModel.Link, viewModel));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Example #10
0
 private void valueContainer(JArray values, ObservationModel observation)
 {
     //If an observation has more than one value, it stores it as a component of ValueQuantity properties
     //We loop over them and add them to our array if this is the case
     if (observation.Component != null)
     {
         for (int i = 0; i < observation.Component.Length; i++)
         {
             JObject entry = new JObject
             {
                 new JProperty("type", observation.Code.Text),
                 new JProperty("value", getCorrectValueTypeForComponent(observation.Component[i]).Value),
                 new JProperty("unit", getCorrectValueTypeForComponent(observation.Component[i]).Unit)
             };
             values.Add(entry);
         }
     }
     //The observation may be stored as a valueCodeableConcept, which is just a comment about a topic
     //e.g. is the patient a smoker- the value stored might be "former smoker"
     else if (observation.valueCodeableConcept != null)
     {
         values.Add(new JObject
         {
             new JProperty("type", observation.Code.Text),
             new JProperty("value",
                           observation.valueCodeableConcept.value)
         });
     }
     else
     {
         values.Add(new JObject
         {
             new JProperty("type", observation.Code.Text),
             new JProperty("value",
                           getCorrectValueType(observation).Value),
             new JProperty("unit",
                           getCorrectValueType(observation).Unit)
         });
     }
 }
Example #11
0
        public IActionResult AddObservation([FromBody] ObservationModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var responce = _dtlManager.GetResponse(model);
                return(Ok(responce));
            }
            catch (DTLException ex)
            {
                var responce = new ErrorModel {
                    Status = "error", Msg = ex.ErrMsg
                };
                return(BadRequest(responce));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Example #12
0
        public ResponseModel GetResponse(ObservationModel model)
        {
            if (Validate(model))
            {
                var response = new ResponseModel {
                    Status = "ok"
                };
                int id;

                using (var repo = _baseRepository.Get <ISequence>())
                {
                    id = repo.GetSequenceId(model.Sequence);
                }

                if (model.Color == Colors.green)
                {
                    var first = model.Numbers[0].ToCharArray();
                    var second = model.Numbers[1].ToCharArray();
                    var fInd = new List <int>(); var sInd = new List <int>(); var temp = new List <int>(); var fRes = new List <int>(); var sRes = new List <int>();

                    for (int i = 0; i < first.Length; i++)
                    {
                        if (first[i] == '1')
                        {
                            fInd.Add(i);
                        }
                        if (second[i] == '1')
                        {
                            sInd.Add(i);
                        }
                    }

                    List <int> startNumbers;
                    using (var repo = _baseRepository.Get <ISequence>())
                    {
                        startNumbers = repo.GetSequenceStartNumbers(id);
                    }

                    if (startNumbers.Count == 0)
                    {
                        for (int i = 0; i < _nums.Count; i++)
                        {
                            for (int j = 0; j < _nums[i].ToCharArray().Length; j++)
                            {
                                if (_nums[i][j] == '1')
                                {
                                    temp.Add(j);
                                }
                            }

                            if (!fInd.Except(temp).Any())
                            {
                                fRes.Add(i);
                            }
                            if (!sInd.Except(temp).Any())
                            {
                                sRes.Add(i);
                            }

                            temp = new List <int>();
                        }

                        var sNums = Map(fRes, sRes);

                        using (var repo = _baseRepository.Get <ISequence>())
                        {
                            sNums.ForEach(i =>
                            {
                                repo.AddSequenceStartNumber(id, i);
                            });
                        }

                        response.Response.Start = sNums;
                    }
                    else
                    {
                        response.Response.Start = startNumbers;
                    }

                    var res = DeleteSectors(id, fInd, sInd);

                    response.Response.Missing[0] = ConstructResult(res.Item1);
                    response.Response.Missing[1] = ConstructResult(res.Item2);
                }
                else
                {
                    int start;
                    using (var repo = _baseRepository.Get <ISequence>())
                    {
                        start = repo.GetStartNumber(model.Sequence);
                        repo.SetFinished(model.Sequence);
                    }

                    response.Response.Start = new List <int> {
                        start - 2
                    };

                    using (var repo = _baseRepository.Get <IObservation>())
                    {
                        response.Response.Missing[0] = ConstructResult(repo.GetFirstSector(id).ToList());
                        response.Response.Missing[1] = ConstructResult(repo.GetSecondSector(id).ToList());
                    }
                }

                return(response);
            }

            return(null);
        }