| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using RTCommunicatorBase;
- using System.Text;
- using TLVProtocal;
- using UniversalNetFrame451.IO;
- namespace AlarmInfoServerSim.Services
- {
- public class InfoSendingService : IInfoSendingService, ITlvProvider, IDisposable
- {
- private readonly ISharedConfig _sharedConfig;
- private readonly ITlvCommunicatorServer _server;
- private bool _isConnected = false;
- private bool disposedValue;
- public InfoSendingService(ISharedConfig sharedConfig)
- {
- _sharedConfig= sharedConfig;
- _server = TlvFactory.GetTlvServer();
- _server.Initialize(this);
- Open();
- }
- public event EventHandler<bool>? ConnectionStatusChanged;
- public event EventHandler<TcpConnection>? TcpConnectionChanged;
- public event EventHandler<(ushort, TlvData)>? DataReceived;
- public bool Open()
- {
- if (_sharedConfig.BasicInfo is not null)
- {
- return _server.Open(_sharedConfig.BasicInfo.RTServerAddress, _sharedConfig.BasicInfo.RTServerPort);
- }
- return false;
- }
- public bool Send(byte tag, ST_ALARM alarm)
- {
- if (_isConnected)
- {
- return _server.Send(tag, alarm);
- }
- return false;
- }
- public void Connected(TcpConnection connection)
- {
- _isConnected = true;
- ConnectionStatusChanged?.Invoke(null, _isConnected);
- TcpConnectionChanged?.Invoke(null, connection);
- }
- public void Disconnected(TcpConnection connection)
- {
- _isConnected = false;
- ConnectionStatusChanged?.Invoke(null, _isConnected);
- TcpConnectionChanged?.Invoke(null, connection);
- }
- public void Received(TlvData data)
- {
- if (data.RawData is null || data.RawData.Length == 0)
- {
- //Log
- return;
- }
- if(data.Tag==1)
- {
- DataReceived?.Invoke(null,(data.Tag, data));
- }
- }
- public TlvData RequestReply(TlvData tlvData)
- {
- throw new NotImplementedException();
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- // TODO: 释放托管状态(托管对象)
- _server.Dispose();
- }
- // TODO: 释放未托管的资源(未托管的对象)并重写终结器
- // TODO: 将大型字段设置为 null
- disposedValue = true;
- }
- }
- // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
- // ~SendInfoService()
- // {
- // // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
- // Dispose(disposing: false);
- // }
- public void Dispose()
- {
- // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
- Dispose(disposing: true);
- GC.SuppressFinalize(this);
- }
- }
- }
|