public UsersController(UserManager <ApplicationUser> userManager, SmartSwitchDbContext context, IMapper mapper, IHttpContextAccessor httpContextAccessor) { _userManager = userManager; _context = context; _mapper = mapper; _currentUsername = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; }
private async void HandleIAmMessage(IWebSocketConnection socket, string[] messageWords) { string currentMac = messageWords[1]; string ownerUsername = messageWords[2]; _macConnectionPairs.Add(new MacConnectionPair() { Mac = currentMac, Socket = socket }); using (ILifetimeScope scope = Program.Container.BeginLifetimeScope()) { SmartSwitchDbContext context = scope.Resolve <SmartSwitchDbContext>(); Plug currentPlug = await context.Plugs.FindAsync(currentMac); if (currentPlug == null) // if the plug is brand new (not in the system) { User owner = await context.Users.FindAsync(ownerUsername); // if the user is not in the system we'll save the connection and wait for the user to register if (owner == null) { if (_unownedMacs.ContainsKey(ownerUsername)) { _unownedMacs[ownerUsername].Add(currentMac); } else { _unownedMacs.TryAdd(ownerUsername, new List <string> { currentMac }); } } else // we'll add it to the owner { Plug newPlug = new Plug(currentMac); owner.Plugs.Add(newPlug); await context.SaveChangesAsync(); } } else // otherwise, if the plug belongs to another user -- we'll transfer ownership { var currentPlugEntry = context.Entry(currentPlug); if (currentPlugEntry.CurrentValues["Username"].ToString() != ownerUsername) { currentPlugEntry.CurrentValues["Username"] = ownerUsername; currentPlug.IsDeleted = false; await context.SaveChangesAsync(); } } } await socket.Send("are-you-on"); }
private async void HandleOnMessage(IWebSocketConnection socket, string[] messageWords) { using (ILifetimeScope scope = Program.Container.BeginLifetimeScope()) { SmartSwitchDbContext context = scope.Resolve <SmartSwitchDbContext>(); // get plug by mac and update its IsOn property Plug currentPlug = await context.Plugs.FindAsync(GetMac(socket)); if (currentPlug != null) // if the plug is owned { currentPlug.IsOn = messageWords[1] == "yes"; await context.SaveChangesAsync(); } } }
private async void HandleSampleMessage(IWebSocketConnection socket, string[] messageWords) { PowerUsageSample newSample = new PowerUsageSample(Convert.ToDouble(messageWords[1]), Convert.ToDouble(messageWords[2])); // get plug by mac and add the new sample using (ILifetimeScope scope = Program.Container.BeginLifetimeScope()) { SmartSwitchDbContext context = scope.Resolve <SmartSwitchDbContext>(); Plug currentPlug = await context.Plugs.FindAsync(GetMac(socket)); if (currentPlug != null) // if the plug is owned { currentPlug.Samples.Add(newSample); await context.SaveChangesAsync(); } } }
public TasksController(SmartSwitchDbContext context, IMapper mapper, IHttpContextAccessor httpContextAccessor) { _context = context; _mapper = mapper; _currentUsername = httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; }
public AuthController(UserManager <ApplicationUser> userManager, SmartSwitchDbContext smartSwitchDbContext) { _userManager = userManager; _smartSwitchDbContext = smartSwitchDbContext; }
public static bool IsNotValidated(string username, Plug plug, SmartSwitchDbContext context) => username != context.Entry(plug).CurrentValues["Username"].ToString();
public static bool IsNotValidated(string username, Task task, SmartSwitchDbContext context) { Plug plug = context.Plugs.Find(task.DeviceMac); return(IsNotValidated(username, plug, context)); }