// Get next entry from csv file public InputDataEntry GetNextEntry() { InputDataEntry csvDataEntry = null; if (csvStreamReader == null) { return(null); } if (csvStreamReader.EndOfStream) { Console.WriteLine("End of file"); return(null); } var csvLine = csvStreamReader.ReadLine(); Regex lineCheckReg = new Regex("^((?<UUID>[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12});((0x){0,1})(?<DataToSend>(([0-9A-Fa-f]{2})+)))$"); Match lineRegexMatch = lineCheckReg.Match(csvLine); if (!lineRegexMatch.Success) { Console.WriteLine("File " + currentFilePath + " line: " + currentLineNumber + " contains data in no supported format. Supported format: yyyy-mm-dd HH-MM-SS;<Hex data>"); } else { csvDataEntry = new InputDataEntry(); csvDataEntry.UUID = new Guid(lineRegexMatch.Groups["UUID"].Value); string dataToSendString = lineRegexMatch.Groups["DataToSend"].Value; csvDataEntry.DataToSend = Enumerable.Range(0, dataToSendString.Length) .Where(x => x % 2 == 0) .Select(x => Convert.ToByte(dataToSendString.Substring(x, 2), 16)) .ToArray(); } currentLineNumber++; return(csvDataEntry); }
private static void Main(string[] args) { var options = new Options(); string macArgInput = ""; string inputDataFilePath = ""; string configurationFilePath = ""; CommandLine.Parser.Default.ParseArguments <Options>(args).WithParsed <Options>(o => { if (o.MacAddress != String.Empty) { Console.WriteLine("MAC: {0}", o.MacAddress); macArgInput = o.MacAddress; } else { PrintWrongMacArgumentInfo(); Environment.Exit(-1); } if (!File.Exists(o.InputDataFilePath)) { PrintWrongInputFileArgumentInfo((o.InputDataFilePath)); Environment.Exit(-1); } else { inputDataFilePath = o.InputDataFilePath; } if (!File.Exists(o.ConfigurationFilePath)) { PrintWrongInputFileArgumentInfo(o.ConfigurationFilePath); Environment.Exit(-1); } else { configurationFilePath = o.ConfigurationFilePath; } } ); watcher = new DeviceWatcher(); deviceConnector = new DeviceConnector(); // Check MAC address string macRegex = @"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"; Regex regex = new Regex(macRegex); if (regex.IsMatch(macArgInput)) { macToFind = macArgInput; watcher.SetMacFilterString(macArgInput); watcher.DeviceFoundEvent += FoundDeviceEventHandler; } else { PrintWrongMacArgumentInfo(); Environment.Exit(-1); } watcher.StartScanning(); // Wait for connection while (!deviceConnector.IsDeviceFound()) { } while (deviceConnector.ServiceList.Count == 0) { // Discover services and characteristics Task.Run(async() => { await deviceConnector.DiscoverServices(); }).GetAwaiter().GetResult(); } Console.WriteLine("Load configuration file"); AppConf.Configuration configuration = AppConf.DeserializeXml("altlight_plug_conf.xml"); bool checkResult = AppConf.ApplyConfigurationConditions(configuration, deviceConnector); Console.WriteLine("Check result: " + checkResult); Console.WriteLine("Load input data " + inputDataFilePath); CSV.InputData.InputDataParser inputDataParser = new CSV.InputData.InputDataParser(); inputDataParser.OpenFile(inputDataFilePath); while (true) { CSV.InputData.InputDataEntry entry = inputDataParser.GetNextEntry(); // If entry is null, then this must be end of file if (entry == null) { break; } Console.WriteLine(entry); // Send data to specified uuid Task.Run(async() => { await deviceConnector.WriteDataToCharacteristic(entry.UUID, entry.DataToSend); }).GetAwaiter().GetResult(); // Wait some time, TODO: Find the way to send data without losing it and not using fixed interval Task.Run(async() => { await Task.Delay(400); }).GetAwaiter().GetResult(); } // Wait some time after sending last part of input data Task.Run(async() => { await Task.Delay(1000); }).GetAwaiter().GetResult(); // Flush received data to .csv files CSV.OutputData.CsvDataWriter.FlushDataToFiles(); }