dominicusmento / CSharpTradFriLibrary

This is a .NET Standard (2.0) library to communicate with the IKEA Home (Tradfri) ZigBee-based Gateway.
GNU General Public License v3.0
37 stars 20 forks source link

Status #7

Closed davidwallis closed 5 years ago

davidwallis commented 6 years ago

Am I missing it somewhere but is it possible to get the status of the bulb from the controller.. IE if I switch it on with the remote - can I get the status from the gateway?

dominicusmento commented 6 years ago

I will check on that later on, when I find some time to put in the project.

davidwallis commented 6 years ago

Did you get chance to check?

bmachuletz commented 6 years ago

Do you need to get an event when switch the bulb with the remote? <- I don't really know how to do this.. maybe someone has a better idea than me.. I use a polling mechanism in my project "HomeMagic" <- not on Github at this time (I have to refactor the one time)... But here is the code snippet that I use to get the bulb state: private bool IsDeviceSwitchedOn(TradfriDevice dev) { bool retval = false;

        if (dev.IsTradfriGroup == true)
        {
            foreach (long currentId in dev.GroupDefinition.DeviceList.DevicesIds)
            {
                var tradfriDevice = (from d in DeviceList
                                     where d.Id == currentId
                                     select d).SingleOrDefault();
                if (IsDeviceSwitchedOn(tradfriDevice))
                {
                    retval = true;
                }
                else
                {
                    retval = false;
                }
            }
        }
        else
        {
            client.UriPath = dev.Path;
            var UpdateLampe = DeviceList.SingleOrDefault(l => l.Id == dev.Id);
            if (UpdateLampe != null)
            {
                UpdateLampe = Newtonsoft.Json.JsonConvert.DeserializeObject<TradfriDevice>(client.Get().PayloadString);
            }
            if (UpdateLampe.TradfriLamp[0].State == 0)
            {
                retval = false;
            }
            else
            {
                retval = true;
            }
        }

        return retval;
    }
davidwallis commented 6 years ago

I would like an event ideally – I suppose something like RX could be used with polling 😊

Ill have a play later, thanks.

From: bmachuletz Sent: 01 March 2018 19:17 To: tomidix/CSharpTradFriLibrary Cc: David Wallis; Author Subject: Re: [tomidix/CSharpTradFriLibrary] Status (#7)

Do you need to get an event when switch the bulb with the remote? <- I don't really know how to do this.. maybe someone has a better idea than me.. I use a polling mechanism in my project "HomeMagic" <- not on Github at this time (I have to refactor the one time)... But here is the code snippet that I use to get the bulb state: private bool IsDeviceSwitchedOn(TradfriDevice dev) { bool retval = false; if (dev.IsTradfriGroup == true) { foreach (long currentId in dev.GroupDefinition.DeviceList.DevicesIds) { var tradfriDevice = (from d in DeviceList where d.Id == currentId select d).SingleOrDefault(); if (IsDeviceSwitchedOn(tradfriDevice)) { retval = true; } else { retval = false; } } } else { client.UriPath = dev.Path; var UpdateLampe = DeviceList.SingleOrDefault(l => l.Id == dev.Id); if (UpdateLampe != null) { UpdateLampe = Newtonsoft.Json.JsonConvert.DeserializeObject(client.Get().PayloadString); } if (UpdateLampe.TradfriLamp[0].State == 0) { retval = false; } else { retval = true; } }

    return retval;
}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

davidwallis commented 6 years ago

