Example #1
0
        public AverageDeviceSendModel GetAverageDeviceDataOneHour(string deviceId)
        {
            DateTime time  = DateTime.Now;
            DateTime time1 = new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0);

            time1 = time1.ToUniversalTime();
            time1 = time1.AddHours(-1);
            List <double>          totals    = new List <double>();
            AverageDeviceSendModel sendModel = new AverageDeviceSendModel
            {
                Id = deviceId
            };
            Device             device       = devices.Find(device => device.Id == deviceId).FirstOrDefault();
            List <Measurement> measurements = device.Measurements
                                              .OrderByDescending(measurement => measurement.CreatedDate).Take(720)
                                              .Where(measurement => measurement.CreatedDate > time1).ToList();

            if (measurements.Count != 0)
            {
                for (int i = 0; i < measurements[0].Value.ElementCount; i++)
                {
                    totals.Add(new double());
                    sendModel.AverageDeviceDatas.Add(new AverageDeviceData
                    {
                        Name = measurements[0].Value.GetElement(i).Name
                    });
                }

                for (int i = 0; i < measurements.Count; i++)
                {
                    for (int j = 0; j < measurements[i].Value.ElementCount; j++)
                    {
                        totals[j] += measurements[i].Value.GetElement(j).Value.ToDouble();
                    }
                }

                for (int i = 0; i < totals.Count; i++)
                {
                    sendModel.AverageDeviceDatas[i].Value = totals[i] / measurements.Count;
                }
            }

            return(sendModel);
        }
        public IActionResult GetAverageDeviceOneHour(string deviceId)
        {
            AverageDeviceSendModel averageDeviceSend = measurementRepository.GetAverageDeviceDataOneHour(deviceId);

            return(Ok(averageDeviceSend));
        }