public void ExtractServo(bool inc) { var ServoList = _context.Sensor.ToList(); if (inc == true) { if (ServoList.Count > _context.ServoDim.ToList().Count) { for (int i = _context.ServoDim.ToList().Count; i < ServoList.Count; i++) { var servo = ServoList[i]; var sensorlog = _context.SensorLog.FirstOrDefault(r => r.sensorID == servo.sensorID); if (sensorlog != null) { var Servo = new ServoDim { SensorID = servo.sensorID, PD_ID = 0, DaysSinceSet = 0, HoursSinceSet = 0, SecondsSinceSet = 0, Timestamp = sensorlog.timestamp }; _context.ServoDim.Add(Servo); } } } } else { foreach (var servo in ServoList) { var sensorlog = _context.SensorLog.FirstOrDefault(r => r.sensorID == servo.sensorID); if (sensorlog != null) { var Servo = new ServoDim { SensorID = servo.sensorID, PD_ID = 0, DaysSinceSet = 0, HoursSinceSet = 0, SecondsSinceSet = 0, Timestamp = sensorlog.timestamp }; _context.ServoDim.Add(Servo); } } } _context.SaveChanges(); }
public void TransformServo() { var servoList = _context.ServoDim.ToList(); var sensorList = _context.Sensor.ToList(); ServoDim servoTemp = null; foreach (ServoDim servo in servoList) { if (servoTemp == null) { if (servo.SensorID == null) { servo.SensorID = 0; } servo.SecondsSinceSet = 0; servo.HoursSinceSet = 0; servo.DaysSinceSet = 0; } else { if (servoTemp != null && sensorList.Where(s => s.sensorID == servo.SensorID).FirstOrDefault().sensorID == sensorList.Where(s => s.sensorID == servoTemp.SensorID).FirstOrDefault().sensorID) { int ts; if (sensorList.Where(s => s.sensorID == servo.SensorID).FirstOrDefault().sensorLog.Where(s => s.timestamp == servo.Timestamp).FirstOrDefault().servoSetting != sensorList.Where(s => s.sensorID == servoTemp.SensorID).ToList().FirstOrDefault().sensorLog.Where(s => s.timestamp == servoTemp.Timestamp).FirstOrDefault().servoSetting) { ts = Convert.ToInt32(servo.Timestamp.Subtract(servoTemp.Timestamp.Date).TotalSeconds); servo.SecondsSinceSet = ts; servo.HoursSinceSet = (int)Math.Round((float)(servo.SecondsSinceSet / 3600)); servo.DaysSinceSet = (int)Math.Round((float)servo.HoursSinceSet / 24); } else { ts = Convert.ToInt32(servo.Timestamp.Subtract(servoTemp.Timestamp).Ticks); servo.SecondsSinceSet = ts; servo.HoursSinceSet = (int)Math.Round((float)(servo.SecondsSinceSet / 3600)); servo.DaysSinceSet = (int)Math.Round((float)servo.HoursSinceSet / 24); } } else { servo.SecondsSinceSet = 0; servo.HoursSinceSet = 0; servo.DaysSinceSet = 0; } } servoTemp = servo; } }