Beispiel #1
0
        public override Task OnDisconnectedAsync(Exception exception)
        {
            try
            {
                if (Device != null)
                {
                    _dataService.DeviceDisconnected(Device.ID);

                    Device.IsOnline = false;

                    var userIDs = _circuitManager.Connections.Select(x => x.User.Id);

                    var filteredUserIDs = _dataService.FilterUsersByDevicePermission(userIDs, Device.ID);

                    var connections = _circuitManager.Connections
                                      .Where(x => x.User.OrganizationID == Device.OrganizationID &&
                                             filteredUserIDs.Contains(x.User.Id));

                    foreach (var connection in connections)
                    {
                        connection.InvokeCircuitEvent(CircuitEventName.DeviceWentOffline, Device);
                    }
                }
                return(base.OnDisconnectedAsync(exception));
            }
            finally
            {
                ServiceConnections.TryRemove(Context.ConnectionId, out _);
            }
        }
 public void Stop()
 {
     _ = Task.WhenAll(ServiceConnections.Select(c => c.Value.StopAsync()));
     foreach (var connectionContext in ConnectionContexts)
     {
         connectionContext.Value.Application.Input.CancelPendingRead();
     }
 }
Beispiel #3
0
        public Task <bool> DeviceCameOnline(Device device)
        {
            try
            {
                if (ServiceConnections.Any(x => x.Value.ID == device.ID))
                {
                    DataService.WriteEvent(new EventLog()
                    {
                        EventType      = EventType.Info,
                        OrganizationID = device.OrganizationID,
                        Message        = $"Device connection for {device?.DeviceName} was denied because it is already connected."
                    });
                    return(Task.FromResult(false));
                }

                var ip = Context.GetHttpContext()?.Connection?.RemoteIpAddress;
                if (ip != null && ip.IsIPv4MappedToIPv6)
                {
                    ip = ip.MapToIPv4();
                }
                device.PublicIP = ip?.ToString();

                if (DataService.AddOrUpdateDevice(device, out var updatedDevice))
                {
                    Device = updatedDevice;
                    ServiceConnections.AddOrUpdate(Context.ConnectionId, Device, (id, d) => Device);

                    var userIDs = BrowserHub.ConnectionIdToUserLookup.Values.Select(x => x.Id);

                    var filteredUserIDs = DataService.FilterUsersByDevicePermission(userIDs, Device.ID);

                    var connectionIds = BrowserHub.ConnectionIdToUserLookup
                                        .Where(x => x.Value.OrganizationID == Device.OrganizationID &&
                                               filteredUserIDs.Contains(x.Value.Id))
                                        .Select(x => x.Key)
                                        .ToList();

                    BrowserHubContext.Clients.Clients(connectionIds).SendAsync("DeviceCameOnline", Device);
                    return(Task.FromResult(true));
                }
                else
                {
                    // Organization wasn't found.
                    return(Task.FromResult(false));
                }
            }
            catch (Exception ex)
            {
                DataService.WriteEvent(ex, Device?.OrganizationID);
            }

            Context.Abort();
            return(Task.FromResult(false));
        }
Beispiel #4
0
        public async Task DeviceCameOnline(Device device)
        {
            try
            {
                if (ServiceConnections.Any(x => x.Value.ID == device.ID))
                {
                    DataService.WriteEvent(new EventLog()
                    {
                        EventType      = EventTypes.Info,
                        OrganizationID = device.OrganizationID,
                        Message        = $"Device connection for {device?.DeviceName} was denied because it is already connected."
                    });
                    Context.Abort();
                    return;
                }
                device.IsOnline   = true;
                device.LastOnline = DateTime.Now;
                Device            = device;
                if (DataService.AddOrUpdateDevice(device))
                {
                    var failCount = 0;
                    while (!ServiceConnections.TryAdd(Context.ConnectionId, device))
                    {
                        if (failCount > 3)
                        {
                            Context.Abort();
                            return;
                        }
                        failCount++;
                        await Task.Delay(1000);
                    }
                    await this.Groups.AddToGroupAsync(this.Context.ConnectionId, device.OrganizationID);

                    await BrowserHub.Clients.Group(Device.OrganizationID).SendAsync("DeviceCameOnline", Device);
                }
                else
                {
                    // Organization wasn't found.
                    await Clients.Caller.SendAsync("UninstallClient");
                }
            }
            catch (Exception ex)
            {
                DataService.WriteEvent(ex);
                throw;
            }
        }
