The main function does almost same thing that the previous program does but now it also send a command to switch on the power switch.
All of those zway_cc_XXX functions take two call back functions. The first one will be called when the command processing was successful and the second call back will be called when the processing went wrong. I am sure what can make the execution fail but I do get the failed cases occasionally. Initially I was throwing an exception in the function, "callback< false >", but since I don't know why it fails, I replaced it with cerr. I think it fails when the ZWave signal is too weak.
The call-back function takes three arguments. The 3rd one is supposed to be whatever is given when the zway_cc_XXX functions are called. This call-back with a custom argument is a common patter with call-back style programming but it can go pretty wrong if muti-thread is envolved. If I give an object pointer as the argument, the object must be valid at the time the call-back function is called from the other side.
One more thing to be careful with the call-back functions is that when those call-back functions are called from zway system, the zway is locked for a ZDataHolder. It means that call-back functions should not try to acquire another lock on zway system. In other words, call-back function can do only limited things in the life time.
Also when the callback function is called, it is called from another thread. So be prepared for race condition.
The class WaitForSignal is to make sure that the main thread waits for any callback function to be called. If the main thread quit or the application end before the callback functions are called, sent command may not complete the transaction; I don't know exactly what's gonna happen if the command is aborted in the middle abruptly.