Skip to content

Commit

Permalink
add function to determine whether the device is online
Browse files Browse the repository at this point in the history
  • Loading branch information
fanliang11 committed Dec 31, 2018
1 parent c3b3c82 commit bb1346c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 221 deletions.
75 changes: 0 additions & 75 deletions src/Surging.Core/Surging.Core.CPlatform/Logging/ConsoleLogger.cs

This file was deleted.

142 changes: 0 additions & 142 deletions src/Surging.Core/Surging.Core.CPlatform/Logging/ILogger.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ await _mqttRemoteInvokeService.InvokeAsync(new MqttRemoteInvokeContext
public abstract Task UnSubscribe(string deviceId, params string[] topics);

public abstract Task Publish(string deviceId, MqttWillMessage willMessage);


public ValueTask<SessionStatus?> GetDeviceStatus(string deviceId)
{
SessionStatus? result = null;
if (!string.IsNullOrEmpty(deviceId))
{
MqttChannels.TryGetValue(deviceId, out MqttChannel mqttChannel);
result = mqttChannel?.SessionStatus;
}
return new ValueTask<SessionStatus?>(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface IChannelService
Task Publish(IChannel channel, PublishPacket mqttPublishMessage);
Task Publish(string deviceId, MqttWillMessage willMessage);
Task Close(string deviceId, bool isDisconnect);
ValueTask<SessionStatus?> GetDeviceStatus(string deviceId);
Task SendWillMsg(MqttWillMessage willMeaasge);
ValueTask<string> GetDeviceId(IChannel channel);
Task UnSubscribe(string deviceId, params string[] topics);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Surging.Core.CPlatform.Ioc;
using Surging.Core.CPlatform.Utilities;
using Surging.Core.Protocol.Mqtt.Internal.Enums;
using Surging.Core.Protocol.Mqtt.Internal.Messages;
using System;
using System.Collections.Generic;
Expand All @@ -12,7 +13,12 @@ public abstract class MqttBehavior: ServiceBase
{
public async Task Publish(string deviceId, MqttWillMessage willMessage)
{
await ServiceLocator.GetService<IChannelService>().Publish(deviceId, willMessage);
await GetService<IChannelService>().Publish(deviceId, willMessage);
}

public async Task<SessionStatus?> GetDeviceStatus(string deviceId)
{
return await this.GetService<IChannelService>().GetDeviceStatus(deviceId);
}

public abstract Task<bool> Authorized(string username, string password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public interface IControllerService : IServiceKey
{
[Command(ShuntStrategy = AddressSelectorMode.HashAlgorithm)]
Task Publish(string deviceId, WillMessage message);

[Command(ShuntStrategy = AddressSelectorMode.HashAlgorithm)]
Task<bool> IsOnline(string deviceId);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Surging.Core.Protocol.Mqtt.Internal.Messages;
using Surging.Core.Protocol.Mqtt.Internal.Enums;
using Surging.Core.Protocol.Mqtt.Internal.Messages;
using Surging.Core.Protocol.Mqtt.Internal.Services;
using Surging.IModuleServices.Common;
using Surging.IModuleServices.Common.Models;
Expand All @@ -19,9 +20,15 @@ public override async Task<bool> Authorized(string username, string password)
return await Task.FromResult(result);
}

public async Task<bool> IsOnline(string deviceId)
{
var status= await base.GetDeviceStatus(deviceId);
return status == SessionStatus.OPEN;
}

public async Task Publish(string deviceId, WillMessage message)
{
await base.Publish(deviceId, new MqttWillMessage
await Publish(deviceId, new MqttWillMessage
{
WillMessage = message.Message,
Qos = message.Qos,
Expand Down

1 comment on commit bb1346c

@fanliang11
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.