Add nsi service handler

This commit is contained in:
2025-08-14 10:41:14 +09:00
parent b8710fd00a
commit de36ea2a13
31 changed files with 9576 additions and 1 deletions

View File

@ -0,0 +1,41 @@
using System.Threading;
using System.Threading.Tasks;
using Nsi = Hcs.Service.Async.Nsi.v15_7_0_1;
namespace Hcs.ClientApi.NsiApi
{
/// <summary>
/// Операции экспорта данных справочников поставщика информации ГИС ЖКХ
/// </summary>
internal class HcsMethodExportNsi : HcsNsiMethod
{
public HcsMethodExportNsi(HcsClientConfig config) : base(config)
{
EnableMinimalResponseWaitDelay = true;
CanBeRestarted = true;
}
/// <summary>
/// Возвращает данные справочников поставщика информации
/// </summary>
public async Task<object[]> GetNsiItem(int regNum, CancellationToken token)
{
var request = new Nsi.exportDataProviderNsiItemRequest
{
Id = HcsConstants.SignedXmlElementId,
RegistryNumber = (Nsi.exportDataProviderNsiItemRequestRegistryNumber)regNum,
// http://open-gkh.ru/Nsi/exportDataProviderNsiItemRequest.html
version = "10.0.1.2"
};
var stateResult = await SendAndWaitResultAsync(request, async (portClient) =>
{
var response = await portClient.exportDataProviderNsiItemAsync(CreateRequestHeader(), request);
return response.AckRequest.Ack;
}, token);
return stateResult.Items;
}
}
}

View File

@ -0,0 +1,28 @@
using System.Threading;
using System.Threading.Tasks;
namespace Hcs.ClientApi.NsiApi
{
public class HcsNsiApi
{
public HcsClientConfig Config { get; private set; }
public HcsNsiApi(HcsClientConfig config)
{
Config = config;
}
public async Task<object[]> GetNsiItem(int regNum, CancellationToken token = default)
{
try
{
var method = new HcsMethodExportNsi(Config);
return await method.GetNsiItem(regNum, token);
}
catch (HcsNoResultsRemoteException)
{
return [];
}
}
}
}

View File

@ -0,0 +1,112 @@
using Hcs.ClientApi.RemoteCaller;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Nsi = Hcs.Service.Async.Nsi.v15_7_0_1;
namespace Hcs.Service.Async.Nsi.v15_7_0_1
{
public partial class AckRequestAck : IHcsAck { }
public partial class getStateResult : IHcsGetStateResult { }
public partial class Fault : IHcsFault { }
public partial class HeaderType : IHcsHeaderType { }
}
namespace Hcs.ClientApi.NsiApi
{
public class HcsNsiMethod : HcsRemoteCallMethod
{
public HcsEndPoints EndPoint => HcsEndPoints.NsiAsync;
public Nsi.RequestHeader CreateRequestHeader() =>
HcsRequestHelper.CreateHeader<Nsi.RequestHeader>(ClientConfig);
public HcsNsiMethod(HcsClientConfig config) : base(config) { }
public System.ServiceModel.EndpointAddress RemoteAddress
=> GetEndpointAddress(HcsConstants.EndPointLocator.GetPath(EndPoint));
private Nsi.NsiPortsTypeAsyncClient NewPortClient()
{
var client = new Nsi.NsiPortsTypeAsyncClient(_binding, RemoteAddress);
ConfigureEndpointCredentials(client.Endpoint, client.ClientCredentials);
return client;
}
public async Task<IHcsGetStateResult> SendAndWaitResultAsync(
object request,
Func<Nsi.NsiPortsTypeAsyncClient, Task<IHcsAck>> sender,
CancellationToken token)
{
token.ThrowIfCancellationRequested();
while (true)
{
try
{
return await SendAndWaitResultAsyncImpl(request, sender, token);
}
catch (HcsRestartTimeoutException e)
{
if (!CanBeRestarted) throw new HcsException("Превышен лимит ожидания выполнения запроса", e);
Log($"Перезапускаем запрос типа {request.GetType().Name}...");
}
}
}
private async Task<IHcsGetStateResult> SendAndWaitResultAsyncImpl(
object request,
Func<Nsi.NsiPortsTypeAsyncClient, Task<IHcsAck>> sender,
CancellationToken token)
{
if (request == null) throw new ArgumentNullException("Null request");
string version = HcsRequestHelper.GetRequestVersionString(request);
_config.Log($"Отправляем запрос: {RemoteAddress.Uri}/{request.GetType().Name} в версии {version}...");
var stopWatch = System.Diagnostics.Stopwatch.StartNew();
IHcsAck ack;
using (var client = NewPortClient())
{
ack = await sender(client);
}
stopWatch.Stop();
_config.Log($"Запрос принят в обработку за {stopWatch.ElapsedMilliseconds}мс., подтверждение {ack.MessageGUID}");
var stateResult = await WaitForResultAsync(ack, true, token);
stateResult.Items.OfType<Nsi.ErrorMessageType>().ToList().ForEach(x =>
{
throw HcsRemoteException.CreateNew(x.ErrorCode, x.Description);
});
return stateResult;
}
/// <summary>
/// Выполняет однократную проверку наличия результата.
/// Возвращает null если результата еще нет.
/// </summary>
protected override async Task<IHcsGetStateResult> TryGetResultAsync(IHcsAck sourceAck, CancellationToken token)
{
using (var client = NewPortClient())
{
var requestHeader = HcsRequestHelper.CreateHeader<Nsi.RequestHeader>(_config);
var requestBody = new Nsi.getStateRequest { MessageGUID = sourceAck.MessageGUID };
var response = await client.getStateAsync(requestHeader, requestBody);
var resultBody = response.getStateResult;
if (resultBody.RequestState == HcsAsyncRequestStateTypes.Ready)
{
return resultBody;
}
return null;
}
}
}
}