using BeDesignerSCADA.Controls;
using BeDesignerSCADA.ViewModel;
using BPA.Communication;
using BPA.Helper;
using BPASmart.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
namespace BPASmart.ConfigurationSoftware
{
///
/// 服务中心
///
public class ServiceCenter
{
private volatile static ServiceCenter _Instance;
public static ServiceCenter GetInstance => _Instance ?? (_Instance = new ServiceCenter());
private ServiceCenter() { }
private PipeServer pipeServer { get; set; } = new PipeServer(PipeTopic.PipeName);
private ConcurrentQueue msg { get; set; } = new ConcurrentQueue();
public ObservableCollection Message { get; set; } = new ObservableCollection();
public List mainCanvasPanels { get; set; } = new List();
public ObservableCollection Pages { get; set; } = new ObservableCollection();
///
/// 页面切换委托
///
public Action PageChange { get; set; }
///
/// 项目名称改变委托
///
public Action ProjectNameChange { get; set; }
public void Init()
{
MessageLog.GetInstance.NotifyShow = new Action((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); });
MessageLog.GetInstance.NotifyShowEx = new Action((o) => { App.Current.Dispatcher.Invoke(() => { Message.Add(o); }); });
PipeMsgMonitor();
ProjectStartInit();
PageAdd();
}
///
/// 退出通知
///
public void ExitNotify()
{
pipeServer.SendCommand(PipeTopic.ExitNotify);
}
///
/// 管道消息监听,和服务启动
///
private void PipeMsgMonitor()
{
ThreadManage.GetInstance().StartLong(new Action(() =>
{
while (msg.Count > 0)
{
if (msg.TryDequeue(out string s))
{
if (s == PipeTopic.VarSave)
{
DataBusModel.GetInstance().RefreshVariableManager(FileConfigModel.VarConfigPath);
for (int i = 0; i < mainCanvasPanels.Count; i++)
{
mainCanvasPanels.ElementAt(i).MainCanvasPanelModel.VariableManagerPath(FileConfigModel.VarConfigPath);
}
}
else
{
MessageLog.GetInstance.Show(s);
}
}
}
Thread.Sleep(100);
}), "管道消息控制");
pipeServer.PushCommand = new Action((o) => { msg.Enqueue(o); });
pipeServer.StartPipeStream();
}
///
/// 通过项目启动文件加载的数据初始化
///
private void ProjectStartInit()
{
if (Json.Data.ProjectName != null && Json.Data.ProjectName.Length > 0)
{
ProjectNameChange?.Invoke(Json.Data.ProjectName);
FileConfigModel.ConstPath = Json.Data.ProjectPath;
DataBusModel.GetInstance().RefreshVariableManager(FileConfigModel.VarConfigPath);
}
if (Json.Data.Pages?.Count > 0)
{
for (int i = 0; i < Json.Data.Pages.Count; i++)
{
Pages.Add(Json.Data.Pages.ElementAt(i).Key);
}
}
}
///
/// 保存配置
///
private void Save()
{
string path = Json.Data.ProjectPath;
string name = Json.Data.ProjectName;
var pages = Json.Data.Pages.OrderBy(p => p.Key).ToDictionary(p => p.Key, s => s.Value);
if (pages != null) Json.Data.Pages = pages;
Json.Save($"{path}\\{name}.project");
}
///
/// 页面添加委托设置
///
private void PageAdd()
{
ActionManage.GetInstance.Register(new Action