homebridge-cbus icon indicating copy to clipboard operation
homebridge-cbus copied to clipboard

Ensure C-Bus network is open before sending any other commands

Open anthonywebb opened this issue 7 years ago • 19 comments

@simonhac cleaned out my install and decided to try a fresh install as a new user would.

I specified an export file, and upon running homebridge this was in the logs:

cbus:client sent command '[100] dbgetxml //WEBB/254' +22ms [4/1/2017, 2:36:50 PM] Homebridge is running on port 51826. cbus:client rx response { commandId: 100, cbus:client code: 440, cbus:client matched: false, cbus:client processed: false, cbus:client type: 'response', cbus:client raw: '[100] 440 There is no tag database to perform this operation on' }

Are you aware of any additional work that needs to be done in cgate to make this feature work? It think I have the latest version of cgate running on windows.

anthonywebb avatar Apr 01 '17 20:04 anthonywebb

OK so I checked and indeed CGate was not running. It would be nice to make sure CGate is running and we are connected before we do operations like this. Good news is once CGate was running my sample config was created as expected, cool!

anthonywebb avatar Apr 01 '17 21:04 anthonywebb

@anthonywebb i think something else was going on here.

if C-Gate was not operational, you wouldn't have received a 440 response from it.

i've had something similar before after installing C-Gate from scratch -- IIRC i had to issue a command (or change a config file?) to load the project. i remember thinking at the time that it'd be nice if cgate-client.js was able to detect C-Gate configuration issues and offer to the user to fix them.

simonhac avatar Apr 02 '17 01:04 simonhac

I suspect many the cgate windows service was running and accepting connections, but the network was not "open", in the cgate ui you open it and have to press "open all networks" which is what actually connects to the CNI. Most of the linux installs I have seen tell cgate to auto connect to a given network. I've never set up my windows install like that.

anthonywebb avatar Apr 02 '17 05:04 anthonywebb

if you can recommend the C-Gate commands to test whether the project is loaded and the network is open (and commands to rectify these if not the case) then i'll happily add code to auto-detect (and fix) this issue.

simonhac avatar Apr 03 '17 04:04 simonhac

That would be fantastic @simonhac. With the config I have running at the moment I need to "sudo service cgate restart" after every reboot of the Pi to restore comm's.

C-Gate is auto-starting with the init.d code from here and homebridge is starting with the systemd from here. All running on a Pi.

  • Greig.

greiginsydney avatar Apr 18 '17 08:04 greiginsydney

@greiginsydney i was planning to just make homebridge-cbus a bit more inquisitive at startup in the case the cgate hasn't got the project loaded or network open. but i don't know enough about the commands required to test.

if anyone knows these, please post and i'll extend. (i don't intend to touch the init.d or systemd stuff. that's above my pay-grade.)

simonhac avatar Apr 20 '17 04:04 simonhac

to find out which projects are loaded and running, use the PROJECT LIST command:

PROJECT LIST
123-project=1NET state=stopped
123-project=2NET state=stopped
123 project=SHAC state=started

to find out whether the network is open and running, use NET LIST command:

NET LIST //PROJECT
131 network=254 State=ok InterfaceState=running

simonhac avatar May 01 '17 08:05 simonhac

Hello Simon an Anthony For Windows with Toolkit and CGate. If you change the following C:\Clipsal\C-Gate2\config "C-GateConfig" file CGate should load and start the project. project.default="projectname" project.start="projectname"

It should also sync automatically. If the network is closed and reopened then I don't think it will do a sync right away. net sync //"projectname"/"network" will do a sync. default sync time in CGate config is 3600 seconds.

DarylMc avatar May 04 '17 00:05 DarylMc

thanks @DarylMc -- i plan to modify homebridge-cbus to automatically open and start the project specified in config.json in order to reduce the configuration to a single point.

i'm still a bit confused around syncing -- how to determine if syncing needs to be done, whether it just happens automagically, etc.

simonhac avatar May 05 '17 00:05 simonhac

Hi Simon I haven't read the whole document but there is a CGate PDF manual in the C:\Clipsal\C-Gate2 folder when you install Toolkit on a Windows machine. It describes "project" command in CGate console to load, use and start a project. Also "net" command to open, close and sync the network.

If you set the project.config and project.start parameters in the CGateConfig text file Cgate will load, use and open the project, open the network and sync automatically when CGate is restarted.

