// GET: AlarmSystemSummary public ActionResult Index(int siteId) { try { if (siteId != null) { IIotContextBase cont = new iotContext(); iotDbConnector.DAL.Site site = cont.Sites.First(s => s.Id == siteId); if (site != null) { Device alrmSysDev = site.Devices.First(d => d.Type.TypeName.Contains("sconnMB")); if (alrmSysDev != null) { AlarmSystemConfigManager mngr = new AlarmSystemConfigManager(alrmSysDev.EndpInfo, alrmSysDev.Credentials); int devs = mngr.GetDeviceNumber(); AlarmSystemSummaryModel model = new AlarmSystemSummaryModel(devs, alrmSysDev); return(View(model)); } } } return(View()); } catch (Exception e) { return(View()); } }
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); } }
private void AddPropertyForMapperAndDevice(sconnPropertyMapper maper, Device edited, int DevNo) { try { iotContext cont = new iotContext(); DeviceProperty prop = new DeviceProperty(); prop.PropertyName = "Input" + maper.SeqNumber; //TODO read from name cfg Device storedDevice = cont.Devices.Where(d => d.Id == edited.Id).First(); //devRepo.GetById(edited.Id); prop.Device = storedDevice; prop.LastUpdateTime = DateTime.Now; cont.Properties.Add(prop); cont.SaveChanges(); DeviceParameter param = new DeviceParameter(); param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[DevNo]); ParameterType extType = ParamTypeForSconnMapper(maper); ParameterType inType = cont.ParamTypes.Where(p => p.Id == extType.Id).First(); param.Type = inType; param.Property = prop; cont.Parameters.Add(param); cont.SaveChanges(); maper.Parameter = param; cont.PropertyResultMappers.Add(maper); cont.SaveChanges(); } catch (Exception e) { nlogger.ErrorException(e.Message, e); } }
static public iotDomain AppDomainForUserContext(HttpContextBase cont) { try { IPrincipal seesionAuth = cont.User; string username = seesionAuth.Identity.Name; ApplicationUser user = GetUserWithName(username); if (user != null) { //DeviceRestfulService cl = new DeviceRestfulService(); iotContext dbcont = new iotContext(); string domainId = DomainSession.GetContextDomain(cont); iotDomain domain = dbcont.Domains.First(d => d.DomainName.Equals(domainId));//cl.GetDomainWithName(domainId); return(domain); } else { return(new iotDomain()); } } catch (Exception e) { return(new iotDomain()); } }
private void AddActionForMapperAndDevice(sconnActionResultMapper maper, Device edited, int DevNo) { try { iotContext cont = new iotContext(); Device storedDevice = cont.Devices.Where(d => d.Id == edited.Id).First(); DeviceAction action = new DeviceAction(); action.RequiredParameters = new List <ActionParameter>(); action.ResultParameters = new List <DeviceActionResult>(); action.ActionName = "Output" + maper.SeqNumber; //TODO read from name cfg action.Device = storedDevice; action.LastActivationTime = DateTime.Now; cont.Actions.Add(action); cont.SaveChanges(); //copy maper for action sconnActionResultMapper actionResultMaper = new sconnActionResultMapper(); actionResultMaper.ConfigType = maper.ConfigType; actionResultMaper.SeqNumber = maper.SeqNumber; sconnActionMapper actionInputMaper = new sconnActionMapper(); actionInputMaper.ConfigType = maper.ConfigType; actionInputMaper.SeqNumber = maper.SeqNumber; ParameterType extType = ParamTypeForSconnMapper(maper); ParameterType inType = cont.ParamTypes.Where(p => p.Id == extType.Id).First(); ActionParameter inparam = new ActionParameter(); inparam.Value = sconnConfigToStringVal(actionInputMaper, site.siteCfg.deviceConfigs[DevNo]); inparam.Type = inType; inparam.Action = action; cont.ActionParameters.Add(inparam); actionInputMaper.ActionParam = inparam; cont.ActionParamMappers.Add(actionInputMaper); cont.SaveChanges(); //create result parameter and bind mapper to it DeviceActionResult param = new DeviceActionResult(); param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[DevNo]); param.Type = inType; param.Action = action; cont.ActionResultParameters.Add(param); cont.SaveChanges(); actionResultMaper.ActionParam = param; cont.ActionResultMappers.Add(actionResultMaper); cont.SaveChanges(); } catch (Exception e) { nlogger.ErrorException(e.Message, e); } }
public static IIotContextBase GetContextForUser(ApplicationUser user) { try { IIotContextBase cont = new iotContext(user.DomainId); return(cont); } catch (Exception e) { _logger.Error(e, e.Message); return(null); } }
/************************ CUSTOM CROSS QUERY ********************/ public Device DeviceWithEndpoint(EndpointInfo endp) { try { iotContext cont = new iotContext(); Device stored = cont.Devices.Where(d => d.EndpInfo.Hostname.Equals(endp.Hostname) && d.EndpInfo.Port == endp.Port).FirstOrDefault(); return(stored); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new Device()); } }
public iotDomain GetDomainWithId(int id) { try { //iotSharedEntityContext<iotDomain> cont = new iotSharedEntityContext<iotDomain>(); iotContext cont = new iotContext(); return(cont.Domains.First(d => d.Id == id)); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(null); } }
public iotDomain DomainWithName(string name) { try { // iotSharedEntityContext<iotDomain> cont = new iotSharedEntityContext<iotDomain>(); iotContext cont = new iotContext(); return(cont.Domains.First(d => d.DomainName.Equals(name))); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(null); } }
public static void SendDeviceUpdate(Device dev) { try { //get act device iotContext cont = new iotContext(); Device dbDevice = (from d in cont.Devices where d.Id == dev.Id select d).First(); DeviceUpdated(dbDevice); } catch (Exception e) { } }
public iotDomain GetDomainWithName(string name) { try { //iotSharedEntityContext<iotDomain> cont = new iotSharedEntityContext<iotDomain>(); iotContext cont = new iotContext(); return(cont.Domains.First(d => d.DomainName.Equals(name))); //InMemoryContext.GetDomainForName(name); // return JsonConvert.SerializeObject(db.GetById(name)); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(null); } }
/************************ CUSTOM CROSS QUERY ********************/ public Device DeviceWithEndpoint(EndpointInfo endp) { try { // iotSharedEntityContext<Device> devCont = new iotSharedEntityContext<Device>(); iotContext cont = new iotContext(); Device dev = cont.Devices.First(d => d.EndpInfo.Id == endp.Id); return(dev); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new Device()); } }
public List <Site> Sites() { try { iotContext cont = new iotContext(); List <Site> sites = (from s in cont.Sites select s).ToList(); return(sites); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new List <Site>()); } }
public bool DomainAdd(iotDomain domain) { try { //iotSharedEntityContext<iotDomain> cont = new iotSharedEntityContext<iotDomain>(); iotContext cont = new iotContext(); cont.Domains.Add(domain); cont.SaveChanges(); return(true); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(false); } }
public Device GetDevice(string id) { try { int DeviceId = Convert.ToInt32(id); iotContext cont = new iotContext(); cont.Configuration.ProxyCreationEnabled = false; cont.Configuration.LazyLoadingEnabled = false; Device dev = cont.Devices.AsNoTracking().Where(d => d.Id == DeviceId).SingleOrDefault(); //repo.GetById(DeviceId); return(dev); //return JsonConvert.SerializeObject(dev); } catch (Exception ex) { throw new FaultException(ex.Message); } }
public static IIotContextBase GetDataContextForUserHttpContext(HttpContextBase context) { try { var cont = new ApplicationDbContext(); var currentUser = (from u in cont.Users where u.UserName.Equals(context.User.Identity.Name) select u).First(); IIotContextBase icont = new iotContext(currentUser.DomainId); return(icont); } catch (Exception e) { _logger.Error(e, e.Message); return(null); } }
public void PublishParamUpdate(DeviceParameter param) { IIotContextBase cont = new iotContext(); DeviceParameter toUpdate = cont.Parameters.Include("Property").FirstOrDefault(e => e.Id == param.Id); //unbind after parent if (toUpdate.Property != null) { toUpdate.Property.Device = null; toUpdate.Property.ResultParameters = null; } string jsonParam = JsonConvert.SerializeObject(toUpdate); Clients.All.updateParam(jsonParam); }
public void PublishActionUpdate(DeviceActionResult param) { IIotContextBase cont = new iotContext(); //cont.Configuration.ProxyCreationEnabled = false; //cont.Configuration.LazyLoadingEnabled = false; DeviceActionResult toUpdate = cont.ActionResultParameters.Include("Action").FirstOrDefault(e => e.Id == param.Id); //unbind after parent if (toUpdate.Action != null) { toUpdate.Action.Device = null; toUpdate.Action.RequiredParameters = null; toUpdate.Action.ResultParameters = null; } string jsonParam = JsonConvert.SerializeObject(toUpdate); Clients.All.updateParam(jsonParam); }
/******************* Add ***********************/ public bool AddDeviceToDomainAtSite(Device dev, int DomainId, int SiteId) { try { //iotSharedEntityContext<iotDomain> cont = new iotSharedEntityContext<iotDomain>(); iotContext cont = new iotContext(); iotDomain domain = cont.Domains.First(d => d.Id == DomainId); //db.GetById(DomainId); //fetch domain from DB if (domain != null) { //add to devices Site site = domain.Sites.First(s => s.Id == SiteId); site.Devices.Add(dev); //cont.UpdateWithHistory(domain); //update domain return(true); } return(false); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(false); } }
/************* Alarm config session **************/ private AlarmSystemConfigManager GetAlarmConfigForContextWithDeviceId(int devid) { try { IIotContextBase cont = new iotContext(); Device alrmSysDev = cont.Devices.First(d => d.Id == devid); if (alrmSysDev != null) { var man = new AlarmSystemConfigManager(alrmSysDev.EndpInfo, alrmSysDev.Credentials); man.RemoteDevice = alrmSysDev; return(man); } else { return(null); } } catch (Exception e) { _logger.Error(e, e.Message); return(null); } }
private bool LoadConfigToDevice(int devId) { try { //iotConnector connt = new iotConnector(); //Device edited = connt.DeviceList().Where(n => n.Id == dev.Id).First(); //iotRepository<Device> devrep = new iotRepository<Device>(); Stopwatch watch = new Stopwatch(); watch.Start(); iotContext cont = new iotContext(); //cont.Configuration.LazyLoadingEnabled = false; //Device edited = cont.Devices.Where(d => d.Id == dev.Id) // .Include(d => d.Actions.Select(a => a.ResultParameters.Select(r => r.sconnMappers))) // .Include(d => d.Properties.Select(p => p.ResultParameters.Select(r => r.sconnMappers))) // .First(); Device edited = cont.Devices.First(d => d.Id == devId); watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ device query"); watch.Reset(); int devAddr = 0; if (site.siteCfg.deviceNo == 0) { return(false); } //Reload properties and actions if they changed //Update int inputs = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrInputsNO]; if (edited.Properties.Count != inputs) { watch.Start(); //clear current properies for (int i = 0; i < edited.Properties.Count; i++) { cont.Properties.Remove(edited.Properties[i]); } cont.SaveChanges(); //load for (int i = 0; i < inputs; i++) { sconnPropertyMapper maper = new sconnPropertyMapper(); maper.ConfigType = ipcDefines.mAdrInput; maper.SeqNumber = i; AddPropertyForMapperAndDevice(maper, edited, devAddr); } watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ readd prop"); watch.Reset(); } else //update { watch.Reset(); watch.Start(); foreach (var item in edited.Properties) { //get parameter Stopwatch watch4 = new Stopwatch(); watch4.Start(); DeviceParameter param = item.ResultParameters.FirstOrDefault(); watch4.Stop(); Debug.WriteLine("Execution time : " + watch4.ElapsedMilliseconds + " ms @ prop update dev param query"); watch4.Restart(); param.Value = sconnConfigToStringVal(param.sconnMappers.FirstOrDefault(), site.siteCfg.deviceConfigs[devAddr]); watch4.Stop(); Debug.WriteLine("Execution time : " + watch4.ElapsedMilliseconds + " ms @ prop update dev param parse"); watch4.Reset(); if (param != null) { //get input mapper Stopwatch watch2 = new Stopwatch(); watch2.Start(); sconnConfigMapper maper = param.sconnMappers.FirstOrDefault(); watch2.Stop(); Debug.WriteLine("Execution time : " + watch2.ElapsedMilliseconds + " ms @ prop update mapper query"); watch2.Reset(); Stopwatch watch3 = new Stopwatch(); watch3.Start(); if (maper != null) { param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[devAddr]); } watch3.Stop(); Debug.WriteLine("Execution time : " + watch3.ElapsedMilliseconds + " ms @ prop update cfg to str"); watch3.Reset(); } } Stopwatch watch1 = new Stopwatch(); watch1.Start(); cont.SaveChanges(); //apply watch1.Stop(); Debug.WriteLine("Execution time : " + watch1.ElapsedMilliseconds + " ms @ prop update save"); watch1.Reset(); watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ prop update"); watch.Reset(); } int outputs = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrOutputsNO]; int relays = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrRelayNO]; if (edited.Actions.Count != outputs + relays) { watch.Start(); //remove existing if (edited.Actions.Count > 0) { for (int i = 0; i < edited.Actions.Count; i++) { cont.Actions.Remove(edited.Actions[i]); } cont.SaveChanges(); } for (int i = 0; i < outputs; i++) { sconnActionResultMapper maper = new sconnActionResultMapper(); maper.ConfigType = ipcDefines.mAdrOutput; maper.SeqNumber = i; AddActionForMapperAndDevice(maper, edited, devAddr); } for (int i = 0; i < relays; i++) { sconnActionResultMapper maper = new sconnActionResultMapper(); maper.ConfigType = ipcDefines.mAdrRelay; maper.SeqNumber = i; AddActionForMapperAndDevice(maper, edited, devAddr); } watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ action readd"); watch.Reset(); } else { watch.Start(); foreach (var item in edited.Actions) { //get action DeviceActionResult param = (from par in item.ResultParameters select par).FirstOrDefault(); if (param != null) { //get input mapper sconnConfigMapper maper = (from cm in param.sconnMappers select cm).FirstOrDefault(); if (maper != null) { param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[devAddr]); param.Type = param.Type; } } } cont.SaveChanges(); //save updates watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ action update"); watch.Reset(); } } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(false); } return(true); }
/************************ ADD/REMOVE WITH PARAM ********************/ public Device DeviceAddWithParams(string SiteId, string Name, string Host, string Port, string Login, string Pass, string Type, string Loc, string Prot) { try { iotContext cont = new iotContext(); int LocId = int.Parse(Loc); int DevTypeId = int.Parse(Type); Device ndev = new Device(); ndev.DeviceName = Name; List <DeviceType> types = cont.Types.ToList(); DeviceType type = (from t in types where t.Id == DevTypeId select t).First(); List <Location> locs = cont.Locations.ToList(); Location loc = (from l in locs where l.Id == LocId select l).First(); ndev.Type = type; ndev.DeviceLocation = loc; DeviceCredentials cred = new DeviceCredentials(); cred.PasswordExpireDate = DateTime.Now.AddYears(100); cred.PermissionExpireDate = DateTime.Now.AddYears(100); cred.Password = Pass; cred.Username = Login; cont.Credentials.Add(cred); cont.SaveChanges(); List <DeviceCredentials> creds = cont.Credentials.ToList(); DeviceCredentials storedCredentials = (from c in creds where c.Username.Equals(cred.Username) select c).FirstOrDefault(); EndpointInfo info = new EndpointInfo(); info.Hostname = Host; info.Port = int.Parse(Port); //TODO //CommProtocolType protocol = (CommProtocolType)Prot; //int.Parse(Prot); //info.EnableProtocolSupport(protocol); info.SupportsSconnProtocol = true; cont.Endpoints.Add(info); cont.SaveChanges(); List <EndpointInfo> endps = cont.Endpoints.ToList(); EndpointInfo storedInfo = (from i in endps where i.Hostname.Equals(info.Hostname) && i.Port == info.Port select i).FirstOrDefault(); if (storedInfo == null) { //err return(null); } ndev.Credentials = storedCredentials; ndev.EndpInfo = storedInfo; int siteIdNum = int.Parse(SiteId); List <Site> sites = cont.Sites.ToList(); Site siteToAppend = (from s in sites where s.Id == siteIdNum select s).FirstOrDefault(); ndev.Site = siteToAppend; cont.Devices.Add(ndev); cont.SaveChanges(); List <Device> devs = cont.Devices.ToList(); Device stored = (from d in devs where d.DeviceName.Equals(ndev.DeviceName) && d.EndpInfo.Hostname.Equals(ndev.EndpInfo.Hostname) select d).FirstOrDefault(); //update device UpdateDeviceProperties(stored); return(stored); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(null); } }
public Device DeviceAddWithParamsEntity(string SiteId, string Name, string Host, string Port, string Login, string Pass, string Type, string Loc, string Prot, int DomainId, bool IsVirtual) { try { iotContext context = new iotContext(); iotDomain domain = context.Domains.First(d => d.Id == DomainId); if (domain != null) { int LocId = int.Parse(Loc); int DevTypeId = int.Parse(Type); Device ndev = new Device(); ndev.DeviceName = Name; List <DeviceType> types = domain.DeviceTypes.ToList(); DeviceType type = (from t in types where t.Id == DevTypeId select t).First(); List <Location> locs = domain.Locations.ToList(); Location loc = (from l in locs where l.Id == LocId select l).First(); ndev.Type = type; ndev.DeviceLocation = loc; DeviceCredentials cred = new DeviceCredentials(); cred.PasswordExpireDate = DateTime.Now.AddYears(100); cred.PermissionExpireDate = DateTime.Now.AddYears(100); cred.Password = Pass; cred.Username = Login; context.Credentials.Add(cred); context.SaveChanges(); DeviceCredentials storedCredentials = context.Credentials.First(c => c.Username.Equals(cred.Username) && c.Password.Equals(cred.Password)); EndpointInfo info = new EndpointInfo(); info.Hostname = Host; info.Port = int.Parse(Port); info.Domain = domain; //TODO //CommProtocolType protocol = (CommProtocolType)Prot; //int.Parse(Prot); //info.EnableProtocolSupport(protocol); info.SupportsSconnProtocol = true; context.Endpoints.Add(info); context.SaveChanges(); EndpointInfo storedInfo = context.Endpoints.First(e => e.Hostname.Equals(info.Hostname) && e.Port == info.Port); if (storedInfo == null) { return(null); } int siteIdNum = int.Parse(SiteId); Site siteToAppend = domain.Sites.First(s => s.Id == siteIdNum); ndev.Site = siteToAppend; ndev.Credentials = storedCredentials; ndev.EndpInfo = storedInfo; ndev.IsVirtual = IsVirtual; context.Devices.Add(ndev); context.SaveChanges(); Device stored = context.Devices.First(d => d.DeviceName.Equals(ndev.DeviceName) && d.EndpInfo.Hostname.Equals(ndev.EndpInfo.Hostname)); //update device if (!ndev.IsVirtual) { UpdateDeviceProperties(stored); } else { DeviceFillWithSampleProperties(ndev); } return(stored); } return(null); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(null); } }
public void Setup() { this._context = new iotContext(); }
public void DeviceFillWithSampleProperties(Device updated) { try { iotContext cont = new iotContext(); Device dev = cont.Devices.First(d => d.Id == updated.Id); dev.Properties = new List <DeviceProperty>(); dev.Actions = new List <DeviceAction>(); cont.SaveChanges(); //add sample Actions ParameterType paramtype = new ParameterType(); paramtype.Name = "ActionParam"; ParameterType storedptype = cont.ParamTypes.Add(paramtype); cont.SaveChanges(); for (int a = 0; a < 4; a++) { DeviceAction act = new DeviceAction(); act.ActionName = "Act" + a.ToString(); act.Device = dev; DeviceAction storedact = cont.Actions.Add(act); cont.SaveChanges(); //required params ActionParameter param = new ActionParameter(); param.Type = storedptype; param.Value = "0"; param.Action = act; storedact.RequiredParameters = new List <ActionParameter>(); ActionParameter storedreqparam = cont.ActionParameters.Add(param); storedact.RequiredParameters.Add(storedreqparam); cont.SaveChanges(); //result params DeviceActionResult param2 = new DeviceActionResult(); param2.Type = storedptype; param2.Value = "0"; param2.Action = act; act.ResultParameters = new List <DeviceActionResult>(); DeviceActionResult storedresparam = cont.ActionResultParameters.Add(param2); act.ResultParameters.Add(storedresparam); cont.SaveChanges(); dev.Actions.Add(act); cont.SaveChanges(); } //add Sample Proprties for (int p = 0; p < 8; p++) { //DeviceProperty prop = new DeviceProperty(); //prop.PropertyName = "Prop" + p.ToString(); //prop.Device = dev; ////result params //DeviceParameter param2 = new DeviceParameter(); //param2.Type = storedptype; //param2.Value = "0"; //param2.Property = prop; //prop.ResultParameters = new List<DeviceParameter>(); //prop.ResultParameters.Add(param2); //dev.Properties.Add(prop); } cont.SaveChanges(); } catch (Exception e) { nlogger.ErrorException(e.Message, e); } }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { try { //create domain for new account then user //IiotDomainServiceClient cl = iotServiceConnector.ServiceClient(); IIotContextBase icont = new iotContext(); iotDomain existingDomain = null; try { existingDomain = icont.Domains.First(dm => dm.DomainName.Equals(model.DomainName)); } catch (Exception e) { } if (existingDomain == null) //domain does not exist { //create domain iotDomain domain = new iotDomain(); domain.DomainName = model.DomainName; icont.Domains.Add(domain); icont.SaveChanges(); iotDomain addedDomain = icont.Domains.First(d => d.DomainName.Equals(domain.DomainName)); if (addedDomain != null) { //create user ApplicationUser user = new ApplicationUser() { UserName = model.UserName }; ApplicationDbContext ucont = new ApplicationDbContext(); var userMan = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var roleManager = new RoleManager <IotUserRole>(new RoleStore <IotUserRole>(ucont)); string DomainRoleFullName = domain.DomainName + "_" + "DomainAdmin"; if (!roleManager.RoleExists(DomainRoleFullName)) { IotUserRole admRole = new IotUserRole(domain.Id, true, IotUserRoleType.DomainAdmin); admRole.Name = DomainRoleFullName; await roleManager.CreateAsync(admRole); } IotUserRole adminRole = roleManager.FindByName(DomainRoleFullName); if (adminRole != null) { //setup user domain user.DomainId = addedDomain.Id; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await userMan.AddToRoleAsync(user.Id, adminRole.Name); await SignInAsync(user, isPersistent : false); return(RedirectToAction("Index", "Home")); } else { AddErrors(result); } } } } else { //domain taken error } } catch (Exception e) { //return error } } return(View(model)); }