public static string ExecuteShellCommand(string command, int waitForExit = 1000) { // execute shell command: var info = new ProcessStartInfo() { FileName = "/bin/bash", Arguments = "-c \"" + command + "\"", UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true }; using (var p = Process.Start(info)) { string err = null; p.ErrorDataReceived += (sender, e) => err += e.Data; string output = p.StandardOutput.ReadToEnd(); if (!p.WaitForExit(waitForExit)) { CALog.LogData(LogID.A, $"timed out waiting for command to exit: {command}"); } else { p.WaitForExit(); // make sure all async events are finished handling https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?view=net-5.0#System_Diagnostics_Process_WaitForExit_System_Int32_ } if (!string.IsNullOrEmpty(err)) { CALog.LogData(LogID.A, $"error while running command {command} - {err}"); } p.ErrorDataReceived -= (sender, e) => err += e.Data; return(output); } }
public IOconfMap(string row, int lineNum) : base(row, lineNum, "Map") { format = "Map;SerialNo/COM1/USB1-1.1;BoxName;[NodeName];[baud rate]"; var list = ToList(); if (list[0] != "Map") { throw new Exception($"IOconfMap: wrong format: {row} {format}"); } bool isWindows = RpiVersion.IsWindows(); if (isWindows && list[1].StartsWith("COM")) { USBPort = list[1]; } else if (!isWindows && list[1].StartsWith("USB")) { USBPort = "/dev/" + list[1]; } else { SerialNumber = list[1]; } BoxName = list[2]; if (list.Count <= 3) { return; } string distributedNodeName = list.Count == 5 ? list[3] : default; var baudrate = 0; if (list.Count >= 5 && !int.TryParse(list[4], out baudrate)) { CALog.LogErrorAndConsoleLn(LogID.A, $"Failed to parse the baud rate for the board: {BoxName}. Attempting with defaults."); } else if (list.Count == 4 && int.TryParse(list[3], out baudrate)) { BaudRate = baudrate; } else { distributedNodeName = list[3]; } BaudRate = baudrate; DistributedNode = distributedNodeName != default ? IOconfFile.GetEntries <IOconfNode>().SingleOrDefault(n => n.Name == distributedNodeName) ?? throw new Exception($"Failed to find node in configuration for Map: {row}. Format: {format}") : !IOconfFile.GetEntries <IOconfNode>().Any() ? DistributedNode : throw new Exception($"The node name is not optional for distributed deployments: {row}. Format: {format}"); }
static async Task MainAsync(string[] args) { try { CALog.LogInfoAndConsoleLn(LogID.A, RpiVersion.GetWelcomeMessage($"Upload temperature data to cloud")); Console.WriteLine("Initializing..."); using (var serial = await SerialNumberMapper.DetectDevices()) { if (args.Length > 0 && args[0] == "-listdevices") { return; // SerialNumberMapper already lists devices, no need for further output. } // close all ports which are not Hub10 serial.McuBoards.Where(x => !x.productType.Contains("Temperature") && !x.productType.Contains("Hub10STM")).ToList().ForEach(x => x.SafeClose(System.Threading.CancellationToken.None).Wait()); var email = IOconfSetup.UpdateIOconf(serial); using var cmd = new CommandHandler(serial); using var usb = new ThermocoupleBox(cmd); using var cloud = new ServerUploader(cmd.GetFullSystemVectorDescription(), cmd); CALog.LogInfoAndConsoleLn(LogID.A, "Now connected to server..."); _ = Task.Run(() => cmd.RunSubsystems()); int i = 0; var uploadThrottle = new TimeThrottle(100); while (cmd.IsRunning) { var(sensorsSamples, vectorTime) = cmd.GetFullSystemVectorValues(); cloud.SendVector(sensorsSamples.Select(v => v.Value).ToList(), vectorTime); Console.Write($"\r data points uploaded: {i++}"); // we don't want this in the log file. uploadThrottle.Wait(); if (i == 20) { DULutil.OpenUrl(cloud.GetPlotUrl()); } } } CALog.LogInfoAndConsoleLn(LogID.A, Environment.NewLine + "Bye..." + Environment.NewLine + "Press any key to exit"); } catch (Exception ex) { ShowHumanErrorMessages(ex); } Console.ReadKey(); }
private static void CheckRules() { // no two rows can have the same type,name combination. var groups = Table.GroupBy(x => x.UniqueKey()); foreach (var g in groups.Where(x => x.Count() > 1)) { CALog.LogErrorAndConsoleLn(LogID.A, $"ERROR in {Directory.GetCurrentDirectory()}\\IO.conf:{Environment.NewLine} Name: {g.First().ToList()[1]} occure {g.Count()} times in this group: {g.First().ToList()[0]}{Environment.NewLine}"); } // no heater can be in several oven areas var heaters = GetOven().Where(x => x.OvenArea > 0).GroupBy(x => x.HeatingElement); foreach (var heater in heaters.Where(x => x.Select(y => y.OvenArea).Distinct().Count() > 1)) { CALog.LogErrorAndConsoleLn(LogID.A, $"ERROR in {Directory.GetCurrentDirectory()}\\IO.conf:{Environment.NewLine} Heater: {heater.Key.Name} occure in several oven areas : {string.Join(", ", heater.Select(y => y.OvenArea).Distinct())}"); } }
private static void ShowHumanErrorMessages(Exception ex) { if (ex.Message.StartsWith("account already exist")) { CALog.LogErrorAndConsoleLn(LogID.A, "Your password was wrong. Please exit and try again.."); CALog.LogInfoAndConsoleLn(LogID.A, Environment.NewLine + "Press any key to exit"); } else if (ex.Message.StartsWith("Could not find any devices connected to USB")) { CALog.LogErrorAndConsoleLn(LogID.A, ex.Message + " Please check USB connections and try again.."); CALog.LogInfoAndConsoleLn(LogID.A, Environment.NewLine + "Press any key to exit"); } else if (ex.InnerException != null && ex.InnerException.Message.Contains("LoopName already used before:")) { CALog.LogErrorAndConsoleLn(LogID.A, "Please change your Webchart name and try again.."); CALog.LogInfoAndConsoleLn(LogID.A, Environment.NewLine + "Press any key to exit"); } else { CALog.LogException(LogID.A, ex); } }
public void TestMethod1() { CALog.LogInfoAndConsoleLn(LogID.B, "Thorium salt energy is gooood"); }