Add project files.
This commit is contained in:
111
Config.cs
Normal file
111
Config.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FAtoPA
|
||||
{
|
||||
class Config
|
||||
{
|
||||
// FSM settings
|
||||
public byte FSM_NetGroup { get; set; } = 1;
|
||||
public byte FSM_NetNode { get; set; } = 1;
|
||||
public byte FSM_PNA { get; set; } = 1;
|
||||
public String FSM_LocalIP { get; set; } = "0.0.0.0";
|
||||
public bool FSM_UseMulticast { get; set; } = true;
|
||||
public String FSM_MulticastIP { get; set; } = "239.192.0.1";
|
||||
public UInt16 FSM_MulticastPort { get; set; } = 25000;
|
||||
|
||||
public UInt16 Modbus_ListenPort { get; set; } = 502;
|
||||
public byte Modbus_DeviceID { get; set; } = 1;
|
||||
public UInt16 Modbus_MaxRegister { get; set; } = 2000;
|
||||
|
||||
public String VX_TargetIP { get; set; } = "192.168.14.1";
|
||||
public UInt16 VX_TargetPort { get; set; } = 5000;
|
||||
|
||||
public Config() {
|
||||
String _configpath = GetConfigPath();
|
||||
if (File.Exists(_configpath)) {
|
||||
// Load Config
|
||||
if (Load())
|
||||
{
|
||||
// do nothing
|
||||
|
||||
} else
|
||||
{
|
||||
// save default config
|
||||
Save();
|
||||
}
|
||||
} else
|
||||
{
|
||||
// save default config
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Save()
|
||||
{
|
||||
String _configPath = GetConfigPath();
|
||||
|
||||
try
|
||||
{
|
||||
String jsonstring = JsonSerializer.Serialize(this);
|
||||
File.WriteAllText(_configPath, jsonstring);
|
||||
Console.WriteLine("Config saved to [" + _configPath+"]");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Out.WriteLine("Config Save to ["+_configPath+"] Error: " + ex.Message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Load()
|
||||
{
|
||||
String _configPath = GetConfigPath();
|
||||
|
||||
if (File.Exists(_configPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
string jsonstring = File.ReadAllText(_configPath);
|
||||
Config? _loaded = JsonSerializer.Deserialize<Config>(jsonstring);
|
||||
if (_loaded != null)
|
||||
{
|
||||
this.FSM_NetGroup = _loaded.FSM_NetGroup;
|
||||
this.FSM_NetNode = _loaded.FSM_NetNode;
|
||||
this.FSM_PNA = _loaded.FSM_PNA;
|
||||
this.FSM_LocalIP = _loaded.FSM_LocalIP;
|
||||
this.FSM_UseMulticast = _loaded.FSM_UseMulticast;
|
||||
this.FSM_MulticastIP = _loaded.FSM_MulticastIP;
|
||||
this.FSM_MulticastPort = _loaded.FSM_MulticastPort;
|
||||
this.Modbus_ListenPort = _loaded.Modbus_ListenPort;
|
||||
this.Modbus_DeviceID = _loaded.Modbus_DeviceID;
|
||||
this.Modbus_MaxRegister = _loaded.Modbus_MaxRegister;
|
||||
this.VX_TargetIP = _loaded.VX_TargetIP;
|
||||
this.VX_TargetPort = _loaded.VX_TargetPort;
|
||||
|
||||
Console.Out.WriteLine("Config Loaded from [" + _configPath + "]");
|
||||
return true;
|
||||
} else Console.Out.WriteLine("Config Load Error: File ["+ _configPath + "] is not valid Config.");
|
||||
} catch(Exception ex)
|
||||
{
|
||||
Console.Out.WriteLine("Config Load from ["+_configPath+"] Error: " + ex.Message);
|
||||
}
|
||||
}
|
||||
else Console.Out.WriteLine("Config Load Error: File ["+ _configPath + "] not found.");
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String GetConfigPath()
|
||||
{
|
||||
String _configPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FAtoPA");
|
||||
_configPath = System.IO.Path.Combine(_configPath, "config.json");
|
||||
return _configPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user