/// <summary> /// Add or update device group. /// </summary> public GXDeviceGroupUpdateResponse Put(GXDeviceGroupUpdateRequest request) { List <GXEventsItem> events = new List <GXEventsItem>(); IAuthSession s = this.GetSession(false); //Normal user can't change device group name or add new one. if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s)) { throw new ArgumentException("Access denied."); } long adderId = Convert.ToInt64(s.Id); bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s); lock (Db) { using (var trans = Db.OpenTransaction(IsolationLevel.ReadCommitted)) { //Add new device groups foreach (GXAmiDeviceGroup it in request.Items) { if (string.IsNullOrEmpty(it.Name)) { throw new ArgumentException("Invalid name."); } //If new device group. if (it.Id == 0) { it.Id = GXAmiSettings.GetNewDeviceID(Db); it.Added = DateTime.Now.ToUniversalTime(); Db.Insert(it); events.Add(new GXEventsItem(ActionTargets.DeviceGroup, Actions.Add, it)); } else //Update device group. { //Get Added time. #if !SS4 GXAmiDeviceGroup orig = Db.GetById <GXAmiDeviceGroup>(it.Id); #else GXAmiDeviceGroup orig = Db.SingleById <GXAmiDeviceGroup>(it.Id); #endif it.Added = orig.Added; Db.Update(it); events.Add(new GXEventsItem(ActionTargets.DeviceGroup, Actions.Edit, it)); } } trans.Commit(); } } AppHost host = this.ResolveService <AppHost>(); host.SetEvents(Db, this.Request, adderId, events); return(new GXDeviceGroupUpdateResponse(request.Items)); }
/// <summary> /// Add or update device. /// </summary> public GXDeviceUpdateResponse Put(GXDeviceUpdateRequest request) { List <GXEventsItem> events = new List <GXEventsItem>(); IAuthSession s = this.GetSession(false); //Normal user can't change device group name or add new one. if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s)) { throw new ArgumentException("Access denied."); } long adderId = Convert.ToInt64(s.Id); lock (Db) { using (var trans = Db.OpenTransaction(IsolationLevel.ReadCommitted)) { bool newDevice; bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s); //Add new device groups foreach (GXAmiDevice it in request.Devices) { if (string.IsNullOrEmpty(it.Name)) { throw new ArgumentException("Invalid name."); } //If new device. newDevice = it.Id == 0; if (newDevice) { it.Id = GXAmiSettings.GetNewDeviceID(Db); it.Added = DateTime.Now.ToUniversalTime(); it.ProfileGuid = Db.Select <GXAmiDeviceProfile>(q => q.Id == it.ProfileId)[0].Guid; Db.Insert(it); events.Add(new GXEventsItem(ActionTargets.Device, Actions.Add, it)); //Add adder to user group if adder is not super admin. foreach (ulong dgId in request.DeviceGroups) { //Can user access to the device group. if (!superAdmin && dgId != 0 && !GXDeviceGroupService.CanUserAccessDeviceGroup(Db, adderId, dgId)) { throw new ArgumentException("Access denied."); } GXAmiDeviceGroupDevice g = new GXAmiDeviceGroupDevice(); g.DeviceGroupID = dgId; g.DeviceID = it.Id; g.Added = DateTime.Now.ToUniversalTime(); Db.Insert(g); events.Add(new GXEventsItem(ActionTargets.DeviceGroup, Actions.Edit, dgId)); } it.Categories = null; it.Tables = null; } else //Update device. { if (request.DeviceGroups != null) { foreach (ulong dgId in request.DeviceGroups) { //Can user access to the device group. if (!superAdmin && !GXDeviceGroupService.CanUserAccessDeviceGroup(Db, adderId, dgId)) { throw new ArgumentException("Access denied."); } if (dgId == 0) { GXAmiDeviceGroupDevice g = new GXAmiDeviceGroupDevice(); g.DeviceGroupID = dgId; g.DeviceID = it.Id; g.Added = DateTime.Now.ToUniversalTime(); Db.Insert(g); events.Add(new GXEventsItem(ActionTargets.DeviceGroup, Actions.Edit, dgId)); } } } //Get Added time. #if !SS4 GXAmiDevice orig = Db.GetById <GXAmiDevice>(it.Id); #else GXAmiDevice orig = Db.SingleById <GXAmiDevice>(it.Id); #endif it.Added = orig.Added; Db.Update(it); events.Add(new GXEventsItem(ActionTargets.Device, Actions.Edit, it)); } //Bind user groups to the device groups. if (request.DeviceGroups != null) { foreach (ulong dgId in request.DeviceGroups) { string query = string.Format("SELECT DeviceID FROM " + GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceGroupDevice>(Db) + "WHERE DeviceID = {0} AND DeviceGroupID = {1}", it.Id, dgId); if (Db.Select <GXAmiUserGroupDeviceGroup>(query).Count == 0) { GXAmiDeviceGroupDevice item = new GXAmiDeviceGroupDevice(); item.DeviceGroupID = dgId; item.DeviceID = it.Id; item.Added = DateTime.Now.ToUniversalTime(); Db.Insert <GXAmiUserGroupDeviceGroup>(); events.Add(new GXEventsItem(ActionTargets.Device, Actions.Edit, it)); events.Add(new GXEventsItem(ActionTargets.DeviceGroup, Actions.Edit, dgId)); } } } /////////////////////////////////////////////// //Update device parameters. UpdateParameters(Db, it.Id, it.Id, it.Parameters, newDevice); //Update device Media Settings. Db.Delete <GXAmiDeviceMedia>(q => q.DeviceId == it.Id); foreach (GXAmiDeviceMedia m in it.Medias) { if (m.DataCollectorId == 0) { m.DataCollectorId = null; } m.DeviceId = it.Id; Db.Insert <GXAmiDeviceMedia>(m); } /////////////////////////////////////////////// //Update categories if (it.Categories == null) { GXAmiCategoryTemplate[] tmp22 = Db.Select <GXAmiCategoryTemplate>(q => q.DeviceID == it.ProfileId).ToArray(); List <GXAmiCategory> categories = new List <GXAmiCategory>(); foreach (GXAmiCategoryTemplate tmp in tmp22) { GXAmiCategory cat = tmp.ToCategory(); cat.DeviceID = it.Id; Db.Insert(cat); categories.Add(cat); List <GXAmiParameterTemplate> list = Db.Select <GXAmiParameterTemplate>(q => q.ParentID == tmp.Id); cat.Parameters = list.ConvertAll <GXAmiParameter>(new Converter <GXAmiParameterTemplate, GXAmiParameter>(p => p.ToParameter())).ToArray(); UpdateParameters(Db, it.Id, cat.Id, cat.Parameters, true); GXAmiPropertyTemplate[] tmp23 = Db.Select <GXAmiPropertyTemplate>(q => q.ParentID == tmp.Id).ToArray(); List <GXAmiProperty> properties = new List <GXAmiProperty>(); foreach (GXAmiPropertyTemplate tmp2 in tmp23) { GXAmiProperty p = tmp2.ToProperty(); p.ParentID = cat.Id; p.DeviceID = it.Id; Db.Insert(p); list = Db.Select <GXAmiParameterTemplate>(q => q.ParentID == tmp2.Id); p.Parameters = list.ConvertAll <GXAmiParameter>(new Converter <GXAmiParameterTemplate, GXAmiParameter>(q => q.ToParameter())).ToArray(); UpdateParameters(Db, it.Id, p.Id, p.Parameters, true); properties.Add(p); } cat.Properties = properties.ToArray(); } it.Categories = categories.ToArray(); } else { foreach (GXAmiCategory cat in it.Categories) { //User can change category so it is not updated. Db.Update(cat); //Update category parameters. UpdateParameters(Db, cat.Id, cat.Id, cat.Parameters, false); //Update properties foreach (GXAmiProperty p in cat.Properties) { //User can change property so it is not updated. Db.Update(p); //Update property parameters. UpdateParameters(Db, p.Id, p.Id, p.Parameters, false); } } } /////////////////////////////////////////////// //Update tables if (it.Tables == null) { GXAmiDataTableTemplate[] tmp22 = Db.Select <GXAmiDataTableTemplate>(q => q.DeviceID == it.ProfileId).ToArray(); List <GXAmiDataTable> tables = new List <GXAmiDataTable>(); foreach (GXAmiDataTableTemplate tmp in tmp22) { GXAmiDataTable table = tmp.ToTable(); table.DeviceID = it.Id; Db.Insert(table); tables.Add(table); List <GXAmiParameterTemplate> list = Db.Select <GXAmiParameterTemplate>(q => q.ParentID == tmp.Id); table.Parameters = list.ConvertAll <GXAmiParameter>(new Converter <GXAmiParameterTemplate, GXAmiParameter>(p => p.ToParameter())).ToArray(); UpdateParameters(Db, it.Id, table.Id, table.Parameters, true); GXAmiPropertyTemplate[] tmp23 = Db.Select <GXAmiPropertyTemplate>(q => q.ParentID == tmp.Id).ToArray(); List <GXAmiProperty> properties = new List <GXAmiProperty>(); foreach (GXAmiPropertyTemplate tmp2 in tmp23) { GXAmiProperty p = tmp2.ToProperty(); p.ParentID = table.Id; p.DeviceID = it.Id; Db.Insert(p); list = Db.Select <GXAmiParameterTemplate>(q => q.ParentID == tmp2.Id); p.Parameters = list.ConvertAll <GXAmiParameter>(new Converter <GXAmiParameterTemplate, GXAmiParameter>(q => q.ToParameter())).ToArray(); UpdateParameters(Db, it.Id, p.Id, p.Parameters, true); properties.Add(p); } table.Columns = properties.ToArray(); } it.Tables = tables.ToArray(); } else { foreach (GXAmiDataTable table in it.Tables) { //User can change table so it is not updated. Db.Update(table); //Update category parameters. UpdateParameters(Db, table.Id, table.Id, table.Parameters, false); //Update properties foreach (GXAmiProperty p in table.Columns) { //User can change category so it is not updated. Db.Update(p); //Update property parameters. UpdateParameters(Db, p.Id, p.Id, p.Parameters, false); } } } } trans.Commit(); } } AppHost host = this.ResolveService <AppHost>(); host.SetEvents(Db, this.Request, adderId, events); return(new GXDeviceUpdateResponse(request.Devices)); }