summaryrefslogtreecommitdiffstats
path: root/lib/data/data_providers/radio_client.dart
blob: 3ee041a492517ed57fb9fddd060910ee07fffe54 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import 'package:flutter_ics_homescreen/export.dart';
import 'package:protos/radio_api.dart' as api;

class RadioClient {
  final RadioConfig config;
  final Ref ref;
  late api.ClientChannel channel;
  late api.RadioClient stub;

  RadioClient({required this.config, required this.ref}) {
    debugPrint(
        "Connecting to radio service at ${config.hostname}:${config.port}");
    api.ChannelCredentials creds = const api.ChannelCredentials.insecure();
    channel = api.ClientChannel(config.hostname,
        port: config.port, options: api.ChannelOptions(credentials: creds));
    stub = api.RadioClient(channel);
  }

  void run() async {
    getBandParameters();

    try {
      var responseStream = stub.getStatusEvents(api.StatusRequest());
      await for (var event in responseStream) {
        handleStatusEvent(event);
      }
    } catch (e) {
      print(e);
    }
  }

  void getBandParameters() async {
    try {
      var response = await stub.getBandParameters(
          api.GetBandParametersRequest(band: api.Band.BAND_FM));
      ref.read(radioStateProvider.notifier).updateBandParameters(
          freqMin: response.min,
          freqMax: response.max,
          freqStep: response.step);

      // Get initial frequency
      var freqResponse = await stub.getFrequency(api.GetFrequencyRequest());
      ref
          .read(radioStateProvider.notifier)
          .updateFrequency(freqResponse.frequency);
    } catch (e) {
      print(e);
    }
  }

  void handleStatusEvent(api.StatusResponse response) {
    switch (response.whichStatus()) {
      case api.StatusResponse_Status.frequency:
        var status = response.frequency;
        ref.read(radioStateProvider.notifier).updateFrequency(status.frequency);
        break;
      case api.StatusResponse_Status.play:
        var status = response.play;
        ref.read(radioStateProvider.notifier).updatePlaying(status.playing);
        break;
      case api.StatusResponse_Status.scan:
        var status = response.scan;
        if (status.stationFound) {
          ref.read(radioStateProvider.notifier).updateScanning(false);
        }
        break;
      default:
        break;
    }
  }

  void start() async {
    try {
      await stub.start(api.StartRequest());
    } catch (e) {
      print(e);
    }
  }

  void stop() async {
    try {
      await stub.stop(api.StopRequest());
    } catch (e) {
      print(e);
    }
  }

  void setFrequency(int frequency) async {
    var radioState = ref.read(radioStateProvider);
    if ((frequency < radioState.freqMin) ||
        (frequency > radioState.freqMax) ||
        ((frequency - radioState.freqMin) % radioState.freqStep) != 0) {
      debugPrint("setFrequency: invalid frequency $frequency!");
      return;
    }
    try {
      await stub
          .setFrequency(api.SetFrequencyRequest(frequency: frequency));
    } catch (e) {
      print(e);
    }
  }

  void tuneForward() async {
    var radioState = ref.read(radioStateProvider);
    if (radioState.freqCurrent < radioState.freqMax) {
      int frequency = radioState.freqCurrent + radioState.freqStep;
      if (frequency > radioState.freqMax) {
        frequency = radioState.freqMax;
      }
      try {
        await stub
            .setFrequency(api.SetFrequencyRequest(frequency: frequency));
      } catch (e) {
        print(e);
      }
    }
  }

  void tuneBackward() async {
    var radioState = ref.read(radioStateProvider);
    if (radioState.freqCurrent > radioState.freqMin) {
      int frequency = radioState.freqCurrent - radioState.freqStep;
      if (frequency < radioState.freqMin) {
        frequency = radioState.freqMin;
      }
      try {
        await stub
            .setFrequency(api.SetFrequencyRequest(frequency: frequency));
      } catch (e) {
        print(e);
      }
    }
  }

  void scanForward() async {
    try {
      await stub.scanStart(api.ScanStartRequest(
          direction: api.ScanDirection.SCAN_DIRECTION_FORWARD));
    } catch (e) {
      print(e);
    }
  }

  void scanBackward() async {
    try {
      await stub.scanStart(api.ScanStartRequest(
          direction: api.ScanDirection.SCAN_DIRECTION_BACKWARD));
    } catch (e) {
      print(e);
    }
  }

  void scanStop() async {
    try {
      await stub.scanStop(api.ScanStopRequest());
    } catch (e) {
      print(e);
    }
  }
}