123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using SCADA.CommonLib;
- using SCADA.RPC;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace SCADA_DAQ.Machines
- {
- [DisplayName("RPC客户端")]
- public class RpcClientService : SCADA.CommonLib.Service.BaseService
- {
- private RpcClient rpcClient;
- private RpcClietnConfig config;
- private static RpcClientService rpcClientService;
- public static RpcClientService Instance { get => rpcClientService ?? (rpcClientService = new RpcClientService()); }
- public override ObservableObject ServiceConfig
- {
- get => config ?? (config = new RpcClietnConfig());
- set => config = (RpcClietnConfig)value;
- }
- public override Type ConfigType => typeof(RpcClietnConfig);
- public RpcClientService() : base()
- {
- if (rpcClientService == null)
- {
- rpcClientService = this;
- }
- }
- public override bool Start()
- {
- base.Start();
- rpcClient = new RpcClient(config.Server, config.Port);
- rpcClient.ClientInfo.ClientID = Env.ComputerTag.Value;
- new System.Threading.Thread(() =>
- {
- while (IsRunning)
- {
- try
- {
- if (rpcClient?.ConnectState == SCADA.Comm.ConnectStates.Connected)
- {
- var dateTime = rpcClient.RpcCall<object>("Sys_GetSysInfo", null);
- }
- }
- catch (Exception ex)
- {
- Log.Error(ex);
- }
- for (int i = 0; i < 20; i++)
- {
- if (IsRunning)
- {
- System.Threading.Thread.Sleep(500);
- }
- else
- {
- break;
- }
- }
- }
- }).Start();
- return true;
- }
- public override bool Stop()
- {
- rpcClient.Dispose();
- return base.Stop();
- }
- }
- class RpcClietnConfig : ObservableObject
- {
- /// <summary>
- ///
- /// </summary>
- [DisplayName("服务器")]
- [Required]
- public string Server
- {
- get { return _Server; }
- set
- {
- if (value != _Server)
- {
- _Server = value;
- OnPropertyChanged(nameof(Server));
- }
- }
- }
- private string _Server = "127.0.0.1";
- /// <summary>
- ///
- /// </summary>
- [DisplayName("端口")]
- [Range(1, 65535)]
- public int Port
- {
- get { return _Port; }
- set
- {
- if (value != _Port)
- {
- _Port = value;
- OnPropertyChanged(nameof(Port));
- }
- }
- }
- private int _Port = 7880;
- }
- }
|