123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using NewLife.IO;
- using OPC_Client;
- namespace ScadaTcpService
- {
- public class Scada
- {
- private readonly string _opcServerName;
- private readonly string _pointName;
- private readonly Client _client = new Client();
- public Scada(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
- }
-
- }
|