123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using NewLife;
- using NewLife.Data;
- using NewLife.Log;
- using NewLife.Net;
- using NewLife.Reflection;
- using NewLife.Serialization;
- using OPC_Client;
- namespace ScadaTcpService
- {
- public class ScadaService
- {
- private static Scada _scada;
- private static TcpServer _tcpServer;
- internal static ConcurrentDictionary<int, bool> InitDic = new ConcurrentDictionary<int, bool>();
- public static void Start()
- {
- var port = ConfigurationManager.AppSettings["Port"].ToInt();
- _tcpServer = new TcpServer
- {
- Port = port,
- Log = XTrace.Log,
- SessionLog = XTrace.Log
- };
- _tcpServer.Start();
- CreateScada();
-
- }
- public static void Stop()
- {
- _scada.Stop();
- _tcpServer.Stop("");
- }
- public static List<Data> GetAllData()
- {
- return _scada.GetAllData();
- }
- private static void CreateScada()
- {
- try
- {
- var serverName = ConfigurationManager.AppSettings["ServerName"];
- var pointFile = ConfigurationManager.AppSettings["PointFile"];
- _scada = new Scada(serverName, pointFile);
- _scada.ChangeEvent += (data, changeItems) =>
- {
- var dataList =
- data.Where(c => changeItems.Contains(c.Key))
- .Select(c => new
- {
- c.Value.dataType,
- c.Value.itemID,
- c.Value.timeStamp,
- c.Value.batchNum,
- c.Value.id,
- c.Value.quality,
- c.Value.DateTime1
- })
- .ToList();
- var sendData = new
- {
- cmd = "sendData",
- data = dataList
- };
- if (_tcpServer.SessionCount >= 1)
- {
- foreach (var item in InitDic.Where(c => c.Value))
- {
- _tcpServer.GetSession(item.Key)?.Send(new Packet(sendData.ToJson().GetBytes()));
- }
- }
- Console.WriteLine($"共改变{changeItems.Count}条数据");
- };
- _scada.Start();
- }
- catch (Exception e)
- {
- XTrace.WriteException(e);
- }
- }
- }
- /// <summary>定义服务端,用于管理所有网络会话</summary>
- internal class TcpServer : NetServer<TcpSession>
- {
- }
- /// <summary>定义会话。每一个远程连接唯一对应一个网络会话,再次重复收发信息</summary>
- internal class TcpSession : NetSession<TcpServer>
- {
- protected override void OnConnected()
- {
- if (!ScadaService.InitDic.ContainsKey(ID))
- ScadaService.InitDic.TryAdd(ID, false);
- var data = new
- {
- cmd = "getAllData",
- data = ScadaService.GetAllData().Select(c => new
- {
- c.dataType,
- c.itemID,
- c.timeStamp,
- c.batchNum,
- c.id,
- c.quality,
- c.DateTime1
- }).ToList()
- };
- Send(new Packet(data.ToJson().GetBytes()));
- base.OnConnected();
- }
- protected override void OnDisconnected(string reason)
- {
- if (ScadaService.InitDic.ContainsKey(ID))
- ScadaService.InitDic.Remove(ID);
- base.OnDisconnected(reason);
- }
- protected override void OnReceive(ReceivedEventArgs e)
- {
- if (e.Packet?.ToStr().ToLower().Contains("start") != true) return;
- if (ScadaService.InitDic.ContainsKey(ID))
- ScadaService.InitDic[ID] = true;
- }
- }
- }
|