using BPA.Message;
using BPA.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BPASmartClient.Business
{
///
/// 状态管理器
///
public class StatusMgr : IPlugin
{
//运行标识
private bool running = false;
//设备管理
private DeviceMgr deviceMgr;
private MQTTMgr mqttMgr;
DeviceStatus deviceStatus = new DeviceStatus();
private bool wholeDeviceHealth;
private Dictionary> wholeDeviceStatus = new Dictionary>();
public void Initialize()
{
deviceMgr = Plugin.GetInstance().GetPlugin();
mqttMgr = Plugin.GetInstance().GetPlugin();
}
public void Start()
{
running = true;
TaskManage.GetInstance.Start(() =>
{
while (running)
{
wholeDeviceHealth = true;
foreach (var device in deviceMgr.GetDevices())
{
wholeDeviceStatus[device.DeviceId] = device.Status.GetStatus();
}
Thread.Sleep(50);
}
}, "设备状态收集", isRestart: true);
deviceStatus.BatchingInfo = new List();
TaskManage.GetInstance.Start(() =>
{
while (running)
{
wholeDeviceHealth = true;
foreach (var device in deviceMgr.GetDevices())
{
wholeDeviceStatus[device.DeviceId] = device.Status.GetStatus();
deviceStatus.Healthy = device.IsHealth ? BPA.Message.Enum.DeviceHealthy.Health : BPA.Message.Enum.DeviceHealthy.UnHealth;
deviceStatus.DeviceType = device.DeviceType;
deviceStatus.BatchingInfo = device.BatchingInfos;
var msg = BPAPackage.Make(deviceStatus, device.DeviceId, device.DeviceType);
mqttMgr.Publish(TopicDefine.GetInstance().PushHeartbeatTopics[device.DeviceType], msg.Serialize());
}
Thread.Sleep(1000);
}
}, "设备心跳上报", isRestart: true);
}
public void Dispose()
{
running = false;
}
}
}