Esempio n. 1
0
 protected override void OnStart(string[] args)
 {
     _eventLogger.LogMessage("Started at " + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + ", with the intervall of " + TimeSpanUtility.ConvertSecondsToMinutes(_settings.PoolingInterval) + "min");
     _appTimer          = new Timer(TimeSpanUtility.ConvertSecondsToMilliseconds(_settings.PoolingInterval));
     _appTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerElapsed);
     _appTimer.Start();
 }
Esempio n. 2
0
        public void EvaluateFormat(object current, Core.Parsing.Format format, ref bool handled, IOutput output, FormatDetails formatDetails)
        {
            if (format != null && format.HasNested)
            {
                return;
            }
            var      formatText = format != null ? format.Text : "";
            TimeSpan fromTime;

            if (current is TimeSpan)
            {
                fromTime = (TimeSpan)current;
            }
            else if (current is DateTime && formatText.StartsWith("timestring"))
            {
                formatText = formatText.Substring(10);
                fromTime   = DateTime.Now.Subtract((DateTime)current);
            }
            else
            {
                return;
            }
            var timeTextInfo = GetTimeTextInfo(formatDetails.Provider);

            if (timeTextInfo == null)
            {
                return;
            }
            var formattingOptions = TimeSpanFormatOptionsConverter.Parse(formatText);
            var timeString        = TimeSpanUtility.ToTimeString(fromTime, formattingOptions, timeTextInfo);

            output.Write(timeString, formatDetails);
            handled = true;
        }
