ScadaService.cs 3.3 KB

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