Fixes in Scales
This commit is contained in:
@ -74,14 +74,15 @@ namespace Elwig {
|
||||
foreach (var s in Config.Scales) {
|
||||
try {
|
||||
var id = s[0];
|
||||
var type = s[1].ToLower();
|
||||
var type = s[1]?.ToLower();
|
||||
var model = s[2];
|
||||
var cnx = s[3];
|
||||
var empty = s[4];
|
||||
var filling = s[5];
|
||||
int? limit = s[6] == null ? null : int.Parse(s[6]);
|
||||
var log = s[7];
|
||||
if (type == "systec") {
|
||||
list.Add(new SystecScale(id, model, cnx, empty, filling, limit));
|
||||
list.Add(new SystecScale(id, model, cnx, empty, filling, limit, log));
|
||||
} else {
|
||||
throw new ArgumentException($"Invalid scale type: \"{type}\"");
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ namespace Elwig.Helpers {
|
||||
public string DatabaseFile = App.DataPath + "database.sqlite3";
|
||||
public string? DatabaseLog = null;
|
||||
public string? Branch = null;
|
||||
public IEnumerable<string[]> Scales;
|
||||
private readonly LinkedList<string[]> ScaleList = new();
|
||||
public IList<string?[]> Scales;
|
||||
private readonly List<string?[]> ScaleList = new();
|
||||
|
||||
public Config(string filename) {
|
||||
FileName = filename;
|
||||
@ -54,9 +54,16 @@ namespace Elwig.Helpers {
|
||||
Scales = ScaleList;
|
||||
if (ini != null) {
|
||||
foreach (var s in ini.Sections.Where(s => s.SectionName.StartsWith("scale."))) {
|
||||
ScaleList.AddLast(new string[] {
|
||||
string? scaleLog = null;
|
||||
if (s.Keys["log"] != null) {
|
||||
scaleLog = s.Keys["log"];
|
||||
if (scaleLog.Length <= 1 || (scaleLog[1] != ':' && scaleLog[0] != '/' && scaleLog[0] != '\\')) {
|
||||
scaleLog = App.DataPath + scaleLog;
|
||||
}
|
||||
}
|
||||
ScaleList.Add(new string?[] {
|
||||
s.SectionName[6..], s.Keys["type"], s.Keys["model"], s.Keys["connection"],
|
||||
s.Keys["empty"], s.Keys["filling"], s.Keys["limit"]
|
||||
s.Keys["empty"], s.Keys["filling"], s.Keys["limit"], scaleLog
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -64,6 +71,8 @@ namespace Elwig.Helpers {
|
||||
|
||||
public void Write() {
|
||||
using var file = new StreamWriter(FileName, false, Encoding.UTF8);
|
||||
file.Write($"\r\n[general]\r\n");
|
||||
if (Branch != null) file.Write($"branch = {Branch}\r\n");
|
||||
file.Write($"\r\n[database]\r\nfile = {DatabaseFile}\r\n");
|
||||
if (DatabaseLog != null) file.Write($"log = {DatabaseLog}\r\n");
|
||||
foreach (var s in ScaleList) {
|
||||
@ -71,6 +80,7 @@ namespace Elwig.Helpers {
|
||||
if (s[4] != null) file.Write($"empty = {s[4]}\r\n");
|
||||
if (s[5] != null) file.Write($"filling = {s[5]}\r\n");
|
||||
if (s[6] != null) file.Write($"limit = {s[6]}\r\n");
|
||||
if (s[7] != null) file.Write($"log = {s[7]}\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
public bool IsReady { get; private set; }
|
||||
public bool HasFillingClearance { get; private set; }
|
||||
public int? WeightLimit { get; private set; }
|
||||
public string? LogPath { get; private set; }
|
||||
|
||||
public GassnerScale(string id, string model, string connection) {
|
||||
ScaleId = id;
|
||||
|
@ -41,6 +41,11 @@ namespace Elwig.Helpers.Weighing {
|
||||
/// </summary>
|
||||
int? WeightLimit { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Where to log the requests and responses from the scale to
|
||||
/// </summary>
|
||||
string? LogPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Get the current weight on the scale without performing a weighing process
|
||||
/// </summary>
|
||||
|
@ -12,6 +12,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
public bool IsReady => throw new NotImplementedException();
|
||||
public bool HasFillingClearance => throw new NotImplementedException();
|
||||
public int? WeightLimit => throw new NotImplementedException();
|
||||
public string? LogPath => throw new NotImplementedException();
|
||||
|
||||
public void Dispose() {
|
||||
throw new NotImplementedException();
|
||||
|
@ -4,6 +4,7 @@ using System.IO.Ports;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Elwig.Helpers.Weighing {
|
||||
public class SystecScale : IScale {
|
||||
@ -25,12 +26,14 @@ namespace Elwig.Helpers.Weighing {
|
||||
public bool IsReady { get; private set; }
|
||||
public bool HasFillingClearance { get; private set; }
|
||||
public int? WeightLimit { get; private set; }
|
||||
public string? LogPath { get; private set; }
|
||||
|
||||
public SystecScale(string id, string model, string connection, string? empty = null, string? fill = null, int? limit = null) {
|
||||
public SystecScale(string id, string model, string connection, string? empty = null, string? fill = null, int? limit = null, string? log = null) {
|
||||
ScaleId = id;
|
||||
Model = model;
|
||||
IsReady = true;
|
||||
HasFillingClearance = false;
|
||||
LogPath = log;
|
||||
|
||||
if (connection.StartsWith("serial:")) {
|
||||
Serial = Utils.OpenSerialConnection(connection);
|
||||
@ -61,29 +64,27 @@ namespace Elwig.Helpers.Weighing {
|
||||
}
|
||||
|
||||
protected static Output? ConvertOutput(string? value) {
|
||||
if (value == null) return null;
|
||||
value = value.ToUpper();
|
||||
if (value == "RTS") {
|
||||
return Output.RTS;
|
||||
} else if (value == "DTR") {
|
||||
return Output.DTR;
|
||||
} else if (value == "OUT1") {
|
||||
return Output.OUT1;
|
||||
} else if (value == "OUT2") {
|
||||
return Output.OUT2;
|
||||
}
|
||||
throw new ArgumentException("Invalid value for argument empty");
|
||||
return value switch {
|
||||
null => null,
|
||||
"RTS" => Output.RTS,
|
||||
"DTR" => Output.DTR,
|
||||
"OUT1" => Output.OUT1,
|
||||
"OUT2" => Output.OUT2,
|
||||
_ => throw new ArgumentException($"Invalid value for argument: '{value}'"),
|
||||
};
|
||||
}
|
||||
|
||||
protected async Task SendCommand(string command) {
|
||||
byte[] bytes = Encoding.ASCII.GetBytes($"<{command}>");
|
||||
await Stream.WriteAsync(bytes);
|
||||
if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"<{command}>");
|
||||
}
|
||||
|
||||
protected async Task<string> ReceiveResponse() {
|
||||
string? line = null;
|
||||
using (var reader = new StreamReader(Stream, Encoding.ASCII, false, -1, true)) {
|
||||
line = await reader.ReadLineAsync();
|
||||
if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"{line}\r\n");
|
||||
}
|
||||
if (line == null || line.Length < 4 || !line.StartsWith("<") || !line.EndsWith(">")) {
|
||||
throw new IOException("Invalid response from scale");
|
||||
|
@ -15,6 +15,7 @@ file = database.sqlite3
|
||||
;connection = serial://COM1:9600,8,N,1
|
||||
;empty = RTS:1000
|
||||
;limit = 4000
|
||||
;log = waage.log
|
||||
|
||||
;[scale.B]
|
||||
;type = Type
|
||||
|
Reference in New Issue
Block a user