sendCommand method

Future<void> sendCommand(
  1. DeviceCommand command
)

Implementation

Future<void> sendCommand(DeviceCommand command) async {
  final startTime = DateTime.now().millisecondsSinceEpoch;
  final timeout = sendTimeout.inMilliseconds;
  logger.finest(
    '[SEND COMMAND]: attempting to send command: [${command.type}]',
  );
  if (command is! GoodbyeCommand) {
    while (_socket?.readyState != WebSocket.open) {
      await Future.delayed(const Duration(seconds: 1));

      final waitedTime = DateTime.now().millisecondsSinceEpoch - startTime;

      if (waitedTime > timeout) {
        logger.warning(
          '[SEND COMMAND]: timeout attempting to send command: [${command.type}] -- [$waitedTime]',
        );
        throw Exception('Timeout');
      }
    }
  }

  if (_socket?.readyState == WebSocket.open) {
    _socket!.add(command.toString());
    if (command is CommandAck || command is PingCommand) {
      logger.finest('[SEND COMMAND]: sent: [${command.type}]');
    } else {
      logger.fine('[SEND COMMAND]: sent: [${command.type}]');
    }
  }
}