ScadaService.cs 4.5 KB

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