private void GetDataButton_Click(object sender, RoutedEventArgs e)
        {
            IStoreData dataReader;

            if (dataViewModel.Location == StorageLocation.LocalFile)
            {
                dataReader = new LocalFileCommunication(this.dataViewModel.FileLocation);
            }
            else
            {
                dataReader = new DBCommunication();
            }

            this.dataViewModel.AllData   = new ObservableCollection <DataTable>(dataReader.GetData());
            this.dataViewModel.ShownData = new ObservableCollection <DataTable>(this.dataViewModel.AllData).ToGridCollection();
        }
Exemple #2
0
        private void Timer_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            if (PLC.Module != null && PLC.Module.Pelecek.IsConnected == true)
            {
                bool temp = Convert.ToBoolean(PLC.Module.ReadModule(windowData.CommunicationBit));
                PLC.Module.WriteToPLC(windowData.CommunicationBit, !temp);

                if (!temp == Convert.ToBoolean(PLC.Module.ReadModule(windowData.CommunicationBit)))
                {
                    windowData.ConnectionString = "Connected";
                }
                else
                {
                    windowData.ConnectionString = "Connection lost... Reconnecting.";
                    timer.Stop();
                    connectButton_Click("Reconnect", new RoutedEventArgs());
                    return;
                }
            }


            if (windowData.ExecuteIntProgram)
            {
                interpreter.Interpret(windowData.InterpreterText, PLC);
                windowData.IntResult = interpreter.RLOobtain();
            }

            string fLocation = FileLocationTextBox.Text;

            #region Uploading data to storage location
            Task.Factory.StartNew(() =>
            {
                object[] values      = new object[windowData.RVariables.Count];
                IStoreData dataStore = null;
                if (storeData)
                {
                    if (windowData.LocationType == StorageLocation.LocalFile)
                    {
                        dataStore = new LocalFileCommunication(fLocation);

                        if (String.IsNullOrEmpty(fLocation))
                        {
                            startTimeButton_Click("NonFile", new RoutedEventArgs());
                        }
                    }
                    else if (windowData.LocationType == StorageLocation.DataBase)
                    {
                        dataStore = new DBCommunication();
                    }
                }

                List <DataTable> readings = new List <DataTable>();

                for (int i = 0; i < windowData.RVariables.Count; i++)
                {
                    values[i] = PLC.Module.ReadModule(windowData.RVariables[i].Variable);
                    readings.Add(new DataTable()
                    {
                        VariableName  = windowData.RVariables[i].Variable,
                        VariableValue = windowData.RVariables[i].Value.ToString() == "ReadError" ? 0 : Convert.ToInt32(windowData.RVariables[i].Value),
                        Time          = now
                    });
                }

                if (windowData.IntResult != null && windowData.ExecuteIntProgram)
                {
                    readings.Add(new DataTable()
                    {
                        VariableName  = "interpreter",
                        VariableValue = Convert.ToInt32(windowData.IntResult),
                        Time          = now
                    });
                }

                dataStore?.StoreData(readings);

                Dispatcher.BeginInvoke(new Action(delegate()
                {
                    for (int i = 0; i < windowData.RVariables.Count; i++)
                    {
                        windowData.RVariables[i] = new rVariable(windowData.RVariables[i].Variable, values[i]);
                    }
                }));
            });

            #endregion
        }