-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add options for automatic startup of instance on startup of the…
… manager
- Loading branch information
Jasper De Keukelaere (imec)
authored and
Jasper De Keukelaere (imec)
committed
Aug 2, 2024
1 parent
292f975
commit 741a3a2
Showing
7 changed files
with
243 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
PLCsimAdvanced_Manager/Services/Persistence/PersistenceHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Siemens.Simatic.Simulation.Runtime; | ||
|
||
namespace PLCsimAdvanced_Manager.Services.Persistence; | ||
|
||
public class PersistenceHandler | ||
{ | ||
private Persistence _persistence = new Persistence(); | ||
private PersistenceSettings _settings = new PersistenceSettings(); | ||
private string _filePath; | ||
|
||
public PersistenceHandler() | ||
{ | ||
} | ||
|
||
private void setStuffRight(string directory) | ||
{ | ||
var managerDirectory = Path.Combine(directory, "manager"); | ||
if (!Directory.Exists(managerDirectory)) | ||
{ | ||
Directory.CreateDirectory(managerDirectory); | ||
} | ||
|
||
_filePath = Path.Combine(managerDirectory, "persistence.json"); | ||
if (!File.Exists(_filePath)) | ||
{ | ||
_settings = new PersistenceSettings(); | ||
// create persistence.json file | ||
_persistence = new Persistence | ||
{ | ||
PersistenceSettings = _settings | ||
}; | ||
var jsonString = JsonSerializer.Serialize(_persistence, new JsonSerializerOptions { WriteIndented = true }); | ||
File.WriteAllText(_filePath, jsonString); | ||
} | ||
} | ||
|
||
public void UpdateSettings(string directory, bool registerOnStartup , bool powerOnOnStartup) | ||
{ | ||
setStuffRight(directory); | ||
_settings.RegisterOnStartup = registerOnStartup; | ||
_settings.PowerOnOnStartup = powerOnOnStartup; | ||
SaveSettings(); | ||
} | ||
|
||
public PersistenceSettings? ReadSettings(string directory) | ||
{ | ||
setStuffRight(directory); | ||
|
||
PersistenceSettings? settings = new PersistenceSettings(); | ||
if (File.Exists(_filePath)) | ||
{ | ||
var settingContent = File.ReadAllText(_filePath); | ||
var persistence = JsonSerializer.Deserialize<Persistence>(settingContent); | ||
settings = persistence?.PersistenceSettings; | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
private void SaveSettings() | ||
{ | ||
_persistence.PersistenceSettings = _settings; | ||
var jsonString = JsonSerializer.Serialize(_persistence, new JsonSerializerOptions { WriteIndented = true }); | ||
File.WriteAllText(_filePath, jsonString); | ||
} | ||
} | ||
|
||
public class PersistenceSettings | ||
{ | ||
[JsonPropertyName("RegisterOnStartup")] | ||
public bool RegisterOnStartup { get; set; } = false; | ||
|
||
[JsonPropertyName("PowerOnOnStartup")] public bool PowerOnOnStartup { get; set; } = false; | ||
} | ||
|
||
public class Persistence | ||
{ | ||
[JsonPropertyName("Settings")] public PersistenceSettings PersistenceSettings { get; set; } = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Siemens.Simatic.Simulation.Runtime; | ||
|
||
namespace PLCsimAdvanced_Manager.Shared; | ||
|
||
public static class StartupTasks | ||
{ | ||
public static void GetPersistantSettings() | ||
{ | ||
var directories = Directory.GetDirectories(@SimulationRuntimeManager.DefaultStoragePath); | ||
foreach (var directory in directories) | ||
{ | ||
var managerDirectory = Path.Combine(directory, "manager"); | ||
if (Directory.Exists(managerDirectory)) | ||
{ | ||
var persistentSettingsFile = Path.Combine(managerDirectory, "persistence.json"); | ||
if (File.Exists(persistentSettingsFile)) | ||
{ | ||
var settingContent = File.ReadAllText(persistentSettingsFile); | ||
var persistence = JsonSerializer.Deserialize<Persistence>(settingContent); | ||
var settings = persistence?.PersistenceSettings; | ||
|
||
// Use the autoStartup boolean value in your logic | ||
if (settings?.RegisterOnStartup == true) | ||
{ | ||
var instanceName = Path.GetFileName(directory); | ||
try | ||
{ | ||
var instance = SimulationRuntimeManager.RegisterInstance(instanceName); | ||
|
||
if (settings.PowerOnOnStartup) | ||
{ | ||
instance.PowerOn(); | ||
|
||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"There was an issue with registering the instance [{instanceName}]: {e.Message}"); | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
|
||
public class PersistenceSettings | ||
{ | ||
[JsonPropertyName("RegisterOnStartup")] public bool RegisterOnStartup { get; set; } = false; | ||
[JsonPropertyName("PowerOnOnStartup")] public bool PowerOnOnStartup { get; set; } = false; | ||
} | ||
|
||
public class Persistence | ||
{ | ||
[JsonPropertyName("Settings")] public PersistenceSettings PersistenceSettings { get; set; } = null; | ||
Check warning on line 61 in PLCsimAdvanced_Manager/Shared/StartupTasks.cs GitHub Actions / release
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters