Skip to content

Commit 6203b40

Browse files
committed
Now using HubContext for triggering the Attention command #211
1 parent c06f6bf commit 6203b40

File tree

7 files changed

+57
-17
lines changed

7 files changed

+57
-17
lines changed

Fritz.Chatbot/AttentionHub.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Fritz.StreamLib.Core;
2+
using Microsoft.AspNetCore.SignalR;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace Fritz.StreamTools.Hubs
9+
{
10+
public interface IAttentionHubClient
11+
{
12+
13+
// Cheer 200 parithon 12/18/2018
14+
// Cheer 500 pharewings 12/18/2018
15+
Task AlertFritz();
16+
Task ClientConnected(string connectionId);
17+
}
18+
19+
public class AttentionHub : Hub<IAttentionHubClient>, IAttentionClient
20+
{
21+
public override Task OnConnectedAsync()
22+
{
23+
return this.Clients.Others.ClientConnected(this.Context.ConnectionId);
24+
}
25+
26+
public Task AlertFritz()
27+
{
28+
return this.Clients.Others.AlertFritz();
29+
}
30+
}
31+
}

Fritz.Chatbot/Commands/AttentionCommand.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,59 @@
22
using System.Text;
33
using System.Threading.Tasks;
44
using Fritz.StreamLib.Core;
5+
using Fritz.StreamTools.Hubs;
6+
using Microsoft.AspNetCore.SignalR;
57
using Microsoft.AspNetCore.SignalR.Client;
68
using Microsoft.Extensions.Configuration;
79
using Microsoft.Extensions.Hosting;
810
using Microsoft.Extensions.Logging;
911

1012
namespace Fritz.Chatbot.Commands
1113
{
12-
public class AttentionCommand : IBasicCommand, IDisposable
14+
public class AttentionCommand : IBasicCommand
1315
{
1416
private readonly IConfiguration Configuration;
1517

1618
public ILogger Logger { get; }
19+
public IHubContext<AttentionHub, IAttentionHubClient> HubContext { get; }
1720

18-
public AttentionCommand(IConfiguration configuration, ILoggerFactory loggerFactory)
21+
public AttentionCommand(IConfiguration configuration, IHubContext<AttentionHub, IAttentionHubClient> hubContext, ILoggerFactory loggerFactory)
1922
{
2023
this.Configuration = configuration;
2124
this.Logger = loggerFactory.CreateLogger("AttentionCommand");
2225

23-
var thisUri = new Uri(configuration["FritzBot:ServerUrl"], UriKind.Absolute);
24-
var attentionUri = new Uri(thisUri, "attentionhub");
26+
this.HubContext = hubContext;
2527

26-
Logger.LogTrace($"Connecting AttentionCommand to: {attentionUri}");
28+
//var thisUri = new Uri(configuration["FritzBot:ServerUrl"], UriKind.Absolute);
29+
//var attentionUri = new Uri(thisUri, "attentionhub");
2730

28-
this.Client = new HubConnectionBuilder().WithUrl(attentionUri.ToString()).Build();
31+
//Logger.LogTrace($"Connecting AttentionCommand to: {attentionUri}");
32+
33+
//this.Client = new HubConnectionBuilder().WithUrl(attentionUri.ToString()).Build();
2934

3035
}
3136

32-
protected HubConnection Client { get; }
37+
//protected HubConnection Client { get; }
3338

3439
public string Trigger => "attention";
3540

3641
public string Description => "Play audio queue to divert attention to chat";
3742

43+
#if DEBUG
44+
public TimeSpan? Cooldown => TimeSpan.FromSeconds(10);
45+
#else
3846
public TimeSpan? Cooldown => TimeSpan.Parse(Configuration["FritzBot:AttentionCommand:Cooldown"]);
47+
#endif
3948

4049
public async Task Execute(IChatService chatService, string userName, ReadOnlyMemory<char> rhs)
4150
{
4251

43-
await this.Client.StartAsync();
44-
45-
await this.Client.InvokeAsync("AlertFritz");
52+
await this.HubContext.Clients.All.AlertFritz();
4653

4754
var attentionText = Configuration["FritzBot:AttentionCommand:TemplateText"];
4855

4956
await chatService.SendMessageAsync(string.Format(attentionText, userName));
5057
}
5158

52-
public void Dispose()
53-
{
54-
Client.DisposeAsync();
55-
}
5659
}
5760
}

Fritz.Chatbot/Fritz.Chatbot.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.4" />
1213
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.0.4" />
1314
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.1" />
1415
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.1" />

Fritz.Chatbot/FritzBot.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ bool CommandsTooFast(string namedCommand, TimeSpan? cooldown = null)
214214
{
215215
Debug.Assert(user != null);
216216

217-
#if !DEBUG
218217
if (e.IsModerator || e.IsOwner)
219218
return false;
220-
#endif
221219

222220
// Check per user cooldown
223221
if (DateTime.UtcNow - user.LastCommandTime < CooldownTime)
224222
{
225223
_logger.LogWarning("Ignoring command {0} from {1} on {2}. Cooldown active", namedCommand, e.UserName, e.ServiceName);
224+
var remain = cooldown.GetValueOrDefault() - (DateTime.UtcNow - user.LastCommandTime);
225+
chatService.SendMessageAsync($"Ignoring command {namedCommand} from {e.UserName}. Cooldown is active for {e.UserName} for {remain.TotalSeconds}");
226226
return true;
227227
}
228228

@@ -235,6 +235,7 @@ bool CommandsTooFast(string namedCommand, TimeSpan? cooldown = null)
235235
var remain = cooldown.GetValueOrDefault() - (now - dt);
236236
_logger.LogWarning("Ignoring command {0} from {1} on {2}. In cooldown for {3} more secs", namedCommand, e.UserName, e.ServiceName,
237237
(int) remain.TotalSeconds);
238+
chatService.SendMessageAsync($"Ignoring command {namedCommand} from {e.UserName}. Cooldown is active for {e.UserName} for {remain.TotalSeconds}");
238239
return true;
239240
}
240241
}

Fritz.StreamTools/Fritz.StreamTools.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<EmbeddedResource Remove="ClientApp\**" />
1212
<None Remove="ClientApp\**" />
1313
</ItemGroup>
14+
<ItemGroup>
15+
<Compile Remove="Hubs\AttentionHub.cs" />
16+
</ItemGroup>
1417
<ItemGroup>
1518
<Content Remove="wwwroot\hey_listen.wav" />
1619
</ItemGroup>

Fritz.StreamTools/Hubs/AttentionHub.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IAttentionHubClient
1616
Task ClientConnected(string connectionId);
1717
}
1818

19-
public class AttentionHub : Hub<IAttentionHubClient>, IAttentionClient
19+
public class AttentionHub : Hub<IAttentionHubClient>
2020
{
2121
public override Task OnConnectedAsync()
2222
{

Fritz.StreamTools/Views/Attention/Index.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<body>
1212
<script src="~/lib/signalr/signalr-client.js"></script>
1313
<script src="~/js/attentionhub.js"></script>
14+
1415
<script>
1516
var audio = new Audio('@Url.Content(@"~/contents/hey_listen.wav")');
1617

0 commit comments

Comments
 (0)