public JsonResult UpdateAlarmMap(AlarmSystemMapEditModel model)
 {
     try
     {
         IIotContextBase cont = new iotContext();
         Device          dev  = cont.Devices.FirstOrDefault(d => d.Id == model.ServerId);
         if (dev != null)
         {
             if (DomainAuthHelper.UserHasDeviceAccess(dev, Context.User)) //authorized
             {
                 if (dev.DeviceMaps.Count > 0)
                 {
                     //find adj map and update
                     var existing = dev.DeviceMaps.FirstOrDefault(m => m.Id == model.MapDefinition.Id);
                     if (existing != null)
                     {
                         //bind relations
                         foreach (var iomd in model.MapDefinition.IoMapDefinitions)
                         {
                             try
                             {
                                 var existingMapDef = existing.IoMapDefinitions.FirstOrDefault(d => d.Id == iomd.Id);
                                 if (existingMapDef != null)
                                 {
                                     existingMapDef.Copy(iomd);
                                 }
                                 else
                                 {
                                     iomd.Definition = existing;
                                     cont.IoMapDefinitions.Add(iomd);
                                     cont.SaveChanges();
                                 }
                             }
                             catch (Exception e)
                             {
                                 _logger.Error(e, e.Message);
                             }
                         }
                         cont.SaveChanges();
                     }
                     else
                     {
                         //has map and new one sent ? todo : ovewrite conditionally
                     }
                 }
                 else
                 {
                     //add the map
                     dev.DeviceMaps.Add(model.MapDefinition);
                     cont.SaveChanges();
                 }
             }
         }
     }
     catch (Exception e)
     {
         _logger.Error(e, e.Message);
     }
     return(new JsonResult());
 }
 public void GetMapData(int ServerId)
 {
     try
     {
         IIotContextBase cont = new iotContext();
         var             serv = cont.Devices.FirstOrDefault(d => d.Id == ServerId);
         if (serv != null)   //correct server
         {
             AlarmSystemMapEditModel model = new AlarmSystemMapEditModel();
             string clientId = Context.ConnectionId;
             if (DomainAuthHelper.UserHasDeviceAccess(serv, Context.User))   //authorized
             {
                 var man = new AlarmSystemConfigManager(serv);
                 AlarmDevicesConfigService deviceprovider = new AlarmDevicesConfigService(man);
                 model          = new AlarmSystemMapEditModel(deviceprovider.GetAll());
                 model.ServerId = ServerId;
                 var fMapDefinition = serv.DeviceMaps.FirstOrDefault();
                 fMapDefinition.Device = null;
                 foreach (var d in fMapDefinition.IoMapDefinitions)
                 {
                     d.Definition = null; //unbind
                 }
                 model.MapDefinition = fMapDefinition;
                 Clients.Client(Context.ConnectionId).UpdateMap(model);
             }
         }
     }
     catch (Exception e)
     {
         _logger.Error(e, e.Message);
     }
 }
Beispiel #3
0
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (!_authorize)
            {
                return(true);
            }

            try
            {
                bool basicAuthed = base.AuthorizeCore(httpContext);
                if (basicAuthed)
                {
                    if (DomainAuthHelper.IsUserContextDomainAuthorized(httpContext))
                    {
                        return(DomainAuthHelper.UserHasDeviceAccess(httpContext));
                    }
                }
            }
            catch (Exception e)
            {
                return(false);
            }
            return(false);
        }