using BPASmartClient.SCADAControl;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.IO;
namespace BeDesignerSCADA.ViewModel
{
public class DataBusModel
{
#region 单例模式
private static string path
{
get
{
Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile\\JSON"));
return AppDomain.CurrentDomain.BaseDirectory + "AccessFile\\JSON\\CommunicationPar.json";
}
}
public static DataBusModel dataBus = null;
public static DataBusModel GetInstance()
{
if (dataBus == null)
{
dataBus = new DataBusModel();
}
return dataBus;
}
#endregion
#region 变量
///
/// 变量地址保存
/// key:地址
/// value:变量
///
private ConcurrentDictionary _KeyValues { get; set; } = new ConcurrentDictionary();
public ConcurrentDictionary KeyValues
{
get
{
return _KeyValues;
}
set
{
_KeyValues = value;
}
}
#endregion
///
/// 刷新界面管理器
///
///
public bool RefreshVariableManager(string path="")
{
bool IsSucess=false;
try
{
if (File.Exists(path))
{
CommunicationPar val = JsonConvert.DeserializeObject(File.ReadAllText(path));
if (val != null)
{
if (KeyValues == null)
{
KeyValues = new ConcurrentDictionary();
}
KeyValues[path] = val;
IsSucess = true;
}
}
}
catch (Exception ex)
{
}
return IsSucess;
}
///
/// 读取文件
///
///
public bool ReadFile()
{
bool IsSucess = false;
try
{
if (File.Exists(path))
{
CommunicationPar val = JsonConvert.DeserializeObject(File.ReadAllText(path));
if (val != null)
{
if (KeyValues == null)
{
KeyValues = new ConcurrentDictionary();
}
KeyValues[path] = val;
IsSucess = true;
}
}
}
catch (Exception ex)
{
}
return IsSucess ;
}
}
}