sendSocketMessage method

Future<String> sendSocketMessage(
  1. String data, {
  2. bool shouldLog = true,
})

Implementation

Future<String> sendSocketMessage(String data, {bool shouldLog = true}) async {
  try {
    if (isConnected && _socket != null) {
      final completer = Completer<String>();
      final requestId = messageID;
      messageID = (messageID % minPlatformMsgID) + 1;
      _requests[requestId] = completer;
      // XmlDocument formattedDOC = XmlDocument.parse(data);
      // String formattedXML = formattedDOC.toXmlString(pretty: true, preserveWhitespace: (_) => true);
      String formattedXML = formatXMLString(data);


      String header = generateHeaderForXML(formattedXML);
      String request = header + formattedXML;
      // print("Sending $messageID --->$data");
      if (shouldLog||true) {
        Logger.writeToLog(formattedXML, from: "Application", to: logIdentifier, logSuffix: logSuffix,header: header);
      }

      if (request.length > 1024) {
        List<String> splitMsgs = msgSplitter(request, 1024);
        for (var msg in splitMsgs) {
          _socket?.write(msg);
        }
      } else {
        _socket?.write(request);
      }
      if (shouldLog ) {
        await Logger.generalLog("Socked Sent Message with ID ${messageID - 1} from $logIdentifier");
      }

      return completer.future;
    } else {
      String xmlRes = '''<?xml version="1.0"?><!--Message sending time 2021-11-18 00:19:29.698-->
<cupps xmlns="http://www.cupps.aero/cupps/01.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" messageID="6" messageName="deviceReleaseResponse">
 <response result="Socket Closed"></response>
</cupps>''';
      return xmlRes;
    }
  } catch (e) {
    log("Sending to Socket ERROR: $e");
    rethrow;
  }
}