just been having a play to see if I can use the observe method ( https://github.com/Com-AugustCellars/CoAP-CSharp/wiki/Observe-Example ) of the coapClient to see if you can observe gateway, device and scene changes - from looking at other libraries Suchas https://github.com/AlCalzone/node-tradfri-client/blob/de88c9186a0dc0817d0f8e7520deb2370fc339f5/build/tradfri-client.js this seems possible - but have not had any success as yet..

davidwallis commented 6 years ago

result :D

Having added a cc.Observe in device controller I can now see switch events of bulbs and dynamically get the status updates :D

Just need to parse the response and then think about how to present these back, any suggestions??

response looks like this:

image

Code needed in device controller class:

        public DeviceController(long _id, CoapClient _cc, bool loadAutomatically = true)
        {
            id = _id;
            cc = _cc;

            if (loadAutomatically)
                GetTradFriDevice();

            cc.Observe(NotifyEvent, FailEvent);
        }

        static void NotifyEvent(Response res)
        {
            System.Console.WriteLine("The notify number {1} has a payload of {0}", res.ResponseText, res.Observe);

            var device = JsonConvert.DeserializeObject<TradFriDevice>(res.PayloadString);
            // do something with the device
        }

        static void FailEvent(CoapClient.FailReason res)
        {
            System.Console.WriteLine("Observe Failed");
        }
dominicusmento commented 6 years ago

Can you push the code for observe method so we can also make a call and create notify model based on response?

davidwallis commented 6 years ago

The Observe method is built in to the Coap Client library in use.. above is as simple as needed - its just wasnt sure how you wanted to do the notify model.. previously I have used ObservableCollections

but not sure if there are better ways such as RX.. (I wouldn't call myself a dev! - I guess not introducing a dependency would be better)

coriumalpha commented 5 years ago

I have experienced some inestability.

Steps I followed: Set an observer over a CoapClient instance (_cc.Observe(delegate ...)) Put a logger in the delegate method that logs a DateTime and a payload every time it gets called. Make some random inputs in the Tradfri controller.

Behaivour I obtained: During the first or second minute of interactions, the delegate method gets called and the logs are created according to the inputs made in the tradfri controller. After making no inputs in the tradfri controller during about two minutes and then making one, the observe method does not call the delegate and no logs are generated, it doesnt either call the error Action.

I didn't had time to digg further, but just want to cross check with you if you're having this issue too. Thank you for your time.

davidwallis commented 5 years ago

my testing had only been limited. so I can't say if I did or didn't, and unfortunatley everything is in boxes as we moved house recently

On Mon, 24 Dec 2018, 18:55 Corium <notifications@github.com wrote:

I have experienced some inestability.

Steps I followed: Set an observer over a CoapClient instance (_cc.Observe(delegate ...)) Put a logger in the delegate method that logs a DateTime and a payload every time it gets called. Make some random inputs in the Tradfri controller.

Behaivour I obtained: During the first or second minute of interactions, the delegate method gets called and the logs are created according to the inputs made in the tradfri controller. After making no inputs in the tradfri controller during about two minutes and then making one, the observe method does not call the delegate and no logs are generated, it doesnt either call the error Action.

I didn't had time to digg further, but just want to cross check with you if you're having this issue too. Thank you for your time.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tomidix/CSharpTradFriLibrary/issues/7#issuecomment-449763496, or mute the thread https://github.com/notifications/unsubscribe-auth/ANNq_k9ZIsfd5NEk7h49MB2sGmWmE4xpks5u8SM7gaJpZM4Rr880 .

coriumalpha commented 5 years ago

I've been making some research and found (in the CoAP library README's last paragraph) that this behaviour is caused by the Windows Firewall. If the EndPoint uses an ephemeral port, the firewall closes this port after about 90 seconds of inactivity (no resource status updates), the timing depends on Windows version. With the Windows Firewall disabled the Observe must lasts until the defined timeout (didn't test it yet because this time is set by the Tradfri Gateway and lasts for days). Therefore, I need to choose between setting that port open manually/programmatically or just setting it static in the ClientConfig and opening it manually in the Windows Firewall.

dominicusmento commented 5 years ago

There is now an option to use Observe/ObserveDevice methods thanks to @coriumalpha ... Windows firewall is I suppose a separate problem and we should have a separate issue for that