1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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]
- /// <summary>
- /// 开始
- /// </summary>
- 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;
- }
- /// <summary>
- /// 停止
- /// </summary>
- public void Stop()
- {
- if (!_client.isConnected) return;
- _client.Disconnect();
- _client.Data_Change -= DataChange;
- }
- public List<Data> GetAllData()
- => _client.dataCenter.Select(c => c.Value).ToList();
- /// <summary>
- /// 改变事件
- /// </summary>
- public event Action<Dictionary<int, Data>, List<int>> ChangeEvent;
- #endregion
- #region [Event]
- /// <summary>
- /// 变更记录
- /// </summary>
- /// <param name="numItems"></param>
- /// <param name="clientHandles"></param>
- private void DataChange(int numItems, ref Array clientHandles)
- {
- var changeItems = clientHandles.Cast<int>().ToList();
- ChangeEvent?.Invoke(_client.dataCenter, changeItems);
- }
- #endregion
- }
- }
|