mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-13 17:22:54 -05:00
Add laundry notifications to Telegram
This commit is contained in:
40
DeviceStatus/Service/LaundryMonitor.cs
Normal file
40
DeviceStatus/Service/LaundryMonitor.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using RestSharp;
|
||||
|
||||
namespace Service;
|
||||
|
||||
public class LaundryMonitor
|
||||
{
|
||||
private readonly RestClient _restClient = new();
|
||||
|
||||
private readonly string _botToken;
|
||||
private readonly string _chatId;
|
||||
|
||||
public LaundryMonitor(IConfiguration configuration)
|
||||
{
|
||||
_botToken = configuration["Telegram:BotToken"];
|
||||
_chatId = configuration["Telegram:ChatId"];
|
||||
}
|
||||
|
||||
public async Task HandleDeviceMessage(Device device)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (device.Name is not ("washer" or "dryer"))
|
||||
return;
|
||||
|
||||
var status = device.Status ? "ON" : "OFF";
|
||||
|
||||
var message = $"The {device.Name} is now {status}.";
|
||||
|
||||
var encodedMessage = Uri.EscapeDataString(message);
|
||||
|
||||
var restRequest = new RestRequest($"https://api.telegram.org/bot{_botToken}/sendMessage?chat_id={_chatId}&text={encodedMessage}");
|
||||
|
||||
await _restClient.GetAsync(restRequest);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using MQTTnet;
|
||||
using MQTTnet.Server;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Service;
|
||||
|
||||
@@ -13,11 +12,13 @@ public class MessageHandler : IHostedService
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly DeviceRepository _deviceRepository;
|
||||
private readonly LaundryMonitor _laundryMonitor;
|
||||
|
||||
public MessageHandler(IConfiguration configuration, DeviceRepository deviceRepository)
|
||||
public MessageHandler(IConfiguration configuration, DeviceRepository deviceRepository, LaundryMonitor laundryMonitor)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_deviceRepository = deviceRepository;
|
||||
_laundryMonitor = laundryMonitor;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -37,10 +38,13 @@ public class MessageHandler : IHostedService
|
||||
|
||||
private async Task OnInterceptingPublishAsync(InterceptingPublishEventArgs arg)
|
||||
{
|
||||
WriteLog($"{arg.ApplicationMessage.Topic}: {arg.ApplicationMessage.ConvertPayloadToString()}");
|
||||
|
||||
_deviceRepository.HandleDeviceMessage(arg.ApplicationMessage.Topic, arg.ApplicationMessage.ConvertPayloadToString());
|
||||
|
||||
Console.WriteLine(arg.ApplicationMessage.Topic);
|
||||
Console.WriteLine(arg.ApplicationMessage.ConvertPayloadToString());
|
||||
var device = _deviceRepository[arg.ApplicationMessage.Topic];
|
||||
|
||||
await _laundryMonitor.HandleDeviceMessage(device);
|
||||
|
||||
if (_hubConnection == null)
|
||||
return;
|
||||
@@ -50,7 +54,7 @@ public class MessageHandler : IHostedService
|
||||
if (_hubConnection.State == HubConnectionState.Disconnected)
|
||||
_hubConnection.StartAsync().Wait();
|
||||
|
||||
var json = JsonSerializer.Serialize(_deviceRepository[arg.ApplicationMessage.Topic]);
|
||||
var json = JsonSerializer.Serialize(device);
|
||||
|
||||
await _hubConnection.InvokeAsync("SendLatestStatus", json);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ using Service;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Host.ConfigureAppConfiguration((_, config) => config.AddEnvironmentVariables());
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
@@ -10,6 +12,7 @@ builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddHostedService<MessageHandler>();
|
||||
|
||||
builder.Services.AddSingleton<DeviceRepository>();
|
||||
builder.Services.AddSingleton<LaundryMonitor>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="6.0.8" />
|
||||
<PackageReference Include="MQTTnet" Version="4.1.0.247" />
|
||||
<PackageReference Include="MQTTnet.AspNetCore" Version="4.1.0.247" />
|
||||
<PackageReference Include="RestSharp" Version="108.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -7,5 +7,9 @@
|
||||
},
|
||||
"Hub": {
|
||||
"DeviceStatus": "http://localhost:5000/device-status"
|
||||
},
|
||||
"Telegram": {
|
||||
"BotToken": "",
|
||||
"ChatId": ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,5 +8,9 @@
|
||||
"AllowedHosts": "*",
|
||||
"Hub": {
|
||||
"DeviceStatus": "http://hub-server/device-status"
|
||||
},
|
||||
"Telegram": {
|
||||
"BotToken": "",
|
||||
"ChatId": ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,16 @@ spec:
|
||||
env:
|
||||
- name: Hub__DeviceStatus
|
||||
value: http://hub-service/device-status
|
||||
- name: Telegram__BotToken
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: telegram
|
||||
key: bot-token
|
||||
- name: Telegram__ChatId
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: telegram
|
||||
key: chat-id
|
||||
restartPolicy: Always
|
||||
terminationGracePeriodSeconds: 30
|
||||
dnsPolicy: ClusterFirst
|
||||
|
||||
Reference in New Issue
Block a user