Esempio n. 3
0
        public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
        {
            var format  = formattingInfo.Format;
            var current = formattingInfo.CurrentValue;

            if (format != null && format.HasNested)
            {
                return(false);
            }
            string options;

            if (formattingInfo.FormatterOptions != "")
            {
                options = formattingInfo.FormatterOptions;
            }
            else if (format != null)
            {
                options = format.GetLiteralText();
            }
            else
            {
                options = "";
            }

            TimeSpan fromTime;

            if (current is TimeSpan)
            {
                fromTime = (TimeSpan)current;
            }
            else if (current is DateTime && formattingInfo.FormatterOptions != "")
            {
                fromTime = DateTime.Now.Subtract((DateTime)current);
            }
            else if (current is DateTime && options.StartsWith("timestring"))
            {
                options  = options.Substring(10);
                fromTime = DateTime.Now.Subtract((DateTime)current);
            }
            else
            {
                return(false);
            }
            var timeTextInfo = GetTimeTextInfo(formattingInfo.FormatDetails.Provider);

            if (timeTextInfo == null)
            {
                return(false);
            }
            var formattingOptions = TimeSpanFormatOptionsConverter.Parse(options);
            var timeString        = TimeSpanUtility.ToTimeString(fromTime, formattingOptions, timeTextInfo);

            formattingInfo.Write(timeString);
            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdDeviceSettings"/> class.
        /// </summary>
        public Hd44780LcdDeviceSettings()
        {
            this.ScreenWidth   = 20;
            this.ScreenHeight  = 2;
            this.PatternWidth  = 5;
            this.PatternHeight = 8;

            this.Encoding  = new Hd44780A00Encoding();
            this.SyncDelay = TimeSpanUtility.FromMicroseconds(1);

            // RightToLeft = false;
        }
Esempio n. 5
0
        private static Scope TimedLog(Action <string> doLog, string message, object[] args)
        {
            string formattedMessage = args == null || args.Length == 0 ? message : message.FormatInvariant(args);

            doLog("(Timed) " + formattedMessage);
            Stopwatch stopwatch = Stopwatch.StartNew();

            return(Scope.Create(
                       delegate
            {
                stopwatch.Stop();
                TimeSpan elapsed = stopwatch.Elapsed;
                doLog("({0}) {1}".FormatInvariant(TimeSpanUtility.FormatForLogging(elapsed.Ticks < 0 ? TimeSpan.Zero : elapsed), formattedMessage));
            }));
        }
Esempio n. 6
0
            public double GetData()
            {
                Connection.Write(0x10);
                HighResolutionTimer.Sleep(TimeSpanUtility.FromMicroseconds(150 * 1000));
                byte[] readBuf = Connection.Read(2);

                var valf = readBuf[0] << 8;

                valf |= readBuf[1];
                return(valf / 1.2 * (69 / 69) / 1);

                // var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2;
                // return valf;

                // return Math.Round(valf / (2 * 1.2), 2);
            }
Esempio n. 7
0
        public void Ago()
        {
            // NOTE: We round up on the quarter-to mark for timespans.

            Assert.AreEqual("a second ago", TimeSpanUtility.Ago(new TimeSpan(0, 0, 1)));
            Assert.AreEqual("3 seconds ago", TimeSpanUtility.Ago(new TimeSpan(0, 0, 3)));
            Assert.AreEqual("about a minute ago", TimeSpanUtility.Ago(new TimeSpan(0, 0, 60)));
            Assert.AreEqual("about a minute ago", TimeSpanUtility.Ago(new TimeSpan(0, 1, 59)));
            Assert.AreEqual("3 minutes ago", TimeSpanUtility.Ago(new TimeSpan(0, 3, 0)));
            Assert.AreEqual("3 minutes ago", TimeSpanUtility.Ago(new TimeSpan(0, 3, 30)));
            Assert.AreEqual("about an hour ago", TimeSpanUtility.Ago(new TimeSpan(1, 0, 0)));
            Assert.AreEqual("about 2 hours ago", TimeSpanUtility.Ago(new TimeSpan(1, 59, 0)));
            Assert.AreEqual("about 2 hours ago", TimeSpanUtility.Ago(new TimeSpan(2, 0, 0)));
            Assert.AreEqual("about 3 hours ago", TimeSpanUtility.Ago(new TimeSpan(2, 59, 0)));
            Assert.AreEqual("about 3 hours ago", TimeSpanUtility.Ago(new TimeSpan(3, 0, 0)));
            Assert.AreEqual("1 day ago", TimeSpanUtility.Ago(new TimeSpan(24, 0, 0)));
            Assert.AreEqual("1 day ago", TimeSpanUtility.Ago(new TimeSpan(47, 0, 0)));
            Assert.AreEqual("2 days ago", TimeSpanUtility.Ago(new TimeSpan(48, 0, 0)));
            Assert.AreEqual("22 days ago", TimeSpanUtility.Ago(new TimeSpan(22, 0, 0, 0)));
        }
Esempio n. 8
0
        /// <summary>Parse the JSON into a Result</summary>
        /// <param name="json">json object to parse</param>
        /// <returns>a Result</returns>
        public static Result Parse(JObject json)
        {
            var r = new Result
            {
                JsonFromResponse = json,
                ID           = (ulong)json["id"],
                TestID       = (ulong)json["test_id"],
                StatusID     = (ulong?)json["status_id"],
                CreatedBy    = (ulong?)json["created_by"],
                CreatedOn    = null == (int?)json["created_on"] ? (DateTime?)null : new DateTime(1970, 1, 1).AddSeconds((int)json["created_on"]),
                AssignedToID = (ulong?)json["assignedto_id"],
                Comment      = (string)json["comment"],
                Version      = (string)json["version"],
                Defects      = (string)json["defects"],
            };

            // separate for easier debugging if necessary
            r.Elapsed = TimeSpanUtility.FromString((string)json["elapsed"]);
            return(r);
        }
Esempio n. 9
0
        public void MajorMinorDuration()
        {
            // NOTE: we don't work beyond weeks.

            Assert.AreEqual("0 seconds", TimeSpanUtility.MajorMinorDuration(new TimeSpan(0, 0, 0)));
            Assert.AreEqual("1 second", TimeSpanUtility.MajorMinorDuration(new TimeSpan(0, 0, 1)));
            Assert.AreEqual("59 seconds", TimeSpanUtility.MajorMinorDuration(new TimeSpan(0, 0, 59)));
            Assert.AreEqual("1 minute", TimeSpanUtility.MajorMinorDuration(new TimeSpan(0, 1, 0)));
            Assert.AreEqual("3 minutes", TimeSpanUtility.MajorMinorDuration(new TimeSpan(0, 3, 0)));
            Assert.AreEqual("3 minutes, 1 second", TimeSpanUtility.MajorMinorDuration(new TimeSpan(0, 3, 1)));
            Assert.AreEqual("3 minutes, 45 seconds", TimeSpanUtility.MajorMinorDuration(new TimeSpan(0, 3, 45)));
            Assert.AreEqual("1 hour", TimeSpanUtility.MajorMinorDuration(new TimeSpan(1, 0, 0)));
            Assert.AreEqual("4 hours", TimeSpanUtility.MajorMinorDuration(new TimeSpan(4, 0, 0)));
            Assert.AreEqual("4 hours, 33 minutes", TimeSpanUtility.MajorMinorDuration(new TimeSpan(4, 33, 0)));
            Assert.AreEqual("1 day", TimeSpanUtility.MajorMinorDuration(new TimeSpan(1, 0, 0, 0)));
            Assert.AreEqual("1 day, 22 hours", TimeSpanUtility.MajorMinorDuration(new TimeSpan(1, 22, 0, 0)));
            Assert.AreEqual("5 days", TimeSpanUtility.MajorMinorDuration(new TimeSpan(5, 0, 2, 3)));
            Assert.AreEqual("6 days, 10 hours", TimeSpanUtility.MajorMinorDuration(new TimeSpan(6, 10, 2, 3)));
            Assert.AreEqual("1 week", TimeSpanUtility.MajorMinorDuration(new TimeSpan(7, 0, 0, 0)));
            Assert.AreEqual("1 week, 1 day", TimeSpanUtility.MajorMinorDuration(new TimeSpan(8, 0, 0, 0)));
            Assert.AreEqual("1 week, 4 days", TimeSpanUtility.MajorMinorDuration(new TimeSpan(11, 0, 0, 0)));
            Assert.AreEqual("2 weeks", TimeSpanUtility.MajorMinorDuration(new TimeSpan(14, 0, 0, 0)));
            Assert.AreEqual("3 weeks, 2 days", TimeSpanUtility.MajorMinorDuration(new TimeSpan(23, 0, 0, 0)));
        }
Esempio n. 10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Hd44780LcdDevice"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="gpioConnectionDriverFactory">The gpio connection driver factory.</param>
        /// <param name="registerSelectPin">The register select pin.</param>
        /// <param name="clockPin">The clock pin.</param>
        /// <param name="hd44780DataPins">The HD44780 data pins.</param>
        /// <param name="backlight">The backlight.</param>
        /// <param name="readWrite">The read write.</param>
        /// <param name="threadFactory">The thread factory.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// pins - There must be either 4 or 8 data pins
        /// or
        /// settings - ScreenHeight must be between 1 and 4 rows
        /// or
        /// settings - PatternWidth must be 5 pixels
        /// or
        /// settings - PatternWidth must be either 7 or 10 pixels height.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// At most 80 characters are allowed
        /// or
        /// 10 pixels height pattern cannot be used with an even number of rows.
        /// </exception>
        public Hd44780LcdDevice(
            Hd44780LcdDeviceSettings settings,
            IGpioConnectionDriverFactory gpioConnectionDriverFactory,
            ConnectorPin registerSelectPin,
            ConnectorPin clockPin,
            Hd44780DataPins hd44780DataPins,
            ConnectorPin?backlight       = null,
            ConnectorPin?readWrite       = null,
            IThreadFactory threadFactory = null)
        {
            this.gpioConnectionDriverFactory = GpioConnectionDriverFactory.EnsureGpioConnectionDriverFactory(gpioConnectionDriverFactory);
            this.gpioConnectionDriver        = gpioConnectionDriverFactory.Get();
            this.pins      = new Hd44780Pins(this.gpioConnectionDriver, registerSelectPin, clockPin, backlight, readWrite, hd44780DataPins.ConnectorPins);
            this.thread    = ThreadFactory.EnsureThreadFactory(threadFactory).Create();
            this.syncDelay = settings.SyncDelay;

            if (this.pins.Data.Length != 4 && this.pins.Data.Length != 8)
            {
                throw new ArgumentOutOfRangeException(nameof(hd44780DataPins), this.pins.Data.Length, "There must be either 4 or 8 data pins");
            }

            this.width  = settings.ScreenWidth;
            this.height = settings.ScreenHeight;
            if (this.height < 1 || this.height > MaxHeight)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.ScreenHeight), this.height, "ScreenHeight must be between 1 and 4 rows");
            }

            if (this.width * this.height > MaxChar)
            {
                throw new ArgumentException("At most 80 characters are allowed");
            }

            if (settings.PatternWidth != 5)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.PatternWidth), settings.PatternWidth, "PatternWidth must be 5 pixels");
            }

            if (settings.PatternHeight != 8 && settings.PatternHeight != 10)
            {
                throw new ArgumentOutOfRangeException(nameof(settings.PatternHeight), settings.PatternHeight, "PatternHeight must be either 7 or 10 pixels height");
            }

            if (settings.PatternHeight == 10 && this.height % 2 == 0)
            {
                throw new ArgumentException("10 pixels height pattern cannot be used with an even number of rows");
            }

            this.functions = (settings.PatternHeight == 8 ? Functions.Matrix5X8 : Functions.Matrix5X10)
                             | (this.height == 1 ? Functions.OneLine : Functions.TwoLines)
                             | (this.pins.Data.Length == 4 ? Functions.Data4Bits : Functions.Data8Bits);

            this.entryModeFlags = /*settings.RightToLeft
                                   * ? EntryModeFlags.EntryRight | EntryModeFlags.EntryShiftDecrement
                                   * :*/EntryModeFlags.EntryLeft | EntryModeFlags.EntryShiftDecrement;

            this.encoding = settings.Encoding;

            this.BacklightEnabled = false;

            this.pins.ReadWrite?.Write(false);

            this.pins.RegisterSelect.Write(false);
            this.pins.Clock.Write(false);
            foreach (var dataPin in this.pins.Data)
            {
                dataPin.Write(false);
            }

            this.WriteByte(0x33, false); // Initialize
            this.WriteByte(0x32, false);

            this.thread.Sleep(TimeSpanUtility.FromMicroseconds(50));

            this.WriteCommand(Command.SetFunctions, (int)this.functions);
            this.WriteCommand(Command.SetDisplayFlags, (int)this.displayFlags);
            this.WriteCommand(Command.SetEntryModeFlags, (int)this.entryModeFlags);

            this.Clear();
            this.BacklightEnabled = true;
        }
 public void NegativeTimeSpan()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => TimeSpanUtility.FormatForLogging(TimeSpan.FromMinutes(-1)));
 }
 public void RoundUp()
 {
     Assert.AreEqual("1ms", TimeSpanUtility.FormatForLogging(TimeSpan.FromMilliseconds(0.5)));
 }
 public void FormatForLogging(int ms, string expected)
 {
     Assert.AreEqual(expected, TimeSpanUtility.FormatForLogging(TimeSpan.FromMilliseconds(ms)));
 }