Frans-Willem / AqaraHub

AqaraHub is an open-source Zigbee hub for Xiaomi Aqara devices. It aims to be a replacement to the Xiaomi Gateway that does not require communication to outside servers, and uses a saner communication option (e.g. MQTT).
252 stars 34 forks source link

Initialization problem #31

Closed mclei-asw closed 6 years ago

mclei-asw commented 6 years ago

Thanks for the great project. It works for me in general and I think it is much better than all other JS crap. But I am facing one problem at daemon start. After reboot or ZigBee stick plug-in I always have to start the daemon two times, as the first start is not able to reset the device. I have to kill the process and on second try it is initialized properly.

This is log of the first start:

May 31 20:11:01 mqtt2 env[707]: [Main] Serial port: /dev/ttyACM0 May 31 20:11:01 mqtt2 env[707]: [Main] Setting up ZNP connection May 31 20:11:01 mqtt2 env[707]: [Main] Setting up MQTT connection May 31 20:11:01 mqtt2 env[707]: [MqttWrapper] Creating connection to localhost : 1883 May 31 20:11:01 mqtt2 env[707]: [Main] Using MQTT prefix 'AqaraHub/' May 31 20:11:01 mqtt2 env[707]: [Main] Using PSK xx xx xx xx xx xx xx xx 00 00 00 00 00 00 00 00 May 31 20:11:01 mqtt2 env[707]: IO Service starting May 31 20:11:01 mqtt2 env[707]: [Initialize] Doing initial reset, without clearing config or state May 31 20:11:01 mqtt2 env[707]: [FRAME] >> SREQ SAPI_WRITE_CONFIGURATION 03 01 00 May 31 20:11:01 mqtt2 env[707]: [MqttWrapper] ConnAckHandler: clean=false ..... May 31 20:11:59 mqtt2 env[707]: [FRAME] << AREQ SYS_RESET_IND 00 02 00 02 06 03

Frans-Willem commented 6 years ago

Hey, I'm running into this issue myself too. Basically the dongle isn't initialized fully when AqaraHub starts, so I should implement something like a time-out in the initialization phase and re-try if I can't set it up within a reasonable time.

Will look into this soon :)

Frans-Willem commented 6 years ago

@mclei-asw, I've removed the SAPI_WRITE_CONFIGURATION call at the start, as it's not really needed anyway. That means that the initial SYS_RESET_IND should be interpreted as a response to the initial (reset) command. I think that should work.

I've also added some more error handling for AqaraHub to exit if things go wrong, so SystemD can attempt to restart it.

Could you verify that this fixed your issue ?

mclei-asw commented 6 years ago

Thanks, it works for me on both x64 machine and Raspberry Pi. First initialization is about 40 seconds, but it works after that.

Frans-Willem commented 6 years ago

Alright! thanks for testing :+1:

The 40 second delay is due to the initialization time of the dongle, not much I can do about that, but at least it will boot up eventually.