Beispiel #5
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            if (Device != null)
            {
                DataService.DeviceDisconnected(Device.ID);
                await this.Groups.RemoveFromGroupAsync(this.Context.ConnectionId, Device.OrganizationID);

                Device.IsOnline = false;
                await BrowserHub.Clients.Group(Device.OrganizationID).SendAsync("DeviceWentOffline", Device);

                while (!ServiceConnections.TryRemove(Context.ConnectionId, out var device))
                {
                    await Task.Delay(1000);
                }
            }

            await base.OnDisconnectedAsync(exception);
        }
        public async Task DeviceCameOnline(Device device)
        {
            try
            {
                if (ServiceConnections.Any(x => x.Value.ID == device.ID))
                {
                    DataService.WriteEvent(new EventLog()
                    {
                        EventType      = EventTypes.Info,
                        OrganizationID = device.OrganizationID,
                        Message        = $"Device connection for {device?.DeviceName} was denied because it is already connected."
                    });
                    Context.Abort();
                    return;
                }

                if (DataService.AddOrUpdateDevice(device, out var updatedDevice))
                {
                    Device = updatedDevice;
                    ServiceConnections.AddOrUpdate(Context.ConnectionId, Device, (id, d) => Device);

                    var onlineOrganizationUsers = BrowserSocketHub.ConnectionIdToUserLookup
                                                  .Where(x => x.Value.OrganizationID == Device.OrganizationID);

                    var authorizedUsers = DataService.GetUsersWithAccessToDevice(onlineOrganizationUsers.Select(x => x.Value.Id), Device);
                    var connectionIds   = onlineOrganizationUsers
                                          .Where(onlineUser => authorizedUsers.Any(authorizedUser => authorizedUser.Id == onlineUser.Value.Id))
                                          .Select(x => x.Key)
                                          .ToList();

                    await BrowserHub.Clients.Clients(connectionIds).SendAsync("DeviceCameOnline", Device);
                }
                else
                {
                    // Organization wasn't found.
                    await Clients.Caller.SendAsync("UninstallClient");
                }
            }
            catch (Exception ex)
            {
                DataService.WriteEvent(ex);
                throw;
            }
        }
Beispiel #7
0
        public Task <bool> DeviceCameOnline(Device device)
        {
            try
            {
                if (ServiceConnections.Any(x => x.Value.ID == device.ID))
                {
                    DataService.WriteEvent(new EventLog()
                    {
                        EventType      = EventType.Info,
                        OrganizationID = device.OrganizationID,
                        Message        = $"Device connection for {device?.DeviceName} was denied because it is already connected."
                    });
                    return(Task.FromResult(false));
                }

                if (DataService.AddOrUpdateDevice(device, out var updatedDevice))
                {
                    Device = updatedDevice;
                    ServiceConnections.AddOrUpdate(Context.ConnectionId, Device, (id, d) => Device);

                    var connectionIds = BrowserSocketHub.ConnectionIdToUserLookup
                                        .Where(x => x.Value.OrganizationID == Device.OrganizationID)
                                        .Select(x => x.Key)
                                        .ToList();

                    BrowserHub.Clients.Clients(connectionIds).SendAsync("DeviceCameOnline", Device);
                    return(Task.FromResult(true));
                }
                else
                {
                    // Organization wasn't found.
                    return(Task.FromResult(false));
                }
            }
            catch (Exception ex)
            {
                DataService.WriteEvent(ex);
            }

            Context.Abort();
            return(Task.FromResult(false));
        }
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            if (Device != null)
            {
                DataService.DeviceDisconnected(Device.ID);

                Device.IsOnline = false;

                var connectionIds = BrowserSocketHub.ConnectionIdToUserLookup
                                    .Where(x => x.Value.OrganizationID == Device.OrganizationID)
                                    .Select(x => x.Key)
                                    .ToList();

                await BrowserHub.Clients.Clients(connectionIds).SendAsync("DeviceWentOffline", Device);

                ServiceConnections.Remove(Context.ConnectionId, out _);
            }

            await base.OnDisconnectedAsync(exception);
        }
Beispiel #9
0
        public async Task DeviceHeartbeat(Device device)
        {
            if (CheckForDeviceBan(device.ID, device.DeviceName))
            {
                return;
            }

            var ip = Context.GetHttpContext()?.Connection?.RemoteIpAddress;

            if (ip != null && ip.IsIPv4MappedToIPv6)
            {
                ip = ip.MapToIPv4();
            }
            device.PublicIP = ip?.ToString();

            if (CheckForDeviceBan(device.PublicIP))
            {
                return;
            }


            _dataService.AddOrUpdateDevice(device, out var updatedDevice);
            Device = updatedDevice;
            ServiceConnections.AddOrUpdate(Context.ConnectionId, Device, (id, d) => Device);

            var userIDs = _circuitManager.Connections.Select(x => x.User.Id);

            var filteredUserIDs = _dataService.FilterUsersByDevicePermission(userIDs, Device.ID);

            var connections = _circuitManager.Connections
                              .Where(x => x.User.OrganizationID == Device.OrganizationID &&
                                     filteredUserIDs.Contains(x.User.Id));

            foreach (var connection in connections)
            {
                _ = connection.InvokeCircuitEvent(CircuitEventName.DeviceUpdate, Device);
            }


            await CheckForPendingSriptRuns();
        }
