mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-13 17:22:54 -05:00
Updates for separate MQTT server
This commit is contained in:
@@ -64,7 +64,7 @@ while True:
|
|||||||
|
|
||||||
device.last_status = pin_status
|
device.last_status = pin_status
|
||||||
|
|
||||||
info = client.publish(device.name, str(pin_status), 1)
|
info = client.publish("device-status/" % (device.name), str(pin_status), 1)
|
||||||
|
|
||||||
info.wait_for_publish()
|
info.wait_for_publish()
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
using Microsoft.AspNetCore.SignalR.Client;
|
using Microsoft.AspNetCore.SignalR.Client;
|
||||||
using MQTTnet;
|
using MQTTnet;
|
||||||
using MQTTnet.Server;
|
using MQTTnet.Client;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace Service;
|
namespace Service;
|
||||||
|
|
||||||
public class MessageHandler : IHostedService
|
public class MessageHandler : IHostedService
|
||||||
{
|
{
|
||||||
private MqttServer? _mqttServer;
|
private IMqttClient? _mqttClient;
|
||||||
private HubConnection? _hubConnection;
|
private HubConnection? _hubConnection;
|
||||||
|
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
@@ -37,26 +37,25 @@ public class MessageHandler : IHostedService
|
|||||||
|
|
||||||
var mqttFactory = new MqttFactory();
|
var mqttFactory = new MqttFactory();
|
||||||
|
|
||||||
var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
|
_mqttClient = mqttFactory.CreateMqttClient();
|
||||||
|
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer(_configuration["Mqtt:Server"]).Build();
|
||||||
|
|
||||||
_mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions);
|
_mqttClient.ApplicationMessageReceivedAsync += OnApplicationMessageReceivedAsync;
|
||||||
_mqttServer.InterceptingPublishAsync += OnInterceptingPublishAsync;
|
|
||||||
|
|
||||||
await _mqttServer.StartAsync();
|
await _mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
|
||||||
|
|
||||||
|
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
|
||||||
|
.WithTopicFilter(
|
||||||
|
f =>
|
||||||
|
{
|
||||||
|
f.WithTopic("device-status/#");
|
||||||
|
})
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
await _mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RequestLatestStatus()
|
private async Task OnApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
|
||||||
{
|
|
||||||
WriteLog("RequestLatestStatus");
|
|
||||||
|
|
||||||
foreach (var device in _deviceRepository.Values)
|
|
||||||
{
|
|
||||||
WriteLog($"RequestLatestStatus: {device.Name}");
|
|
||||||
await SendDeviceStatus(device);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task OnInterceptingPublishAsync(InterceptingPublishEventArgs arg)
|
|
||||||
{
|
{
|
||||||
var topic = arg.ApplicationMessage.Topic;
|
var topic = arg.ApplicationMessage.Topic;
|
||||||
var payload = arg.ApplicationMessage.ConvertPayloadToString();
|
var payload = arg.ApplicationMessage.ConvertPayloadToString();
|
||||||
@@ -82,6 +81,17 @@ public class MessageHandler : IHostedService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task RequestLatestStatus()
|
||||||
|
{
|
||||||
|
WriteLog("RequestLatestStatus");
|
||||||
|
|
||||||
|
foreach (var device in _deviceRepository.Values)
|
||||||
|
{
|
||||||
|
WriteLog($"RequestLatestStatus: {device.Name}");
|
||||||
|
await SendDeviceStatus(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async void OnDeviceTimer(object? state)
|
private async void OnDeviceTimer(object? state)
|
||||||
{
|
{
|
||||||
var device = (Device)state!;
|
var device = (Device)state!;
|
||||||
@@ -135,8 +145,8 @@ public class MessageHandler : IHostedService
|
|||||||
if (_hubConnection != null)
|
if (_hubConnection != null)
|
||||||
await _hubConnection.StopAsync(cancellationToken);
|
await _hubConnection.StopAsync(cancellationToken);
|
||||||
|
|
||||||
if (_mqttServer != null)
|
if (_mqttClient != null)
|
||||||
await _mqttServer.StopAsync();
|
await _mqttClient.DisconnectAsync(new MqttClientDisconnectOptionsBuilder().Build(), CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteLog(string message)
|
private static void WriteLog(string message)
|
||||||
|
|||||||
@@ -11,5 +11,8 @@
|
|||||||
"Telegram": {
|
"Telegram": {
|
||||||
"BotToken": "",
|
"BotToken": "",
|
||||||
"ChatId": ""
|
"ChatId": ""
|
||||||
|
},
|
||||||
|
"Mqtt": {
|
||||||
|
"Server": "172.23.10.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,5 +15,8 @@
|
|||||||
},
|
},
|
||||||
"DeviceStatus": {
|
"DeviceStatus": {
|
||||||
"DelayTime": "00:01:00"
|
"DelayTime": "00:01:00"
|
||||||
|
},
|
||||||
|
"Mqtt": {
|
||||||
|
"Server": "mosquitto"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,18 +56,6 @@ spec:
|
|||||||
app: device-status-service
|
app: device-status-service
|
||||||
type: ClusterIP
|
type: ClusterIP
|
||||||
---
|
---
|
||||||
kind: Service
|
|
||||||
apiVersion: v1
|
|
||||||
metadata:
|
|
||||||
name: device-status-service-mqtt
|
|
||||||
spec:
|
|
||||||
ports:
|
|
||||||
- name: client
|
|
||||||
port: 1883
|
|
||||||
selector:
|
|
||||||
app: device-status-service
|
|
||||||
type: LoadBalancer
|
|
||||||
---
|
|
||||||
apiVersion: traefik.containo.us/v1alpha1
|
apiVersion: traefik.containo.us/v1alpha1
|
||||||
kind: IngressRoute
|
kind: IngressRoute
|
||||||
metadata:
|
metadata:
|
||||||
|
|||||||
51
Mosquitto/mosquitto.yaml
Normal file
51
Mosquitto/mosquitto.yaml
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: mosquitto
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: mosquitto
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: mosquitto
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: mosquitto
|
||||||
|
image: eclipse-mosquitto
|
||||||
|
ports:
|
||||||
|
- containerPort: 1883
|
||||||
|
- containerPort: 9001
|
||||||
|
volumeMounts:
|
||||||
|
- mountPath: /mosquitto/config/mosquitto.conf
|
||||||
|
subPath: mosquitto.conf
|
||||||
|
name: config
|
||||||
|
volumes:
|
||||||
|
- name: config
|
||||||
|
configMap:
|
||||||
|
name: mosquitto-config
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: mosquitto-config
|
||||||
|
data:
|
||||||
|
mosquitto.conf: |
|
||||||
|
# DO NOT USE IN PRODUCTION
|
||||||
|
allow_anonymous true
|
||||||
|
|
||||||
|
listener 1883
|
||||||
|
protocol mqtt
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: mosquitto
|
||||||
|
spec:
|
||||||
|
type: LoadBalancer
|
||||||
|
selector:
|
||||||
|
app: mosquitto
|
||||||
|
ports:
|
||||||
|
- port: 1883
|
||||||
Reference in New Issue
Block a user