Skip to content

Commit

Permalink
Merge pull request #12 from iberisoft/master
Browse files Browse the repository at this point in the history
Calling Gateway.EnableLight results to report message from the gateway however it fires an exception in the new MiHome.GetOrAddDeviceByCommand method.
  • Loading branch information
sergey-brutsky committed Aug 25, 2019
2 parents 474f5cd + 6eb6b65 commit 2198245
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
8 changes: 3 additions & 5 deletions MiHomeLib/Commands/GatewayLightCommand.cs
@@ -1,19 +1,17 @@
namespace MiHomeLib.Commands
{
internal class GatewayLightCommand: Command
internal class GatewayLightCommand : Command
{
private readonly long _rgb;
private readonly int _illumination;

public GatewayLightCommand(long rgb, int illumination)
public GatewayLightCommand(long rgb)
{
_rgb = rgb;
_illumination = illumination;
}

public override string ToString()
{
return $"{{\"rgb\":{_rgb},\"illumination\":{_illumination}}}";
return $"{{\"rgb\":{_rgb}}}";
}
}
}
10 changes: 5 additions & 5 deletions MiHomeLib/Devices/Gateway.cs
Expand Up @@ -43,18 +43,18 @@ public override string ToString()
return $"Rgb: {Rgb}, Illumination: {Illumination}, ProtoVersion: {ProtoVersion}";
}

public void EnableLight(byte r = 255, byte g = 255, byte b = 255, int illumination = 1000)
public void EnableLight(byte r = 255, byte g = 255, byte b = 255, int brightness = 100)
{
var rgb = 0xFF000000 | r << 16 | g << 8 | b; // found this trick from chinise gateway docs
var rgb = (uint)brightness << 24 | r << 16 | g << 8 | b;

if (illumination < 300 || illumination > 1300) throw new ArgumentException("Illumination must be in range 300 - 1300");
if (brightness < 1 || brightness > 100) throw new ArgumentException("Brightness must be in range 1 - 100");

_transport.SendWriteCommand(Sid, Type, new GatewayLightCommand(rgb, illumination));
_transport.SendWriteCommand(Sid, Type, new GatewayLightCommand(rgb));
}

public void DisableLight()
{
_transport.SendWriteCommand(Sid, Type, new GatewayLightCommand(0, 0));
_transport.SendWriteCommand(Sid, Type, new GatewayLightCommand(0));
}

public void StartPlayMusic(int midNo = 0)
Expand Down
3 changes: 2 additions & 1 deletion MiHomeLib/MiHome.cs
Expand Up @@ -136,7 +136,6 @@ private async Task StartReceivingMessages(CancellationToken ct)

private void ProcessReport(ResponseCommand cmd)
{
if (_gateway != null && cmd.Sid == _gateway.Sid) return;
GetOrAddDeviceByCommand(cmd).ParseData(cmd.Data);
}

Expand All @@ -160,6 +159,8 @@ private void ProcessReadAck(ResponseCommand cmd)

private MiHomeDevice GetOrAddDeviceByCommand(ResponseCommand cmd)
{
if (_gateway != null && cmd.Sid == _gateway.Sid) return _gateway;

if (_devicesList.ContainsKey(cmd.Sid)) return _devicesList[cmd.Sid];

var device = _devicesMap[cmd.Model](cmd.Sid);
Expand Down

0 comments on commit 2198245

Please sign in to comment.