/// <summary> /// Remove selected device template /// </summary> /// <param name="request"></param> /// <returns></returns> public GXDeviceProfilesDeleteResponse Post(GXDeviceProfilesDeleteRequest request) { List <GXEventsItem> events = new List <GXEventsItem>(); IAuthSession s = this.GetSession(false); int id = Convert.ToInt32(s.Id); if (id == 0) { throw new ArgumentException("Remove failed. Invalid session ID."); } bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s); lock (Db) { foreach (ulong it in request.DeviceProfileIDs) { if (it == 0) { throw new ArgumentException("ID is required"); } if (!superAdmin && GetDeviceProfiles(s, Db, id, 0, false, null, false).Count == 0) { throw new ArgumentException("Access denied."); } #if !SS4 GXAmiDeviceProfile dt = Db.GetById <GXAmiDeviceProfile>(it); #else GXAmiDeviceProfiles dt = Db.SingleById <GXAmiDeviceProfiles>(it); #endif if (request.Permanently) { Db.DeleteById <GXAmiDeviceProfile>(it); } else { Db.UpdateOnly(new GXAmiDeviceProfile { Removed = DateTime.Now }, p => p.Removed, p => p.Id == it); } events.Add(new GXEventsItem(ActionTargets.DeviceProfile, Actions.Remove, dt)); } } AppHost host = this.ResolveService <AppHost>(); host.SetEvents(Db, this.Request, 0, events); return(new GXDeviceProfilesDeleteResponse()); }
/// <summary> /// Notify from new device profile. /// </summary> /// <param name="Db"></param> /// <param name="template"></param> /// <param name="e"></param> void HandleDeviceProfiles(GXAmiDeviceProfile template, GXEventsItem e) { ulong mask = (ulong)((int)e.Target << 16 | (int)e.Action); //Find is anyone interested from this device template event. foreach (GXSession it in Sessions) { foreach (GXEvent e1 in it.NotifyClients) { if (e1.UserID != 0 && (mask & e1.Mask) != 0) { //Notify only super admin from new templates. if (e1.SuperAdmin) { e1.Rows.Add(e); it.Received.Set(); } } } } }
public GXDeviceProfilesResponse(GXAmiDeviceProfile[] profiles) { this.Profiles = profiles; }
public List<object[]> Import(string path, string target, out byte[] data, out string assemblyName, out string addInType) { data = null; TargetDirectory = target; AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); GXDeviceProfile type = GXZip.Import(null, path, target); GXDeviceList.Update(target); string filename = Path.Combine(target, type.DeviceGuid + ".gxp"); using (FileStream s = File.OpenRead(filename)) { long size = s.Length; size = 0; } GXDevice device = GXDevice.Load(filename); addInType = device.AddIn.ToString(); assemblyName = device.AddIn.GetType().Assembly.FullName; GXAmiDeviceProfile dt = new GXAmiDeviceProfile(); dt.Guid = device.Guid; dt.Protocol = device.ProtocolName; dt.Profile = device.DeviceProfile; dt.PresetName = device.PresetName; dt.Manufacturer = device.Manufacturer; dt.Model = device.Model; dt.Version = device.Version; dt.UpdateInterval = device.UpdateInterval; dt.WaitTime = device.WaitTime; dt.ResendCount = device.ResendCount; dt.Added = DateTime.Now.ToUniversalTime(); List<GXAmiMediaType> list = new List<GXAmiMediaType>(); List<string> medias = new List<string>(Gurux.Communication.GXClient.GetAvailableMedias()); foreach (GXMediaType it in device.AllowedMediaTypes) { GXAmiMediaType mt = new GXAmiMediaType(); mt.Name = it.Name; //If default media settings are not given, ask them from the parser. if (string.IsNullOrEmpty(it.DefaultMediaSettings) && medias.Contains(it.Name)) { Gurux.Common.IGXMedia m = device.GXClient.SelectMedia(it.Name); mt.Settings = m.Settings; } else { mt.Settings = it.DefaultMediaSettings; } list.Add(mt); } dt.AllowedMediaTypes = list.ToArray(); List<object[]> items = new List<object[]>(); items.Add(new object[]{dt, dt}); ImportParameters(device, device.ID, dt, items); foreach (GXCategory cat in device.Categories) { GXAmiCategoryTemplate ct = new GXAmiCategoryTemplate(); items.Add(new object[]{dt, ct}); ct.Name = cat.Name; ct.Id = cat.ID; ImportParameters(cat, cat.ID, ct, items); foreach (GXProperty prop in cat.Properties) { GXAmiPropertyTemplate pt = new GXAmiPropertyTemplate(); pt.Id = prop.ID; pt.ParentID = cat.ID; pt.Name = prop.Name; pt.Unit = prop.Unit; if (prop.ValueType != null) { pt.TypeAsString = prop.ValueType.ToString(); } pt.AccessMode = prop.AccessMode; items.Add(new object[]{dt, pt}); ImportParameters(prop, prop.ID, pt, items); } } foreach (GXTable table in device.Tables) { GXAmiDataTableTemplate tt = new GXAmiDataTableTemplate(); items.Add(new object[] { dt, tt }); tt.Name = table.Name; tt.Id = table.ID; ImportParameters(table, table.ID, tt, items); foreach (GXProperty prop in table.Columns) { GXAmiPropertyTemplate pt = new GXAmiPropertyTemplate(); pt.Id = prop.ID; pt.ParentID = table.ID; pt.Name = prop.Name; pt.Unit = prop.Unit; if (prop.ValueType != null) { pt.TypeAsString = prop.ValueType.ToString(); } pt.AccessMode = prop.AccessMode; items.Add(new object[] { dt, pt }); ImportParameters(prop, prop.ID, pt, items); } } return items; }
void Client_OnDeviceProfilesAdded(object sender, GXAmiDeviceProfile[] profiles) { if (profiles != null) { if (this.InvokeRequired) { this.BeginInvoke(new DeviceProfilesAddedEventHandler(OnDeviceProfilesAdded), sender, profiles); } else { OnDeviceProfilesAdded(sender, profiles); } } }
/// <summary> /// Show device profiles. /// </summary> /// <param name="sender"></param> /// <param name="profiles"></param> void OnDeviceProfilesAdded(object sender, GXAmiDeviceProfile[] profiles) { List<ListViewItem> list = new List<ListViewItem>(); foreach (GXAmiDeviceProfile it in profiles) { ListViewItem li = new ListViewItem(new string[]{it.Manufacturer, it.Model, it.Version, it.PresetName, it.Protocol, it.Profile, it.ProfileVersion.ToString()}); li.Tag = it; list.Add(li); } DeviceProfilesList.Items.AddRange(list.ToArray()); }
/// <summary> /// Remove selected device profile. /// </summary> /// <param name="sender"></param> /// <param name="profiles"></param> void OnDeviceProfilesRemoved(object sender, GXAmiDeviceProfile[] profiles) { foreach(GXAmiDeviceProfile dt in profiles) { foreach (ListViewItem it in DeviceProfilesList.Items) { if ((it.Tag as GXAmiDeviceProfile).Id == dt.Id) { it.Remove(); break; } } } }
GXAmiDeviceProfile FindDeviceProfiles(ulong id, GXAmiDeviceProfile[] templates) { if (id == 0) { return null; } foreach (GXAmiDeviceProfile it in templates) { if (it.Id == Device.ProfileId) { return it; } } return null; }
public GXDeviceProfilesUpdateResponse(long[] groups, GXAmiDeviceProfile profile) { UserGroups = groups; this.Profile = profile; }
/// <summary> /// Return all device profiles that user can access. /// </summary> /// <param name="request"></param> /// <returns></returns> public GXDeviceProfilesResponse Post(GXDeviceProfilesRequest request) { lock (Db) { IAuthSession s = this.GetSession(false); List <GXAmiDeviceProfile> list; //Returns all devices profiles of the user. if (request.UserID != 0) { list = GetDeviceProfiles(s, Db, request.UserID, 0, request.Preset, request.Protocol, request.Removed); } //Returns all devices profiles from the user goup else if (request.UserGroupID != 0) { list = GetDeviceProfiles(s, Db, 0, request.UserGroupID, request.Preset, request.Protocol, request.Removed); } //Returns all devices profiles that gives device(s) use. else if (request.DeviceIDs != null && request.DeviceIDs.Length != 0) { list = new List <GXAmiDeviceProfile>(); foreach (ulong it in request.DeviceIDs) { string query = string.Format("SELECT {0}.* FROM {0} INNER JOIN {1} ON {0}.ID = {1}.ProfileId WHERE {1}.Removed IS NULL AND {1}.ID = {2}", GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceProfile>(Db), GuruxAMI.Server.AppHost.GetTableName <GXAmiDevice>(Db), it); list.AddRange(Db.Select <GXAmiDeviceProfile>(query)); } } //Returns all devices profiles. else if (request.ProfileIDs != null && request.ProfileIDs.Length != 0) { list = new List <GXAmiDeviceProfile>(); foreach (ulong it in request.ProfileIDs) { string query = string.Format("SELECT {0}.* FROM {0} WHERE {0}.ID = {1}", GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceProfile>(Db), it); list.AddRange(Db.Select <GXAmiDeviceProfile>(query)); } } //Returns all devices profiles that gives data collectors use. else if (request.DataCollectorIDs != null && request.DataCollectorIDs.Length != 0) { list = new List <GXAmiDeviceProfile>(); foreach (ulong it in request.DataCollectorIDs) { string query = string.Format("SELECT {0}.* FROM {0} INNER JOIN {1} ON {0}.ID = {1}.ProfileId INNER JOIN {2} ON {1}.ID = {2}.DeviceID INNER JOIN {3} ON ({2}.DataCollectorID IS NULL OR {2}.DataCollectorID = {3}.ID) WHERE {1}.Removed IS NULL AND {3}.ID = {4}", GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceProfile>(Db), GuruxAMI.Server.AppHost.GetTableName <GXAmiDevice>(Db), GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceMedia>(Db), GuruxAMI.Server.AppHost.GetTableName <GXAmiDataCollector>(Db), it); list.AddRange(Db.Select <GXAmiDeviceProfile>(query)); } } //Returns all device profiles else { list = GetDeviceProfiles(s, Db, 0, 0, request.Preset, request.Protocol, request.Removed); } //If only latest version is wanted. if (!request.All) { Dictionary <Guid, GXAmiDeviceProfile> versions = new Dictionary <Guid, GXAmiDeviceProfile>(); for (int pos = 0; pos != list.Count; ++pos) { GXAmiDeviceProfile it = list[pos]; if (versions.ContainsKey(it.Guid)) { GXAmiDeviceProfile tmp = versions[it.Guid]; //If older version from the list. if (tmp.ProfileVersion < it.ProfileVersion) { list.Remove(tmp); versions[it.Guid] = it; } else { list.Remove(it); } --pos; } else { versions.Add(it.Guid, it); } } } //Get allowed mediatypes and device parameters. foreach (GXAmiDeviceProfile it in list) { it.AllowedMediaTypes = Db.Select <GXAmiMediaType>(q => q.DeviceProfileId == it.Id).ToArray(); List <GXAmiParameterTemplate> list2 = Db.Select <GXAmiParameterTemplate>(q => q.ParentID == it.Id << 16); it.Parameters = list2.ConvertAll <GXAmiParameter>(new Converter <GXAmiParameterTemplate, GXAmiParameter>(p => p.ToParameter())).ToArray(); //Get possible values for the parameter. foreach (GXAmiParameter param in it.Parameters) { param.Values = Db.Select <GXAmiValueItem>(q => q.ParameterId == param.TemplateId).ToArray(); } } return(new GXDeviceProfilesResponse(list.ToArray())); } }
/// <summary> /// Add new device profile. /// </summary> /// <param name="request"></param> /// <returns></returns> public GXDeviceProfilesUpdateResponse Post(GXDeviceProfilesUpdateRequest request) { List <GXEventsItem> events = new List <GXEventsItem>(); GXAmiDeviceProfile DeviceProfiles = null; lock (Db) { using (var trans = Db.OpenTransaction(IsolationLevel.ReadCommitted)) { string filename = Path.GetTempFileName(); using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)) { BinaryWriter w = new BinaryWriter(stream); w.Write(request.Data); } string pathToDll = this.GetType().Assembly.CodeBase; // Create an Application Domain: AppDomainSetup domainSetup = new AppDomainSetup { PrivateBinPath = pathToDll }; System.AppDomain td = null; string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + Path.DirectorySeparatorChar); //Try to load device template and unload assmbly. try { td = AppDomain.CreateDomain("TestDomain", null, domainSetup); GXProxyClass pc = (GXProxyClass)(td.CreateInstanceFromAndUnwrap(pathToDll, typeof(GXProxyClass).FullName)); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } byte[] data; string addInType, assemblyName; List <object[]> items = pc.Import(filename, dir, out data, out assemblyName, out addInType); ulong DeviceProfileID = 0; ulong categoryTemplateID = 0; ulong propertyTemplateID = 0; ulong tableTemplateID = 0; int version = 0; foreach (object[] it in items) { if (it[1] is GXAmiDeviceProfile) { DeviceProfiles = it[1] as GXAmiDeviceProfile; events.Add(new GXEventsItem(ActionTargets.DeviceProfile, Actions.Add, DeviceProfiles)); //Find new version number for the template. foreach (GXAmiDeviceProfile dt in Db.Select <GXAmiDeviceProfile>(q => q.Guid == DeviceProfiles.Guid)) { if (dt.ProfileVersion > version) { version = dt.ProfileVersion; } } ++version; DeviceProfiles.ProtocolAddInType = addInType; DeviceProfiles.ProtocolAssembly = assemblyName; DeviceProfiles.ProfileVersion = version; } else if (it[1] is GXAmiCategoryTemplate) { tableTemplateID = 0; (it[1] as GXAmiCategoryTemplate).DeviceID = DeviceProfileID; (it[1] as GXAmiCategoryTemplate).Id += DeviceProfileID << 16; (it[1] as GXAmiCategoryTemplate).TemplateVersion = version; } else if (it[1] is GXAmiDataTableTemplate) { categoryTemplateID = 0; (it[1] as GXAmiDataTableTemplate).DeviceID = DeviceProfileID; (it[1] as GXAmiDataTableTemplate).Id += DeviceProfileID << 16; (it[1] as GXAmiDataTableTemplate).TemplateVersion = version; } else if (it[1] is GXAmiPropertyTemplate) { (it[1] as GXAmiPropertyTemplate).DeviceID = DeviceProfileID; (it[1] as GXAmiPropertyTemplate).TemplateVersion = version; if (categoryTemplateID != 0) { (it[1] as GXAmiPropertyTemplate).ParentID = categoryTemplateID; } else if (tableTemplateID != 0) { (it[1] as GXAmiPropertyTemplate).ParentID = tableTemplateID; } else { throw new ArgumentOutOfRangeException("Parent ID."); } (it[1] as GXAmiPropertyTemplate).Id += DeviceProfileID << 16; } else if (it[1] is GXAmiParameterTemplate) { (it[1] as GXAmiParameterTemplate).DeviceID = DeviceProfileID; (it[1] as GXAmiParameterTemplate).TemplateVersion = version; if (it[0] is GXAmiDeviceProfile) { (it[1] as GXAmiParameterTemplate).ParentID = DeviceProfileID << 16; } else if (it[0] is GXAmiCategoryTemplate) { (it[1] as GXAmiParameterTemplate).ParentID = categoryTemplateID; } else if (it[0] is GXAmiDataTableTemplate) { (it[1] as GXAmiParameterTemplate).ParentID = tableTemplateID; } else if (it[0] is GXAmiPropertyTemplate) { (it[1] as GXAmiParameterTemplate).ParentID = propertyTemplateID; } } Db.Insert(it[1]); if (it[1] is GXAmiDeviceProfile) { #if !SS4 ulong value = (ulong)Db.GetLastInsertId(); #else ulong value = (ulong)Db.LastInsertId(); #endif (it[1] as GXAmiDeviceProfile).Id = value; DeviceProfileID = value; //Update allowed media types. foreach (GXAmiMediaType mt in (it[1] as GXAmiDeviceProfile).AllowedMediaTypes) { mt.DeviceProfileId = DeviceProfileID; Db.Insert(mt); } } else if (it[1] is GXAmiCategoryTemplate) { categoryTemplateID = (it[1] as GXAmiCategoryTemplate).Id; } else if (it[1] is GXAmiDataTableTemplate) { tableTemplateID = (it[1] as GXAmiDataTableTemplate).Id; } else if (it[1] is GXAmiPropertyTemplate) { propertyTemplateID = (it[1] as GXAmiPropertyTemplate).Id; if ((it[1] as GXAmiPropertyTemplate).Values != null && (it[1] as GXAmiPropertyTemplate).Values.Length != 0) { #if !SS4 ulong value = (ulong)Db.GetLastInsertId(); #else ulong value = (ulong)Db.LastInsertId(); #endif foreach (GXAmiValueItem vi in (it[1] as GXAmiPropertyTemplate).Values) { vi.ProfileId = DeviceProfileID; vi.PropertyId = value; Db.Insert(vi); } } } else if (it[1] is GXAmiParameterTemplate) { if ((it[1] as GXAmiParameterTemplate).Values != null && (it[1] as GXAmiParameterTemplate).Values.Length != 0) { #if !SS4 ulong value = (ulong)Db.GetLastInsertId(); #else ulong value = (ulong)Db.LastInsertId(); #endif foreach (GXAmiValueItem vi in (it[1] as GXAmiParameterTemplate).Values) { vi.ProfileId = DeviceProfileID; vi.ParameterId = value; Db.Insert(vi); } } } } //Save device template to data blocks. foreach (GXAmiDeviceProfilesDataBlock it in SplitDataToPackets(DeviceProfileID, filename)) { Db.Insert(it); } foreach (GXAmiDeviceProfilesDataBlock it in SplitDataToPackets(DeviceProfileID, filename)) { Db.Insert(it); } if (request.UserGroups != null) { foreach (long ugId in request.UserGroups) { GXAmiUserGroupDeviceProfile it = new GXAmiUserGroupDeviceProfile(); it.DeviceProfileID = DeviceProfileID; it.UserGroupID = ugId; Db.Insert(it); } } } finally { // Unload the application domain: if (td != null) { System.AppDomain.Unload(td); } if (dir != null) { try { Directory.Delete(dir, true); } catch { //It's OK if this fails. } } } trans.Commit(); } } AppHost host = this.ResolveService <AppHost>(); host.SetEvents(Db, this.Request, 0, events); return(new GXDeviceProfilesUpdateResponse(request.UserGroups, DeviceProfiles)); }
public List <object[]> Import(string path, string target, out byte[] data, out string assemblyName, out string addInType) { data = null; TargetDirectory = target; AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); GXDeviceProfile type = GXZip.Import(null, path, target); GXDeviceList.Update(target); string filename = Path.Combine(target, type.DeviceGuid + ".gxp"); using (FileStream s = File.OpenRead(filename)) { long size = s.Length; size = 0; } GXDevice device = GXDevice.Load(filename); addInType = device.AddIn.ToString(); assemblyName = device.AddIn.GetType().Assembly.FullName; GXAmiDeviceProfile dt = new GXAmiDeviceProfile(); dt.Guid = device.Guid; dt.Protocol = device.ProtocolName; dt.Profile = device.DeviceProfile; dt.PresetName = device.PresetName; dt.Manufacturer = device.Manufacturer; dt.Model = device.Model; dt.Version = device.Version; dt.UpdateInterval = device.UpdateInterval; dt.WaitTime = device.WaitTime; dt.ResendCount = device.ResendCount; dt.Added = DateTime.Now.ToUniversalTime(); List <GXAmiMediaType> list = new List <GXAmiMediaType>(); List <string> medias = new List <string>(Gurux.Communication.GXClient.GetAvailableMedias()); foreach (GXMediaType it in device.AllowedMediaTypes) { GXAmiMediaType mt = new GXAmiMediaType(); mt.Name = it.Name; //If default media settings are not given, ask them from the parser. if (string.IsNullOrEmpty(it.DefaultMediaSettings) && medias.Contains(it.Name)) { Gurux.Common.IGXMedia m = device.GXClient.SelectMedia(it.Name); mt.Settings = m.Settings; } else { mt.Settings = it.DefaultMediaSettings; } list.Add(mt); } dt.AllowedMediaTypes = list.ToArray(); List <object[]> items = new List <object[]>(); items.Add(new object[] { dt, dt }); ImportParameters(device, device.ID, dt, items); foreach (GXCategory cat in device.Categories) { GXAmiCategoryTemplate ct = new GXAmiCategoryTemplate(); items.Add(new object[] { dt, ct }); ct.Name = cat.Name; ct.Id = cat.ID; ImportParameters(cat, cat.ID, ct, items); foreach (GXProperty prop in cat.Properties) { GXAmiPropertyTemplate pt = new GXAmiPropertyTemplate(); pt.Id = prop.ID; pt.ParentID = cat.ID; pt.Name = prop.Name; pt.Unit = prop.Unit; if (prop.ValueType != null) { pt.TypeAsString = prop.ValueType.ToString(); } pt.AccessMode = prop.AccessMode; items.Add(new object[] { dt, pt }); ImportParameters(prop, prop.ID, pt, items); } } foreach (GXTable table in device.Tables) { GXAmiDataTableTemplate tt = new GXAmiDataTableTemplate(); items.Add(new object[] { dt, tt }); tt.Name = table.Name; tt.Id = table.ID; ImportParameters(table, table.ID, tt, items); foreach (GXProperty prop in table.Columns) { GXAmiPropertyTemplate pt = new GXAmiPropertyTemplate(); pt.Id = prop.ID; pt.ParentID = table.ID; pt.Name = prop.Name; pt.Unit = prop.Unit; if (prop.ValueType != null) { pt.TypeAsString = prop.ValueType.ToString(); } pt.AccessMode = prop.AccessMode; items.Add(new object[] { dt, pt }); ImportParameters(prop, prop.ID, pt, items); } } return(items); }