重新实现关电控制

现在通过线程的方式来实现延迟关电,性能不是很好
PtzNew
Matthew 3 months ago
parent 3902d69997
commit c8610406ef

@ -23,10 +23,7 @@
#define IOT_PARAM_READ 0xAF #define IOT_PARAM_READ 0xAF
std::mutex GpioControl::m_locker; std::mutex GpioControl::m_locker;
CSemaphore GpioControl::m_semaphore;
std::vector<GpioControl::ITEM> GpioControl::m_items; std::vector<GpioControl::ITEM> GpioControl::m_items;
std::thread GpioControl::m_thread;
bool GpioControl::m_exitSignal = false;
bool GpioControl::m_cameraPowerStatus = false; bool GpioControl::m_cameraPowerStatus = false;
size_t GpioControl::turnOnImpl(const IOT_PARAM& param) size_t GpioControl::turnOnImpl(const IOT_PARAM& param)
@ -56,7 +53,7 @@ size_t GpioControl::turnOnImpl(const IOT_PARAM& param)
if (it == m_items.end()) if (it == m_items.end())
{ {
oldRef = 0; oldRef = 0;
ITEM item = {param.cmd, references, 0, 0, now}; ITEM item = {param.cmd, references, now};
m_items.push_back(item); m_items.push_back(item);
SetCamerastatus(param.cmd, true); SetCamerastatus(param.cmd, true);
} }
@ -231,14 +228,15 @@ size_t GpioControl::TurnOffImmediately(int cmd)
{ {
if (it->cmd == cmd) if (it->cmd == cmd)
{ {
ref = it->references; if (it->references > 0)
it->closeCmds++; {
it->closeTime = ts; it->references = 0;
setInt(it->cmd, 0);
}
break; break;
} }
} }
m_locker.unlock(); m_locker.unlock();
m_semaphore.release();
#ifdef _DEBUG #ifdef _DEBUG
ALOGI("PWR TurnOffNow cmd=%d ref=%u", cmd, (uint32_t)ref); ALOGI("PWR TurnOffNow cmd=%d ref=%u", cmd, (uint32_t)ref);
#endif #endif
@ -254,22 +252,34 @@ size_t GpioControl::TurnOff(int cmd, uint32_t delayedCloseTime/* = 0*/)
} }
size_t ref = 0; size_t ref = 0;
std::vector<ITEM>::iterator it; std::vector<ITEM>::iterator it;
if (delayedCloseTime > 0)
{
std::shared_ptr<PowerControl> powerCtrl = std::make_shared<PowerControl>(cmd);
std::thread th([delayedCloseTime, powerCtrl]() mutable {
std::this_thread::sleep_for(std::chrono::seconds(delayedCloseTime));
powerCtrl.reset();
});
th.detach();
}
m_locker.lock(); m_locker.lock();
for (it = m_items.begin(); it != m_items.end(); ++it) for (it = m_items.begin(); it != m_items.end(); ++it)
{ {
if (it->cmd == cmd) if (it->cmd == cmd)
{ {
ref = it->references; if (it->references > 0)
it->closeCmds++; {
if (ts > it->closeTime) it->references--;
if (it->references == 0)
{ {
it->closeTime = ts; setInt(it->cmd, 0);
}
} }
break; break;
} }
} }
m_locker.unlock(); m_locker.unlock();
m_semaphore.release();
#ifdef _DEBUG #ifdef _DEBUG
ALOGI("PWR TurnOff cmd=%d ref=%u", cmd, (uint32_t)ref); ALOGI("PWR TurnOff cmd=%d ref=%u", cmd, (uint32_t)ref);
#endif #endif
@ -285,6 +295,17 @@ size_t GpioControl::TurnOff(const std::vector<int>& cmds, uint32_t delayedCloseT
} }
std::vector<ITEM>::iterator it; std::vector<ITEM>::iterator it;
std::vector<int>::const_reverse_iterator itCmd; std::vector<int>::const_reverse_iterator itCmd;
if (delayedCloseTime > 0)
{
std::shared_ptr<PowerControl> powerCtrl = std::make_shared<PowerControl>(cmds);
std::thread th([delayedCloseTime, powerCtrl]() mutable {
std::this_thread::sleep_for(std::chrono::seconds(delayedCloseTime));
powerCtrl.reset();
});
th.detach();
}
m_locker.lock(); m_locker.lock();
// turnOnImpl(param); // turnOnImpl(param);
for (itCmd = cmds.crbegin(); itCmd != cmds.crend(); ++itCmd) for (itCmd = cmds.crbegin(); itCmd != cmds.crend(); ++itCmd)
@ -293,25 +314,39 @@ size_t GpioControl::TurnOff(const std::vector<int>& cmds, uint32_t delayedCloseT
{ {
if (it->cmd == *itCmd) if (it->cmd == *itCmd)
{ {
it->closeCmds++; if (it->references > 0)
if (ts > it->closeTime) {
it->references--;
if (it->references == 0)
{ {
it->closeTime = ts; setInt(it->cmd, 0);
}
} }
break; break;
} }
} }
} }
m_locker.unlock(); m_locker.unlock();
m_semaphore.release();
return 0; return 0;
} }
size_t GpioControl::TurnOff(const std::vector<std::pair<int, uint32_t> >& cmds) size_t GpioControl::TurnOff(const std::vector<std::pair<int, uint32_t> >& cmds)
{ {
time_t ts = time(NULL); for (auto itCmd = cmds.cbegin(); itCmd != cmds.end(); ++itCmd)
time_t ts2; {
if (itCmd->second > 0)
{
uint32_t delayedCloseTime = itCmd->second;
std::shared_ptr<PowerControl> powerCtrl = std::make_shared<PowerControl>(itCmd->first);
std::thread th([delayedCloseTime, powerCtrl]() mutable {
std::this_thread::sleep_for(std::chrono::seconds(delayedCloseTime));
powerCtrl.reset();
});
th.detach();
}
}
std::vector<ITEM>::iterator it; std::vector<ITEM>::iterator it;
std::vector<std::pair<int, uint32_t> >::const_iterator itCmd; std::vector<std::pair<int, uint32_t> >::const_iterator itCmd;
m_locker.lock(); m_locker.lock();
@ -321,14 +356,12 @@ size_t GpioControl::TurnOff(const std::vector<std::pair<int, uint32_t> >& cmds)
{ {
if (it->cmd == itCmd->first) if (it->cmd == itCmd->first)
{ {
if (it->references > 0)
it->closeCmds++;
if (itCmd->second != 0)
{ {
ts2 = itCmd->second + ts; it->references--;
if (ts2 > it->closeTime) if (it->references == 0)
{ {
it->closeTime = ts2; setInt(it->cmd, 0);
} }
} }
break; break;
@ -336,7 +369,6 @@ size_t GpioControl::TurnOff(const std::vector<std::pair<int, uint32_t> >& cmds)
} }
} }
m_locker.unlock(); m_locker.unlock();
m_semaphore.release();
return 0; return 0;
} }
@ -373,7 +405,7 @@ bool GpioControl::GetSelftestStatus(time_t wait_time)
std::vector<ITEM>::iterator it; std::vector<ITEM>::iterator it;
for (it = m_items.begin(); it != m_items.end(); ++it) for (it = m_items.begin(); it != m_items.end(); ++it)
{ {
if (it->cmd == cmd && it->openTime!=0 && (now - it->openTime >= wait_time)) if (it->cmd == cmd && it->references > 0 && it->openTime!=0 && (now - it->openTime >= wait_time))
{ {
return true;//自检完成 return true;//自检完成
} }
@ -395,128 +427,13 @@ time_t GpioControl::GetSelfTestRemain(time_t wait_time)
std::vector<ITEM>::iterator it; std::vector<ITEM>::iterator it;
for (it = m_items.begin(); it != m_items.end(); ++it) for (it = m_items.begin(); it != m_items.end(); ++it)
{ {
if (it->cmd == cmd && it->openTime!=0 && (now - it->openTime <= wait_time)) if (it->cmd == cmd && it->references > 0)
{ {
time_t remaintime =wait_time - (now - it->openTime); time_t remaintime = (now - it->openTime);
remaintime = (wait_time > remaintime) ? (wait_time - remaintime) : 0;
return remaintime;//自检完成 return remaintime;//自检完成
} }
} }
return 0; return 0;
} }
void GpioControl::PowerControlThreadProc()
{
time_t ts = 0;
std::vector<ITEM>::iterator it;
std::vector<int> items;
time_t minDelayTime = 0;
time_t delayTime = 0;
int fd = -1;
int res = -1;
m_cameraPowerStatus = 0;
while(1)
{
// Check if there is close cmd
ts = time(NULL);
minDelayTime = std::numeric_limits<time_t>::max();
m_locker.lock();
for (it = m_items.begin(); it != m_items.end(); ++it)
{
if (it->references == 0 && it->closeCmds == 0 && it->closeTime == 0)
{
#ifdef _DEBUG
ALOGI("PWR THREAD cmd=%d ref=%u closeCmds=%u", it->cmd, (uint32_t)it->references, (uint32_t)it->closeCmds);
#endif
continue;
}
if (it->closeCmds > 0)
{
if (it->references <= it->closeCmds)
{
it->references = 0;
}
else
{
it->references -= it->closeCmds;
if(it->references < 0)
it->references = 0;
}
it->closeCmds = 0;
}
if (it->references == 0)
{
// Should turn off the power
if ((it->closeTime == 0) || (it->closeTime <= ts))
{
// close it directly
setInt(it->cmd, 0);
it->closeTime = 0;
it->openTime = 0;
#ifdef _DEBUG
ALOGI("PWR THREAD DO TurnOff cmd=%d", it->cmd);
#endif
SetCamerastatus(it->cmd, false);
}
else
{
// Check Time
delayTime = ts - it->closeTime;
if (delayTime < minDelayTime)
{
minDelayTime = delayTime;
}
}
}
#ifdef _DEBUG
ALOGI("PWR THREAD cmd=%d ref=%u closeCmds=%u", it->cmd, (uint32_t)it->references, (uint32_t)it->closeCmds);
#endif
}
m_locker.unlock();
if (minDelayTime < std::numeric_limits<time_t>::max())
{
m_semaphore.try_acquire_for(std::chrono::seconds(1));
}
else
{
m_semaphore.acquire();
}
if (m_exitSignal)
{
break;
}
}
}
bool GpioControl::Startup()
{
if (m_thread.joinable())
{
return true;
}
// if (m_thread.)
m_exitSignal = false;
m_thread = std::thread(PowerControlThreadProc);
#ifdef _DEBUG
pthread_t nativeHandle = m_thread.native_handle();
pthread_setname_np(nativeHandle, "gpioclose");
#endif
return true;
}
void GpioControl::Stop()
{
// Notify
m_exitSignal = true;
m_semaphore.release();
m_thread.detach();
}

