using NewLife.IO;
using OPC_Client;
using System.Collections.Generic;
using System;
using System.Linq;
namespace Pas.ScadaService.Core
{
public class ScadaService
{
private readonly string _opcServerName;
private readonly string _pointName;
private readonly Client _client = new Client();
public ScadaService(string serverName, string pointName)
{
_opcServerName = serverName;
_pointName = pointName;
}
#region [Public]
///
/// 开始
///
public void Start()
{
using (var csv = new CsvFile(_pointName))
{
var headers = csv.ReadLine();
var all = csv.ReadAll();
if (headers?.Length != 3) return;
foreach (var row in all)
{
_client.itemIDs.Add(row[0]);
_client.descritions.Add(row[1]);
_client.equipMentCodes.Add(row[2]);
}
}
//建立连接
_client._opcServerName = _opcServerName;
_client.Connect();
if (!_client.isConnected)
{
throw new Exception("连接失败");
}
_client.Data_Change += DataChange;
}
///
/// 停止
///
public void Stop()
{
if (!_client.isConnected) return;
_client.Disconnect();
_client.Data_Change -= DataChange;
}
public List GetAllData()
=> _client.dataCenter.Select(c => c.Value).ToList();
///
/// 改变事件
///
public event Action, List> ChangeEvent;
#endregion
#region [Event]
///
/// 变更记录
///
///
///
private void DataChange(int numItems, ref Array clientHandles)
{
var changeItems = clientHandles.Cast().ToList();
ChangeEvent?.Invoke(_client.dataCenter, changeItems);
}
#endregion
}
}