113 lines
4.2 KiB
C#
113 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
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; } = 2;
|
|
public byte FSM_NetNode { get; set; } = 1;
|
|
public byte FSM_PNA { get; set; } = 2;
|
|
public String FSM_LocalIP { get; set; } = "192.168.10.23";
|
|
public bool FSM_UseMulticast { get; set; } = false;
|
|
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;
|
|
private String _configpath = "";
|
|
|
|
public Config() {
|
|
_configpath = GetConfigPath();
|
|
// kalau belum ada, create default
|
|
if (!File.Exists(_configpath)) Save();
|
|
}
|
|
|
|
public Boolean FileIsExist() { return File.Exists(_configpath); }
|
|
|
|
public bool Save()
|
|
{
|
|
|
|
Debug.WriteLine("Will Save Config to [" + _configpath + "]");
|
|
|
|
try
|
|
{
|
|
String jsonstring = JsonSerializer.Serialize(this);
|
|
File.WriteAllText(_configpath, jsonstring);
|
|
Debug.WriteLine("Config saved to [" + _configpath + "]");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine("Config Save to ["+ _configpath + "] Error: " + ex.Message);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool Load()
|
|
{
|
|
|
|
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;
|
|
|
|
Debug.WriteLine("Config Loaded from [" + _configpath + "]");
|
|
return true;
|
|
} else Debug.WriteLine("Config Load Error: File ["+ _configpath + "] is not valid Config.");
|
|
} catch(Exception ex)
|
|
{
|
|
Debug.WriteLine("Config Load from ["+ _configpath + "] Error: " + ex.Message);
|
|
}
|
|
}
|
|
else Debug.WriteLine("Config Load Error: File ["+ _configpath + "] not found.");
|
|
return false;
|
|
}
|
|
|
|
private static String GetConfigPath()
|
|
{
|
|
String _xx = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FAtoPA");
|
|
Debug.WriteLine("Config Path: " + _xx);
|
|
if (!Directory.Exists(_xx)) {
|
|
try
|
|
{
|
|
Directory.CreateDirectory(_xx);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine("Config Path Error: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
return System.IO.Path.Combine(_xx, "config.json");
|
|
}
|
|
}
|
|
}
|