@ -160,17 +160,12 @@ public:
{ {
int cmd; int cmd;
size_t references; size_t references;
size_t closeCmds;
time_t closeTime;
time_t openTime; time_t openTime;
}; };
private: private:
static std::mutex m_locker; static std::mutex m_locker;
static CSemaphore m_semaphore;
static std::vector<ITEM> m_items; static std::vector<ITEM> m_items;
static bool m_exitSignal;
static std::thread m_thread;
static bool m_cameraPowerStatus; static bool m_cameraPowerStatus;
protected: protected:
@ -191,11 +186,6 @@ public:
static bool GetSelftestStatus(time_t wait_time); static bool GetSelftestStatus(time_t wait_time);
static time_t GetSelfTestRemain(time_t wait_time); static time_t GetSelfTestRemain(time_t wait_time);
static void PowerControlThreadProc();
static bool Startup();
static void Stop();
public: public:
static void setInt(int cmd, int value); static void setInt(int cmd, int value);
static int getInt(int cmd); static int getInt(int cmd);
@ -445,6 +435,12 @@ public:
m_cmds.resize(1, cmd1); m_cmds.resize(1, cmd1);
TurnOn(); TurnOn();
} }
PowerControl(const std::vector<int>& cmds) : m_delayCloseTime(0)
{
m_cmds = cmds;
TurnOn();
}
PowerControl(int cmd1, uint32_t closeDelayTime) : m_delayCloseTime(closeDelayTime) PowerControl(int cmd1, uint32_t closeDelayTime) : m_delayCloseTime(closeDelayTime)
{ {
m_cmds.resize(1, cmd1); m_cmds.resize(1, cmd1);
@ -521,7 +517,7 @@ public:
GpioControl::TurnOff(m_cmds, m_delayCloseTime); GpioControl::TurnOff(m_cmds, m_delayCloseTime);
#ifdef OUTPUT_DBG_INFO #ifdef OUTPUT_DBG_INFO
std::string status = GetStatus(); std::string status = GetStatus();
XYLOG(XYLOG_SEVERITY_INFO, "PWR After TurnOff %s", status.c_str()); XYLOG(XYLOG_SEVERITY_INFO, "PWR After TurnOff %s, DelayCloseTime=%u", status.c_str(), m_delayCloseTime);
#endif #endif
} }

@ -509,8 +509,6 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa
RegisterHandlerForSignal(SIGUSR2); RegisterHandlerForSignal(SIGUSR2);
GpioControl::Startup();
LoadNetworkInfo(); LoadNetworkInfo();
m_vm = vm; m_vm = vm;
@ -605,8 +603,6 @@ CPhoneDevice::~CPhoneDevice()
delete m_network; delete m_network;
m_network = NULL; m_network = NULL;
} }
GpioControl::Stop();
} }
void CPhoneDevice::SetListener(IListener* listener) void CPhoneDevice::SetListener(IListener* listener)

Loading…
Cancel
Save