Skip to content

Commit

Permalink
implemented datablock serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed Nov 28, 2018
1 parent 71502ac commit 7949cfb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion SoftPlc/Interfaces/IPlcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IPlcService
{
IEnumerable<DatablockDescription> 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);
Expand Down
13 changes: 12 additions & 1 deletion SoftPlc/Models/DatablockDescription.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace SoftPlc.Models
using System;
using Newtonsoft.Json;

namespace SoftPlc.Models
{
public class DatablockDescription
{
Expand All @@ -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);
}
}
}
52 changes: 48 additions & 4 deletions SoftPlc/Services/PlcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using SoftPlc.Interfaces;
using SoftPlc.Models;
using Microsoft.Extensions.Configuration;

using System.IO;
using Newtonsoft.Json;

namespace SoftPlc.Services
{
Expand Down Expand Up @@ -34,17 +35,60 @@ 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()
{
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<Dictionary<int, DatablockDescription>>(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()
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion SoftPlc/Services/PlcServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ public void RemoveDatablock(int id)
{
throw new System.NotImplementedException();
}
}

public void AddDatablock(int id, DatablockDescription datablock)
{
throw new System.NotImplementedException();
}
}
}
1 change: 1 addition & 0 deletions SoftPlc/SoftPlc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 7949cfb

Please sign in to comment.