public async Task RunProcesses(BlueToothCmds oBlueToothCmds) { DateTime dtCurrent = DateTime.UtcNow; foreach (BluetoothCmd bcmd in oBlueToothCmds) { bcmd.dtNext = dtCurrent.AddMilliseconds(bcmd.Rate); } await Task.Run(() => { while (true) { foreach (BluetoothCmd bcmd in oBlueToothCmds) { dtCurrent = DateTime.UtcNow; if (dtCurrent >= bcmd.dtNext) { // ToDo: main processing routines! bcmd.dtNext = dtCurrent.AddMilliseconds(bcmd.Rate); Debug.WriteLine("Process:" + bcmd.Name); } } } }).ConfigureAwait(false); }
/// <summary> /// Performs Bluetooth connection operations /// </summary> /// <param name="name"></param> /// <param name="address"></param> private async void OpenBluetooth(string name, string address) { if (!Bluetooth.CheckAdapterPresent()) // Check if bluetooth is available on this device: display message and return on failure { DisplayMessage(Bluetooth.GetStatusMessage()); return; } if (!Bluetooth.CheckAdapterEnabled()) // Check if bluetooth is enabled on this device: display message and return on failure { // ToDo: open OS settings page? DisplayMessage(Bluetooth.GetStatusMessage()); return; } Bluetooth oBth = new Bluetooth(true); // Create connection object if (!oBth.LoadPairedDevices()) // Attempt to load paired devices: display message and return on failure { DisplayMessage(Bluetooth.GetStatusMessage()); return; } if (!oBth.CheckPairedDevices()) // Check if there are paired devices available: display message and return on failure { // ToDo: open OS settings page? DisplayMessage(Bluetooth.GetStatusMessage()); return; } if (!await oBth.OpenPairedDevice(name, address)) // Attempt to open paired device: if failed get list of paired devices { List <BluetoothConnection> bcs = oBth.GetPairedDevices(); // ToDo: populate a listview and let user select the OBDII device // Retry oBth.OpenPairedDevice(name, address); return; } // Success! ////////// // Load some test commands and run processing loop BlueToothCmds oBthCmds = new BlueToothCmds(); oBthCmds.CreateTestCommands(); await oBth.RunProcesses(oBthCmds); }
private static string GetProcessHeader(BlueToothCmds bthcmds) { StringBuilder sb = new StringBuilder(); sb.Append("Time" + LOG_DELIMIT); bthcmds.ForEach(x => { if (!string.IsNullOrEmpty(x.Name) && (x.Selection_Type == BlueToothCmds.SELECTION_TYPE.USER || x.Selection_Type == BlueToothCmds.SELECTION_TYPE.USER_PROCESS)) { sb.Append(x.Name + LOG_DELIMIT); } }); return(sb.ToString()); }
public bool InitLogging(BlueToothCmds blueToothCmds) { isError = false; try { lBluetoothCmds = new List <BluetoothCmd>(); oBlueToothCmds = blueToothCmds ?? throw new Exception("Invalid Command List"); ProcessHeader = GetProcessHeader(blueToothCmds, lBluetoothCmds); CreateLogFile(); return(true); } catch (Exception e) { isError = true; status_message = e.Message; return(false); } }
private static string GetProcessHeader(BlueToothCmds bthcmds, ICollection <BluetoothCmd> lbthcmds) { StringBuilder sb = new StringBuilder(); sb.Append("Time" + LOG_DELIMIT); lbthcmds.Clear(); bthcmds.ForEach(x => { // If valid append to string and add to working list if (!string.IsNullOrEmpty(x.Name) && (x.Selection_Type == BlueToothCmds.SELECTION_TYPE.USER || x.Selection_Type == BlueToothCmds.SELECTION_TYPE.USER_PROCESS)) { sb.Append(x.Name + LOG_DELIMIT); lbthcmds.Add(x); } }); sb.Append(Environment.NewLine); return(sb.ToString()); }
public static List <BluetoothCmd> CreateList() { BluetoothCmd coolant_temp = new BluetoothCmd() { Id = 1, Name = "coolant temp", Units_Imperial = "F", Units_Metric = "C", Cmd = "01051", Rate = 3000, Decimals = 0, Expression_Imperial = "((a-40)*9/5+32)", Expression_Metric = "(a - 40)", Bytes = 4, isRxBytes = true, isSelected = false, Value_Min = -40, Value_Max = 215, sCommand_Types = COMMAND_DEFAULT }; BluetoothCmd vss = new BluetoothCmd() { Id = 2, Name = "VSS", Units_Imperial = "MPH", Units_Metric = "KPH", Cmd = "010D1", Rate = 1000, Decimals = 1, Expression_Imperial = "(a / 1.609)", Expression_Metric = "", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = true, Value_Min = 0, Value_Max = 255 }; BluetoothCmd iat = new BluetoothCmd() { Id = 3, Name = "IAT", Units_Imperial = "F", Units_Metric = "C", Cmd = "010F1", Rate = 1000, Decimals = 1, Expression_Imperial = "((a-40)*9/5+32)", Expression_Metric = "(a - 40)", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = -40, Value_Max = 215 }; BluetoothCmd map = new BluetoothCmd() { Id = 4, Name = "MAP", Units_Imperial = "PSI", Units_Metric = "KPA", Cmd = "010B1", Rate = 1000, Decimals = 1, Expression_Imperial = "(a*0.145)", Expression_Metric = "(a)", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 255 }; BluetoothCmd tps = new BluetoothCmd() { Id = 5, Name = "TPS", Units_Imperial = "%", Units_Metric = "%", Cmd = "01111", Rate = 500, Decimals = 0, Expression_Imperial = "(a * 100 / 255)", Expression_Metric = "(a * 100 / 255)", Bytes = 1, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 100 }; BluetoothCmd maf = new BluetoothCmd() { Id = 6, Name = "MAF", Units_Imperial = "G/S", Units_Metric = "G/S", Cmd = "010D1", Rate = 1000, Decimals = 1, Expression_Imperial = "(a * 1)", Expression_Metric = "(a * 1)", Bytes = 1, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 10000 }; BluetoothCmd rpm = new BluetoothCmd() { Id = 7, Name = "RPM", Units_Imperial = "x1000", Units_Metric = "x1000", Cmd = "010C1", Rate = 500, Decimals = 0, Expression_Imperial = "(a * 1)", Expression_Metric = "(a * 1)", Bytes = 1, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 10000 }; BluetoothCmd mpg = new BluetoothCmd() { Id = 8, Name = "miles per gallon", Units_Imperial = "MPG", Units_Metric = "KPG", Cmd = "", Rate = 2000, Decimals = 1, Expression_Imperial = "(14.7*b*1740.572)/(3600*c/100)", Expression_Metric = "(14.7*b*1740.572)/(3600*c/100)", Bytes = 0, isRxBytes = false, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 100 }; BluetoothCmd fuel_system_status = new BluetoothCmd() { Id = 9, Name = "fuel system status", Units_Imperial = "", Units_Metric = "", Cmd = "01031", Rate = 5000, Decimals = 0, Expression_Imperial = "", Expression_Metric = "", Bytes = 4, isRxBytes = false, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Max = 0, Value_Min = 0 }; BluetoothCmd fuel_pressure = new BluetoothCmd() { Id = 10, Name = "fuel pressure", Units_Imperial = "", Units_Metric = "", Cmd = "010A1", Rate = 500, Decimals = 0, Expression_Imperial = "(100/128 * a - 100)", Expression_Metric = "", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 765 }; List <BluetoothCmd> commands_list = new List <BluetoothCmd>(); commands_list.Add(coolant_temp); commands_list.Add(vss); commands_list.Add(iat); commands_list.Add(map); commands_list.Add(tps); commands_list.Add(maf); commands_list.Add(rpm); commands_list.Add(mpg); commands_list.Add(fuel_system_status); commands_list.Add(fuel_pressure); //commands_list.Add(ign); // Command type insert for any missing values foreach (BluetoothCmd bthCmd in commands_list.Where(bthCmd => string.IsNullOrEmpty(bthCmd.sCommand_Types))) { bthCmd.sCommand_Types = BlueToothCmds.CommandTypesToJson(new[] { BlueToothCmds.COMMAND_TYPE.DEFAULT }); } return(commands_list); }
public Logging(BlueToothCmds blueToothCmds) { oBlueToothCmds = blueToothCmds ?? throw new Exception("Invalid Command List"); ProcessHeader = GetProcessHeader(blueToothCmds); }