Sending noop to TellStick every 5 minutes, attempting to reconnect if TellStick is missing. In Linux.

This commit is contained in:
Stefan Persson 2012-01-10 16:00:14 +01:00
parent dfae48ef33
commit 673daf471d
5 changed files with 53 additions and 3 deletions

View file

@ -114,6 +114,7 @@ std::wstring Socket::read(int timeout) {
void Socket::stopReadWait(){ void Socket::stopReadWait(){
TelldusCore::MutexLocker locker(&d->mutex); TelldusCore::MutexLocker locker(&d->mutex);
d->connected = false; d->connected = false;
//TODO somehow signal the socket here?
} }
void Socket::write(const std::wstring &msg) { void Socket::write(const std::wstring &msg) {

View file

@ -3,6 +3,7 @@
#include "Mutex.h" #include "Mutex.h"
#include "TellStick.h" #include "TellStick.h"
#include "Log.h" #include "Log.h"
#include "../client/telldus-core.h"
#include <map> #include <map>
#include <stdio.h> #include <stdio.h>
@ -99,7 +100,7 @@ void ControllerManager::loadControllers() {
bool found = false; bool found = false;
ControllerMap::const_iterator cit = d->controllers.begin(); ControllerMap::const_iterator cit = d->controllers.begin();
for(; cit != d->controllers.end(); ++cit) { for(; cit != d->controllers.end(); ++cit) {
Log::notice("Something in second the loop"); Log::notice("Something in the second loop");
TellStick *tellstick = reinterpret_cast<TellStick*>(cit->second); TellStick *tellstick = reinterpret_cast<TellStick*>(cit->second);
if (!tellstick) { if (!tellstick) {
Log::notice("No tellstick"); Log::notice("No tellstick");
@ -128,13 +129,51 @@ void ControllerManager::loadControllers() {
} }
} }
void ControllerManager::queryControllerStatus(){
std::list<TellStick *> tellStickControllers;
{
TelldusCore::MutexLocker locker(&d->mutex);
for(ControllerMap::iterator it = d->controllers.begin(); it != d->controllers.end(); ++it) {
Log::notice("found a controller");
TellStick *tellstick = reinterpret_cast<TellStick*>(it->second);
if (tellstick) {
Log::notice("found a tellstick");
tellStickControllers.push_back(tellstick);
}
}
}
bool reloadControllers = false;
std::string noop = "noop";
for(std::list<TellStick *>::iterator it = tellStickControllers.begin(); it != tellStickControllers.end(); ++it) {
int success = (*it)->send(noop);
if(success == TELLSTICK_ERROR_COMMUNICATION){
Log::warning("TellStick query: Error in communication with TellStick, resetting USB");
resetController(*it);
Log::notice("has reset");
}
if(success == TELLSTICK_ERROR_COMMUNICATION || success == TELLSTICK_ERROR_NOT_FOUND){
reloadControllers = true;
Log::notice("Set reload");
}
}
if(!tellStickControllers.size() || reloadControllers){
//no tellstick at all found, or controller was reset
Log::warning("TellStick query: Rescanning USB ports");
loadControllers();
}
}
int ControllerManager::resetController(Controller *controller) { int ControllerManager::resetController(Controller *controller) {
TellStick *tellstick = reinterpret_cast<TellStick*>(controller); TellStick *tellstick = reinterpret_cast<TellStick*>(controller);
if (!tellstick) { if (!tellstick) {
return true; //not tellstick, nothing to reset at the moment, just return true return true; //not tellstick, nothing to reset at the moment, just return true
} }
Log::notice("resettingController"); Log::notice("resettingController");
int success = controller->reset(); //ehh, här är väl controllern borttagen förresten? int success = controller->reset();
Log::notice("Remove device"); Log::notice("Remove device");
deviceInsertedOrRemoved(tellstick->vid(), tellstick->pid(), tellstick->serial(), false); //remove from list and delete deviceInsertedOrRemoved(tellstick->vid(), tellstick->pid(), tellstick->serial(), false); //remove from list and delete
Log::notice("Device removed"); Log::notice("Device removed");

View file

@ -15,6 +15,7 @@ public:
Controller *getBestControllerById(int id); Controller *getBestControllerById(int id);
void loadControllers(); void loadControllers();
void queryControllerStatus();
int resetController(Controller *controller); int resetController(Controller *controller);
private: private:

View file

@ -215,6 +215,14 @@ int TellStick::send( const std::string &strMessage ) {
} }
delete[] tempMessage; delete[] tempMessage;
if(strMessage == "noop"){
if(c){
return TELLSTICK_SUCCESS;
}
else{
return TELLSTICK_ERROR_COMMUNICATION;
}
}
int retrycnt = 500; int retrycnt = 500;
unsigned char in; unsigned char in;

View file

@ -43,7 +43,7 @@ void TelldusMain::start(void) {
EventRef dataEvent = d->eventHandler.addEvent(); EventRef dataEvent = d->eventHandler.addEvent();
EventRef janitor = d->eventHandler.addEvent(); //Used for regular cleanups EventRef janitor = d->eventHandler.addEvent(); //Used for regular cleanups
Timer supervisor(janitor); //Tells the janitor to go back to work Timer supervisor(janitor); //Tells the janitor to go back to work
supervisor.setInterval(60*5); //Every 5 minutes supervisor.setInterval(10); //Every 5 minutes TODO how often? 60*5
supervisor.start(); supervisor.start();
ControllerManager controllerManager(dataEvent.get()); ControllerManager controllerManager(dataEvent.get());
@ -113,6 +113,7 @@ void TelldusMain::start(void) {
janitor->popSignal(); janitor->popSignal();
} }
Log::debug("Do Janitor cleanup"); Log::debug("Do Janitor cleanup");
controllerManager.queryControllerStatus();
} }
} }