mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-02-17 02:51:39 -05:00
Fix getting device name from topic
This commit is contained in:
@@ -58,24 +58,25 @@ public class MessageHandler : IHostedService
|
|||||||
private async Task OnApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
|
private async Task OnApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
|
||||||
{
|
{
|
||||||
var topic = arg.ApplicationMessage.Topic;
|
var topic = arg.ApplicationMessage.Topic;
|
||||||
|
var deviceName = topic["device-status/".Length..];
|
||||||
var payload = arg.ApplicationMessage.ConvertPayloadToString();
|
var payload = arg.ApplicationMessage.ConvertPayloadToString();
|
||||||
|
|
||||||
WriteLog($"Topic: {topic} = {payload}");
|
WriteLog($"Topic: {topic} = {payload}");
|
||||||
|
|
||||||
var newDevice = new Device(topic, payload);
|
var newDevice = new Device(deviceName, payload);
|
||||||
|
|
||||||
if (_deviceTimers.ContainsKey(newDevice.Name))
|
if (_deviceTimers.ContainsKey(newDevice.Name))
|
||||||
await _deviceTimers[newDevice.Name].DisposeAsync();
|
await _deviceTimers[newDevice.Name].DisposeAsync();
|
||||||
|
|
||||||
if (!_deviceRepository.ContainsKey(newDevice.Name) || newDevice.Status)
|
if (!_deviceRepository.ContainsKey(newDevice.Name) || newDevice.Status)
|
||||||
{
|
{
|
||||||
WriteLog($"{arg.ApplicationMessage.Topic}: Handling status immediately");
|
WriteLog($"{deviceName}: Handling status immediately");
|
||||||
|
|
||||||
await HandleDeviceMessage(newDevice);
|
await HandleDeviceMessage(newDevice);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
WriteLog($"{arg.ApplicationMessage.Topic}: Setting timer for status");
|
WriteLog($"{deviceName}: Setting timer for status");
|
||||||
|
|
||||||
_deviceTimers[newDevice.Name] = new Timer(OnDeviceTimer, newDevice, _deviceDelayTime, Timeout.InfiniteTimeSpan);
|
_deviceTimers[newDevice.Name] = new Timer(OnDeviceTimer, newDevice, _deviceDelayTime, Timeout.InfiniteTimeSpan);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user