Refactor code structure for improved readability and maintainability
This commit is contained in:
202
DaireApplication/DataBase/ConfigrationTable.cs
Normal file
202
DaireApplication/DataBase/ConfigrationTable.cs
Normal file
@@ -0,0 +1,202 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace DaireApplication.DataBase;
|
||||
|
||||
public class ConfigrationTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int Max { get; set; } = 0;
|
||||
public int Min { get; set; } = 0;
|
||||
public List<int> H_out { get; set; } = new();
|
||||
public List<int> FC_out { get; set; } = new();
|
||||
public List<int> SC_out { get; set; } = new();
|
||||
public int kp { get; set; } = 0;
|
||||
public int ki { get; set; } = 0;
|
||||
public int kd { get; set; } = 0;
|
||||
public int kl { get; set; } = 0;
|
||||
public string name { get; set; } = "";
|
||||
public float i_neut { get; set; } = 0;
|
||||
public float i_mot1 { get; set; } = 0;
|
||||
public float i_mot2 { get; set; } = 0;
|
||||
public float FC_Threshold { get; set; } = 3;
|
||||
public float HeatConRange { get; set; } = 50;
|
||||
|
||||
public List<ConfigrationTable> ReadConfigrations()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Configration.csv");
|
||||
List<ConfigrationTable> configrations = new List<ConfigrationTable>();
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
using StreamReader reader = new(filePath);
|
||||
string header = reader.ReadLine();
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
string[] columns = reader.ReadLine().Split(',');
|
||||
configrations.Add(new ConfigrationTable
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
Max = int.Parse(columns[1]),
|
||||
Min = int.Parse(columns[2]),
|
||||
H_out = columns[3].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(),
|
||||
FC_out = columns[4].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(),
|
||||
SC_out = columns[5].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(),
|
||||
kp = int.Parse(columns[6]),
|
||||
ki = int.Parse(columns[7]),
|
||||
kd = int.Parse(columns[8]),
|
||||
kl = int.Parse(columns[9]),
|
||||
name=columns[10],
|
||||
i_neut= float.Parse(columns[11]),
|
||||
i_mot1= float.Parse(columns[12]),
|
||||
i_mot2= float.Parse(columns[13]),
|
||||
FC_Threshold = float.Parse(columns[14]),
|
||||
HeatConRange = float.Parse(columns[15]),
|
||||
});
|
||||
}
|
||||
return configrations;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int GetMaxId()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Configration.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
return File.ReadAllLines(filePath)
|
||||
.Select(line => line.Split(','))
|
||||
.Where(columns => columns.Length > 0)
|
||||
.Select(columns => int.TryParse(columns[0], out int id) ? id : 0)
|
||||
.Max();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public ConfigrationTable ReadConfigrationById(string id)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Configration.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
foreach (var line in File.ReadLines(filePath))
|
||||
{
|
||||
string[] columns = line.Split(',');
|
||||
if (columns[0] == id)
|
||||
{
|
||||
return new ConfigrationTable
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
Max = int.Parse(columns[1]),
|
||||
Min = int.Parse(columns[2]),
|
||||
H_out = columns[3].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(),
|
||||
FC_out = columns[4].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(),
|
||||
SC_out = columns[5].Split('|', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList(),
|
||||
kp = int.Parse(columns[6]),
|
||||
ki = int.Parse(columns[7]),
|
||||
kd = int.Parse(columns[8]),
|
||||
kl = int.Parse(columns[9]),
|
||||
name=columns[10],
|
||||
i_neut = float.Parse(columns[11]),
|
||||
i_mot1 = float.Parse(columns[12]),
|
||||
i_mot2 = float.Parse(columns[13]),
|
||||
FC_Threshold = float.Parse(columns[14]),
|
||||
HeatConRange = float.Parse(columns[15]),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool AddConfigration(ConfigrationTable data)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Configration.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string newEntry = string.Join(",", [
|
||||
GetMaxId() + 1,
|
||||
data.Max,
|
||||
data.Min,
|
||||
string.Join("|", data.H_out),
|
||||
string.Join("|", data.FC_out),
|
||||
string.Join("|", data.SC_out),
|
||||
data.kp,
|
||||
data.ki,
|
||||
data.kd,
|
||||
data.kl,
|
||||
data.name,
|
||||
data.i_neut,
|
||||
data.i_mot1,
|
||||
data.i_mot2,
|
||||
data.FC_Threshold,
|
||||
data.HeatConRange
|
||||
]);
|
||||
File.AppendAllText(filePath, newEntry + Environment.NewLine);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool DeleteConfigration(string id)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Configration.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var filteredLines = File.ReadLines(filePath).Where(line => !line.StartsWith(id + ",")).ToArray();
|
||||
File.WriteAllLines(filePath, filteredLines);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool UpdateConfigration(ConfigrationTable updatedConfig)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Configration.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool configFound = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
if (columns.Length < 10)
|
||||
continue;
|
||||
|
||||
if (int.Parse(columns[0]) == updatedConfig.Id)
|
||||
{
|
||||
columns[1] = updatedConfig.Max.ToString();
|
||||
columns[2] = updatedConfig.Min.ToString();
|
||||
columns[3] = string.Join("|", updatedConfig.H_out);
|
||||
columns[4] = string.Join("|", updatedConfig.FC_out);
|
||||
columns[5] = string.Join("|", updatedConfig.SC_out);
|
||||
columns[6] = updatedConfig.kp.ToString();
|
||||
columns[7] = updatedConfig.ki.ToString();
|
||||
columns[8] = updatedConfig.kd.ToString();
|
||||
columns[9] = updatedConfig.kl.ToString();
|
||||
columns[10] = updatedConfig.name;
|
||||
columns[11] = updatedConfig.i_neut.ToString(CultureInfo.InvariantCulture);
|
||||
columns[12] = updatedConfig.i_mot1.ToString(CultureInfo.InvariantCulture);
|
||||
columns[13] = updatedConfig.i_mot2.ToString(CultureInfo.InvariantCulture);
|
||||
columns[14] = updatedConfig.FC_Threshold.ToString(CultureInfo.InvariantCulture);
|
||||
columns[15] = updatedConfig.HeatConRange.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
lines[i] = string.Join(",", columns);
|
||||
configFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (configFound)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
311
DaireApplication/DataBase/DataPathManager.cs
Normal file
311
DaireApplication/DataBase/DataPathManager.cs
Normal file
@@ -0,0 +1,311 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DaireApplication.DataBase
|
||||
{
|
||||
public static class DataPathManager
|
||||
{
|
||||
private static readonly string AppName = "DaireApplication";
|
||||
private static string _dataDirectory;
|
||||
|
||||
// Expected CSV headers for each file
|
||||
private static readonly string UsersCsvHeader = "ID,UserName,Password,CanEdit,IsAdmin,IsActive";
|
||||
private static readonly string RecipeCsvHeader = "ID,Name,TankTemp,FountainTemp,Mixer,Fountain,MoldHeater,Vibration,VibHeater,Pedal,PedalOnTime,PedalOffTime,HeatingGoal,CoolingGoal,PouringGoal";
|
||||
private static readonly string MachineCsvHeader = "ID,TankMaxHeat,PumbMaxHeat,PumbDelay,MixerDelay,HeatingDelay,CoolingDelay,PouringDelay,PumbMinHeat,AbsMaxTemp,AbsMinTemp,PreHeatingTemp,SetTemp1,SetTemp2,SetTemp3,SetTemp4";
|
||||
private static readonly string MappingCsvHeader = "Id,Name,Address,IsRead,BitNumbers";
|
||||
private static readonly string ConfigrationCsvHeader = "Id,Max,Min,H_out,FC_out,SC_out,kp,ki,kd,kl,Name,I_Nuet,I_Mot1,I_Mot2,FC_Threshold,HeatConRange";
|
||||
private static readonly string ErrorSettingsCsvHeader = "Id,gridFreq,phaseNumber,extPower,phaseVoltage";
|
||||
private static readonly string ScreenCsvHeader = "Id,Brightness,DimSec,OffSec,Port,BoundRate,Parity,StopBits,SendingTime,WarningLimit,ErrorLimit";
|
||||
|
||||
static DataPathManager()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Determine the data directory based on the OS
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
// Windows: Use LocalApplicationData
|
||||
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
_dataDirectory = Path.Combine(appDataPath, AppName);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Linux/macOS: Use ~/.local/share/DaireApplication
|
||||
string home = Environment.GetEnvironmentVariable("HOME") ?? "/tmp";
|
||||
_dataDirectory = Path.Combine(home, ".local", "share", AppName);
|
||||
}
|
||||
|
||||
// Ensure the directory exists
|
||||
Directory.CreateDirectory(_dataDirectory);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to CreateDirectory: {ex.Message}");
|
||||
Console.WriteLine($"_dataDirectory: {_dataDirectory}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the full path for a data file, ensuring the directory exists.
|
||||
/// </summary>
|
||||
public static string GetDataFilePath(string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Ensure the directory exists before returning the file path
|
||||
Directory.CreateDirectory(_dataDirectory);
|
||||
return Path.Combine(_dataDirectory, fileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to GetDataFilePath: {ex.Message}");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Migrates legacy data files from the application's base directory to the new data directory.
|
||||
/// </summary>
|
||||
public static void MigrateLegacyData()
|
||||
{
|
||||
try
|
||||
{
|
||||
string[] csvFiles = { "Users.csv", "Recipe.csv", "Machine.csv", "Mapping.csv",
|
||||
"Configration.csv", "ErrorSettings.csv", "Screen.csv" };
|
||||
|
||||
foreach (string file in csvFiles)
|
||||
{
|
||||
string legacyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);
|
||||
string newPath = GetDataFilePath(file);
|
||||
|
||||
// If the file exists in the old location but not in the new location, move it
|
||||
if (File.Exists(legacyPath) && !File.Exists(newPath))
|
||||
{
|
||||
// Ensure the target directory exists
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(newPath) ?? _dataDirectory);
|
||||
File.Move(legacyPath, newPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to MigrateLegacyData: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Migrates CSV files by adding missing columns with default values instead of deleting them.
|
||||
/// </summary>
|
||||
public static void MigrateCsvFiles()
|
||||
{
|
||||
MigrateCsvFile("Users.csv", UsersCsvHeader, GetUsersDefaultValues);
|
||||
MigrateCsvFile("Recipe.csv", RecipeCsvHeader, GetRecipeDefaultValues);
|
||||
MigrateCsvFile("Machine.csv", MachineCsvHeader, GetMachineDefaultValues);
|
||||
MigrateCsvFile("Mapping.csv", MappingCsvHeader, GetMappingDefaultValues);
|
||||
MigrateCsvFile("Configration.csv", ConfigrationCsvHeader, GetConfigrationDefaultValues);
|
||||
MigrateCsvFile("ErrorSettings.csv", ErrorSettingsCsvHeader, GetErrorSettingsDefaultValues);
|
||||
MigrateCsvFile("Screen.csv", ScreenCsvHeader, GetScreenDefaultValues);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Migrates a CSV file by adding missing columns with default values.
|
||||
/// </summary>
|
||||
private static void MigrateCsvFile(string fileName, string expectedHeader, Func<string, string> getDefaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
string filePath = GetDataFilePath(fileName);
|
||||
if (!File.Exists(filePath))
|
||||
return;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
if (lines.Length == 0)
|
||||
return;
|
||||
|
||||
string actualHeader = lines[0];
|
||||
if (string.Equals(actualHeader.Trim(), expectedHeader.Trim(), StringComparison.OrdinalIgnoreCase))
|
||||
return; // No migration needed
|
||||
|
||||
string[] expectedColumns = expectedHeader.Split(',');
|
||||
string[] actualColumns = actualHeader.Split(',');
|
||||
|
||||
// Find missing columns
|
||||
var missingColumns = new List<(int index, string columnName)>();
|
||||
for (int i = 0; i < expectedColumns.Length; i++)
|
||||
{
|
||||
if (i >= actualColumns.Length || !string.Equals(actualColumns[i].Trim(), expectedColumns[i].Trim(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
missingColumns.Add((i, expectedColumns[i]));
|
||||
}
|
||||
}
|
||||
|
||||
if (missingColumns.Count == 0)
|
||||
return;
|
||||
|
||||
// Migrate the file
|
||||
var migratedLines = new List<string>();
|
||||
|
||||
// Add new header
|
||||
migratedLines.Add(expectedHeader);
|
||||
|
||||
// Migrate data rows
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] dataColumns = lines[i].Split(',');
|
||||
var newDataColumns = new List<string>(dataColumns);
|
||||
|
||||
// Add missing columns with default values
|
||||
foreach (var missing in missingColumns)
|
||||
{
|
||||
if (missing.index >= newDataColumns.Count)
|
||||
{
|
||||
newDataColumns.Add(getDefaultValue(missing.columnName));
|
||||
}
|
||||
else
|
||||
{
|
||||
newDataColumns.Insert(missing.index, getDefaultValue(missing.columnName));
|
||||
}
|
||||
}
|
||||
|
||||
migratedLines.Add(string.Join(",", newDataColumns));
|
||||
}
|
||||
|
||||
// Write the migrated file
|
||||
File.WriteAllLines(filePath, migratedLines);
|
||||
Console.WriteLine($"Migrated {fileName} - added {missingColumns.Count} missing columns");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to migrate {fileName}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// Default value providers for each file type
|
||||
private static string GetUsersDefaultValues(string columnName)
|
||||
{
|
||||
return columnName switch
|
||||
{
|
||||
"IsActive" => "0",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetRecipeDefaultValues(string columnName)
|
||||
{
|
||||
return columnName switch
|
||||
{
|
||||
"TankTemp" => "0",
|
||||
"FountainTemp" => "0",
|
||||
"Mixer" => "0",
|
||||
"Fountain" => "0",
|
||||
"MoldHeater" => "0",
|
||||
"Vibration" => "0",
|
||||
"VibHeater" => "0",
|
||||
"Pedal" => "0",
|
||||
"PedalOnTime" => "0",
|
||||
"PedalOffTime" => "0",
|
||||
"HeatingGoal" => "46",
|
||||
"CoolingGoal" => "27",
|
||||
"PouringGoal" => "30",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetMachineDefaultValues(string columnName)
|
||||
{
|
||||
return columnName switch
|
||||
{
|
||||
"TankMaxHeat" => "50",
|
||||
"PumbMaxHeat" => "50",
|
||||
"PumbDelay" => "60",
|
||||
"MixerDelay" => "60",
|
||||
"HeatingDelay" => "60",
|
||||
"CoolingDelay" => "60",
|
||||
"PouringDelay" => "60",
|
||||
"PumbMinHeat" => "-10",
|
||||
"AbsMaxTemp" => "65",
|
||||
"AbsMinTemp" => "-14",
|
||||
"PreHeatingTemp" => "5",
|
||||
"SetTemp1" => "0",
|
||||
"SetTemp2" => "-10",
|
||||
"SetTemp3" => "5",
|
||||
"SetTemp4" => "0",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetMappingDefaultValues(string columnName)
|
||||
{
|
||||
return columnName switch
|
||||
{
|
||||
"BitNumbers" => "",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetConfigrationDefaultValues(string columnName)
|
||||
{
|
||||
return columnName switch
|
||||
{
|
||||
"Max" => "0",
|
||||
"Min" => "0",
|
||||
"H_out" => "",
|
||||
"FC_out" => "",
|
||||
"SC_out" => "",
|
||||
"kp" => "0",
|
||||
"ki" => "0",
|
||||
"kd" => "0",
|
||||
"kl" => "0",
|
||||
"Name" => "",
|
||||
"I_Nuet" => "0",
|
||||
"I_Mot1" => "0",
|
||||
"I_Mot2" => "0",
|
||||
"FC_Threshold" => "3",
|
||||
"HeatConRange" => "50",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetErrorSettingsDefaultValues(string columnName)
|
||||
{
|
||||
return columnName switch
|
||||
{
|
||||
"gridFreq" => "50",
|
||||
"phaseNumber" => "3",
|
||||
"extPower" => "1",
|
||||
"phaseVoltage" => "220",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetScreenDefaultValues(string columnName)
|
||||
{
|
||||
return columnName switch
|
||||
{
|
||||
"Brightness" => "100",
|
||||
"DimSec" => "3300",
|
||||
"OffSec" => "3600",
|
||||
"Port" => "",
|
||||
"BoundRate" => "19200",
|
||||
"Parity" => "1",
|
||||
"StopBits" => "0",
|
||||
"SendingTime" => "50",
|
||||
"WarningLimit" => "0.5",
|
||||
"ErrorLimit" => "2",
|
||||
_ => ""
|
||||
};
|
||||
}
|
||||
|
||||
// Legacy delete methods (kept for backward compatibility but now call migration)
|
||||
public static void DeleteUsersCsv() => MigrateCsvFile("Users.csv", UsersCsvHeader, GetUsersDefaultValues);
|
||||
public static void DeleteRecipeCsv() => MigrateCsvFile("Recipe.csv", RecipeCsvHeader, GetRecipeDefaultValues);
|
||||
public static void DeleteMachineCsv() => MigrateCsvFile("Machine.csv", MachineCsvHeader, GetMachineDefaultValues);
|
||||
public static void DeleteMappingCsv() => MigrateCsvFile("Mapping.csv", MappingCsvHeader, GetMappingDefaultValues);
|
||||
public static void DeleteConfigrationCsv() => MigrateCsvFile("Configration.csv", ConfigrationCsvHeader, GetConfigrationDefaultValues);
|
||||
public static void DeleteErrorSettingsCsv() => MigrateCsvFile("ErrorSettings.csv", ErrorSettingsCsvHeader, GetErrorSettingsDefaultValues);
|
||||
public static void DeleteScreenCsv() => MigrateCsvFile("Screen.csv", ScreenCsvHeader, GetScreenDefaultValues);
|
||||
}
|
||||
}
|
||||
81
DaireApplication/DataBase/ErrorSettingsTable.cs
Normal file
81
DaireApplication/DataBase/ErrorSettingsTable.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DaireApplication.DataBase
|
||||
{
|
||||
public class ErrorSettingsTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int gridFreq { get; set; }
|
||||
public int phaseNumber { get; set; }
|
||||
public int phaseVoltage { get; set; }
|
||||
public bool extPower { get; set; }
|
||||
|
||||
public List<ErrorSettingsTable>? ReadErrorSettings()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("ErrorSettings.csv");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return null;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
List<ErrorSettingsTable> errors = new();
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
errors.Add(new ErrorSettingsTable
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
gridFreq = int.Parse(columns[1]),
|
||||
phaseNumber = int.Parse(columns[2]),
|
||||
extPower = columns[3] == "1",
|
||||
phaseVoltage = int.Parse(columns[4]),
|
||||
});
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
public bool UpdateError(ErrorSettingsTable updatedError)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("ErrorSettings.csv");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return false;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool updated = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
if (int.Parse(columns[0]) == updatedError.Id)
|
||||
{
|
||||
columns[1] = updatedError.gridFreq.ToString();
|
||||
columns[2] = updatedError.phaseNumber.ToString();
|
||||
columns[3] = updatedError.extPower ? "1" : "0";
|
||||
columns[4] = updatedError.phaseVoltage .ToString();
|
||||
lines[i] = string.Join(",", columns);
|
||||
updated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (updated)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
124
DaireApplication/DataBase/MachineTable.cs
Normal file
124
DaireApplication/DataBase/MachineTable.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using AvaloniaApplication1.DataBase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DaireApplication.DataBase
|
||||
{
|
||||
public class MachineTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public float TankMaxHeat { get; set; } = 50;
|
||||
public float PumbMaxHeat { get; set; } = 50;
|
||||
public int PumbDelay { get; set; } = 60;
|
||||
public int MixerDelay { get; set; } = 60;
|
||||
public int HeatingDelay { get; set; } = 60;
|
||||
public int CoolingDelay { get; set; } = 60;
|
||||
public int PouringDelay { get; set; } = 60;
|
||||
public float PumbMinHeat { get; set; } = -10;
|
||||
public float AbsMaxHeat { get; set; } = 65;
|
||||
public float AbsMinHeat { get; set; } = -14;
|
||||
public float PreHeatingTemp { get; set; } = 5;
|
||||
public float setTemp1 { get; set; } =0;
|
||||
public float setTemp2 { get; set; } =-10;
|
||||
public float setTemp3 { get; set; } =+5;
|
||||
public float setTemp4 { get; set; } =+0;
|
||||
|
||||
public MachineTable ReadMachine()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Machine.csv");
|
||||
MachineTable machine = new MachineTable();
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string[] columns = line.Split(',');
|
||||
|
||||
if (columns[0] == "1")
|
||||
{
|
||||
machine.Id = int.Parse(columns[0]);
|
||||
machine.TankMaxHeat = float.Parse(columns[1]);
|
||||
machine.PumbMaxHeat = float.Parse(columns[2]);
|
||||
machine.PumbDelay = int.Parse(columns[3]);
|
||||
machine.MixerDelay = int.Parse(columns[4]);
|
||||
machine.HeatingDelay = int.Parse(columns[5]);
|
||||
machine.CoolingDelay = int.Parse(columns[6]);
|
||||
machine.PouringDelay = int.Parse(columns[7]);
|
||||
machine.PumbMinHeat = int.Parse(columns[8]);
|
||||
machine.AbsMaxHeat = int.Parse(columns[9]);
|
||||
machine.AbsMinHeat = int.Parse(columns[10]);
|
||||
machine.PreHeatingTemp = float.Parse(columns[11]);
|
||||
machine.setTemp1 = float.Parse(columns[12]);
|
||||
machine.setTemp2 = float.Parse(columns[13]);
|
||||
machine.setTemp3 = float.Parse(columns[14]);
|
||||
machine.setTemp4 = float.Parse(columns[15]);
|
||||
return machine;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool UpdateMachine(MachineTable updatedMachine)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Machine.csv");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return false;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool machineFound = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
if (columns.Length < 8)
|
||||
continue;
|
||||
|
||||
if (int.Parse(columns[0]) == updatedMachine.Id)
|
||||
{
|
||||
columns[1] = updatedMachine.TankMaxHeat.ToString(CultureInfo.InvariantCulture);
|
||||
columns[2] = updatedMachine.PumbMaxHeat.ToString(CultureInfo.InvariantCulture);
|
||||
columns[3] = updatedMachine.PumbDelay.ToString();
|
||||
columns[4] = updatedMachine.MixerDelay.ToString();
|
||||
columns[5] = updatedMachine.HeatingDelay.ToString();
|
||||
columns[6] = updatedMachine.CoolingDelay.ToString();
|
||||
columns[7] = updatedMachine.PouringDelay.ToString();
|
||||
columns[8] = updatedMachine.PumbMinHeat.ToString(CultureInfo.InvariantCulture);
|
||||
columns[9] = updatedMachine.AbsMaxHeat.ToString(CultureInfo.InvariantCulture);
|
||||
columns[10] = updatedMachine.AbsMinHeat.ToString(CultureInfo.InvariantCulture);
|
||||
columns[11] = updatedMachine.PreHeatingTemp.ToString(CultureInfo.InvariantCulture);
|
||||
columns[12] = updatedMachine.setTemp1.ToString(CultureInfo.InvariantCulture);
|
||||
columns[13] = updatedMachine.setTemp2.ToString(CultureInfo.InvariantCulture);
|
||||
columns[14] = updatedMachine.setTemp3.ToString(CultureInfo.InvariantCulture);
|
||||
columns[15] = updatedMachine.setTemp4.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
lines[i] = string.Join(",", columns);
|
||||
machineFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (machineFound)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
196
DaireApplication/DataBase/Mapping.cs
Normal file
196
DaireApplication/DataBase/Mapping.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace DaireApplication.DataBase;
|
||||
|
||||
public class Mapping
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; } = "";
|
||||
public string Address { get; set; } = "";
|
||||
public bool IsRead { get; set; } = false;
|
||||
public List<int> BitNumbers { get; set; } = new();
|
||||
|
||||
|
||||
public ushort getSetTempAddress(int tempAddress)
|
||||
{
|
||||
string tAddress = "4000";
|
||||
tempAddress -= 30004;
|
||||
tAddress += tempAddress;
|
||||
return ushort.Parse(tAddress);
|
||||
}
|
||||
public List<Mapping> ReadMappings()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Mapping.csv");
|
||||
List<Mapping> mappings = new();
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
using StreamReader reader = new(filePath);
|
||||
string header = reader.ReadLine();
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
string[] columns = reader.ReadLine().Split(',');
|
||||
mappings.Add(new Mapping
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
Name = columns[1],
|
||||
Address = columns[2],
|
||||
IsRead = columns[3] == "1"|| columns[3] == "True",
|
||||
BitNumbers =columns[4]!=""? columns[4].Split('|').Select(int.Parse).ToList():new List<int>()
|
||||
});
|
||||
}
|
||||
return mappings;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int GetMaxId()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Mapping.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
return File.ReadAllLines(filePath)
|
||||
.Select(line => line.Split(','))
|
||||
.Where(columns => columns.Length > 0)
|
||||
.Select(columns => int.TryParse(columns[0], out int id) ? id : 0)
|
||||
.Max();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Mapping ReadMappingById(string id)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Mapping.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
foreach (var line in File.ReadLines(filePath))
|
||||
{
|
||||
string[] columns = line.Split(',');
|
||||
if (columns[0] == id)
|
||||
{
|
||||
return new Mapping
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
Name = columns[1],
|
||||
Address = columns[2],
|
||||
IsRead = columns[3] == "1",
|
||||
BitNumbers = columns[4].Split('|').Select(int.Parse).ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool AddMapping(Mapping data)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Mapping.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string newEntry = string.Join(",", [
|
||||
GetMaxId() + 1,
|
||||
data.Name,
|
||||
data.Address,
|
||||
data.IsRead ? "1" : "0",
|
||||
string.Join("|", data.BitNumbers)
|
||||
]);
|
||||
File.AppendAllText(filePath, newEntry + Environment.NewLine);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool DeleteMapping(string id)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Mapping.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var filteredLines = File.ReadLines(filePath).Where(line => !line.StartsWith(id + ",")).ToArray();
|
||||
File.WriteAllLines(filePath, filteredLines);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool UpdateMapping(Mapping updatedMapping)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Mapping.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool mappingFound = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
if (columns.Length < 5)
|
||||
continue;
|
||||
|
||||
if (int.Parse(columns[0]) == updatedMapping.Id)
|
||||
{
|
||||
columns[1] = updatedMapping.Name;
|
||||
columns[2] = updatedMapping.Address;
|
||||
columns[3] = updatedMapping.IsRead ? "1" : "0";
|
||||
columns[4] = string.Join("|", updatedMapping.BitNumbers);
|
||||
lines[i] = string.Join(",", columns);
|
||||
mappingFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (mappingFound)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool DeleteBitNumber(int id, int bitNumber)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Mapping.csv");
|
||||
if (!File.Exists(filePath))
|
||||
return false;
|
||||
|
||||
List<Mapping> records = ReadMappings();
|
||||
Mapping? record = records.Find(x => x.Id == id);
|
||||
|
||||
if (record != null && record.BitNumbers.Remove(bitNumber)) // Remove bit number if found
|
||||
{
|
||||
string header;
|
||||
using (StreamReader sr = new StreamReader(filePath))
|
||||
{
|
||||
header = sr.ReadLine() ?? string.Empty;
|
||||
}
|
||||
|
||||
using (StreamWriter writer = new StreamWriter(filePath))
|
||||
{
|
||||
writer.WriteLine(header);
|
||||
string bitNumbersStr = "";
|
||||
foreach (var rec in records)
|
||||
{
|
||||
if (rec.Id==id)
|
||||
{
|
||||
bitNumbersStr = record.BitNumbers.Count > 0 ? string.Join("|", record.BitNumbers) : "";
|
||||
writer.WriteLine($"{rec.Id},{rec.Name},{rec.Address},{rec.IsRead},{bitNumbersStr}");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
bitNumbersStr = rec.BitNumbers.Count > 0 ? string.Join("|", rec.BitNumbers) : "";
|
||||
writer.WriteLine($"{rec.Id},{rec.Name},{rec.Address},{rec.IsRead},{bitNumbersStr}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
239
DaireApplication/DataBase/RecipeTable.cs
Normal file
239
DaireApplication/DataBase/RecipeTable.cs
Normal file
@@ -0,0 +1,239 @@
|
||||
using DaireApplication.DataBase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace AvaloniaApplication1.DataBase
|
||||
{
|
||||
public class RecipeTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public float TankTemp { get; set; } = 0;
|
||||
public float FountainTemp { get; set; } = 0;
|
||||
public bool? Mixer { get; set; } = false;
|
||||
public bool? Fountain { get; set; }=false;
|
||||
public bool? MoldHeater { get; set; }=false;
|
||||
public bool? Vibration { get; set; } = false;
|
||||
public bool? VibHeater { get; set; } = false;
|
||||
public bool? Pedal { get; set; } = false;
|
||||
public int PedalOnTime { get; set; } = 0;
|
||||
public int PedalOffTime { get; set; } = 0;
|
||||
public float HeatingGoal { get; set; } = 46;
|
||||
public float CoolingGoal { get; set; } = 27;
|
||||
public float PouringGoal { get; set; } = 30;
|
||||
|
||||
public RecipeTable()
|
||||
{
|
||||
Mixer = null;
|
||||
Fountain = null;
|
||||
MoldHeater = null;
|
||||
Vibration = null;
|
||||
VibHeater = null;
|
||||
Pedal = null;
|
||||
}
|
||||
|
||||
|
||||
public List<RecipeTable> ReadRecipes()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Recipe.csv");
|
||||
List<RecipeTable> recipes = new List<RecipeTable>();
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(filePath))
|
||||
{
|
||||
// Skip header
|
||||
string header = reader.ReadLine();
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
string line = reader.ReadLine();
|
||||
string[] columns = line.Split(',');
|
||||
recipes.Add(new RecipeTable
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
Name = columns[1],
|
||||
TankTemp = float.Parse(columns[2]),
|
||||
FountainTemp = float.Parse(columns[3]),
|
||||
Mixer = columns[4] == "1",
|
||||
Fountain = columns[5] == "1",
|
||||
MoldHeater = columns[6]=="1",
|
||||
Vibration = columns[7] == "1",
|
||||
VibHeater = columns[8]== "1",
|
||||
Pedal = columns[9]=="1",
|
||||
PedalOnTime = int.Parse(columns[10]),
|
||||
PedalOffTime = int.Parse(columns[11]),
|
||||
HeatingGoal = float.Parse(columns[12]),
|
||||
CoolingGoal = float.Parse(columns[13]),
|
||||
PouringGoal = float.Parse(columns[14]),
|
||||
});
|
||||
}
|
||||
}
|
||||
return recipes;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public bool DoesNameExist(string searchName)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Recipe.csv");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return false;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
|
||||
return lines
|
||||
.Select(line => line.Split(','))
|
||||
.Where(columns => columns.Length > 1)
|
||||
.Any(columns => string.Equals(columns[1].Trim(), searchName, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
public int GetMaxId()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Recipe.csv");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
int maxId = lines
|
||||
.Select(line => line.Split(','))
|
||||
.Where(columns => columns.Length > 0)
|
||||
.Select(columns => int.TryParse(columns[0], out int id) ? id : 0)
|
||||
.Max();
|
||||
return maxId;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
public RecipeTable ReadRecipesById(string id)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Recipe.csv");
|
||||
RecipeTable recipe = new RecipeTable();
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string[] columns = line.Split(',');
|
||||
|
||||
if (columns[0] == id)
|
||||
{
|
||||
recipe.Id = int.Parse(columns[0]);
|
||||
recipe.Name = columns[1];
|
||||
recipe.TankTemp = float.Parse(columns[2]);
|
||||
recipe.FountainTemp = float.Parse(columns[3]);
|
||||
recipe.Mixer = columns[4] == "1";
|
||||
recipe.Fountain = columns[5] == "1";
|
||||
recipe.MoldHeater = columns[6] == "1";
|
||||
recipe.Vibration = columns[7] == "1";
|
||||
recipe.VibHeater = columns[8] == "1";
|
||||
recipe.Pedal = columns[9] == "1";
|
||||
recipe.PedalOnTime = int.Parse(columns[10]);
|
||||
recipe.PedalOffTime = int.Parse(columns[11]);
|
||||
recipe.HeatingGoal = float.Parse(columns[12]);
|
||||
recipe.CoolingGoal = float.Parse(columns[13]);
|
||||
recipe.PouringGoal = float.Parse(columns[14]);
|
||||
return recipe;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public bool AddRecipe(RecipeTable data)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Recipe.csv");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string newEntry = string.Join(",", [GetMaxId() +1, data.Name,data.TankTemp, data.FountainTemp, data.Mixer.Value?"1":"0", data.Fountain.Value ? "1" : "0", data.MoldHeater.Value ? "1" : "0", data.Vibration.Value ? "1" : "0", data.VibHeater.Value ? "1" : "0", data.Pedal.Value ? "1":"0",data.PedalOnTime,data.PedalOffTime, data.HeatingGoal,data.CoolingGoal,data.PouringGoal]);
|
||||
|
||||
File.AppendAllText(filePath, newEntry + Environment.NewLine);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool DeleteRecipe(string id)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Recipe.csv");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
var filteredLines = lines.Where(line => !line.StartsWith(id + ",")).ToArray();
|
||||
|
||||
File.WriteAllLines(filePath, filteredLines);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool UpdateRecipe(RecipeTable updatedRecipe)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Recipe.csv");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return false;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool recipeFound = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
if (columns.Length < 15)
|
||||
continue;
|
||||
|
||||
if (int.TryParse(columns[0], out int id) && id == updatedRecipe.Id)
|
||||
{
|
||||
columns[1] = updatedRecipe.Name ?? "";
|
||||
columns[2] = updatedRecipe.TankTemp.ToString(CultureInfo.InvariantCulture);
|
||||
columns[3] = updatedRecipe.FountainTemp.ToString(CultureInfo.InvariantCulture);
|
||||
columns[4] = (updatedRecipe.Mixer ?? false) ? "1" : "0";
|
||||
columns[5] = (updatedRecipe.Fountain ?? false) ? "1" : "0";
|
||||
columns[6] = (updatedRecipe.MoldHeater ?? false) ? "1" : "0";
|
||||
columns[7] = (updatedRecipe.Vibration ?? false) ? "1" : "0";
|
||||
columns[8] = (updatedRecipe.VibHeater ?? false) ? "1" : "0";
|
||||
columns[9] = (updatedRecipe.Pedal ?? false) ? "1" : "0";
|
||||
columns[10] = updatedRecipe.PedalOnTime.ToString();
|
||||
columns[11] = updatedRecipe.PedalOffTime.ToString();
|
||||
columns[12] = updatedRecipe.HeatingGoal.ToString(CultureInfo.InvariantCulture);
|
||||
columns[13] = updatedRecipe.CoolingGoal.ToString(CultureInfo.InvariantCulture);
|
||||
columns[14] = updatedRecipe.PouringGoal.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
lines[i] = string.Join(",", columns);
|
||||
recipeFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (recipeFound)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
112
DaireApplication/DataBase/ScreeenTable.cs
Normal file
112
DaireApplication/DataBase/ScreeenTable.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DaireApplication.DataBase
|
||||
{
|
||||
public class ScreeenTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int brightness { get; set; }
|
||||
public float dimSec { get; set; }
|
||||
public float offSec { get; set; }
|
||||
public string port { get; set; }
|
||||
public int boundRate { get; set; }
|
||||
public int stopBits { get; set; }
|
||||
public int parity { get; set; }
|
||||
public int sendingTime { get; set; }
|
||||
public double warningLimit { get; set; }
|
||||
public double errorLimit { get; set; }
|
||||
|
||||
public List<ScreeenTable>? ReadScreens()
|
||||
{
|
||||
try
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Screen.csv");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return null;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
List<ScreeenTable> screens = new();
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
if (columns.Length < 8)
|
||||
continue;
|
||||
|
||||
screens.Add(new ScreeenTable
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
brightness = int.Parse(columns[1]),
|
||||
dimSec = float.Parse(columns[2]),
|
||||
offSec = float.Parse(columns[3]),
|
||||
port = columns[4],
|
||||
boundRate = int.Parse(columns[5]),
|
||||
stopBits = int.Parse(columns[6]),
|
||||
parity = int.Parse(columns[7]),
|
||||
sendingTime = int.Parse(columns[8]),
|
||||
warningLimit = double.Parse(columns[9]),
|
||||
errorLimit = double.Parse(columns[10]),
|
||||
});
|
||||
}
|
||||
|
||||
return screens;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool UpdateScreen(ScreeenTable updatedScreen)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Screen.csv");
|
||||
|
||||
if (!File.Exists(filePath))
|
||||
return false;
|
||||
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool updated = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
|
||||
if (columns.Length < 8)
|
||||
continue;
|
||||
|
||||
if (int.Parse(columns[0]) == updatedScreen.Id)
|
||||
{
|
||||
columns[1] = updatedScreen.brightness.ToString();
|
||||
columns[2] = updatedScreen.dimSec.ToString();
|
||||
columns[3] = updatedScreen.offSec.ToString();
|
||||
columns[4] = updatedScreen.port;
|
||||
columns[5] = updatedScreen.boundRate.ToString();
|
||||
columns[6] = updatedScreen.stopBits.ToString();
|
||||
columns[7] = updatedScreen.parity.ToString();
|
||||
columns[8] = updatedScreen.sendingTime.ToString();
|
||||
columns[9] = updatedScreen.warningLimit.ToString();
|
||||
columns[10] = updatedScreen.errorLimit.ToString();
|
||||
|
||||
lines[i] = string.Join(",", columns);
|
||||
updated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (updated)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
83
DaireApplication/DataBase/UserTable.cs
Normal file
83
DaireApplication/DataBase/UserTable.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using DaireApplication.DataBase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AvaloniaApplication1.DataBase
|
||||
{
|
||||
public class UserTable
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string Password { get; set; }
|
||||
public bool CanEdit { get; set; }
|
||||
public bool IsAdmin { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
|
||||
public List<UserTable> ReadUsers()
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Users.csv");
|
||||
List<UserTable> users = new List<UserTable>();
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
|
||||
for (int i = 1; i < lines.Length; i++) // Skip header
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
users.Add(new UserTable
|
||||
{
|
||||
Id = int.Parse(columns[0]),
|
||||
UserName = columns[1],
|
||||
Password = columns[2],
|
||||
CanEdit = columns[3] =="1" ?true:false,
|
||||
IsAdmin=columns[4] =="1" ?true: false,
|
||||
IsActive=columns[5] =="1" ?true: false,
|
||||
});
|
||||
}
|
||||
return users;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool UpdateUser(UserTable updatedUser)
|
||||
{
|
||||
string filePath = DataPathManager.GetDataFilePath("Users.csv");
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(filePath);
|
||||
bool userFound = false;
|
||||
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
string[] columns = lines[i].Split(',');
|
||||
if (int.Parse(columns[0]) == updatedUser.Id)
|
||||
{
|
||||
columns[1] = updatedUser.UserName;
|
||||
columns[2] = updatedUser.Password;
|
||||
columns[3] = updatedUser.CanEdit ? "1" : "0";
|
||||
columns[4] = updatedUser.IsAdmin ? "1" : "0";
|
||||
columns[5] = updatedUser.IsActive ? "1" : "0";
|
||||
lines[i] = string.Join(",", columns);
|
||||
userFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (userFound)
|
||||
{
|
||||
File.WriteAllLines(filePath, lines);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user