/// <summary>
        /// Getting observed values.
        /// </summary>
        /// <returns></returns>
        public ObservedValues GetObservedValues()
        {
            using (SQLiteCommand cmd = (SQLiteCommand)DataAccess.Instance.Connection.CreateCommand())
            {
                cmd.CommandText = "select * from " + Tables.OBSERVED_VALUES;
                using (DbDataReader rd = cmd.ExecuteReader())
                {
                    if (rd.Read())
                    {
                        ObservedValues ov = new ObservedValues();
                        ov.ActualDate =
                            !rd.IsDBNull(rd.GetOrdinal(Columns.OBSERVED_VALUES.ACTUAL_DATE))
                            ? (DateTime?)rd.GetDateTime(rd.GetOrdinal(Columns.OBSERVED_VALUES.ACTUAL_DATE))
                            : null;
                        ov.Duration =
                            !rd.IsDBNull(rd.GetOrdinal(Columns.OBSERVED_VALUES.DURATION))
                            ? (long?)rd.GetInt64(rd.GetOrdinal(Columns.OBSERVED_VALUES.DURATION))
                            : null;
                        return(ov);
                    }
                }
            }

            return(null);
        }
        /// <summary>
        /// Refresh data on dialog.
        /// </summary>
        private void RefreshData()
        {
            lock (synobj)
            {
                try
                {
                    ObservedValuesRemote remote = new ObservedValuesRemote(connection.Hostname, connection.Port.Value);
                    ObservedValues       ov     = remote.GetObservedValues();

                    txtActualDate.Text =
                        (ov.ActualDate != null
                        ? ov.ActualDate.Value.ToString(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern)
                        : "");
                    txtDuration.Text = "";
                    if (ov.Duration != null)
                    {
                        TimeSpan t    = TimeSpan.FromMilliseconds(ov.Duration.Value);
                        string   time = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
                        txtDuration.Text = time;
                    }
                }
                catch (System.Exception ex)
                {
                    timer1.Stop();
                    timer1.Dispose();
                    throw ex;
                }
            }
        }
        /// <summary>
        /// Storing observed values.
        /// </summary>
        /// <param name="ov"></param>
        public void StoreObservedValues(ObservedValues ov)
        {
            using (DbTransaction tra = DataAccess.Instance.Connection.BeginTransaction())
            {
                using (SQLiteCommand cmd = (SQLiteCommand)DataAccess.Instance.Connection.CreateCommand())
                {
                    StringBuilder sql = new StringBuilder();
                    sql.Append("update ").Append(Tables.OBSERVED_VALUES).Append(" ");
                    sql.Append("set ").Append(Columns.OBSERVED_VALUES.ACTUAL_DATE).Append(" = @actualDate, ");
                    sql.Append(Columns.OBSERVED_VALUES.DURATION).Append(" = @duration");

                    cmd.CommandText = sql.ToString();

                    cmd.Parameters.Add("actualDate", DbType.Date).Value = ov.ActualDate;
                    cmd.Parameters.Add("duration", DbType.Int64).Value  = ov.Duration;

                    int rows = cmd.ExecuteNonQuery();
                    if (rows == 0)
                    {
                        throw new InvalidOperationException("Could not update OBSERVED VALUES.");
                    }
                    else if (rows > 1)
                    {
                        throw new InvalidOperationException("Too many rows updated in OBSERVED VALUES.");
                    }
                }
                tra.Commit();
            }
        }
示例#4
0
        public void TestRulesForCanContinueWithDates()
        {
            DateTime now = DateTime.ParseExact("05.01.2018 13:45", "dd.MM.yyyy HH:mm", null);

            ObservedValues ov = new ObservedValues();

            ov.ActualDate = now;
            ov.Duration   = (58L * 60000L);

            List <Rule> list = new List <Rule>();

            Rule r1 = new Rule();

            r1.Name              = "Test 1";
            r1.Enabled           = true;
            r1.FromDateTime      = DateTime.ParseExact("01.01.2018 00:00", "dd.MM.yyyy HH:mm", null);
            r1.ToDateTime        = DateTime.ParseExact("10.01.2018 23:59", "dd.MM.yyyy HH:mm", null);
            r1.DurationInMinutes = 60;
            list.Add(r1);

            Rule r2 = new Rule();

            r2.Name              = "Test 2";
            r2.Enabled           = true;
            r2.DayOfWeek         = (int?)System.DayOfWeek.Friday; // 05.01.2018 is friday
            r2.DurationInMinutes = 60;
            list.Add(r2);

            Assert.IsTrue(pcsj.CanContinue(now, ov, list.ToArray()));
        }
示例#5
0
        /// <summary>
        /// Reset observed values.
        /// </summary>
        public void Reset()
        {
            ObservedValues ov = new ObservedValues();

            ov.ActualDate = null;
            ov.Duration   = null;
            observedValuesDAO.StoreObservedValues(ov);
        }
        public void OnNext(IOnNextEvent onNextEvent)
        {
            if (ObservedValues == null)
            {
                ObservedValues = new List <ObservedValue>();
            }

            ObservedValues.Add(new ObservedValue(onNextEvent));
            ValuesProduced++;
        }
