/*
         * This exception was originally thrown at this call stack:
         * [External Code]
         * CementControl.MainWindow.DisplayWeight(object, double) in MainWindow.cs
         * CementControl.HandlerRs232WeigthScale.DataReceived(object, string) in HandlerRs232WeigthScale.cs
         * CementControl.SerialConnection.DataReceivedHandler(object, System.IO.Ports.SerialDataReceivedEventArgs) in SerialConnection.cs
         * [External Code]
         * System.InvalidOperationException: 'Cross-thread operation not valid: Control 'labelReadWeight' accessed from a thread other than the thread it was created on.'
         */


        public MainWindow()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

            InitializeComponent();

            // db context
            _dataLogFile = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\ECO\Datafiles\CementLog.{DateTime.Now:yyyyMMdd_HHmmss}.csv";

            _dataHandle = new DataFileHandle(_dataLogFile);

            // Initialize logging
            App.ConfigureLogging();
            _logger = Log.ForContext <MainWindow>();
            _logger.Information("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  New Run  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

            // Init GUI

            // Initialize app
            //  Container = App.ConfigureDependencyInjection();

            //Log.Debug("Test");

            InitConfigFileItems();

            _logger.Information("Tool initiation MainWindow completed");

            labelVers.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }
        public CementDataServices(DataFileHandle dataFileHandle)
        {
            _dataFileHandle = dataFileHandle;

            var header = $"Dato{_sep}Description{_sep}State{_sep}CurrentWeight{_sep}AverageWeight{_sep}CurrentVoltage{_sep}CementLoadGoal{_sep}CementLoaded{_sep}StartingWeight{_sep}{Environment.NewLine}";

            SaveCementData(header);
        }
        public void SaveCementDataTest()
        {
            DataFileHandle     dataFileHandle     = new DataFileHandle(@"c:/temp/dataFile.csv");
            CementDataServices cementDataServices = new CementDataServices(dataFileHandle);

            CementData c1 = new CementData("Hole", 1.10, 1.10, ExecutionState.Loading, 2.20, 3.30, 4.40, 5.50);
            CementData c2 = new CementData("Hole", 1.11, 1.10, ExecutionState.Loading, 2.21, 3.31, 4.41, 5.51);
            CementData c3 = new CementData("Hole", 1.12, 1.10, ExecutionState.Loading, 2.22, 3.32, 4.42, 5.52);

            cementDataServices.SaveCementData(c1);
            cementDataServices.SaveCementData(c2);
            cementDataServices.SaveCementData(c3);

            dataFileHandle.Close();
        }