Beispiel #1
0
        private void NetworkAction(Persistence.Models.Network network, string action)
        {
            var computerRepository = _repositoryFactory.GetComputerRepository();
            var scriptRepository   = _repositoryFactory.GetScriptRepository();
            var taskRepository     = _repositoryFactory.GetTaskRepository();
            var runScript          = new Computer.Actions.RunScript(computerRepository, scriptRepository, taskRepository);

            runScript.Run(
                computer: network.AttatchedComputer,
                scriptText: $"<HomeAutomation.{action} Network=\"{network.Address}\" />\n<HomeAutomation.SyncWithCloud />",
                source: "RoomieBot",
                updateLastRunScript: false,
                user: _user
                );
        }
Beispiel #2
0
        private Response <string[]> SyncWholeNetwork(Persistence.Models.Network network, Persistence.Models.Computer computer, string[] devicesXml)
        {
            var computerRepository = _repositoryFactory.GetComputerRepository();
            var deviceRepository   = _repositoryFactory.GetDeviceRepository();
            var syncWholeNetwork   = new Actions.SyncWholeNetwork(computerRepository, deviceRepository, _networkRepository);

            var sentDevices = devicesXml
                              .Select(x => XElement.Parse(x))
                              .ToArray();

            var existingDevices = syncWholeNetwork.Run(
                computer: computer,
                network: network,
                sentDevices: sentDevices,
                user: _user
                );

            var existingDevicesXml = existingDevices
                                     .Select(x => x.ToXElement())
                                     .Select(x => x.ToString())
                                     .ToArray();

            return(Response.Create(existingDevicesXml));
        }
Beispiel #3
0
        private void UpdateDevices(IEnumerable <IDeviceState> sentDevices, IEnumerable <Persistence.Models.Device> registeredDevices, Persistence.Models.Network network)
        {
            // go through the devices from the client and update the entries in the database
            foreach (var sentDevice in sentDevices)
            {
                var registeredDevice = registeredDevices.FirstOrDefault(x => x.Address == sentDevice.Address && x.Network.Address == sentDevice.NetworkState.Address);

                if (registeredDevice == null)
                {
                    var newDevice = Persistence.Models.Device.Create(
                        address: sentDevice.Address,
                        isConnected: sentDevice.IsConnected,
                        name: sentDevice.Name,
                        network: network,
                        location: sentDevice.Location,
                        type: sentDevice.Type
                        );

                    newDevice.Update(sentDevice);

                    _deviceRepository.Add(newDevice);
                }
                else
                {
                    _deviceRepository.Update(registeredDevice.Id, sentDevice);
                }
            }
        }
Beispiel #4
0
        private IDeviceState[] ProcessSentDevices(IEnumerable <XElement> sentDevices, Persistence.Models.Network network)
        {
            var result = sentDevices
                         .Select(x => x.ToDeviceState())
                         .Select(x => x.NewWithNetwork(network))
                         .ToArray();

            return(result);
        }
Beispiel #5
0
 public Persistence.Models.Device[] Run(Persistence.Models.Computer computer, Persistence.Models.User user, Persistence.Models.Network network, IEnumerable <XElement> sentDevices)
 {
     return(Run(computer, user, network, ProcessSentDevices(sentDevices, network)));
 }
Beispiel #6
0
        public Persistence.Models.Device[] Run(Persistence.Models.Computer computer, Persistence.Models.User user, Persistence.Models.Network network, IDeviceState[] sentDevices)
        {
            computer.UpdatePing();
            _computerRepository.Update(computer);

            network.UpdatePing(computer);
            _networkRepository.Update(network);

            var existingDevices = _deviceRepository.Get(network);

            UpdateDevices(sentDevices, existingDevices, network);

            var devicesToRemove = GetRemovedDevices(sentDevices, existingDevices);

            RemoveDevices(devicesToRemove);

            return(existingDevices);
        }
Beispiel #7
0
        private static Persistence.Models.Network GetSerializableVersion(Persistence.Models.Network network)
        {
            Persistence.Models.Computer computer = null;

            if (network.AttatchedComputer != null)
            {
                computer = new Persistence.Models.Computer(
                    accessKey: null,
                    address: null,
                    encryptionKey: null,
                    id: network.AttatchedComputer.Id,
                    lastPing: network.AttatchedComputer.LastPing,
                    lastScript: null,
                    name: network.AttatchedComputer.Name,
                    owner: null
                    );
            }

            Persistence.Models.Device[] devices = null;

            if (network.Devices != null)
            {
                devices = network.Devices
                          .Select(x => new Persistence.Models.Device(
                                      address: x.Address,
                                      id: x.Id,
                                      lastPing: null,
                                      name: x.Name,
                                      network: null,
                                      scripts: null,
                                      state: null,
                                      tasks: null,
                                      type: x.Type
                                      ))
                          .ToArray();
            }

            Persistence.Models.User owner = null;

            if (network.Owner != null)
            {
                owner = new Persistence.Models.User(
                    alias: network.Owner.Alias,
                    email: null,
                    id: network.Owner.Id,
                    registeredTimestamp: null,
                    secret: null,
                    token: null
                    );
            }

            return(new Persistence.Models.Network(
                       address: network.Address,
                       attatchedComputer: computer,
                       devices: devices,
                       id: network.Id,
                       lastPing: network.LastPing,
                       name: network.Name,
                       owner: owner
                       ));
        }