diff --git a/SoftPlc/Interfaces/IPlcService.cs b/SoftPlc/Interfaces/IPlcService.cs index a731dd2..c6a44fc 100644 --- a/SoftPlc/Interfaces/IPlcService.cs +++ b/SoftPlc/Interfaces/IPlcService.cs @@ -7,7 +7,7 @@ public interface IPlcService { IEnumerable GetDatablocksInfo(); DatablockDescription GetDatablock(int id); - void AddDatablock(int id, int size, byte[] data); + void AddDatablock(int id, DatablockDescription datablock); void AddDatablock(int id, int size); void UpdateDatablockData(int id, byte[] data); void RemoveDatablock(int id); diff --git a/SoftPlc/Models/DatablockDescription.cs b/SoftPlc/Models/DatablockDescription.cs index 1599a0c..207d808 100644 --- a/SoftPlc/Models/DatablockDescription.cs +++ b/SoftPlc/Models/DatablockDescription.cs @@ -1,4 +1,7 @@ -namespace SoftPlc.Models +using System; +using Newtonsoft.Json; + +namespace SoftPlc.Models { public class DatablockDescription { @@ -12,5 +15,13 @@ public DatablockDescription(int id, int size) Size = size; Data = new byte[Size]; } + + [JsonConstructor]public DatablockDescription(int id, int size, byte[] data) + { + Id = id; + Size = size; + Data = new byte[Size]; + Array.Copy(data, Data, data.Length); + } } } \ No newline at end of file diff --git a/SoftPlc/Services/PlcService.cs b/SoftPlc/Services/PlcService.cs index 14b5324..8f41c1a 100644 --- a/SoftPlc/Services/PlcService.cs +++ b/SoftPlc/Services/PlcService.cs @@ -4,7 +4,8 @@ using SoftPlc.Interfaces; using SoftPlc.Models; using Microsoft.Extensions.Configuration; - +using System.IO; +using Newtonsoft.Json; namespace SoftPlc.Services { @@ -34,6 +35,7 @@ public PlcService(IConfiguration configuration) serverRunning = error == 0; if (serverRunning) Console.WriteLine($"plc server started on port {usedPlcPort}!"); else Console.WriteLine($"plc server error {error}"); + //ReadDataBlocks(); } private void CheckServerRunning() @@ -41,10 +43,52 @@ private void CheckServerRunning() if(!serverRunning) throw new Exception("Plc server is not running"); } + private void SaveDataBlocks() + { + var settingsFile = Path.Combine(GetSaveLocation(), "datablocks.json"); + var json = JsonConvert.SerializeObject(datablocks, Formatting.Indented); + File.WriteAllText(settingsFile, json); + } + + private void ReadDataBlocks() + { + var settingsFile = Path.Combine(GetSaveLocation(), "datablocks.json"); + + try + { + if(File.Exists(settingsFile)) + { + var json = File.ReadAllText(Path.Combine(GetSaveLocation(),settingsFile)); + var retrievedDatablock = JsonConvert.DeserializeObject>(json); + foreach(var item in retrievedDatablock) + AddDatablock(item.Key, item.Value); + } + } + catch(Exception e) + { + Console.WriteLine($"Error while deserializing data blocks {e.Message}"); + } + + } + + private string GetSaveLocation() + { + try + { + return Environment.GetEnvironmentVariable("DATA_PATH"); + } + catch(Exception e) + { + Console.WriteLine($"Error during retrieving env variable DATA_PATH {e.Message}"); + return string.Empty; + } + } + private void ReleaseUnmanagedResources() { Console.WriteLine("Stopping plc server..."); server.Stop(); + //SaveDataBlocks(); } public void Dispose() @@ -73,10 +117,10 @@ public DatablockDescription GetDatablock(int id) throw new InvalidOperationException("Datablock not found"); } - public void AddDatablock(int id, int size, byte[] data) + public void AddDatablock(int id, DatablockDescription datablock) { - AddDatablock(id, size); - UpdateDatablockData(id, data); + AddDatablock(id, datablock.Size); + UpdateDatablockData(id, datablock.Data); } public void AddDatablock(int id, int size) diff --git a/SoftPlc/Services/PlcServiceMock.cs b/SoftPlc/Services/PlcServiceMock.cs index 22f1c9c..d7cf6f8 100644 --- a/SoftPlc/Services/PlcServiceMock.cs +++ b/SoftPlc/Services/PlcServiceMock.cs @@ -35,5 +35,10 @@ public void RemoveDatablock(int id) { throw new System.NotImplementedException(); } - } + + public void AddDatablock(int id, DatablockDescription datablock) + { + throw new System.NotImplementedException(); + } + } } \ No newline at end of file diff --git a/SoftPlc/SoftPlc.csproj b/SoftPlc/SoftPlc.csproj index af7581e..6327433 100644 --- a/SoftPlc/SoftPlc.csproj +++ b/SoftPlc/SoftPlc.csproj @@ -41,6 +41,7 @@ +