RpcClientService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using SCADA.CommonLib;
  2. using SCADA.RPC;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. namespace SCADA_DAQ.Machines
  12. {
  13. [DisplayName("RPC客户端")]
  14. public class RpcClientService : SCADA.CommonLib.Service.BaseService
  15. {
  16. private RpcClient rpcClient;
  17. private RpcClietnConfig config;
  18. private static RpcClientService rpcClientService;
  19. public static RpcClientService Instance { get => rpcClientService ?? (rpcClientService = new RpcClientService()); }
  20. public override ObservableObject ServiceConfig
  21. {
  22. get => config ?? (config = new RpcClietnConfig());
  23. set => config = (RpcClietnConfig)value;
  24. }
  25. public override Type ConfigType => typeof(RpcClietnConfig);
  26. public RpcClientService() : base()
  27. {
  28. if (rpcClientService == null)
  29. {
  30. rpcClientService = this;
  31. }
  32. }
  33. public override bool Start()
  34. {
  35. base.Start();
  36. rpcClient = new RpcClient(config.Server, config.Port);
  37. rpcClient.ClientInfo.ClientID = Env.ComputerTag.Value;
  38. new System.Threading.Thread(() =>
  39. {
  40. while (IsRunning)
  41. {
  42. try
  43. {
  44. if (rpcClient?.ConnectState == SCADA.Comm.ConnectStates.Connected)
  45. {
  46. var dateTime = rpcClient.RpcCall<object>("Sys_GetSysInfo", null);
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. Log.Error(ex);
  52. }
  53. for (int i = 0; i < 20; i++)
  54. {
  55. if (IsRunning)
  56. {
  57. System.Threading.Thread.Sleep(500);
  58. }
  59. else
  60. {
  61. break;
  62. }
  63. }
  64. }
  65. }).Start();
  66. return true;
  67. }
  68. public override bool Stop()
  69. {
  70. rpcClient.Dispose();
  71. return base.Stop();
  72. }
  73. }
  74. class RpcClietnConfig : ObservableObject
  75. {
  76. /// <summary>
  77. ///
  78. /// </summary>
  79. [DisplayName("服务器")]
  80. [Required]
  81. public string Server
  82. {
  83. get { return _Server; }
  84. set
  85. {
  86. if (value != _Server)
  87. {
  88. _Server = value;
  89. OnPropertyChanged(nameof(Server));
  90. }
  91. }
  92. }
  93. private string _Server = "127.0.0.1";
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. [DisplayName("端口")]
  98. [Range(1, 65535)]
  99. public int Port
  100. {
  101. get { return _Port; }
  102. set
  103. {
  104. if (value != _Port)
  105. {
  106. _Port = value;
  107. OnPropertyChanged(nameof(Port));
  108. }
  109. }
  110. }
  111. private int _Port = 7880;
  112. }
  113. }