/// <summary> /// Returns the named value if it exists in the parameter string otherwise /// returns a specified default value. /// </summary> /// <param name="name">The name.</param> /// <param name="def">The default value.</param> /// <returns>The named value from the parameter string if it is present, the default value otherwise.</returns> public DateTime Get(string name, DateTime def) { var value = this[name]; return(value != null?Serialize.Parse(value, def) : def); }
//--------------------------------------------------------------------- // IParsable implementation /// <summary> /// Attempts to parse the configuration value. /// </summary> /// <param name="value">The configuration value.</param> /// <returns><c>true</c> if the value could be parsed, <b></b> if the value is not valid for the type.</returns> public bool TryParse(string value) { string[] fields; double minutes = 0; double seconds = 0; if (value == null) { return(false); } if (value.ToLower().StartsWith("disabled")) { this.type = RecurringTimerType.Disabled; return(true); } fields = value.Split(','); switch (fields[0].Trim().ToLower()) { case "minute": this.type = RecurringTimerType.Minute; this.timeOffset = TimeSpan.Zero; return(true); case "quarterhour": if (fields.Length != 2) { return(false); } fields = fields[1].Split(':'); if (fields.Length > 2) { return(false); } this.type = RecurringTimerType.QuarterHour; try { minutes = double.Parse(fields[0]); if (fields.Length > 1) { seconds = double.Parse(fields[1]); } timeOffset = TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds); if (timeOffset >= TimeSpan.FromMinutes(15)) { timeOffset = TimeSpan.Zero; } } catch { return(false); } return(true); case "hourly": if (fields.Length != 2) { return(false); } fields = fields[1].Split(':'); if (fields.Length > 2) { return(false); } this.type = RecurringTimerType.Hourly; try { minutes = double.Parse(fields[0]); if (fields.Length > 1) { seconds = double.Parse(fields[1]); } timeOffset = TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds); if (timeOffset >= TimeSpan.FromHours(1)) { timeOffset = TimeSpan.Zero; } } catch { return(false); } return(true); case "daily": TimeOfDay timeOfDay; if (fields.Length != 2) { return(false); } if (!TimeOfDay.TryParse(fields[1], out timeOfDay)) { return(false); } this.type = RecurringTimerType.Daily; this.timeOffset = timeOfDay.TimeSpan; if (timeOffset >= TimeSpan.FromDays(1)) { timeOffset = TimeSpan.Zero; } return(true); case "interval": if (fields.Length != 2) { return(false); } timeOffset = Serialize.Parse(fields[1], TimeSpan.FromSeconds(-1)); if (timeOffset < TimeSpan.Zero) { return(false); } this.type = RecurringTimerType.Interval; return(true); default: return(false); } }
/// <summary> /// Sets a name/value pair in the collection. /// </summary> /// <param name="name">The name.</param> /// <param name="value">The value.</param> public void Set(string name, NetworkBinding value) { Set(name, Serialize.ToString(value)); }
/// <summary> /// Sets a name/value pair in the collection. /// </summary> /// <param name="name">The name.</param> /// <param name="value">The value.</param> public void Set(string name, Uri value) { Set(name, Serialize.ToString(value)); }
/// <summary> /// Sets a name/value pair in the collection. /// </summary> /// <param name="name">The name.</param> /// <param name="value">The value.</param> public void Set(string name, IPAddress value) { Set(name, Serialize.ToString(value)); }
/// <summary> /// Sets a name/value pair in the collection. /// </summary> /// <param name="name">The name.</param> /// <param name="value">The value.</param> public void Set(string name, DateTime value) { Set(name, Serialize.ToString(value)); }
/// <summary> /// Sets a name/value pair in the collection. /// </summary> /// <param name="name">The name.</param> /// <param name="value">The value.</param> public void Set(string name, TimeSpan value) { Set(name, Serialize.ToString(value)); }
/// <summary> /// Parses a string into a position record. /// </summary> /// <param name="input">The input string in the same format as generated by <see cref="ToString()" />.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="input" /> is <c>null</c>.</exception> /// <exception cref="FormatException">Thrown if the input string is not correctly formatted.</exception> public GeoFix(string input) : this() { if (input == null) { throw new ArgumentNullException("input"); } var fields = input.Split(','); if (fields.Length < 3) { throw new FormatException("Bad GeoFix record: at least [3] comma separated fields are expected."); } try { if (string.IsNullOrWhiteSpace(fields[0])) { TimeUtc = null; } else { TimeUtc = Serialize.Parse(fields[0], DateTime.MinValue); } if (!string.IsNullOrWhiteSpace(fields[1]) && !string.IsNullOrWhiteSpace(fields[2])) { Latitude = double.Parse(fields[1]); Longitude = double.Parse(fields[2]); } if (fields.Length == 3) { return; } if (!string.IsNullOrWhiteSpace(fields[3])) { Altitude = double.Parse(fields[3]); } if (fields.Length == 4) { return; } if (!string.IsNullOrWhiteSpace(fields[4])) { Course = double.Parse(fields[4]); } if (fields.Length == 5) { return; } if (!string.IsNullOrWhiteSpace(fields[5])) { Speed = double.Parse(fields[5]); } if (fields.Length == 6) { return; } if (!string.IsNullOrWhiteSpace(fields[6])) { HorizontalAccuracy = double.Parse(fields[6]); } if (fields.Length == 7) { return; } if (!string.IsNullOrWhiteSpace(fields[7])) { VerticalAccurancy = double.Parse(fields[7]); } if (fields.Length == 8) { return; } if (!string.IsNullOrWhiteSpace(fields[8])) { Technology = Serialize.Parse <GeoFixTechnology>(fields[8], GeoFixTechnology.Unknown); } if (fields.Length == 9) { return; } if (!string.IsNullOrWhiteSpace(fields[9])) { NetworkStatus = Serialize.Parse <NetworkStatus>(fields[9], NetworkStatus.Unknown); } } catch (Exception e) { throw new FormatException(string.Format("Bad GeoFix record: {0}", e.Message)); } }
/// <summary> /// Serializes the specified fields of the position record into a string suitable for logging or transmitting. /// </summary> /// <param name="fields">A <see cref="GeoFixField" /> bitmap describing the fields to be written.</param> /// <returns>The serialized record.</returns> public string ToString(GeoFixField fields) { return(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", (fields & GeoFixField.TimeUtc) != 0 && TimeUtc.HasValue ? Serialize.ToString(TimeUtc.Value) : string.Empty, (fields & GeoFixField.Latitude) != 0 && !double.IsNaN(Latitude) ? Latitude.ToString() : string.Empty, (fields & GeoFixField.Longitude) != 0 && !double.IsNaN(Longitude) ? Longitude.ToString() : string.Empty, (fields & GeoFixField.Altitude) != 0 && !double.IsNaN(Altitude) ? Altitude.ToString() : string.Empty, (fields & GeoFixField.Course) != 0 && !double.IsNaN(Course) ? Course.ToString() : string.Empty, (fields & GeoFixField.Speed) != 0 && !double.IsNaN(Speed) ? Speed.ToString() : string.Empty, (fields & GeoFixField.HorizontalAccuracy) != 0 && !double.IsNaN(HorizontalAccuracy) ? HorizontalAccuracy.ToString() : string.Empty, (fields & GeoFixField.VerticalAccurancy) != 0 && !double.IsNaN(VerticalAccurancy) ? VerticalAccurancy.ToString() : string.Empty, (fields & GeoFixField.Technology) != 0 && Technology != GeoFixTechnology.Unknown ? Technology.ToString() : string.Empty, (fields & GeoFixField.NetworkStatus) != 0 && NetworkStatus != NetworkStatus.Unknown ? NetworkStatus.ToString() : string.Empty)); }