fixing the library
This commit is contained in:
parent
c19c32a1ec
commit
0357980b3a
|
@ -75,14 +75,14 @@ public static class SessionMethods
|
|||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
Process process = new Process();
|
||||
var process = new Process();
|
||||
process.StartInfo.FileName = "/bin/bash";
|
||||
process.StartInfo.Arguments = $"{scriptPath} {vpnIp} {batteryNode} {version}";
|
||||
process.StartInfo.UseShellExecute = false;
|
||||
process.StartInfo.RedirectStandardOutput = true;
|
||||
|
||||
process.Start();
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
var output = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
Console.WriteLine(output);
|
||||
});
|
||||
|
|
|
@ -227,8 +227,8 @@ public static class Aggregator
|
|||
Console.WriteLine($"Max SOC: {aggregatedData.MaxSoc}");
|
||||
Console.WriteLine($"Min SOC: {aggregatedData.MinSoc}");
|
||||
|
||||
Console.WriteLine($"ChargingBatteryPower: {aggregatedData.DischargingBatteryPower}");
|
||||
Console.WriteLine($"DischargingBatteryBattery: {aggregatedData.ChargingBatteryPower}");
|
||||
Console.WriteLine($"DischargingBatteryBattery: {aggregatedData.DischargingBatteryPower}");
|
||||
Console.WriteLine($"ChargingBatteryPower: {aggregatedData.ChargingBatteryPower}");
|
||||
|
||||
Console.WriteLine($"SumGridExportPower: {aggregatedData.GridExportPower}");
|
||||
Console.WriteLine($"SumGridImportPower: {aggregatedData.GridImportPower}");
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/*using InnovEnergy.Lib.Protocols.Modbus.Channels;
|
||||
using InnovEnergy.Lib.Protocols.Modbus.Channels;
|
||||
using InnovEnergy.Lib.Protocols.Modbus.Clients;
|
||||
using InnovEnergy.Lib.Protocols.Modbus.Slaves;
|
||||
using InnovEnergy.Lib.Utils;
|
||||
|
@ -49,83 +49,4 @@ public class Iem3KGridMeterDevice: ModbusDevice<Iem3KGridMeterRegisters>
|
|||
}
|
||||
|
||||
|
||||
}*/
|
||||
|
||||
using InnovEnergy.Lib.Protocols.Modbus.Channels;
|
||||
using InnovEnergy.Lib.Protocols.Modbus.Clients;
|
||||
using InnovEnergy.Lib.Protocols.Modbus.Slaves;
|
||||
using InnovEnergy.Lib.Utils;
|
||||
using System;
|
||||
|
||||
namespace InnovEnergy.Lib.Devices.IEM3kGridMeter
|
||||
{
|
||||
public class Iem3KGridMeterDevice : ModbusDevice<Iem3KGridMeterRegisters>
|
||||
{
|
||||
private readonly string _hostname;
|
||||
private readonly ushort _port;
|
||||
private readonly byte _slaveId;
|
||||
|
||||
public Iem3KGridMeterDevice(string hostname, ushort port = 502, byte slaveId = 1)
|
||||
: this(new TcpChannel(hostname, port), slaveId)
|
||||
{
|
||||
_hostname = hostname ?? throw new ArgumentNullException(nameof(hostname));
|
||||
_port = port;
|
||||
_slaveId = slaveId;
|
||||
}
|
||||
|
||||
private Iem3KGridMeterDevice(TcpChannel channel, byte slaveId = 1)
|
||||
: base(new ModbusTcpClient(channel, slaveId))
|
||||
{
|
||||
_hostname = channel.Host;
|
||||
_port = channel.Port;
|
||||
_slaveId = slaveId;
|
||||
Console.WriteLine($"Initializing Iem3KGridMeterDevice with channel: {channel.Host}:{channel.Port}");
|
||||
}
|
||||
|
||||
public Iem3KGridMeterDevice(ModbusClient client)
|
||||
: base(client)
|
||||
{
|
||||
if (client is ModbusTcpClient tcpClient)
|
||||
{
|
||||
_hostname = tcpClient.Channel.Host;
|
||||
_port = tcpClient.Channel.Port;
|
||||
_slaveId = tcpClient.SlaveId;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Invalid client type", nameof(client));
|
||||
}
|
||||
Console.WriteLine("Initializing Iem3KGridMeterDevice with ModbusClient");
|
||||
}
|
||||
|
||||
public new Iem3KGridMeterRegisters? Read()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Attempting to read data from {_hostname}:{_port} with slaveId {_slaveId}");
|
||||
return base.Read();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to read data from {nameof(Iem3KGridMeterDevice)}: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public new void Write(Iem3KGridMeterRegisters registers)
|
||||
{
|
||||
try
|
||||
{
|
||||
base.Write(registers);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Failed to write data to {nameof(Iem3KGridMeterDevice)}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ using System.Net.Sockets;
|
|||
using InnovEnergy.Lib.Protocols.Modbus.Protocol;
|
||||
using InnovEnergy.Lib.Utils.Net;
|
||||
|
||||
/*namespace InnovEnergy.Lib.Protocols.Modbus.Channels;
|
||||
namespace InnovEnergy.Lib.Protocols.Modbus.Channels;
|
||||
|
||||
public class TcpChannel : ConnectionChannel<TcpClient>
|
||||
{
|
||||
|
@ -82,104 +82,4 @@ public class TcpChannel : ConnectionChannel<TcpClient>
|
|||
var array = data.ToArray();
|
||||
tcpClient.GetStream().Write(array, 0, array.Length);
|
||||
}
|
||||
}*/
|
||||
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace InnovEnergy.Lib.Protocols.Modbus.Channels
|
||||
{
|
||||
public class TcpChannel : Channel, IDisposable
|
||||
{
|
||||
public string Host { get; }
|
||||
public ushort Port { get; }
|
||||
|
||||
private const int TimeoutMs = 500; // TODO: parametrize
|
||||
private Socket? Socket { get; set; }
|
||||
private byte[] Buffer { get; }
|
||||
|
||||
public TcpChannel(string hostname, ushort port)
|
||||
{
|
||||
Host = hostname ?? throw new ArgumentNullException(nameof(hostname));
|
||||
Port = port;
|
||||
Buffer = new byte[8192]; // Buffer size can be adjusted
|
||||
}
|
||||
|
||||
public override IReadOnlyList<byte> Read(int nBytes)
|
||||
{
|
||||
if (Socket == null)
|
||||
throw new InvalidOperationException("Socket is not connected.");
|
||||
|
||||
var buffer = new byte[nBytes];
|
||||
int bytesRead = 0;
|
||||
|
||||
while (bytesRead < nBytes)
|
||||
{
|
||||
var read = Socket.Receive(buffer, bytesRead, nBytes - bytesRead, SocketFlags.None);
|
||||
if (read == 0)
|
||||
throw new Exception("Socket closed.");
|
||||
|
||||
bytesRead += read;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public override void Write(IReadOnlyList<byte> bytes)
|
||||
{
|
||||
if (Socket == null)
|
||||
throw new InvalidOperationException("Socket is not connected.");
|
||||
|
||||
Socket.Send(bytes.ToArray(), SocketFlags.None);
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (Socket != null)
|
||||
return;
|
||||
|
||||
Socket = new Socket(SocketType.Stream, ProtocolType.Tcp)
|
||||
{
|
||||
Blocking = true,
|
||||
NoDelay = true,
|
||||
LingerState = new LingerOption(false, 0),
|
||||
ReceiveTimeout = TimeoutMs,
|
||||
SendTimeout = TimeoutMs
|
||||
};
|
||||
|
||||
var cts = new CancellationTokenSource();
|
||||
cts.CancelAfter(TimeoutMs);
|
||||
|
||||
try
|
||||
{
|
||||
Socket.ConnectAsync(Host, Port).Wait(TimeoutMs);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Socket = null;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
if (Socket == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
Socket.Close();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Socket = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace InnovEnergy.Lib.Protocols.Modbus.Clients;
|
|||
using UInt16s = IReadOnlyCollection<UInt16>;
|
||||
using Booleans = IReadOnlyCollection<Boolean>;
|
||||
|
||||
/*public class ModbusTcpClient : ModbusClient
|
||||
public class ModbusTcpClient : ModbusClient
|
||||
{
|
||||
public const UInt16 DefaultPort = 502;
|
||||
private UInt16 _Id;
|
||||
|
@ -184,171 +184,4 @@ using Booleans = IReadOnlyCollection<Boolean>;
|
|||
return new MbData(rxFrm.RegistersRead.RawData, readAddress, Endian);
|
||||
}
|
||||
|
||||
}*/
|
||||
|
||||
public class ModbusTcpClient : ModbusClient
|
||||
{
|
||||
public const ushort DefaultPort = 502;
|
||||
private ushort _Id;
|
||||
public TcpChannel Channel { get; }
|
||||
|
||||
public ModbusTcpClient(TcpChannel channel, byte slaveId) : base(channel, slaveId)
|
||||
{
|
||||
Channel = channel;
|
||||
Channel.Connect();
|
||||
}
|
||||
|
||||
private ushort NextId() => unchecked(++_Id);
|
||||
|
||||
public override MbData ReadCoils(ushort readAddress, ushort nValues)
|
||||
{
|
||||
var id = NextId(); // TODO: check response id
|
||||
|
||||
var cmd = new ReadCoilsCommandFrame(SlaveId, readAddress, nValues);
|
||||
var hdr = new MbapHeader(id, cmd.Data.Count);
|
||||
var frm = new ModbusTcpFrame(hdr, cmd);
|
||||
|
||||
Channel.Write(frm.Data);
|
||||
|
||||
var hData = Channel.Read(MbapHeader.Size).ToArray();
|
||||
var rxHdr = new MbapHeader(hData);
|
||||
|
||||
var rxFrm = Channel
|
||||
.Read(rxHdr.FrameLength)
|
||||
.ToArray()
|
||||
.Apply(ReadCoilsResponseFrame.Parse)
|
||||
.Apply(cmd.VerifyResponse);
|
||||
|
||||
return new MbData(rxFrm.Coils.RawData, readAddress, Endian);
|
||||
}
|
||||
|
||||
public override MbData ReadDiscreteInputs(ushort readAddress, ushort nValues)
|
||||
{
|
||||
var id = NextId(); // TODO: check response id
|
||||
|
||||
var cmd = new ReadDiscreteInputsCommandFrame(SlaveId, readAddress, nValues);
|
||||
var hdr = new MbapHeader(id, cmd.Data.Count);
|
||||
var frm = new ModbusTcpFrame(hdr, cmd);
|
||||
|
||||
Channel.Write(frm.Data);
|
||||
|
||||
var hData = Channel.Read(MbapHeader.Size).ToArray();
|
||||
var rxHdr = new MbapHeader(hData);
|
||||
|
||||
var rxFrm = Channel
|
||||
.Read(rxHdr.FrameLength)
|
||||
.ToArray()
|
||||
.Apply(ReadDiscreteInputsResponseFrame.Parse)
|
||||
.Apply(cmd.VerifyResponse);
|
||||
|
||||
return new MbData(rxFrm.Inputs.RawData, readAddress, Endian);
|
||||
}
|
||||
|
||||
public override MbData ReadInputRegisters(ushort readAddress, ushort nValues)
|
||||
{
|
||||
var id = NextId(); // TODO: check response id
|
||||
|
||||
var cmd = new ReadInputRegistersCommandFrame(SlaveId, readAddress, nValues);
|
||||
var hdr = new MbapHeader(id, cmd.Data.Count);
|
||||
var frm = new ModbusTcpFrame(hdr, cmd);
|
||||
|
||||
Channel.Write(frm.Data);
|
||||
|
||||
var hData = Channel.Read(MbapHeader.Size).ToArray();
|
||||
var rxHdr = new MbapHeader(hData);
|
||||
|
||||
var rxFrm = Channel
|
||||
.Read(rxHdr.FrameLength)
|
||||
.ToArray()
|
||||
.Apply(ReadInputRegistersResponseFrame.Parse)
|
||||
.Apply(cmd.VerifyResponse);
|
||||
|
||||
return new MbData(rxFrm.RegistersRead.RawData, readAddress, Endian);
|
||||
}
|
||||
|
||||
public override MbData ReadHoldingRegisters(ushort readAddress, ushort nValues)
|
||||
{
|
||||
var id = NextId(); // TODO: check response id
|
||||
|
||||
var cmd = new ReadHoldingRegistersCommandFrame(SlaveId, readAddress, nValues);
|
||||
var hdr = new MbapHeader(id, cmd.Data.Count);
|
||||
var frm = new ModbusTcpFrame(hdr, cmd);
|
||||
|
||||
Channel.Write(frm.Data);
|
||||
|
||||
var hData = Channel.Read(MbapHeader.Size).ToArray();
|
||||
var rxHdr = new MbapHeader(hData);
|
||||
|
||||
var rxFrm = Channel
|
||||
.Read(rxHdr.FrameLength)
|
||||
.ToArray()
|
||||
.Apply(ReadHoldingRegistersResponseFrame.Parse)
|
||||
.Apply(cmd.VerifyResponse);
|
||||
|
||||
return new MbData(rxFrm.RegistersRead.RawData, readAddress, Endian);
|
||||
}
|
||||
|
||||
public override ushort WriteCoils(ushort writeAddress, Booleans coils)
|
||||
{
|
||||
var id = NextId(); // TODO: check response id
|
||||
var cmd = new WriteCoilsCommandFrame(SlaveId, writeAddress, coils);
|
||||
var hdr = new MbapHeader(id, cmd.Data.Count);
|
||||
var frm = new ModbusTcpFrame(hdr, cmd);
|
||||
|
||||
Channel.Write(frm.Data);
|
||||
|
||||
var hData = Channel.Read(MbapHeader.Size).ToArray();
|
||||
var rxHdr = new MbapHeader(hData);
|
||||
|
||||
var rxFrm = Channel
|
||||
.Read(rxHdr.FrameLength)
|
||||
.ToArray()
|
||||
.Apply(WriteCoilsResponseFrame.Parse)
|
||||
.Apply(cmd.VerifyResponse);
|
||||
|
||||
return rxFrm.NbWritten;
|
||||
}
|
||||
|
||||
public override ushort WriteRegisters(ushort writeAddress, UInt16s values)
|
||||
{
|
||||
var id = NextId(); // TODO: check response id
|
||||
var cmd = new WriteRegistersCommandFrame(SlaveId, writeAddress, values);
|
||||
var hdr = new MbapHeader(id, cmd.Data.Count);
|
||||
var frm = new ModbusTcpFrame(hdr, cmd);
|
||||
|
||||
Channel.Write(frm.Data);
|
||||
|
||||
var hData = Channel.Read(MbapHeader.Size).ToArray();
|
||||
var rxHdr = new MbapHeader(hData);
|
||||
|
||||
var rxFrm = Channel
|
||||
.Read(rxHdr.FrameLength)
|
||||
.ToArray()
|
||||
.Apply(WriteRegistersResponseFrame.Parse)
|
||||
.Apply(cmd.VerifyResponse);
|
||||
|
||||
return rxFrm.NbWritten;
|
||||
}
|
||||
|
||||
public override MbData ReadWriteRegisters(ushort readAddress, ushort nbToRead, ushort writeAddress, UInt16s registersToWrite)
|
||||
{
|
||||
var id = NextId(); // TODO: check response id
|
||||
|
||||
var cmd = new ReadWriteRegistersCommandFrame(SlaveId, readAddress, nbToRead, writeAddress, registersToWrite);
|
||||
|
||||
var hdr = new MbapHeader(id, cmd.Data.Count);
|
||||
var frm = new ModbusTcpFrame(hdr, cmd);
|
||||
|
||||
Channel.Write(frm.Data);
|
||||
|
||||
var hData = Enumerable.ToArray(Channel.Read(MbapHeader.Size));
|
||||
var rxHdr = new MbapHeader(hData);
|
||||
|
||||
var fData = Enumerable.ToArray(Channel.Read(rxHdr.FrameLength));
|
||||
var rxFrm = fData
|
||||
.Apply(ReadWriteRegistersResponseFrame.Parse)
|
||||
.Apply(cmd.VerifyResponse);
|
||||
|
||||
return new MbData(rxFrm.RegistersRead.RawData, readAddress, Endian);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue