|
- 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 变量
- /// <summary>
- /// 变量地址保存
- /// key:地址
- /// value:变量
- /// </summary>
- private ConcurrentDictionary<string, CommunicationPar> _KeyValues { get; set; } = new ConcurrentDictionary<string, CommunicationPar>();
- public ConcurrentDictionary<string, CommunicationPar> KeyValues
- {
- get
- {
- return _KeyValues;
- }
- set
- {
- _KeyValues = value;
-
- }
- }
- #endregion
- /// <summary>
- /// 刷新界面管理器
- /// </summary>
- /// <param name="path"></param>
- public bool RefreshVariableManager(string path="")
- {
- bool IsSucess=false;
- try
- {
- if (File.Exists(path))
- {
- CommunicationPar val = JsonConvert.DeserializeObject<CommunicationPar>(File.ReadAllText(path));
- if (val != null)
- {
- if (KeyValues == null)
- {
- KeyValues = new ConcurrentDictionary<string, CommunicationPar>();
- }
- KeyValues[path] = val;
- IsSucess = true;
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- return IsSucess;
- }
-
- /// <summary>
- /// 读取文件
- /// </summary>
- /// <returns></returns>
- public bool ReadFile()
- {
- bool IsSucess = false;
- try
- {
- if (File.Exists(path))
- {
- CommunicationPar val = JsonConvert.DeserializeObject<CommunicationPar>(File.ReadAllText(path));
- if (val != null)
- {
- if (KeyValues == null)
- {
- KeyValues = new ConcurrentDictionary<string, CommunicationPar>();
- }
- KeyValues[path] = val;
- IsSucess = true;
- }
- }
- }
- catch (Exception ex)
- {
-
- }
- return IsSucess ;
- }
-
- }
- }
|