aboutsummaryrefslogtreecommitdiffstats
path: root/lib/grpc/voice_agent_client.dart
blob: 089d2a5fcc4891e8b07b0e335c5fce062fe8336a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'dart:async';
import 'package:grpc/grpc.dart';
import './generated/voice_agent.pbgrpc.dart';

class VoiceAgentClient {
  late ClientChannel _channel;
  late VoiceAgentServiceClient _client;

  VoiceAgentClient(String host, int port) {
    // Initialize the client channel without connecting immediately
    _channel = ClientChannel(
      host,
      port: port,
      options: ChannelOptions(
        credentials: ChannelCredentials.insecure(),
      ),
    );

    _client = VoiceAgentServiceClient(_channel);
  }

  Future<ServiceStatus> checkServiceStatus() async {
    final empty = Empty();
    try {
      final response = await _client.checkServiceStatus(empty);
      return response;
    } catch (e) {
      print('Error calling CheckServiceStatus: $e');
      // Handle the error gracefully, such as returning an error status
      return ServiceStatus()..status = false;
    }
  }

  Stream<WakeWordStatus> detectWakeWord() {
    final empty = Empty();
    try {
      return _client.detectWakeWord(empty);
    } catch (e) {
      print('Error calling DetectWakeWord: $e');
      // Handle the error gracefully, such as returning a default status
      return Stream.empty(); // An empty stream as a placeholder
    }
  }

  Future<RecognizeResult> recognizeVoiceCommand(
      Stream<RecognizeVoiceControl> controlStream) async {
    try {
      final response = await _client.recognizeVoiceCommand(controlStream);
      return response;
    } catch (e) {
      print('Error calling RecognizeVoiceCommand: $e');
      // Handle the error gracefully, such as returning a default RecognizeResult
      return RecognizeResult()..status = RecognizeStatusType.REC_ERROR;
    }
  }

  Future<RecognizeResult> recognizeTextCommand(
      RecognizeTextControl controlInput) async {
    try {
      final response = await _client.recognizeTextCommand(controlInput);
      return response;
    } catch (e) {
      print('Error calling RecognizeTextCommand: $e');
      // Handle the error gracefully, such as returning a default RecognizeResult
      return RecognizeResult()..status = RecognizeStatusType.REC_ERROR;
    }
  }

  Future<ExecuteResult> executeCommand(ExecuteInput input) async {
    try {
      final response = await _client.executeCommand(input);
      return response;
    } catch (e) {
      print('Error calling ExecuteVoiceCommand: $e');
      // Handle the error gracefully, such as returning an error status
      return ExecuteResult()..status = ExecuteStatusType.EXEC_ERROR;
    }
  }

  Future<void> shutdown() async {
    await _channel.shutdown();
  }
}