|
|
@ -2,10 +2,12 @@ package a8k.app.service.background; |
|
|
|
|
|
|
|
|
|
|
|
import a8k.OS; |
|
|
|
import a8k.app.a8ktype.error.AppError; |
|
|
|
import a8k.app.a8ktype.exception.AppException; |
|
|
|
import a8k.app.service.bases.AppEventBusService; |
|
|
|
import a8k.app.service.setting.AppSettingsMgrService; |
|
|
|
import a8k.app.hardware.driver.TemperatureControlDriver; |
|
|
|
import a8k.teststate.VirtualDevice; |
|
|
|
import a8k.app.a8ktype.exception.AppException; |
|
|
|
import jakarta.annotation.PostConstruct; |
|
|
|
import jakarta.annotation.Resource; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -15,130 +17,202 @@ import org.springframework.stereotype.Component; |
|
|
|
@Slf4j |
|
|
|
public class TemperatureCtrlService { |
|
|
|
|
|
|
|
enum TemperaControlMode { |
|
|
|
kFixTemperature, |
|
|
|
kAutoChangeFromAppSetting |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
TemperatureControlDriver temperatureControlDriver; |
|
|
|
@Resource |
|
|
|
VirtualDevice virtualDevice; |
|
|
|
|
|
|
|
@Resource |
|
|
|
AppSettingsMgrService appSettingsMgrService; |
|
|
|
|
|
|
|
Integer targetTemperature = 0; |
|
|
|
TemperaControlMode controlMode = TemperaControlMode.kFixTemperature; |
|
|
|
Thread temperatureCtrlThread = null; |
|
|
|
Boolean workingFlag = false; |
|
|
|
|
|
|
|
AppSettingsMgrService appSettingsMgrService; |
|
|
|
@Resource |
|
|
|
AppEventBusService eventBus; |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
// eventBus.regListener(this::onAppEvent); |
|
|
|
} |
|
|
|
|
|
|
|
synchronized public Integer getTargetTemperature() { |
|
|
|
return targetTemperature; |
|
|
|
} |
|
|
|
Integer targetTemperatureSetVal = null; |
|
|
|
Integer targetTemperature = 0; |
|
|
|
Boolean workingFlag = false; |
|
|
|
|
|
|
|
/** |
|
|
|
* 控制温度,当APP配置发生变化时,自动调整温度 |
|
|
|
* @throws AppException 温度控制异常 |
|
|
|
*/ |
|
|
|
synchronized public void startTemperatureControl() throws AppException { |
|
|
|
stopTemperatureControl(); |
|
|
|
controlMode = TemperaControlMode.kAutoChangeFromAppSetting; |
|
|
|
targetTemperature = 0; |
|
|
|
startTemperatureControlInternal(); |
|
|
|
} |
|
|
|
Thread temperatureCtrlThread = null; |
|
|
|
|
|
|
|
/** |
|
|
|
* 控制温度,根据设定值控制温度 |
|
|
|
* @param tem 设定温度 |
|
|
|
* @throws AppException 温度控制异常 |
|
|
|
*/ |
|
|
|
synchronized public void startFixTemperatureControl(Integer tem) throws AppException { |
|
|
|
stopTemperatureControl(); |
|
|
|
controlMode = TemperaControlMode.kFixTemperature; |
|
|
|
targetTemperature = tem; |
|
|
|
startTemperatureControlInternal(); |
|
|
|
} |
|
|
|
AppError error = null; |
|
|
|
|
|
|
|
synchronized public boolean isTemperatureControlRunning() { |
|
|
|
return temperatureCtrlThread != null; |
|
|
|
} |
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
// eventBus.regListener(this::onAppEvent); |
|
|
|
|
|
|
|
synchronized public void startTemperatureControlInternal() throws AppException { |
|
|
|
if (virtualDevice.isEnable()) { |
|
|
|
return; |
|
|
|
} |
|
|
|
temperatureCtrlThread = new Thread(() -> { |
|
|
|
OS.forceSleep(3000); //等待webSocket连接 |
|
|
|
|
|
|
|
if (temperatureCtrlThread != null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
while (true) { |
|
|
|
if (virtualDevice.isEnable()) { |
|
|
|
OS.forceSleep(1000); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
if (controlMode.equals(TemperaControlMode.kAutoChangeFromAppSetting)) { |
|
|
|
var setting = appSettingsMgrService.getDeviceSetting(); |
|
|
|
targetTemperature = setting.getTemperature(); |
|
|
|
} |
|
|
|
temperatureControlDriver.startCtrlTemperature(targetTemperature.doubleValue()); |
|
|
|
|
|
|
|
/* |
|
|
|
* 启动线程,定时检查温度设定值,如果发生变化则调用温度控制器 |
|
|
|
*/ |
|
|
|
workingFlag = true; |
|
|
|
temperatureCtrlThread = new Thread(() -> { |
|
|
|
log.info("Temperature thread run start"); |
|
|
|
while (workingFlag) { |
|
|
|
try { |
|
|
|
|
|
|
|
OS.forceSleep(1000); |
|
|
|
if (controlMode.equals(TemperaControlMode.kAutoChangeFromAppSetting)) { |
|
|
|
Integer settingTem = appSettingsMgrService.getDeviceSetting().getTemperature(); |
|
|
|
if (!targetTemperature.equals(settingTem)) { |
|
|
|
targetTemperature = settingTem; |
|
|
|
temperatureControlDriver.startCtrlTemperature(targetTemperature.doubleValue()); |
|
|
|
} |
|
|
|
Integer targetSetVal = getTargetTemperatureSetVal(); |
|
|
|
if (isTargetTemperatureChanged(targetSetVal)) { |
|
|
|
startCtrlTemperature(targetSetVal); |
|
|
|
} |
|
|
|
|
|
|
|
clearAppError(); |
|
|
|
} catch (AppException e) { |
|
|
|
setAppError(e); |
|
|
|
log.error("Temperature control error: {}", e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
temperatureControlDriver.stopCtrlTemperature(); |
|
|
|
} catch (AppException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
|
|
|
|
log.info("Temperature thread run end"); |
|
|
|
}); |
|
|
|
temperatureCtrlThread.start(); |
|
|
|
temperatureCtrlThread.setName("TemperatureCtrlThread"); |
|
|
|
} |
|
|
|
|
|
|
|
synchronized public void stopTemperatureControl() throws AppException { |
|
|
|
if (virtualDevice.isEnable()) { |
|
|
|
return; |
|
|
|
|
|
|
|
synchronized public AppError getError() { |
|
|
|
return error; |
|
|
|
} |
|
|
|
|
|
|
|
synchronized public void setTargetTemperature(Integer temperature) { |
|
|
|
targetTemperatureSetVal = temperature; |
|
|
|
} |
|
|
|
|
|
|
|
synchronized public void clearTargetTemperature() { |
|
|
|
targetTemperatureSetVal = null; |
|
|
|
} |
|
|
|
synchronized public Integer getTargetTemperature() { |
|
|
|
return targetTemperature; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Boolean isTargetTemperatureChanged(Integer newTargetTemperature) { |
|
|
|
return !newTargetTemperature.equals(targetTemperature); |
|
|
|
} |
|
|
|
|
|
|
|
private Integer getTargetTemperatureSetVal() { |
|
|
|
if (targetTemperatureSetVal != null) { |
|
|
|
return targetTemperatureSetVal; |
|
|
|
} |
|
|
|
return appSettingsMgrService.getDeviceSetting().getTemperature(); |
|
|
|
} |
|
|
|
|
|
|
|
if (temperatureCtrlThread != null) { |
|
|
|
temperatureCtrlThread.interrupt(); |
|
|
|
workingFlag = false; |
|
|
|
try { |
|
|
|
temperatureCtrlThread.join(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
temperatureCtrlThread = null; |
|
|
|
private void startCtrlTemperature(Integer temperature) throws AppException { |
|
|
|
temperatureControlDriver.startCtrlTemperature(temperature.doubleValue()); |
|
|
|
targetTemperature = temperature; |
|
|
|
workingFlag = true; |
|
|
|
} |
|
|
|
|
|
|
|
synchronized private void setAppError(AppException e) { |
|
|
|
if (error == null) { |
|
|
|
eventBus.pushAppExceptionEvent(e); |
|
|
|
} |
|
|
|
log.info("Temperature control stopped"); |
|
|
|
error = e.getError(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
synchronized private void clearAppError() { |
|
|
|
error = null; |
|
|
|
} |
|
|
|
|
|
|
|
// |
|
|
|
// synchronized public Integer getTargetTemperature() { |
|
|
|
// return targetTemperature; |
|
|
|
// } |
|
|
|
// |
|
|
|
// /** |
|
|
|
// * 控制温度,当APP配置发生变化时,自动调整温度 |
|
|
|
// * @throws AppException 温度控制异常 |
|
|
|
// */ |
|
|
|
// synchronized public void startTemperatureControl() throws AppException { |
|
|
|
// stopTemperatureControl(); |
|
|
|
// targetTemperature = 0; |
|
|
|
// startTemperatureControlInternal(); |
|
|
|
// } |
|
|
|
// |
|
|
|
// /** |
|
|
|
// * 控制温度,根据设定值控制温度 |
|
|
|
// * @param tem 设定温度 |
|
|
|
// * @throws AppException 温度控制异常 |
|
|
|
// */ |
|
|
|
// synchronized public void startFixTemperatureControl(Integer tem) throws AppException { |
|
|
|
// stopTemperatureControl(); |
|
|
|
// targetTemperature = tem; |
|
|
|
// startTemperatureControlInternal(); |
|
|
|
// } |
|
|
|
// |
|
|
|
// synchronized public boolean isTemperatureControlRunning() { |
|
|
|
// return temperatureCtrlThread != null; |
|
|
|
// } |
|
|
|
// |
|
|
|
// |
|
|
|
// synchronized public void startTemperatureControlInternal() throws AppException { |
|
|
|
// if (virtualDevice.isEnable()) { |
|
|
|
// return; |
|
|
|
// } |
|
|
|
// |
|
|
|
// if (temperatureCtrlThread != null) { |
|
|
|
// return; |
|
|
|
// } |
|
|
|
// |
|
|
|
// if (controlMode.equals(TemperaControlMode.kAutoChangeFromAppSetting)) { |
|
|
|
// var setting = appSettingsMgrService.getDeviceSetting(); |
|
|
|
// targetTemperature = setting.getTemperature(); |
|
|
|
// } |
|
|
|
// |
|
|
|
// |
|
|
|
// /* |
|
|
|
// * 启动线程,定时检查温度设定值,如果发生变化则调用温度控制器 |
|
|
|
// */ |
|
|
|
// workingFlag = true; |
|
|
|
// temperatureCtrlThread = new Thread(() -> { |
|
|
|
// log.info("Temperature thread run start"); |
|
|
|
// |
|
|
|
// temperatureControlDriver.startCtrlTemperature(targetTemperature.doubleValue()); |
|
|
|
// |
|
|
|
// while (workingFlag) { |
|
|
|
// try { |
|
|
|
// |
|
|
|
// OS.forceSleep(1000); |
|
|
|
// if (controlMode.equals(TemperaControlMode.kAutoChangeFromAppSetting)) { |
|
|
|
// Integer settingTem = appSettingsMgrService.getDeviceSetting().getTemperature(); |
|
|
|
// if (!targetTemperature.equals(settingTem)) { |
|
|
|
// targetTemperature = settingTem; |
|
|
|
// temperatureControlDriver.startCtrlTemperature(targetTemperature.doubleValue()); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// } catch (AppException e) { |
|
|
|
// log.error("Temperature control error: {}", e.getMessage()); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// try { |
|
|
|
// temperatureControlDriver.stopCtrlTemperature(); |
|
|
|
// } catch (AppException e) { |
|
|
|
// throw new RuntimeException(e); |
|
|
|
// } |
|
|
|
// |
|
|
|
// log.info("Temperature thread run end"); |
|
|
|
// }); |
|
|
|
// temperatureCtrlThread.start(); |
|
|
|
// temperatureCtrlThread.setName("TemperatureCtrlThread"); |
|
|
|
// } |
|
|
|
// |
|
|
|
// synchronized public void stopTemperatureControl() throws AppException { |
|
|
|
// if (virtualDevice.isEnable()) { |
|
|
|
// return; |
|
|
|
// } |
|
|
|
// |
|
|
|
// if (temperatureCtrlThread != null) { |
|
|
|
// temperatureCtrlThread.interrupt(); |
|
|
|
// workingFlag = false; |
|
|
|
// try { |
|
|
|
// temperatureCtrlThread.join(); |
|
|
|
// } catch (InterruptedException e) { |
|
|
|
// throw new RuntimeException(e); |
|
|
|
// } |
|
|
|
// temperatureCtrlThread = null; |
|
|
|
// } |
|
|
|
// log.info("Temperature control stopped"); |
|
|
|
// |
|
|
|
// } |
|
|
|
|
|
|
|
} |