示例#7
0
        /// <summary>
        /// Checking whether for can be shutting down or not. True value is No :).
        /// </summary>
        /// <param name="now"></param>
        /// <param name="rules"></param>
        /// <param name="ov"></param>
        /// <returns></returns>
        public bool CanContinue(DateTime now, ObservedValues ov, Rule[] rules)
        {
            List <Rule> list          = rules.OfType <Rule>().ToList().FindAll(rule => rule.Enabled);
            List <Rule> negativeRules = list.Where(rule =>
                                                   (rule.DayOfWeek != null && rule.DayOfWeek != (int)now.DayOfWeek) ||
                                                   (rule.FromDateTime != null && now.CompareTo(rule.FromDateTime) <= 0) ||
                                                   (rule.ToDateTime != null && now.CompareTo(rule.ToDateTime) >= 0) ||
                                                   (rule.DurationInMinutes != null && ov.Duration != null && ov.Duration >= ((long)(rule.DurationInMinutes * MILISECONDS_PER_MINUTE)))).ToList();

            negativeRules.ForEach(rule => {
                list.Remove(rule);
            });
            return(list.Count > 0);
        }
示例#8
0
        /// <summary>
        /// Button - show information.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInfo_Click(object sender, EventArgs e)
        {
            ObservedValuesRemote remote = new ObservedValuesRemote();
            ObservedValues       ov     = remote.GetObservedValues();

            String actualDate =
                (ov.ActualDate != null
                ? ov.ActualDate.Value.ToString(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern)
                : "");
            String duration = "";

            if (ov.Duration != null)
            {
                TimeSpan t    = TimeSpan.FromMilliseconds(ov.Duration.Value);
                string   time = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
                duration = time;
            }

            MessageBox.Show(actualDate + "\n" + duration, ParentControlClient.Strings.GetString("information"));
        }
示例#9
0
        #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        private async void doSomething(long diff)
        #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            lock (synobj)
            {
                ObservedValuesDAO observedValuesDao = new ObservedValuesDAO();
                ObservedValues    ov  = observedValuesDao.GetObservedValues();
                DateTime          now = DateTime.Now;

                // If at least one of the rules is true, code is here.
                ov.Duration   = (ov.ActualDate != null && ov.Duration != null && ov.ActualDate.Value.Date.Equals(now.Date)) ? (ov.Duration = ov.Duration + diff) : diff;
                ov.ActualDate = now.Date;
                observedValuesDao.StoreObservedValues(ov);

                logger.Debug("Duration ... " + ov.Duration);

                // We need list of the rules. And control it.
                RulesDAO rulesDao    = new RulesDAO();
                Rule[]   rules       = rulesDao.ListRules();
                bool     canContinue = CanContinue(now, ov, rules);


                // If we can not continue, shutdown computer ...
                if (!canContinue)
                {
                    logger.Info("Condition is not truthfully, run command for shutdown ...");

                    BaseSettings bs = baseSettings.GetBaseSettings();
                    if (bs.ShutdownCommand != null && bs.ShutdownCommand.Length > 0)
                    {
                        System.Diagnostics.Process process =
                            System.Diagnostics.Process.Start("cmd", "/C \"" + bs.ShutdownCommand + "\"");
                        // TODO Do something with process outputs.
                    }
                }
            }
        }
示例#10
0
        public void TestRulesForCanContinueOnlyDaysOfWeek()
        {
            DateTime now = DateTime.ParseExact("06.01.2018 13:45", "dd.MM.yyyy HH:mm", null);

            ObservedValues ov = new ObservedValues();

            ov.ActualDate = now;
            ov.Duration   = (58L * 60000L);

            List <Rule> list = new List <Rule>();

            Rule r1 = new Rule();

            r1.Name              = "Test 1";
            r1.Enabled           = true;
            r1.DayOfWeek         = (int?)System.DayOfWeek.Thursday;
            r1.DurationInMinutes = 60;
            list.Add(r1);

            Rule r2 = new Rule();

            r2.Name              = "Test 2";
            r2.Enabled           = true;
            r2.DayOfWeek         = (int?)System.DayOfWeek.Friday;
            r2.DurationInMinutes = 60;
            list.Add(r2);

            Rule r3 = new Rule();

            r3.Name              = "Test 3";
            r3.Enabled           = true;
            r3.DayOfWeek         = (int?)System.DayOfWeek.Saturday;
            r3.DurationInMinutes = 60;
            list.Add(r3);

            Assert.IsTrue(pcsj.CanContinue(now, ov, list.ToArray()));
        }
示例#11
0
 public void OnNext(IOnNextEvent onNextEvent)
 {
     ObservedValues.Add(new RxSpyObservedValueModel(onNextEvent));
     ValuesProduced++;
 }