/// <summary> /// Load device list from xml file. /// </summary> /// <param name="path"></param> public void Load(string path) { try { NotifyLoadBegin(); IsLoading = true; ++FreezeEvents; Clear(); GXDeviceList list; GXSite site; if (GXJsonParser.IsJSONFile(path)) { GXJsonParser parser = new GXJsonParser(); parser.OnCreateObject += new CreateObjectEventhandler(ParserOnCreateObject); list = parser.LoadFile(path, typeof(GXDeviceList)) as GXDeviceList; parser.OnCreateObject -= new CreateObjectEventhandler(ParserOnCreateObject); } else { Type deviceType = null; System.Collections.Generic.List<Type> types = new System.Collections.Generic.List<Type>(); if (System.Environment.OSVersion.Platform != PlatformID.Unix) { foreach (GXProtocolAddIn it in GXDeviceList.Protocols.Values) { GetAddInInfo(it, out deviceType, types); } } using (FileStream reader = new FileStream(path, FileMode.Open)) { DataContractSerializer x = new DataContractSerializer(this.GetType(), types.ToArray()); list = (GXDeviceList)x.ReadObject(reader); reader.Close(); } } list.m_sched = null; this.Name = list.Name; this.CustomUI = list.CustomUI; //Move items to other collection. this.DeviceGroups.AddRange(list.DeviceGroups); foreach (GXDeviceGroup it in this.DeviceGroups) { it.Parent = this.DeviceGroups; } list.DeviceGroups.Clear(); //Move items to other collection. this.Schedules.AddRange(list.Schedules); foreach (GXSchedule it in this.Schedules) { it.Parent = this.Schedules; site = it as GXSite; site.NotifySerialized(false); } list.Schedules.Clear(); this.DisabledActions = list.DisabledActions; NotifySerialized(false, this.DeviceGroups); site = this as GXSite; site.NotifySerialized(false); foreach (GXSchedule schedule in this.Schedules) { string[] items = null; if (schedule.SerializedItems != null) { items = schedule.SerializedItems.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string it in items) { ulong id = ulong.Parse(it); object target = FindItemByID(id); schedule.Items.Add(target); } } if (schedule.SerializedExcludedItems != null) { items = schedule.SerializedExcludedItems.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string it in items) { ulong id = ulong.Parse(it); object target = FindItemByID(id); schedule.ExcludedItems.Add(target); } } } //Find event listeners. foreach(GXDevice it in this.DeviceGroups.GetDevicesRecursive()) { IGXEventHandler handler; if (!EventHandlers.ContainsKey(it.ProfileGuid)) { handler = EventHandlers[it.ProfileGuid] = null; foreach (Type type in it.GetType().Assembly.GetTypes()) { if (typeof(IGXEventHandler).IsAssignableFrom(type)) { handler = EventHandlers[it.ProfileGuid] = Activator.CreateInstance(type) as IGXEventHandler; handler.Clients = this; break; } } } else { handler = EventHandlers[it.ProfileGuid]; } if (handler != null) { it.GXClient.AddEventHandler(handler, this); } } this.FileName = path; this.Dirty = false; } finally { IsLoading = false; --FreezeEvents; NotifyLoadEnd(); } }
/// <summary> /// Load device settings from the XML File. /// </summary> /// <param name="filePath">Path to the XML file.</param> static public GXDevice Load(string filePath) { if (!File.Exists(filePath)) { System.Diagnostics.Debug.WriteLine(Resources.FailedToLoadFile + filePath); throw new FileNotFoundException(Resources.FailedToLoadFile + filePath); } GXDevice device = null; if (GXJsonParser.IsJSONFile(filePath)) { GXJsonParser parser = new GXJsonParser(); parser.OnCreateObject += new CreateObjectEventhandler(ParserOnCreateObject); device = parser.LoadFile(filePath, typeof(GXDevice)) as GXDevice; parser.OnCreateObject -= new CreateObjectEventhandler(ParserOnCreateObject); UpdateAttributes(device); } else { //If in old way. GXProtocolAddIn addIn; string protocolName = null, type = null; Guid deviceGuid; bool preset; GetProtocolInfo(filePath, out preset, out protocolName, out type, out deviceGuid); if (!GXDeviceList.Protocols.ContainsKey(protocolName)) { throw new Exception(Resources.InvalidProtocolAddIn + protocolName); } addIn = GXDeviceList.Protocols[protocolName]; System.Collections.Generic.List<Type> types = new System.Collections.Generic.List<Type>(); Type deviceType = null; GXDeviceList.GetAddInInfo(addIn, out deviceType, types); using (FileStream reader = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { DataContractSerializer x = new DataContractSerializer(deviceType, deviceType.Name, "", types.ToArray()); device = (GXDevice)x.ReadObject(reader); //Update serializated target after load. device.Keepalive.SerializedTarget = device.Keepalive.SerializedTarget; reader.Close(); } device.m_AddIn = addIn; UpdateAttributes(device); GXSite site = device as GXSite; site.NotifySerialized(false); foreach (GXCategory cat in device.Categories) { foreach (GXProperty prop in cat.Properties) { site = prop as GXSite; site.NotifySerialized(false); } site = cat as GXSite; site.NotifySerialized(false); } foreach (GXTable table in device.Tables) { foreach (GXProperty prop in table.Columns) { site = prop as GXSite; site.NotifySerialized(false); } site = table as GXSite; site.NotifySerialized(false); } } return device; }