ScadaService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using NewLife;
  7. using NewLife.Data;
  8. using NewLife.Log;
  9. using NewLife.Net;
  10. using NewLife.Reflection;
  11. using NewLife.Serialization;
  12. using OPC_Client;
  13. namespace ScadaTcpService
  14. {
  15. public class ScadaService
  16. {
  17. private static Scada _scada;
  18. private static TcpServer _tcpServer;
  19. internal static ConcurrentDictionary<int, bool> InitDic = new ConcurrentDictionary<int, bool>();
  20. public static void Start()
  21. {
  22. var port = ConfigurationManager.AppSettings["Port"].ToInt();
  23. _tcpServer = new TcpServer
  24. {
  25. Port = port,
  26. Log = XTrace.Log,
  27. SessionLog = XTrace.Log
  28. };
  29. _tcpServer.Start();
  30. CreateScada();
  31. }
  32. public static void Stop()
  33. {
  34. _scada.Stop();
  35. _tcpServer.Stop("");
  36. }
  37. public static List<Data> GetAllData()
  38. {
  39. return _scada.GetAllData();
  40. }
  41. private static void CreateScada()
  42. {
  43. try
  44. {
  45. var serverName = ConfigurationManager.AppSettings["ServerName"];
  46. var pointFile = ConfigurationManager.AppSettings["PointFile"];
  47. _scada = new Scada(serverName, pointFile);
  48. _scada.ChangeEvent += (data, changeItems) =>
  49. {
  50. var dataList =
  51. data.Where(c => changeItems.Contains(c.Key))
  52. .Select(c => new
  53. {
  54. c.Value.dataType,
  55. c.Value.itemID,
  56. c.Value.timeStamp,
  57. c.Value.batchNum,
  58. c.Value.id,
  59. c.Value.quality,
  60. c.Value.DateTime1
  61. })
  62. .ToList();
  63. var sendData = new
  64. {
  65. cmd = "sendData",
  66. data = dataList
  67. };
  68. if (_tcpServer.SessionCount >= 1)
  69. {
  70. foreach (var item in InitDic.Where(c => c.Value))
  71. {
  72. _tcpServer.GetSession(item.Key)?.Send(new Packet(sendData.ToJson().GetBytes()));
  73. }
  74. }
  75. Console.WriteLine($"共改变{changeItems.Count}条数据");
  76. };
  77. _scada.Start();
  78. }
  79. catch (Exception e)
  80. {
  81. XTrace.WriteException(e);
  82. }
  83. }
  84. }
  85. /// <summary>定义服务端,用于管理所有网络会话</summary>
  86. internal class TcpServer : NetServer<TcpSession>
  87. {
  88. }
  89. /// <summary>定义会话。每一个远程连接唯一对应一个网络会话,再次重复收发信息</summary>
  90. internal class TcpSession : NetSession<TcpServer>
  91. {
  92. protected override void OnConnected()
  93. {
  94. if (!ScadaService.InitDic.ContainsKey(ID))
  95. ScadaService.InitDic.TryAdd(ID, false);
  96. var data = new
  97. {
  98. cmd = "getAllData",
  99. data = ScadaService.GetAllData().Select(c => new
  100. {
  101. c.dataType,
  102. c.itemID,
  103. c.timeStamp,
  104. c.batchNum,
  105. c.id,
  106. c.quality,
  107. c.DateTime1
  108. }).ToList()
  109. };
  110. Send(new Packet(data.ToJson().GetBytes()));
  111. base.OnConnected();
  112. }
  113. protected override void OnDisconnected(string reason)
  114. {
  115. if (ScadaService.InitDic.ContainsKey(ID))
  116. ScadaService.InitDic.Remove(ID);
  117. base.OnDisconnected(reason);
  118. }
  119. protected override void OnReceive(ReceivedEventArgs e)
  120. {
  121. if (e.Packet?.ToStr().ToLower().Contains("start") != true) return;
  122. if (ScadaService.InitDic.ContainsKey(ID))
  123. ScadaService.InitDic[ID] = true;
  124. }
  125. }
  126. }