public void PixelShaderEffect_FeatureLevelValidation() { const string hlsl = @" float4 main() : SV_Target { return 0; } "; var effect93 = new PixelShaderEffect(ShaderCompiler.CompileShader(hlsl, "ps_4_0_level_9_3")); var effect40 = new PixelShaderEffect(ShaderCompiler.CompileShader(hlsl, "ps_4_0")); using (var featureLevel93Device = DeviceCreator.CreateDevice(useFeatureLevel93: true)) using (var canvasDevice = CanvasDevice.CreateFromDirect3D11Device(featureLevel93Device)) { Assert.IsTrue(effect93.IsSupported(canvasDevice)); Assert.IsFalse(effect40.IsSupported(canvasDevice)); using (var renderTarget = new CanvasRenderTarget(canvasDevice, 1, 1, 96)) using (var drawingSession = renderTarget.CreateDrawingSession()) { drawingSession.DrawImage(effect93); Utils.AssertThrowsException <COMException>( () => drawingSession.DrawImage(effect40), "This shader requires a higher Direct3D feature level than is supported by the device. Check PixelShaderEffect.IsSupported before using it."); } } }
public void TestMethodCovariance() { ICreator <Device> deviceCreator = new DeviceCreator(); ICreator <Keyboard> keyboardCreator = new KeyboardCreator(); deviceCreator = keyboardCreator; }
public AddDeviceControl(List <Device> deviceList, DeviceManager manager) { devices = deviceList; this.manager = manager; creator = new DeviceCreator(manager, deviceList); ctrCreator = new ControlCreator(manager); Initializer(); }
private ThingsOfFoi LoadDevices(Configuration configuration) { ThingsOfFoi thingsOfFoi = new ThingsOfFoi(); Output output = Output.GetInstance(); List <Dictionary <string, string> > sensorsList = Csv.Parse(configuration.SensorsFilePath); List <Dictionary <string, string> > actuatorsList = Csv.Parse(configuration.ActuatorsFilePath); DeviceCreator deviceCreator = new DeviceCreator(); foreach (var sensor in sensorsList) { Device device = deviceCreator.CreateDevice(sensor, DeviceType.Sensor, thingsOfFoi); if (device.IsDeviceValid()) { thingsOfFoi.AddSensor(device); } else { output.WriteLine("Unos za uređaj: '" + device.Name + "' nije dobar. Preskačem!"); } } if (sensorsList.Count == 0 || thingsOfFoi.Sensors.Count == 0) { output.WriteLine("Nije učitan nijedan senzor. Program ne može nastaviti!"); return(null); } foreach (var actuator in actuatorsList) { Device device = deviceCreator.CreateDevice(actuator, DeviceType.Actuator, thingsOfFoi); if (device.IsDeviceValid()) { thingsOfFoi.AddActuator(device); } else { output.WriteLine("Unos za uređaj: '" + device.Name + "' nije dobar. Preskačem!"); } } if (actuatorsList.Count == 0 || thingsOfFoi.Actuators.Count == 0) { output.WriteLine("Nije učitan nijedan aktuator. Program ne može nastaviti!"); return(null); } return(thingsOfFoi); }
// GET: SmartHome public ActionResult Index() { smartHoseDevicesDictionary = new Dictionary <string, Device>(); DeviceCreator myDCreator = new DeviceCreator(); smartHoseDevicesDictionary.Add("Nord", myDCreator.CreateDevice("fridge")); smartHoseDevicesDictionary.Add("Mitsubishi", myDCreator.CreateDevice("conditioner")); smartHoseDevicesDictionary.Add("Spidola", myDCreator.CreateDevice("radio")); smartHoseDevicesDictionary.Add("Indesit", myDCreator.CreateDevice("oven")); smartHoseDevicesDictionary.Add("Siemens", myDCreator.CreateDevice("mwoven")); smartHoseDevicesDictionary.Add("Seiko", myDCreator.CreateDevice("radiolamp")); Session["Devices"] = smartHoseDevicesDictionary; return(View(smartHoseDevicesDictionary)); }
public ActionResult AddDevice(string nameOfDevice, string typeOfDevice) { smartHoseDevicesDictionary = (Dictionary <string, Device>)Session["Devices"]; DeviceCreator dc = new DeviceCreator(); try { switch (typeOfDevice) { case "fridge": smartHoseDevicesDictionary.Add(nameOfDevice, dc.CreateDevice("fridge")); break; case "mwoven": smartHoseDevicesDictionary.Add(nameOfDevice, dc.CreateDevice("mwoven")); break; case "oven": smartHoseDevicesDictionary.Add(nameOfDevice, dc.CreateDevice("oven")); break; case "radio": smartHoseDevicesDictionary.Add(nameOfDevice, dc.CreateDevice("radio")); break; case "radiolamp": smartHoseDevicesDictionary.Add(nameOfDevice, dc.CreateDevice("radiolamp")); break; case "conditioner": default: smartHoseDevicesDictionary.Add(nameOfDevice, dc.CreateDevice("conditioner")); break; } } catch { } Session["Devices"] = smartHoseDevicesDictionary; return(View("Index", smartHoseDevicesDictionary)); }
protected void Page_Init(object sender, EventArgs e) { if (IsPostBack) { devices = (List <Device>)Session["devices"]; channels = (List <Channel>)Session["channels"]; DeviceCreator deviceCreator = new DeviceCreator(manager, devices); controlCreator = new ControlCreatorByParts(manager); currentConsumption = ConsumptionCounter(); } else { controlCreator = new ControlCreatorByParts(manager); channels = manager.GetChannelList(); devices = manager.GetDevicesList(); Session["devices"] = devices; Session["channels"] = channels; currentConsumption = ConsumptionCounter(); } }
private ThingsOfFoi LoadDevices(Configuration configuration) { ThingsOfFoi thingsOfFoi = new ThingsOfFoi(); List <Dictionary <string, string> > sensorsList = CSV.Parse(configuration.SensorsFilePath); List <Dictionary <string, string> > actuatorsList = CSV.Parse(configuration.ActuatorsFilePath); DeviceCreator deviceCreator = new DeviceCreator(); Output output = GetOutput(configuration); foreach (var sensor in sensorsList) { Device device = deviceCreator.CreateDevice(sensor, DeviceCreator.DeviceType.Sensor); if (device.IsDeviceValid()) { thingsOfFoi.AddSensor(device); } else { output.WriteLine("Unos za uređaj: '" + device.Name + "' nije dobar. Preskačem!"); } } foreach (var actuator in actuatorsList) { Device device = deviceCreator.CreateDevice(actuator, DeviceCreator.DeviceType.Actuator); if (device.IsDeviceValid()) { thingsOfFoi.AddActuator(device); } else { output.WriteLine("Unos za uređaj: '" + device.Name + "' nije dobar. Preskačem!"); } } return(thingsOfFoi); }
private static DeviceFile FromTcxV2_Base(XmlDocument Doc) { var ns = new XmlNamespaceManager(Doc.NameTable); ns.AddNamespace("tcd", "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"); ns.AddNamespace("ae", "http://www.garmin.com/xmlschemas/ActivityExtension/v2"); XMLParserHelper.Manager = ns; //var Activities = Doc.SelectNodes("/tcd:TrainingCenterDatabase/tcd:Activities/tcd:Activity", ns); var Activities = Doc.GetElementsByTagName("Activity"); DeviceActivity CurrentActivity; DeviceLap CurrentLap; DevicePoint CurrentPoint; DeviceCreator CurrentCreator; DeviceAuthor CurrentAuthor; XmlNode WorkNode; XmlAttribute WorkAttr; DateTime dtTryParse; DateTimeOffset dtoTryParse; DeviceFile File = new DeviceFile(); CurrentAuthor = new DeviceAuthor(); File.Author = CurrentAuthor; string VMajor, VMinor, BMajor, BMinor; CurrentAuthor.Name = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Name"); CurrentAuthor.Language = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:LangID"); CurrentAuthor.PartNumber = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:PartNumber"); VMajor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:VersionMajor", "0"); VMinor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:VersionMinor", "0"); BMajor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:BuildMajor", "0"); BMinor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:BuildMinor", "0"); CurrentAuthor.Version = VMajor + "." + VMinor + "." + BMajor + "." + BMinor; foreach (XmlNode Activity in Activities) { CurrentActivity = new DeviceActivity(); File.Activities.Add(CurrentActivity); CurrentActivity.Id = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Id"); if (CurrentActivity.Id != null && DateTimeOffset.TryParse(CurrentActivity.Id, out dtoTryParse)) { //dtoTryParse = FSSecurity.Current.ToUserTime(dtoTryParse); dtTryParse = new DateTime(dtoTryParse.Year, dtoTryParse.Month, dtoTryParse.Day, dtoTryParse.Hour, dtoTryParse.Minute, dtoTryParse.Second); CurrentActivity.ActivityTime = dtTryParse; } CurrentActivity.Sport = XMLParserHelper.SelectSingleAttributeString(Activity, "Sport", "Unknown"); CurrentCreator = new DeviceCreator(); CurrentActivity.Creator = CurrentCreator; CurrentCreator.Name = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Name"); CurrentCreator.UnitID = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:UnitId"); CurrentCreator.ProductID = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:ProductID"); VMajor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:VersionMajor", "0"); VMinor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:VersionMinor", "0"); BMajor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:BuildMajor", "0"); BMinor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:BuildMinor", "0"); CurrentCreator.Version = VMajor + "." + VMinor + "." + BMajor + "." + BMinor; var CreatorName = CurrentCreator.Name ?? ""; var Laps = Activity.SelectNodes("tcd:Lap", ns); var IsFirst = true; foreach (XmlNode Lap in Laps) { var MyInt = XMLParserHelper.SelectSingleTextString(Lap, "tcd:Intensity", ""); // This is for trailing resting tomtom laps that have zero distance and time if (MyInt.ToLower() == "resting" && CreatorName.ToLower().IndexOf("tomtom") >= 0) { continue; } if (IsFirst) { IsFirst = false; var TestSeconds = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:TotalTimeSeconds", null); var TestDist = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:DistanceMeters", null); if (TestSeconds == 0 && TestDist == 0) { continue; } } CurrentLap = new DeviceLap(); CurrentActivity.Laps.Add(CurrentLap); WorkAttr = Lap.Attributes["StartTime"]; if (null != WorkAttr && null != CurrentActivity.ActivityTime && DateTimeOffset.TryParse(WorkAttr.Value, out dtoTryParse)) { //dtoTryParse = FSSecurity.Current.ToUserTime(dtoTryParse); dtTryParse = new DateTime(dtoTryParse.Year, dtoTryParse.Month, dtoTryParse.Day, dtoTryParse.Hour, dtoTryParse.Minute, dtoTryParse.Second); CurrentLap.StartSeconds = (dtTryParse - (CurrentActivity.ActivityTime ?? DateTime.Now)).TotalSeconds; } CurrentLap.Time = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:TotalTimeSeconds", null); CurrentLap.Distance = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:DistanceMeters", null); CurrentLap.SpeedAvg = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:Extensions/ae:LX/ae:AvgSpeed", null); CurrentLap.SpeedMax = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:MaximumSpeed", null); CurrentLap.Calories = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Calories", null); CurrentLap.RPMAvg = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Cadence", null); CurrentLap.RPMMax = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Extensions/ae:LX/ae:MaxBikeCadence", null); CurrentLap.HeartRateAvg = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:AverageHeartRateBpm/tcd:Value", null); CurrentLap.HeartRateMax = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:MaximumHeartRateBpm/tcd:Value", null); CurrentLap.WattsAvg = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Extensions/ae:LX/ae:AvgWatts", null); CurrentLap.WattsMax = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Extensions/ae:LX/ae:MaxWatts", null); var TrackPoints = Lap.SelectNodes("tcd:Track/tcd:Trackpoint", ns); DevicePoint LastTrackPoint = null; foreach (XmlNode Point in TrackPoints) { CurrentPoint = new DevicePoint(); CurrentLap.Track.Add(CurrentPoint); WorkNode = Point.SelectSingleNode("tcd:Time", ns); if (null != WorkNode && null != CurrentActivity.ActivityTime && DateTimeOffset.TryParse(WorkNode.InnerText, out dtoTryParse)) { //dtoTryParse = FSSecurity.Current.ToUserTime(dtoTryParse); dtTryParse = new DateTime(dtoTryParse.Year, dtoTryParse.Month, dtoTryParse.Day, dtoTryParse.Hour, dtoTryParse.Minute, dtoTryParse.Second); CurrentPoint.StartSeconds = (dtTryParse - (CurrentActivity.ActivityTime ?? DateTime.Now)).TotalSeconds; } CurrentPoint.Latitude = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:Position/tcd:LatitudeDegrees", null); CurrentPoint.Longitude = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:Position/tcd:LongitudeDegrees", null); CurrentPoint.Altitude = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:AltitudeMeters", null); CurrentPoint.Distance = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:DistanceMeters", null); CurrentPoint.HR = XMLParserHelper.SelectSingleTextInt(Point, "tcd:HeartRateBpm/tcd:Value", null); CurrentPoint.RPM = XMLParserHelper.SelectSingleTextInt(Point, "tcd:Cadence", null); CurrentPoint.CAD = XMLParserHelper.SelectSingleTextInt(Point, "tcd:Extensions/ae:TPX/ae:RunCadence", null); if (CurrentPoint.CAD != null) { CurrentPoint.CAD = CurrentPoint.CAD.Value * 2; } CurrentPoint.Speed = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:Extensions/ae:TPX/ae:Speed", null); CurrentPoint.Watts = XMLParserHelper.SelectSingleTextInt(Point, "tcd:Extensions/ae:TPX/ae:Watts", null); if (LastTrackPoint != null && CurrentPoint.Speed == null && (CurrentPoint.Distance != null && CurrentPoint.StartSeconds != null) && (LastTrackPoint.Distance != null && LastTrackPoint.StartSeconds != null)) { // Last Distance and Time vs Current Distance and Time -> var SecondDelta = (decimal)(CurrentPoint.StartSeconds - LastTrackPoint.StartSeconds); var DistanceDelta = CurrentPoint.Distance - LastTrackPoint.Distance; if (SecondDelta == 0 || DistanceDelta == 0) { CurrentPoint.Speed = 0; } else { CurrentPoint.Speed = DistanceDelta / SecondDelta; } } LastTrackPoint = CurrentPoint; } var CadencePoints = from x in CurrentLap.Track where x.CAD != null select x; if (CadencePoints.Any()) { var m = (from x in CadencePoints select x.CAD).Max(); var a = (from x in CadencePoints select x.CAD).Average(); CurrentLap.CADAvg = (int?)a; CurrentLap.CADMax = (int?)m; } } } return(File); }
static void Main(string[] args) { DeviceCreator myDCreator = new DeviceCreator(); ConsoleUIDevice myUI = new ConsoleUIDevice(); FileOperations fOp = new FileOperations(); Dictionary <string, Device> smartHoseDevices = new Dictionary <string, Device>(); //Dictionary<string, Device> smartHoseDevices = new Dictionary<string, Device>(); //smartHoseDevices.Add("nord", myDCreator.CreateDevice("fridge")); //smartHoseDevices.Add("mitsubishi", myDCreator.CreateDevice("conditioner")); //smartHoseDevices.Add("spidola", myDCreator.CreateDevice("radio")); //smartHoseDevices.Add("indesit", myDCreator.CreateDevice("oven")); //smartHoseDevices.Add("siemens", myDCreator.CreateDevice("mwoven")); //smartHoseDevices.Add("seiko", myDCreator.CreateDevice("radiolamp")); while (true) {//while (true) Console.WriteLine("Манипуляции с выключенными устройствами НЕВОЗМОЖНЫ!\nПроверяйте статус устройства."); foreach (var s in smartHoseDevices) { Console.WriteLine(s.Key + "-" + s.Value); } Console.WriteLine(); Console.Write("Enter command: "); string[] commands = Console.ReadLine().ToLower().Split(' '); switch (commands[0]) { case "file": SaveLoadMenu(smartHoseDevices); break; case "exit": return; case "add": try { smartHoseDevices.Add(commands[1], myDCreator.CreateDevice(commands[2])); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); } break; case "del": smartHoseDevices.Remove(commands[1]); break; case "power": var res = smartHoseDevices.Where(s => s.Key == commands[1]); foreach (var device in res) { device.Value.Power(); } break; case "modework": var res1 = smartHoseDevices.Where(s => s.Key == commands[1] && s.Value is IModeable); foreach (var device in res1) { myUI.ControlWithModeable((IModeable)device.Value); } //тут неясно как сообщить о несуществующем интерфейсе break; case "controlvolume": var res2 = smartHoseDevices.Where(s => s.Key == commands[1]); foreach (var device in res2) { if (device.Value is IVolumeable) { myUI.ControlWithVolumeable((IVolumeable)device.Value); } else { Console.WriteLine("Device does not have such a function!\nPress any key"); Console.ReadKey(); } } break; case "controlchannel": var res3 = smartHoseDevices.Where(s => s.Key == commands[1]); foreach (var device in res3) { if (device.Value is IChannelable) { myUI.ControlWithIChannelable((IChannelable)device.Value); } else { Console.WriteLine("Device does not have such a function!\nPress any key"); Console.ReadKey(); } } break; case "controltemp": var res4 = smartHoseDevices.Where(s => s.Key == commands[1]); foreach (var device in res4) { if (device.Value is IFridgeable) { myUI.ControlWithFridgeable((IFridgeable)device.Value); } else { Console.WriteLine("Device does not have such a function!\nPress any key"); Console.ReadKey(); } } break; case "controlbrightness": var res5 = smartHoseDevices.Where(s => s.Key == commands[1]); foreach (var device in res5) { if (device.Value is ILightable) { myUI.ControlWithLightable((ILightable)device.Value); } else { Console.WriteLine("Device does not have such a function!\nPress any key"); Console.ReadKey(); } } break; case "door": var res6 = smartHoseDevices.Where(s => s.Key == commands[1]); foreach (var device in res6) { if (device.Value is IDoorable) { myUI.ControlWithIDoorable((IDoorable)device.Value); } else { Console.WriteLine("Device does not have such a function!\nPress any key"); Console.ReadKey(); } } break; default: Help(); break; } }//while (true) }
private ThingsOfFoi LoadDevices(Configuration configuration) { ThingsOfFoi thingsOfFoi = new ThingsOfFoi(); List <Dictionary <string, string> > sensorsList = Csv.Parse(configuration.SensorsFilePath); List <Dictionary <string, string> > actuatorsList = Csv.Parse(configuration.ActuatorsFilePath); DeviceCreator deviceCreator = new DeviceCreator(); foreach (var row in sensorsList) { Sensor sensor = (Sensor)deviceCreator.CreateDevice(row, DeviceType.Sensor, thingsOfFoi); if (sensor.IsDeviceValid()) { if (!thingsOfFoi.Sensors.Exists(s => s.ModelIdentifier == sensor.ModelIdentifier)) { thingsOfFoi.AddSensor(sensor); } else { Data.Add("Model senzora sa ID-jem: '" + sensor.UniqueIdentifier + " (" + sensor.Name + ") " + "' vec postoji. Preskacem!"); } } else { Data.Add("Unos za uredaj: '" + sensor.Name + "' nije dobar. Preskacem!"); } } if (sensorsList.Count == 0 || thingsOfFoi.Sensors.Count == 0) { Data.Add("Nije ucitan nijedan senzor. Program ne moze nastaviti!"); return(null); } foreach (var row in actuatorsList) { Actuator actuator = (Actuator)deviceCreator.CreateDevice(row, DeviceType.Actuator, thingsOfFoi); if (actuator.IsDeviceValid()) { if (!thingsOfFoi.Actuators.Exists(a => a.ModelIdentifier == actuator.ModelIdentifier)) { thingsOfFoi.AddActuator(actuator); } else { Data.Add("Aktuator sa ID-jem: " + actuator.UniqueIdentifier + " (" + actuator.Name + ") " + " vec postoji. Preskacem!"); } } else { Data.Add("Unos za uredaj: '" + actuator.Name + "' nije dobar. Preskacem!"); } } if (actuatorsList.Count == 0 || thingsOfFoi.Actuators.Count == 0) { Data.Add("Nije ucitan nijedan aktuator. Program ne moze nastaviti!"); return(null); } return(thingsOfFoi); }
private static DeviceFile FromGPXV1_Base(XmlDocument document, decimal lapIntervalMeters) { var nav = document.CreateNavigator(); nav.MoveToFollowing(XPathNodeType.Element); if (nav == null) { throw new Exception("File Type Not Supported"); } var namespaces = nav.GetNamespacesInScope(XmlNamespaceScope.Local); if (namespaces == null || !namespaces.Keys.Any()) { throw new Exception("File Type Not Supported"); } var namespacePrefixes = namespaces.Join( ValidNamespaces, o => o.Value.ToLower(), i => i.Value.ToLower(), (i, o) => new { Type = o.Key, Prefix = string.IsNullOrWhiteSpace(i.Key) ? "gpx" : i.Key, Url = o.Value }) .ToList(); var prefixGpx = namespacePrefixes.Single(x => x.Type == NamespaceType.GPX).Prefix; var prefixTrackPointExt = namespacePrefixes.SingleOrDefault(x => x.Type == NamespaceType.TrackPointExtensions)?.Prefix; var prefixCluetrustExt = namespacePrefixes.SingleOrDefault(x => x.Type == NamespaceType.CluetrustExtensions)?.Prefix; var namespaceManager = new XmlNamespaceManager(document.NameTable); namespacePrefixes.ForEach(x => namespaceManager.AddNamespace(x.Prefix, x.Url)); XMLParserHelper.Manager = namespaceManager; var creator = new DeviceCreator { Name = XMLParserHelper.SelectSingleAttributeString( document.DocumentElement, "creator"), Version = XMLParserHelper.SelectSingleAttributeString( document.DocumentElement, "version") }; var author = new DeviceAuthor { Name = creator.Name, Version = creator.Version }; var file = new DeviceFile(); file.Author = author; DateTimeOffset dtoFileTime; DateTime? fileTime = null; var fileTimeString = XMLParserHelper.SelectSingleTextString(document, $"/{prefixGpx}:gpx/{prefixGpx}:metadata/{prefixGpx}:time"); if (!string.IsNullOrWhiteSpace(fileTimeString) && DateTimeOffset.TryParse(fileTimeString, out dtoFileTime)) { // TODO: dtoFileTime = FSSecurity.Current.ToUserTime(dtoFileTime); fileTime = dtoFileTime.DateTime; } // Walk through all tracks and assign to Activities; usually just 1... var activityNodes = document.GetElementsByTagName("trk"); foreach (XmlNode activityNode in activityNodes) { var activity = new DeviceActivity(); file.Activities.Add(activity); activity.Id = XMLParserHelper.SelectSingleTextString(activityNode, $"{prefixGpx}:name"); activity.Creator = creator; activity.Sport = XMLParserHelper.SelectSingleTextString(activityNode, $"{prefixGpx}:type", "Unknown"); activity.ActivityTime = fileTime; // Parse entire list of track points... var allTrackPoints = new List <DevicePoint>(); var trackPointNodes = activityNode.SelectNodes($"{prefixGpx}:trkseg/{prefixGpx}:trkpt", namespaceManager); if (trackPointNodes != null) { DateTime?activityStartTimeUtc = null; foreach (XmlNode trackPointNode in trackPointNodes) { var point = new DevicePoint(); var pointTimeUtc = XMLParserHelper.SelectSingleTextDateTime(trackPointNode, $"{prefixGpx}:time"); if (pointTimeUtc != null) { if (activityStartTimeUtc == null) { activityStartTimeUtc = pointTimeUtc; point.StartSeconds = 0; point.Distance = 0; } else { point.StartSeconds = (pointTimeUtc.Value - activityStartTimeUtc.Value.AddSeconds(1)).TotalSeconds; } } // Parse latitude / longitude... point.Latitude = XMLParserHelper.SelectSingleAttributeDecimal(trackPointNode, "lat"); point.Longitude = XMLParserHelper.SelectSingleAttributeDecimal(trackPointNode, "lon"); // Parse altitude... point.Altitude = XMLParserHelper.SelectSingleTextDecimal( trackPointNode, $"{prefixGpx}:ele"); // Parse TrackPoint extension data... if (prefixTrackPointExt != null) { // Parse heart rate... point.HR = XMLParserHelper.SelectSingleTextInt( trackPointNode, $"{prefixGpx}:extensions/{prefixTrackPointExt}:TrackPointExtension/{prefixTrackPointExt}:hr"); // Parse ambient temp... point.Temp = XMLParserHelper.SelectSingleTextInt( trackPointNode, $"{prefixGpx}:extensions/{prefixTrackPointExt}:TrackPointExtension/{prefixTrackPointExt}:atemp"); // Parse cadence... point.CAD = XMLParserHelper.SelectSingleTextInt( trackPointNode, $"{prefixGpx}:extensions/{prefixTrackPointExt}:TrackPointExtension/{prefixTrackPointExt}:cad"); } // Parse Cluetrust extension data... if (prefixCluetrustExt != null) { // Parse heart rate... if (point.HR == null) { point.HR = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixCluetrustExt}:hr"); } // Parse ambient temp... if (point.Temp == null) { point.Temp = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixCluetrustExt}:temp"); } // Parse cadence... if (point.CAD == null) { point.CAD = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixCluetrustExt}:cadence"); } } // Parse any generic extensions data... // Parse heart rate... if (point.HR == null) { point.HR = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:heartrate"); } // Parse cadence... if (point.CAD == null) { point.CAD = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:cadence"); } // Parse distance... if (point.Distance == null) { point.Distance = XMLParserHelper.SelectSingleTextDecimal(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:distance"); } // Parse power... if (point.Watts == null) { point.Watts = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:power"); } // Multiply CAD result x 2 like in TCX? // TODO: Bike no...Run yes? if (point.CAD != null) { point.CAD = point.CAD * 2; } allTrackPoints.Add(point); } } // Re-run through point list to calculate distances and break up into laps... var pointsHaveTimeInfo = allTrackPoints.All(p => p.StartSeconds.HasValue); var lapList = new List <DeviceLap>(); var currentLap = new DeviceLap { StartSeconds = pointsHaveTimeInfo ? (double?)0 : null }; for (var i = 1; i < allTrackPoints.Count; i++) { var currentPoint = allTrackPoints[i]; var previousPoint = allTrackPoints[i - 1]; if (currentLap.Track.Count == 0) { var previousLap = activity.Laps.LastOrDefault(); if (previousLap != null && pointsHaveTimeInfo) { previousLap.Time = (decimal)(currentPoint.StartSeconds.Value - previousLap.StartSeconds.Value); } activity.Laps.Add(currentLap); } decimal?currentPointDistanceDelta = null; if (currentPoint.Distance == null) { if (currentPoint.Latitude == null || currentPoint.Longitude == null || previousPoint.Latitude == null || previousPoint.Longitude == null) { continue; } var distanceKm = HaversineInKM( (double)previousPoint.Latitude.Value, (double)previousPoint.Longitude.Value, (double)currentPoint.Latitude.Value, (double)currentPoint.Longitude.Value); currentPointDistanceDelta = (decimal)(distanceKm * 1000.0); currentPoint.Distance = currentPointDistanceDelta + (previousPoint.Distance ?? 0); } if (i == 1) { previousPoint.Distance = previousPoint.Distance ?? 0; currentLap.Track.Add(previousPoint); } currentLap.Track.Add(currentPoint); if (pointsHaveTimeInfo) { var startSecondsDelta = (decimal)(currentPoint.StartSeconds.Value - previousPoint.StartSeconds.Value); startSecondsDelta = startSecondsDelta == 0 ? 1 : startSecondsDelta; currentPointDistanceDelta = currentPointDistanceDelta ?? currentPoint.Distance - previousPoint.Distance; currentPoint.Speed = currentPointDistanceDelta / startSecondsDelta; if (currentLap.StartSeconds == null) { currentLap.StartSeconds = currentPoint.StartSeconds; } } currentLap.Distance = currentLap.Track.Last().Distance.Value - currentLap.Track.First().Distance.Value; if (currentLap.Distance.Value < lapIntervalMeters) { continue; } // Reached the end of the lap, start a new one... currentLap = new DeviceLap { StartSeconds = currentLap.Track.Last().StartSeconds }; } // Calculate Time for last lap... var lastLap = activity.Laps.Last(); if (pointsHaveTimeInfo) { lastLap.Time = (decimal)(lastLap.Track.Last().StartSeconds.Value - lastLap.Track.First().StartSeconds.Value); } // Loop through resulting laps to calculate Time and remaining aggregates... activity.Laps.ForEach(lap => { // Calculate HRMax and HRAvg from lap's collection of HR points... var hrPoints = lap.Track.Where(t => t.HR != null).Select(x => x.HR.Value).ToList(); lap.HeartRateMax = hrPoints.Any() ? (int?)hrPoints.Max() : null; lap.HeartRateAvg = hrPoints.Any() ? (int?)hrPoints.Average() : null; // Calculate CADMax and CADAvg from lap's collection of CAD points... var cadPoints = lap.Track.Where(t => t.CAD != null).Select(x => x.CAD.Value).ToList(); lap.CADMax = cadPoints.Any() ? (int?)cadPoints.Max() : null; lap.CADAvg = cadPoints.Any() ? (int?)cadPoints.Average() : null; // Calculate TempMax and TempAvg from lap's collection of Temp points... var tempPoints = lap.Track.Where(t => t.Temp != null).Select(x => x.Temp.Value).ToList(); lap.TempMax = tempPoints.Any() ? (int?)tempPoints.Max() : null; lap.TempAvg = tempPoints.Any() ? (int?)tempPoints.Average() : null; // Calculage SpeedMax and SpeedAvg from lap's collection of points... var speedPoints = lap.Track.Where(t => t.Speed != null).Select(x => x.Speed.Value).ToList(); lap.SpeedMax = speedPoints.Any() ? (decimal?)speedPoints.Max() : null; lap.SpeedAvg = speedPoints.Any() ? (decimal?)speedPoints.Average() : null; }); } return(file); }
/// <summary> /// Initializes a new instance of the <see cref="Device"/> class. /// </summary> /// <param name="deviceCreator"> /// The device Creator. /// </param> public Device(DeviceCreator deviceCreator) { this.configurator = deviceCreator.CreateConfigurator(); this.testor = deviceCreator.CreateTestor(); }