handleControlRequest method

Future handleControlRequest(
  1. HttpRequest request
)

Implementation

Future handleControlRequest(HttpRequest request) async {
  var bytes = await request.fold(
    <int>[], (List<int> a, List<int> b) => a..addAll(b)
  );
  var xml = XML.XmlDocument.parse(utf8.decode(bytes));
  var root = xml.rootElement;
  var body = root.firstChild;
  var service = device.findService(request.uri.pathSegments.last);

  if (service == null) {
    service = device.findService(Uri.decodeComponent(request.uri.pathSegments.last));
  }

  if (service == null) {
    request.response
      ..statusCode = HttpStatus.notFound
      ..close();
    return;
  }

  for (XML.XmlNode node in body!.children) {
    if (node is XML.XmlElement) {
      var name = node.name.local;
      var act = service.actions.firstWhereOrNull((x) => x.name == name);
      if (act == null) {
        request.response
          ..statusCode = HttpStatus.badRequest
          ..close();
        return;
      }

      if (act.handler != null) {
        // TODO(kaendfinger): make this have inputs and outputs.
        await act.handler!({});
        request.response
          ..statusCode = HttpStatus.ok
          ..close();
        return;
      }
    }
  }
}