Skip to content

Commit

Permalink
Merge pull request #7 from TomaT3/decouple-callbacks-from-state-changes
Browse files Browse the repository at this point in the history
Fix excpetions when TryGetState in callbackmethod from SubscribeState is called
  • Loading branch information
TomaT3 authored May 22, 2022
2 parents ce426e9 + 25fbae5 commit 4404b37
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
25 changes: 18 additions & 7 deletions src/ioBroker.net/ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ static async Task Main(string[] args)
//ioBroker.ConnectionString = @"http://192.168.178.192:8084";
ioBroker.ConnectionString = @"http://iobroker:8084";
await ioBroker.ConnectAsync(TimeSpan.FromSeconds(5));
await ioBroker.SubscribeStateAsync<bool>("linkeddevices.0.motionsensors.gangoben.presence", MotionDetectionChanged);
await ioBroker.SubscribeStateAsync<int>("linkeddevices.0.lights.GangOben.level", LightChanged);
await ioBroker.SubscribeStateAsync<string>("javascript.0.managers.lights.EgGang.Mode", StringChanged);

var boolValue = await ioBroker.TryGetStateAsync<bool>("linkeddevices.0.motionsensors.gangoben.presence");
var intValue = await ioBroker.TryGetStateAsync<int>("linkeddevices.0.lights.GangOben.level");
var stringValue = await ioBroker.TryGetStateAsync<string>("javascript.0.managers.lights.EgGang.Mode");
//var res = await ioBroker.TrySetStateAsync<bool>("linkeddevices.0.plugs.luftentfeuchterwaschraum.on", false);

//var value = await ioBroker.TryGetStateAsync<bool>("linkeddevices.0.plugs.luftentfeuchterwaschraum.on");


await ioBroker.SubscribeStateAsync<bool>("linkeddevices.0.motionsensors.gangoben.presence", async (changedValue) => await MotionDetectionChanged(changedValue, ioBroker));
//await ioBroker.SubscribeStateAsync<int>("linkeddevices.0.lights.GangOben.level", LightChanged);
//await ioBroker.SubscribeStateAsync<string>("javascript.0.managers.lights.EgGang.Mode", StringChanged);

//var boolValue = await ioBroker.TryGetStateAsync<bool>("linkeddevices.0.motionsensors.gangoben.presence");
//var intValue = await ioBroker.TryGetStateAsync<int>("linkeddevices.0.lights.GangOben.level");
//var stringValue = await ioBroker.TryGetStateAsync<string>("javascript.0.managers.lights.EgGang.Mode");


//var tempCountId = "javascript.0.socketio.0.Test_12345";
Expand Down Expand Up @@ -53,9 +59,14 @@ private static void StringChanged(string stringChanged)
Console.WriteLine($"StringChanged: {stringChanged}");
}

private static void MotionDetectionChanged(bool motionDetected)
private static async Task MotionDetectionChanged(bool motionDetected, IoBrokerDotNet ioBrokerDotNet)
{
Console.WriteLine($"MotionDetectionChanged: {motionDetected}");
var currentValue = await ioBrokerDotNet.TryGetStateAsync<int>("linkeddevices.0.lights.GangOben.level");
Console.WriteLine($"CurrentValue error: {currentValue.Error} valueToWrite: {currentValue.Value}");

var result = await ioBrokerDotNet.TrySetStateAsync<int>("linkeddevices.0.lights.GangOben.level", 10);
Console.WriteLine($"SetResult error: {result.Error} valueToWrite: {result.ValueToWrite}");
}

private static void LightChanged(int lightChanged)
Expand Down
15 changes: 9 additions & 6 deletions src/ioBroker.net/ioBroker.net/IoBrokerDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,16 @@ private void GetStateResponse<T>(SocketIOResponse response, EventWaitHandle stat

private void HandleStateChanged(SocketIOResponse response)
{
var topic = response.GetValue<string>();
var obj = response.GetValue<State>(1);

if (_subscriptions.TryGetValue(topic, out List<Action<State>> callbacks))
Task.Run(() =>
{
Parallel.ForEach(callbacks, (callbackMethod) => callbackMethod(obj));
}
var topic = response.GetValue<string>();
var obj = response.GetValue<State>(1);

if (_subscriptions.TryGetValue(topic, out List<Action<State>> callbacks))
{
Parallel.ForEach(callbacks, (callbackMethod) => callbackMethod(obj));
}
});
}
}
}

0 comments on commit 4404b37

Please sign in to comment.