|
|
@ -23,32 +23,13 @@ void DeviceIOService::initialize(string uartpath, int baudrate) { |
|
|
|
if (!suc) { |
|
|
|
logger->error("modbusMaster initializeTtyChannel fail"); |
|
|
|
} |
|
|
|
thread.reset(new Thread("input_device_state_monitor", [this]() { inputStateMonitorLoop(); })); |
|
|
|
#if 0
|
|
|
|
idcard_read_thread.reset(new Thread("idcard_read", [this]() { |
|
|
|
ThisThread thisThread; |
|
|
|
int loopperiod = 50; |
|
|
|
while (!thisThread.getExitFlag()) { |
|
|
|
try { |
|
|
|
bool state; |
|
|
|
string info; |
|
|
|
|
|
|
|
idcardread(state, info); |
|
|
|
if (state) { |
|
|
|
logger->info("idcard read info:{}", info); |
|
|
|
onidcard(info); |
|
|
|
} |
|
|
|
loopperiod = 50; |
|
|
|
} catch (zexception& e) { |
|
|
|
logger->error("idcard read fail:{}", e.what()); |
|
|
|
loopperiod = 10000; |
|
|
|
} |
|
|
|
thisThread.sleepForMs(loopperiod); |
|
|
|
} |
|
|
|
})); |
|
|
|
thread.reset(new Thread("input_device_state_monitor", [this]() { inputStateMonitorLoop(); })); |
|
|
|
idcard_read_thread.reset(new Thread("idcard_read", [this]() {idcardreadLoop();})); |
|
|
|
#endif
|
|
|
|
} |
|
|
|
void DeviceIOService::relayControl(relay_device_type_t type, bool value) { |
|
|
|
void DeviceIOService::relayControl(uint32_t type, bool value) { |
|
|
|
/**
|
|
|
|
* @brief |
|
|
|
* 8(16Byte) | 继电器0 ->继电器15 掩码 |
|
|
@ -79,7 +60,7 @@ DeviceIOService::RelayDeviceState DeviceIOService::relayStateGet() { |
|
|
|
DeviceIOService::RelayDeviceState state = {}; |
|
|
|
Modbus03Rx modbus03rx; |
|
|
|
EXEC_MODBUS(modbusMaster->modbus03(ksubboarddeviceid, 10, 2, modbus03rx, kovertime)); |
|
|
|
uint32_t regstate = uint32_t(modbus03rx.getReg(2)) | uint32_t(modbus03rx.getReg(3) << 16); |
|
|
|
uint32_t regstate = uint32_t(modbus03rx.getReg(10)) | uint32_t(modbus03rx.getReg(11) << 16); |
|
|
|
state.state = regstate; |
|
|
|
return state; |
|
|
|
} |
|
|
@ -89,7 +70,6 @@ uint16_t DeviceIOService::getReg(int index) { |
|
|
|
EXEC_MODBUS(modbusMaster->modbus03(ksubboarddeviceid, index, 1, modbus03rx, kovertime)); |
|
|
|
return modbus03rx.getReg(index); |
|
|
|
} |
|
|
|
|
|
|
|
DeviceIOService::InputDeviceState DeviceIOService::getinputState() { |
|
|
|
#if 0
|
|
|
|
寄存器: |
|
|
@ -100,47 +80,14 @@ Addr | 功能 |
|
|
|
DeviceIOService::InputDeviceState inputDeviceState; |
|
|
|
Modbus03Rx modbus03rx; |
|
|
|
EXEC_MODBUS(modbusMaster->modbus03(ksubboarddeviceid, 12, 1, modbus03rx, kovertime)); |
|
|
|
uint32_t regstate = uint32_t(modbus03rx.getReg(4)) | uint32_t(modbus03rx.getReg(5) << 16); |
|
|
|
uint32_t regstate = uint32_t(modbus03rx.getReg(12)) | 0; |
|
|
|
inputDeviceState.state = regstate; |
|
|
|
return inputDeviceState; |
|
|
|
} |
|
|
|
|
|
|
|
void DeviceIOService::inputStateMonitorLoop() { |
|
|
|
ThisThread thisThread; |
|
|
|
int loopperiod = 50; |
|
|
|
while (!thisThread.getExitFlag()) { |
|
|
|
try { |
|
|
|
uint32_t newkeystate = getinputState().state; |
|
|
|
for (size_t i = 0; i < 32; i++) { |
|
|
|
bool state = (newkeystate & (1 << i)) != 0; |
|
|
|
bool oldstate = (keystate & (1 << i)) != 0; |
|
|
|
if (state != oldstate) { |
|
|
|
logger->info("on {} state change, state={}", input_device_type2str(input_device_type_t(i)), state); |
|
|
|
onInputStateChange((input_device_type_t)i, state); |
|
|
|
} |
|
|
|
} |
|
|
|
keystate = newkeystate; |
|
|
|
loopperiod = 50; |
|
|
|
} catch (const std::exception& e) { |
|
|
|
/**
|
|
|
|
* @brief 如果出现异常,那么就延时10秒,然后再次尝试 |
|
|
|
*/ |
|
|
|
logger->error("[:{}] catch exception, {}", __LINE__, e.what()); |
|
|
|
loopperiod = 10000; |
|
|
|
} catch (...) { |
|
|
|
logger->error("[:{}] catch exception, unknown error", __LINE__); |
|
|
|
loopperiod = 10000; |
|
|
|
} |
|
|
|
thisThread.sleepForMs(loopperiod); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
uint32_t DeviceIOService::getInterTemperature() { return getReg(16); } |
|
|
|
|
|
|
|
DeviceIOService::env_sensor_state_t DeviceIOService::getEnvSensorState() { |
|
|
|
Modbus03Rx modbus03rx; |
|
|
|
env_sensor_state_t env_sensor_state = {0}; |
|
|
|
EXEC_MODBUS(modbusMaster->modbus03(ksubboarddeviceid, 48, 11, modbus03rx, kovertime)); |
|
|
|
/**
|
|
|
|
48(16Byte) | 风速 |
|
|
|
49(16Byte) | 风向 |
|
|
@ -156,6 +103,7 @@ DeviceIOService::env_sensor_state_t DeviceIOService::getEnvSensorState() { |
|
|
|
59(16Byte) | light_intensity H |
|
|
|
60(16Byte) | light_intensity L |
|
|
|
*/ |
|
|
|
EXEC_MODBUS(modbusMaster->modbus03(ksubboarddeviceid, 48, 13, modbus03rx, kovertime)); |
|
|
|
env_sensor_state.wind_speed = modbus03rx.getReg(48); |
|
|
|
env_sensor_state.wind_direction = modbus03rx.getReg(49); |
|
|
|
env_sensor_state.temperature = modbus03rx.getReg(50); |
|
|
@ -199,16 +147,6 @@ void DeviceIOService::fanGetState(int id, float& power, uint16_t& error) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
float DeviceIOService::fanGetState(int id) { |
|
|
|
float power = 0; |
|
|
|
uint16_t error = 0; |
|
|
|
fanGetState(id, power, error); |
|
|
|
if (error != 0) { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
return power; |
|
|
|
} |
|
|
|
|
|
|
|
void DeviceIOService::fanSetState(int id, float power) { |
|
|
|
logger->debug("fanSetState, id={},power={}", id, power); |
|
|
|
int startindex = 0; |
|
|
@ -260,3 +198,69 @@ void DeviceIOService::idcardread(bool& state, string& info) { |
|
|
|
logger->debug("idcardread, state={},info={}", state, info); |
|
|
|
return; |
|
|
|
} |
|
|
|
/*************************************************************************************
|
|
|
|
* * |
|
|
|
* 非MODBUS直接调用接口 * |
|
|
|
* * |
|
|
|
*************************************************************************************/ |
|
|
|
float DeviceIOService::fanGetState(int id) { |
|
|
|
float power = 0; |
|
|
|
uint16_t error = 0; |
|
|
|
fanGetState(id, power, error); |
|
|
|
if (error != 0) { |
|
|
|
return -1; |
|
|
|
} |
|
|
|
return power; |
|
|
|
} |
|
|
|
|
|
|
|
void DeviceIOService::inputStateMonitorLoop() { |
|
|
|
ThisThread thisThread; |
|
|
|
int loopperiod = 50; |
|
|
|
while (!thisThread.getExitFlag()) { |
|
|
|
try { |
|
|
|
uint32_t newkeystate = getinputState().state; |
|
|
|
for (size_t i = 0; i < 32; i++) { |
|
|
|
bool state = (newkeystate & (1 << i)) != 0; |
|
|
|
bool oldstate = (keystate & (1 << i)) != 0; |
|
|
|
if (state != oldstate) { |
|
|
|
logger->info("on {} state change, state={}", input_device_type2str(input_device_type_t(i)), state); |
|
|
|
onInputStateChange((input_device_type_t)i, state); |
|
|
|
} |
|
|
|
} |
|
|
|
keystate = newkeystate; |
|
|
|
loopperiod = 50; |
|
|
|
} catch (const std::exception& e) { |
|
|
|
/**
|
|
|
|
* @brief 如果出现异常,那么就延时10秒,然后再次尝试 |
|
|
|
*/ |
|
|
|
logger->error("[:{}] catch exception, {}", __LINE__, e.what()); |
|
|
|
loopperiod = 10000; |
|
|
|
} catch (...) { |
|
|
|
logger->error("[:{}] catch exception, unknown error", __LINE__); |
|
|
|
loopperiod = 10000; |
|
|
|
} |
|
|
|
thisThread.sleepForMs(loopperiod); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void DeviceIOService::idcardreadLoop() { |
|
|
|
ThisThread thisThread; |
|
|
|
int loopperiod = 50; |
|
|
|
while (!thisThread.getExitFlag()) { |
|
|
|
try { |
|
|
|
bool state; |
|
|
|
string info; |
|
|
|
|
|
|
|
idcardread(state, info); |
|
|
|
if (state) { |
|
|
|
logger->info("idcard read info:{}", info); |
|
|
|
onidcard(info); |
|
|
|
} |
|
|
|
loopperiod = 50; |
|
|
|
} catch (zexception& e) { |
|
|
|
logger->error("idcard read fail:{}", e.what()); |
|
|
|
loopperiod = 10000; |
|
|
|
} |
|
|
|
thisThread.sleepForMs(loopperiod); |
|
|
|
} |
|
|
|
} |