-
Notifications
You must be signed in to change notification settings - Fork 4
/
SpeakerClient.cs
326 lines (280 loc) · 8.65 KB
/
SpeakerClient.cs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using HomeSeer.PluginSdk;
using HomeSeer.PluginSdk.Logging;
using HomeSeer.PluginSdk.Speech;
using HSCF.Communication.Scs.Communication.EndPoints.Tcp;
using HSCF.Communication.ScsServices.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HomeSeer.PluginSdk.Constants;
namespace HSPI_HomeSeerSamplePlugin
{
class SpeakerClient : IFromSpeaker
{
private ISpeechAPI _speakHost;
private IScsServiceClient<ISpeechAPI> _client;
private const int SPEAKER_INTERFACE_VERSION = 10; // 10=first version for HS3 with new WCF like API
private string _clientName = "";
public static object objlock = new object();
public SpeakerClient(string name)
{
_clientName = name;
}
public bool Connect(string username, string password)
{
lock (objlock)
{
string ipAddress = "127.0.0.1";
Logger.LogInfo("Connecting speaker client {0} to HomeSeer IP {1}", _clientName, ipAddress);
try
{
_client = ScsServiceClientBuilder.CreateClient<ISpeechAPI>(new ScsTcpEndPoint(ipAddress, 10401), this);
_client.Disconnected += new EventHandler(ClientDisconnected);
_client.Connected += new EventHandler(ClientConnected);
}
catch (Exception e)
{
Logger.LogError(e.ToString());
return false;
}
try
{
_client.Connect();
_speakHost = _client.ServiceProxy;
//make sure the interface is supported
int v;
try
{
v = _speakHost.version();
}
catch (Exception e)
{
Logger.LogError("Error attempting to connect to server, please check your connection options: {0}", e.Message);
return false;
}
if (v != SPEAKER_INTERFACE_VERSION)
{
Logger.LogError("Speaker API version mismatch");
return false;
}
//string localIp = Util.GetLocalIp();
string rval = _speakHost.Connect("Sample Plugin", _clientName, "127.0.0.1", username, password);
if (!string.IsNullOrEmpty(rval))
{
Logger.LogError("Error, Unable to connect speaker client interface: {0}", rval);
DisconnectNow();
return false;
}
}
catch (Exception e)
{
Logger.LogError("Error while connecting speaker client: {0}", e.ToString());
return false;
}
}
return true;
}
private void ClientDisconnected(object sender, EventArgs e)
{
Logger.LogInfo("Speaker client {0} has disconnected", _clientName);
}
private void ClientConnected(object sender, EventArgs e)
{
Logger.LogInfo("Speaker client {0} has connected", _clientName);
}
private void DisconnectNow()
{
lock (objlock)
{
try
{
if (_client != null)
{
try
{
if (_speakHost != null)
{
_speakHost = null;
}
_client.Disconnect();
_client.Dispose();
}
catch (Exception)
{
}
}
}
catch (Exception)
{
}
}
}
public bool Disconnect()
{
DisconnectNow();
return true;
}
public void SpeakText(string speaktxt, bool wait)
{
Logger.LogInfo("Speaking from Sample Speaker Client: " + speaktxt);
}
public void PlayWaveFile(string fname, int fsize)
{
Logger.LogInfo("Playing wave file from Sample Speaker Client, file: " + fname);
}
public bool IsBusy()
{
//throw new NotImplementedException();
return false;
}
public void VRChanged()
{
//throw new NotImplementedException();
}
public void StartListen()
{
//throw new NotImplementedException();
}
public void StopListen()
{
//throw new NotImplementedException();
}
public short SetVoice(string voice)
{
//throw new NotImplementedException();
return 0;
}
public void SpeakToFile(string txt, string fname, string voice = "")
{
//throw new NotImplementedException();
}
public int SendMessage(string message, bool showballoon)
{
//throw new NotImplementedException();
return 0;
}
public void PauseAudio()
{
//throw new NotImplementedException();
}
public void UnPauseAudio()
{
//throw new NotImplementedException();
}
public void MuteAudio()
{
//throw new NotImplementedException();
}
public void UnMuteAudio()
{
//throw new NotImplementedException();
}
public bool GetMuteStatus()
{
//throw new NotImplementedException();
return false;
}
public bool GetListenStatus()
{
//throw new NotImplementedException();
return false;
}
public short GetPauseStatus()
{
//throw new NotImplementedException();
return 0;
}
public string GetVoiceName()
{
//throw new NotImplementedException();
return string.Empty;
}
public int MEDIAPlay(string filename, int fsize)
{
//throw new NotImplementedException();
return 0;
}
public string MEDIAFunction(EMediaOperation operation, string p)
{
//throw new NotImplementedException();
return string.Empty;
}
public int GetVolume()
{
//throw new NotImplementedException();
return 0;
}
public void SetVolume(int vol)
{
}
public void StopSpeaking()
{
}
public int SetListenMode(int mode)
{
return 0;
}
public int GetListenMode()
{
return 0;
}
public void SetVRState(string state)
{
}
public void SetSpeaker(string speaker_name)
{
}
public int SetSpeakingSpeed(int speed)
{
return 0;
}
}
internal static class Logger
{
public static void Log(string line, ELogType level)
{
try
{
Program._plugin.WriteLog(level, line);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void LogDebug(string line)
{
Log(line, ELogType.Debug);
}
public static void LogDebug(string format, params object[] args)
{
LogDebug(string.Format(format, args));
}
public static void LogInfo(string line)
{
Log(line, ELogType.Info);
}
public static void LogInfo(string format, params object[] args)
{
LogInfo(string.Format(format, args));
}
public static void LogWarning(string line)
{
Log(line, ELogType.Warning);
}
public static void LogWarning(string format, params object[] args)
{
LogWarning(string.Format(format, args));
}
public static void LogError(string line)
{
Log(line, ELogType.Error);
}
public static void LogError(string format, params object[] args)
{
LogError(string.Format(format, args));
}
}
}