Přeskočit obsah

HTTP Request-Reply

Use when: the device answers HTTP requests — REST, RPC over HTTP (getState, setOutput), XML or JSON — and the adapter polls it or sends commands to it.

Examples: perimeter units with an HTTP status API, controllers with a REST interface, any device whose integration guide lists URLs and their replies.

Framework provides: the HTTP client, one channel per device connection, one request at a time, timeout per request, offline after a run of failed requests and reconnect, exchange and trace lines, the AuthenticatedProtocol verdict, TLS with trust on first use and the client certificate.

You implement: one HttpOperation<T> record per vendor operation, the reply parsers, the signer when the device signs requests, and the protocol logic.


Configure the transport

transport:
  type: http
  tls: true            # when the device offers HTTPS
  protocol:
    event_type: "MyAdapter.Communication.MyEvent"

frame_type is not allowed for http.

Define operations

/// <summary>
/// Reads the unit state (Vendor API, chapter 6.2, getState).
/// </summary>
public sealed record GetState() : HttpOperation<UnitState>("getState")
{
    public override HttpRequestMessage Build(Uri root) => Get(root, "getState");

    public override UnitState Parse(HttpReply reply) => StateParser.Parse(reply.Body);
}

The name is the operation name of the vendor documentation; it is the packet name of every trace and exchange line (COMM-008).

Connect

internal async Task<bool> Connect(Uri root, IEventRouter<MyEvent> router, CancellationToken ct)
{
    AttachChannel(Transport.OpenChannel(new HttpEndpoint(root), router, new HttpChannelOptions { Signer = _signer }));
    var state = await Send(new GetState(), ct);
    if (state.IsOk)
        return true;

    await CloseChannel();
    return false;
}

Connect returns true only after a request the device answered (RULE-018). The framework sets the authentication slot from that answer; the adapter never does.

Results

HttpOutcome Meaning Counts toward offline
Ok 2xx, Value parsed no
NotModified 304 no
Refused 401/403 no
DeviceError other status no
CertificateRejected TLS handshake failed or certificate refused yes
Timeout no answer within HttpChannelOptions.Timeout yes
Lost request did not reach the device, or the channel is closed yes

After HttpChannelOptions.FailuresToOffline (default 3) counted failures in a row the device goes offline and the framework reconnects.

Signing

A device that authenticates every request (Basic header, HMAC over the body, a timestamp) gets an IHttpSigner. The channel calls Sign(request, body) once per request with the final body bytes. Test the signer against the vendor's test vector (COMM-009).

Common pitfalls

  • an own HttpClient for request/reply traffic (COMM-013)
  • accepting any certificate instead of transport.tls (COMM-013)
  • setting AuthenticatedProtocol by hand in an http adapter (RULE-058)
  • operation names invented instead of taken from the vendor documentation (COMM-008)

Good first deliverable

Connect, read one state operation, and see #1 getState -> 200 (…) ok … ms in the exchange log. Override HttpOperation<T>.Describe for the state operation, so its RX trace record shows the parsed reply in vendor field names.

See Transport types and CertificateTrust.