ScadaService.cs 4.2 KB

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