Beispiel #10
0
        public Task DeviceHeartbeat(Device device)
        {
            if (CheckForDeviceBan(device.ID, device.DeviceName))
            {
                return(Task.CompletedTask);
            }

            var ip = Context.GetHttpContext()?.Connection?.RemoteIpAddress;

            if (ip != null && ip.IsIPv4MappedToIPv6)
            {
                ip = ip.MapToIPv4();
            }
            device.PublicIP = ip?.ToString();

            if (CheckForDeviceBan(device.PublicIP))
            {
                return(Task.CompletedTask);
            }


            DataService.AddOrUpdateDevice(device, out var updatedDevice);
            Device = updatedDevice;
            ServiceConnections.AddOrUpdate(Context.ConnectionId, Device, (id, d) => Device);

            var userIDs = BrowserHub.ConnectionIdToUserLookup.Values.Select(x => x.Id);

            var filteredUserIDs = DataService.FilterUsersByDevicePermission(userIDs, Device.ID);

            var connectionIds = BrowserHub.ConnectionIdToUserLookup
                                .Where(x => x.Value.OrganizationID == Device.OrganizationID &&
                                       filteredUserIDs.Contains(x.Value.Id))
                                .Select(x => x.Key)
                                .ToList();

            return(BrowserHubContext.Clients.Clients(connectionIds).SendAsync("DeviceHeartbeat", Device));
        }
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            if (Device != null)
            {
                DataService.DeviceDisconnected(Device.ID);

                Device.IsOnline = false;

                var onlineOrganizationUsers = BrowserSocketHub.ConnectionIdToUserLookup
                                              .Where(x => x.Value.OrganizationID == Device.OrganizationID);

                var authorizedUsers = DataService.GetUsersWithAccessToDevice(onlineOrganizationUsers.Select(x => x.Value.Id), Device);
                var connectionIds   = onlineOrganizationUsers
                                      .Where(onlineUser => authorizedUsers.Any(authorizedUser => authorizedUser.Id == onlineUser.Value.Id))
                                      .Select(x => x.Key)
                                      .ToList();

                await BrowserHub.Clients.Clients(connectionIds).SendAsync("DeviceWentOffline", Device);

                ServiceConnections.Remove(Context.ConnectionId, out _);
            }

            await base.OnDisconnectedAsync(exception);
        }
Beispiel #12
0
        public Task <bool> DeviceCameOnline(Device device)
        {
            try
            {
                if (CheckForDeviceBan(device.ID, device.DeviceName))
                {
                    return(Task.FromResult(false));
                }

                if (ServiceConnections.Any(x => x.Value.ID == device.ID))
                {
                    _dataService.WriteEvent(new EventLog()
                    {
                        EventType      = EventType.Info,
                        OrganizationID = device.OrganizationID,
                        Message        = $"Device connection for {device?.DeviceName} was denied because it is already connected."
                    });
                    return(Task.FromResult(false));
                }

                var ip = Context.GetHttpContext()?.Connection?.RemoteIpAddress;
                if (ip != null && ip.IsIPv4MappedToIPv6)
                {
                    ip = ip.MapToIPv4();
                }
                device.PublicIP = ip?.ToString();

                if (CheckForDeviceBan(device.PublicIP))
                {
                    return(Task.FromResult(false));
                }

                if (_dataService.AddOrUpdateDevice(device, out var updatedDevice))
                {
                    Device = updatedDevice;
                    ServiceConnections.AddOrUpdate(Context.ConnectionId, Device, (id, d) => Device);

                    var userIDs = _circuitManager.Connections.Select(x => x.User.Id);

                    var filteredUserIDs = _dataService.FilterUsersByDevicePermission(userIDs, Device.ID);

                    var connections = _circuitManager.Connections
                                      .Where(x => x.User.OrganizationID == Device.OrganizationID &&
                                             filteredUserIDs.Contains(x.User.Id));

                    foreach (var connection in connections)
                    {
                        connection.InvokeCircuitEvent(CircuitEventName.DeviceUpdate, Device);
                    }
                    return(Task.FromResult(true));
                }
                else
                {
                    // Organization wasn't found.
                    return(Task.FromResult(false));
                }
            }
            catch (Exception ex)
            {
                _dataService.WriteEvent(ex, device?.OrganizationID);
            }

            Context.Abort();
            return(Task.FromResult(false));
        }