Here is an example where I removed project.default and project.start entries. `C-Gate is running. project dir 123-project=SHED2 123 project=OFFICE project load office 20170505-120937 751 //OFFICE - Tag information changed at tag address: //OFFICE/Installation oldtag: null newtag: null 20170505-120937 742 //OFFICE/254 d046f2c0-1365-1035-8e18-c5cb640aec17 Network created type=CNI address=192.168.1.2:4001 200 OK. project use office 200 OK. project list 123 project=OFFICE state=stopped project start office 200 OK. 20170505-121025 836 //OFFICE/254/p/202 d04e6cd0-1365-1035-8e49-c5cb640aec17 unit configuration changed (6 changes) 20170505-121038 836 //OFFICE/254/p/1 d04d3450-1365-1035-8e41-c5cb640aec17 unit configuration changed (3 changes) 20170505-121039 836 //OFFICE/254/p/2 d04d5b60-1365-1035-8e42-c5cb640aec17 unit configuration changed (8 changes) 20170505-121040 836 //OFFICE/254/p/3 d04d8270-1365-1035-8e43-c5cb640aec17 unit configuration changed (5 changes) 20170505-121041 836 //OFFICE/254/p/4 d04da980-1365-1035-8e44-c5cb640aec17 unit configuration changed (6 changes) 20170505-121042 836 //OFFICE/254/p/5 d04dd090-1365-1035-8e45-c5cb640aec17 unit configuration changed (3 changes) 20170505-121044 836 //OFFICE/254/p/8 d04df7a0-1365-1035-8e46-c5cb640aec17 unit configuration changed (4 changes) 20170505-121045 836 //OFFICE/254/p/9 d04e1eb0-1365-1035-8e47-c5cb640aec17 unit configuration changed (4 changes) 20170505-121048 836 //OFFICE/254/p/21 d04e45c0-1365-1035-8e48-c5cb640aec17 unit configuration changed (9 changes)

net list 131 network=254 State=ok InterfaceState=running project list 123 project=OFFICE state=started`

DarylMc avatar May 05 '17 02:05 DarylMc

@anthonywebb @simonhac @greiginsydney On pi3 running cgate and homebridge. When both cgate and homebridge services are started at boot. I found homebridge went into a weird connection state with cgate that would not recover automatically. The symptom of this faulty state was home app could control lights but would not sync with changes coming from the cbus network. You would think maybe restarting cgate while homebridge is running might cause the same problem but I have not found that to be the case. So I added a timer to delay homebridge.service long enough to allow cgate to be running and synced. All starts and recovers nicely from reboot, loss of ethernet, router reboot and has not stopped working for any reason to date.

DarylMc avatar Jul 04 '17 23:07 DarylMc

please leave this ticket open -- i'd like to fix this in homekit-cbus, but greatly appreciate @DarylMc's workaround.

(current implementation doesn't retry if the initial connection attempt fails. should be easy to reuse the reconnect-on-closed connection logic.)

simonhac avatar Jul 06 '17 17:07 simonhac

@simonhac I put it down to both cgate and homebridge running as services at startup on pi. Never had the problem before when using a remote cgate. I think I could reproduce what happens if the timer is disabled if that is of any help

DarylMc avatar Jul 06 '17 17:07 DarylMc

thanks @DarylMc, but no need -- i'm pretty sure i know what's going on and it should be an easy fix. it's definitely a bug with homebridge-cbus: i'm pretty sure it fails if c-gate is not available at homebridge startup, regardless of when that is or where c-gate is running.

simonhac avatar Jul 06 '17 17:07 simonhac

But I spent so much time to make that timer :) That will be great.

DarylMc avatar Jul 06 '17 17:07 DarylMc

I believe it does not happen if homebridge is running and cgate service is restarted. Have tested that. Thanks so much for all the work you have done. When I go to make an image I'm not worried about anything other than my own inputs. I've been refining my homebridge spreadsheet instructions and I'm really happy with it. https://onedrive.live.com/?authkey=%21ANlD74Wg0RsHv98&id=142E343EE7CCA768%2119603&cid=142E343EE7CCA768

DarylMc avatar Jul 06 '17 17:07 DarylMc

great job @DarylMc -- i'll fix this issue when i'm next in coding mode.

simonhac avatar Jul 06 '17 17:07 simonhac

@simonhac just to mention i reduced the timer to 20 seconds so cgate was definitely started before homebridge but not fully synced it caused the problem for sure

DarylMc avatar Jul 07 '17 05:07 DarylMc

it's hard to say because i have done so many rebuilds lately (due to implementing more and more things as i could figure out how) but i don't remember this always being the case. eg im sure it was all working at reboot at some stage without the timer

DarylMc avatar Jul 07 '17 05:07 DarylMc