private static void ScanDevices(CommandLineApplication command) { command.Description = "Scan local IPv4 network for WAGO controllers"; command.OnExecute(() => { Console.WriteLine("Scanning for local IPv4 devices ..."); var deviceAddresses = Browser.FindIpV4Devices(); foreach (var address in deviceAddresses) { Console.Write(address + "\t: "); var di = WagoService.QueryDeviceInfo(address.ToString()); Console.WriteLine(di != null ? di.ToString() : "no WAGO controller"); } Console.WriteLine("done."); return(0); }); }
private static void ResetDevice(CommandLineApplication command) { command.Description = "Reset specified WAGO controller"; var controller = command.Argument( "controller", "The ip address of the controller"); command.OnExecute(() => { if (string.IsNullOrEmpty(controller.Value)) { Console.WriteLine("ERROR: A controller address has to be specified."); return(1); } Console.WriteLine($"Resetting {controller.Value} ..."); WagoService.ResetDevice(controller.Value); Console.WriteLine("done."); return(0); }); }
private static void QueryDevice(CommandLineApplication command) { command.Description = "Query system info of the specified WAGO controller"; var controller = command.Argument( "controller", "The ip address of the controller"); command.OnExecute(() => { if (string.IsNullOrEmpty(controller.Value)) { Console.WriteLine("ERROR: A controller address has to be specified."); return(1); } Console.WriteLine($"Querying {controller.Value} ..."); Console.WriteLine(); var di = WagoService.QueryDeviceInfo(controller.Value); if (di != null) { Console.WriteLine($"Order number: {di.OrderNumber}"); Console.WriteLine($"Description: {di.Description}"); Console.WriteLine($"Serial number: {di.SerialNumber}"); Console.WriteLine($"Software version: {di.SoftwareVersion}"); Console.WriteLine($"Hardware version: {di.HardwareVersion}"); Console.WriteLine($"Firmware loader version: {di.FirmwareLoaderVersion}"); Console.WriteLine($"Baud rate: {di.BaudRate}"); Console.WriteLine($"Firmware burn date: {di.FirmwareBurnDate}"); Console.WriteLine($"Product serial number: {di.ProductSerialNumber}"); Console.WriteLine($"QS string: {di.QsString}"); } else { Console.WriteLine("no WAGO controller"); } return(0); }); }
internal static int Execute() { if (string.IsNullOrEmpty(_controller.Value)) { Console.WriteLine("ERROR: A controller address has to be specified."); return(1); } var packageName = _package.Value; if (string.IsNullOrEmpty(packageName)) { var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); packageName = Directory.EnumerateFiles(path, "*.wago").FirstOrDefault(); if (string.IsNullOrEmpty(packageName)) { Console.WriteLine("ERROR: No package specified or found locally."); return(1); } Console.WriteLine($"Using package {Path.GetFileName(packageName)}"); } var ext = Path.GetExtension(packageName); if (string.IsNullOrEmpty(ext)) { packageName += ".wago"; } Console.WriteLine(); Console.WriteLine($"Loading {Path.GetFileName(packageName)} to {_controller.Value} ..."); var di = WagoService.QueryDeviceInfo(_controller.Value); if (di == null) { Console.WriteLine("ERROR: At the specified address is no WAGO controller."); return(1); } var package = WagoPackage.Load(packageName); if (package == null) { return(1); } var product = package.Specification.System.Products .FirstOrDefault(p => p.SerialNumber == di.ProductSerialNumber); if (product == null) { Console.WriteLine($"ERROR: The target controller type ({di.ProductSerialNumber}) does not match the package specification."); return(1); } Console.WriteLine(); Console.WriteLine($"Installing package {Path.GetFileNameWithoutExtension(packageName)}"); Console.WriteLine($" {package.Specification.Description} - version {package.Specification.Version}"); Console.WriteLine($" on WAGO controller {di.ProductSerialNumber}"); Console.WriteLine(); var shell = new RemoteShell(_controller.Value); // get root password from package - if already applied var specRootPwd = package.Specification.Users.Linux.FirstOrDefault(up => up.Name == "root"); var rootPwd = specRootPwd?.Password ?? ""; // detect current root password from the list to test rootPwd = shell.GetRootPassword(new List <string> { "wago", rootPwd }); if (rootPwd == null) { Console.WriteLine("ERROR: Root password not matching."); Console.WriteLine(); Console.WriteLine("Try factory resetting the controller."); return(1); } // set linux users as given in the packet foreach (var linuxUser in package.Specification.Users.Linux) { Console.WriteLine(shell.ChangePassword(rootPwd, linuxUser.Name, linuxUser.Password) ? $"Changed password for linux user {linuxUser.Name}." : $"ERROR: Failed to change password for linux user {linuxUser.Name}."); if (linuxUser.Name == "root") { // save new root password rootPwd = linuxUser.Password; } } // set timezone var timezone = package.Specification.System.Timezone; if (!string.IsNullOrEmpty(timezone)) { Console.WriteLine("Setting time zone to " + timezone); EchoShellResponse(Timezone.SetTimezone(shell, rootPwd, timezone)); } if (package.Specification.System.SetDateTime) { var utcNow = DateTime.UtcNow; Console.WriteLine("Setting date and time to UTC " + utcNow); EchoShellResponse(Clock.SetDateTime(shell, rootPwd, utcNow)); } // set WBM users as given in the packet Console.WriteLine("Loading WBM users..."); const string pwdFileName = "lighttpd-htpasswd.user"; try { File.WriteAllText(pwdFileName, ""); var pwdFile = new PasswordFile(pwdFileName); foreach (var wbmUser in package.Specification.Users.Wbm) { pwdFile.SetPassword(wbmUser.Name, wbmUser.Password); } var inf = new FileInfo(pwdFileName); using (var scp = new ScpClient(_controller.Value, "root", rootPwd)) { scp.RemotePathTransformation = RemotePathTransformation.ShellQuote; scp.Connect(); if (!scp.IsConnected) { Console.WriteLine("ERROR: Could connect upload SCP."); return(1); } scp.Upload(inf, $"/etc/lighttpd/{pwdFileName}"); } } catch (Exception ex) { Console.WriteLine("ERROR: Failed to upload WBM passwords: " + ex.Message); return(1); } finally { File.Delete(pwdFileName); } // transfer CodeSys project var tmpPrg = Path.GetTempFileName(); var tmpChk = Path.GetTempFileName(); try { var prgName = product.PackageName; var chkName = Path.ChangeExtension(product.PackageName, "chk"); Console.WriteLine($"Loading Codesys project {prgName}..."); using (var scp = new ScpClient(_controller.Value, "root", rootPwd)) { scp.RemotePathTransformation = RemotePathTransformation.ShellQuote; scp.Connect(); if (!scp.IsConnected) { Console.WriteLine("ERROR: Could connect upload project."); return(1); } package.ExtractFile(prgName, tmpPrg); var prg = new FileInfo(tmpPrg); scp.Upload(prg, "/home/codesys/DEFAULT.PRG"); package.ExtractFile(chkName, tmpChk); var chk = new FileInfo(tmpChk); scp.Upload(chk, "/home/codesys/DEFAULT.CHK"); } } catch (Exception ex) { Console.WriteLine("ERROR: Failed to upload project files: " + ex.Message); return(1); } finally { if (File.Exists(tmpPrg)) { File.Delete(tmpPrg); } if (File.Exists(tmpChk)) { File.Delete(tmpChk); } } // transfer file system files var contentFiles = package.GetPackageFiles() .Where(fn => fn.StartsWith("filesystem", StringComparison.InvariantCultureIgnoreCase)) .ToList(); if (contentFiles.Count > 0) { Console.WriteLine("Creating file system content directories..."); var contentDirectories = contentFiles .Select(cf => Path.GetDirectoryName(cf.Substring("filesystem".Length)).Replace(Path.DirectorySeparatorChar, '/')) .Distinct() .ToList(); foreach (var contentDirectory in contentDirectories) { EchoShellResponse(shell.ExecCommand("root", rootPwd, $"mkdir {contentDirectory}")); } Console.WriteLine($"Loading {contentFiles.Count} file system content files..."); foreach (var contentFile in contentFiles) { var tmpFile = Path.GetTempFileName(); try { using (var scp = new ScpClient(_controller.Value, "root", rootPwd)) { scp.RemotePathTransformation = RemotePathTransformation.ShellQuote; scp.Connect(); if (!scp.IsConnected) { Console.WriteLine(); Console.WriteLine("ERROR: Could connect upload content."); return(1); } package.ExtractFile(contentFile, tmpFile); var sourceFile = new FileInfo(tmpFile); var targetFileName = contentFile.Substring("filesystem".Length).Replace(Path.DirectorySeparatorChar, '/'); Console.Write("."); scp.Upload(sourceFile, targetFileName); } } catch (Exception ex) { Console.WriteLine(); Console.WriteLine($"ERROR: Failed to upload content file {contentFile}: " + ex.Message); return(1); } finally { if (File.Exists(tmpFile)) { File.Delete(tmpFile); } } } } Console.WriteLine(); Console.WriteLine("Done."); if (_noReboot.HasValue()) { Console.WriteLine("Do NOT restart controller."); return(0); } WagoService.ResetDevice(_controller.Value); Console.WriteLine("Restarting controller."); return(0); }