Exemple #1
0
 public void AppendAllLinesTest()
 {
     ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
     SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
     string path = string.Empty; // TODO: Initialize to an appropriate value
     IEnumerable<string> contents = null; // TODO: Initialize to an appropriate value
     target.AppendAllLines(path, contents);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
Exemple #2
0
        public void ConfigurateDevice(StationModel station)
        {
            if (station?.InformationTable == null)
            {
                return;
            }
            UserName = station?.InformationTable?.UserNameDevice;
            Password = station?.InformationTable?.PasswordDevice;
            var ipAddressDevice = station?.InformationTable?.IpDevice;
            var stationId       = station.Id;

            station.InformationTable
            ?.CheckAccessCode();
            var accessCode = station.InformationTable
                             ?.AccessCode;

            using (var ssh = new SshClient(ipAddressDevice, UserName, Password))
            {
                try
                {
                    ssh.Connect();
                }
                catch (Exception ex)
                {
                    var manager = new ContextManager();
                    if (CheckPingDevice(ipAddressDevice))
                    {
                        StationModel newStation = null;
                        if (string.Equals(station.InformationTable.PasswordDevice, "$olnechniKrug2019"))
                        {
                            newStation = manager.ChangePassword(station, "$olnechniKrug");
                        }
                        else
                        {
                            newStation = manager.ChangePassword(station, "$olnechniKrug2019");
                        }
                        ConfigurateDevice(newStation);
                    }
                    Logger.WriteLog($"Произошла ошибка при попытки соеденения {ex.Message}, подробности {ex.StackTrace}", "ConfigurateDevice");
                    return;
                }
                var command = ssh.CreateCommand("ls .config/autostart", Encoding.UTF8);
                command.Execute();
                var answer = command.Result;
                if (answer.Contains("chromium.desktop\n"))
                {
                    command = ssh.CreateCommand("rm -f .config/autostart/chromium.desktop\n");
                    command.Execute();
                }
                using (var sftp = new SftpClient(ipAddressDevice, UserName, Password))
                {
                    try
                    {
                        sftp.Connect();
                    }
                    catch (SshConnectionException ex)
                    {
                        Logger.WriteLog($"Произошла ошибка при попытки соеденения {ex.Message}, подробности {ex.StackTrace}", "ConfigurateDevice");
                        return;
                    }
                    var text = new List <string>();
                    text.Add("[Desktop Entry]");
                    text.Add("Encoding=UTF-8");
                    text.Add("Name=Connect");
                    text.Add("Comment=Checks internet connectivity");
                    text.Add($"Exec=/usr/bin/chromium-browser -incognito --noerrdialogs --kiosk http://92.50.187.210/citystations/Home/DisplayInformationTable?stationId={stationId}&accessCode={accessCode}");
                    try
                    {
                        sftp.AppendAllLines(".config/autostart/chromium.desktop", text);
                    }
                    catch (SftpPermissionDeniedException ex)
                    {
                        Logger.WriteLog($"Произошла ошибка при попытки создания файлов {ex.Message}, подробности {ex.StackTrace}", "ConfigurateDevice");
                        if (!ssh.IsConnected)
                        {
                            ssh.Connect();
                        }
                        command = ssh.CreateCommand("sudo rm -f -r .config/autostart\n");
                        try
                        {
                            command.Execute();
                        }
                        catch (SshException sshex)
                        {
                            Logger.WriteLog($"Произошла ошибка при попытки удаления папки {ex.Message}, подробности {ex.StackTrace}", "ConfigurateDevice");
                            return;
                        }
                        command = ssh.CreateCommand("mkdir .config/autostart\n");
                        try
                        {
                            command.Execute();
                        }
                        catch (SshException sshexep)
                        {
                            Logger.WriteLog($"Произошла ошибка при попытки создания папки {ex.Message}, подробности {ex.StackTrace}", "ConfigurateDevice");
                            return;
                        }
                        try
                        {
                            sftp.AppendAllLines(".config/autostart/chromium.desktop", text);
                        }
                        catch (SftpPermissionDeniedException sftpError)
                        {
                            Logger.WriteLog($"Произошла ошибка при попытки добавления данных в файл {ex.Message}, подробности {ex.StackTrace}", "ConfigurateDevice");
                            return;
                        }
                    }
                }
                if (!ssh.IsConnected)
                {
                    ssh.Connect();
                }
                command = ssh.CreateCommand("sudo reboot now\n", Encoding.UTF8);
                try
                {
                    command.Execute();
                }
                catch (SshConnectionException ex)
                {
                    Logger.WriteLog($"Выполнена перезагрузка устройства {ipAddressDevice}", "ConfigurateDevice");
                    return;
                }
            }
        }