private void UpdateGui(SessionRecord sessionRecord)
        {
            using (var context = new HelicopterModelEntities())
            {
                var yaw = sessionRecord.ControllerRecords.Single(x => x.MotorType == MotorType.Yaw.ToString());
                var tilt = sessionRecord.ControllerRecords.Single(x => x.MotorType == MotorType.Tilt.ToString());

                SessionStartTimeTextBlock.Text = sessionRecord.StartTime.ToString();
                SessionEndTimeTextBlock.Text = sessionRecord.EndTime.ToString();

                YawCWPGainTextBlock.Text = yaw.CWProportionalGain.ToString();
                YawCWIGainTextBlock.Text = yaw.CWIntegralGain.ToString();
                YawCWDGainTextBlock.Text = yaw.CWDerivativeGain.ToString();
                YawCCWPGainTextBlock.Text = yaw.CCWProportionalGain.ToString();
                YawCCWIGainTextBlock.Text = yaw.CCWIntegralGain.ToString();
                YawCCWDGainTextBlock.Text = yaw.CCWDerivativeGain.ToString();
                YawWindupTextBlock.Text = yaw.IntegralWindupThreshold.ToString();
                YawOutputRateLimitTextBlock.Text = yaw.OutputRateLimit.ToString();
                YawDriverType.Text = yaw.DriverType;

                TiltPGainTextBlock.Text = tilt.CWProportionalGain.ToString();
                TiltIGainTextBlock.Text = tilt.CWIntegralGain.ToString();
                TiltDGainTextBlock.Text = tilt.CWDerivativeGain.ToString();
                TiltWindupTextBlock.Text = yaw.IntegralWindupThreshold.ToString();
                TiltOutputRateLimitTextBlock.Text = tilt.OutputRateLimit.ToString();
                TiltDriverType.Text = tilt.DriverType;

                SessionComment.Text = sessionRecord.Comment;
            }
        }
 public static void UpdateSessionComment(int recordId, string comment)
 {
     using (var context = new HelicopterModelEntities())
     {
         var sessionRecord = context.SessionRecords.Single(x => x.Id == recordId);
         sessionRecord.Comment = comment;
         context.SaveChanges();
         Log.DebugFormat("Updated comment for session with ID: {0}", recordId);
     }
 }
        // Gets a collection of DatabaseRow objects by searching through the database using the specified search paramaters
        // These DatabaseRow objects can be easily bound to and displayed from a datagrid
        public static ObservableCollection<DatabaseRow> GetQueriedRecords(bool isSearchingById, int recordId, DateTime date)
        {
            using (var context = new HelicopterModelEntities())
            {
                IQueryable<SessionRecord> records;
                if (isSearchingById)
                {
                    records = context.SessionRecords.Where(x => x.Id <= recordId).OrderByDescending(x => x.Id).Take(INT_MaxRowsReturned);
                }
                else
                {
                    date = date.AddDays(1);
                    records = context.SessionRecords.Where(x => x.StartTime <= date).OrderByDescending(x => x.Id).Take(INT_MaxRowsReturned);
                }

                var queriedRecords = LoadRecords(records);

                return queriedRecords;
            }
        }
        private void OnRowDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var recordId = helicopterViewModel.SelectedRecord.Id;

            using (var context = new HelicopterModelEntities())
            {
                var sessionRecord = DatabaseManager.GetSessionRecord(recordId, context);
                sessionPidChartWindow = new SessionPidChartWindow(sessionRecord);
                sessionPidChartWindow.Show();
                Log.DebugFormat("Loaded an old session with ID: {0}", recordId);
            }
        }
        public static SessionRecord CreateNewSessionRecord(Session session, HelicopterSettings settings)
        {
            SessionRecord sessionRecord;
            var yaw = session.YawDataSeries;
            var tilt = session.TiltDataSeries;

            using (var context = new HelicopterModelEntities())
            {
                var settingsRecordId = UpdateSettingsRecord(settings, context);

                sessionRecord = new SessionRecord
                {
                    SettingsId = settingsRecordId,
                    StartTime = session.StartTime,
                    EndTime = session.EndTime,
                    Comment = String.Empty
                };

                var yawRecord = new ControllerRecord
                {
                    MotorType = yaw.MotorType.ToString(),
                    DriverType = yaw.MotorDriver.ToString(),
                    CWProportionalGain = yaw.CWProportionalGain,
                    CWIntegralGain = yaw.CWIntegralGain,
                    CWDerivativeGain = yaw.CWDerivativeGain,
                    CCWProportionalGain = yaw.CCWProportionalGain,
                    CCWIntegralGain = yaw.CCWIntegralGain,
                    CCWDerivativeGain = yaw.CCWDerivativeGain,
                    IntegralWindupThreshold = yaw.IWindupThreshold,
                    OutputRateLimit = yaw.OutputRateLimit,
                    MeasurementRecords = yaw.ControllerData.Select(x => new MeasurementRecord
                    {
                        TimeStamp = x.TimeStamp,
                        SetPoint = x.SetPoint,
                        CurrentAngle = x.CurrentAngle
                    }).ToList()
                };

                var tiltRecord = new ControllerRecord
                {
                    MotorType = tilt.MotorType.ToString(),
                    DriverType = tilt.MotorDriver.ToString(),
                    CWProportionalGain = tilt.CWProportionalGain,
                    CWIntegralGain = tilt.CWIntegralGain,
                    CWDerivativeGain = tilt.CWDerivativeGain,
                    IntegralWindupThreshold = tilt.IWindupThreshold,
                    OutputRateLimit = tilt.OutputRateLimit,
                    MeasurementRecords = tilt.ControllerData.Select(x => new MeasurementRecord
                    {
                        TimeStamp = x.TimeStamp,
                        SetPoint = x.SetPoint,
                        CurrentAngle = x.CurrentAngle
                    }).ToList()
                };

                sessionRecord.ControllerRecords.Add(yawRecord);
                sessionRecord.ControllerRecords.Add(tiltRecord);

                context.SessionRecords.Add(sessionRecord);
                context.SaveChanges();
                Log.Debug("Created and saved new session with ID");
            }

            return sessionRecord;
        }
 public static SessionRecord GetSessionRecord(int recordId, HelicopterModelEntities context)
 {
     return context.SessionRecords.SingleOrDefault(x => x.Id == recordId);
 }
        private static int UpdateSettingsRecord(HelicopterSettings settings, HelicopterModelEntities context)
        {
            var hash = GetSHA1Hash(settings.XmlText);
            var settingsRecord = context.SettingsRecords.SingleOrDefault(x => x.Hash == hash);

            if (settingsRecord != null) return settingsRecord.Id;

            settingsRecord = new SettingsRecord
            {
                Created = DateTime.Now,
                Hash = hash,
                Text = settings.XmlText
            };

            context.SettingsRecords.Add(settingsRecord);
            context.SaveChanges();

            return settingsRecord.Id;
        }