Commit df813d4c authored by Carit Zhu's avatar Carit Zhu 🎱

Add offset update in redis for updateTDLOffset interface in TDLDeviceController.

parent 132b828f
Pipeline #419 passed with stage
in 0 seconds
...@@ -2,7 +2,6 @@ package com.example.tdl.mapper; ...@@ -2,7 +2,6 @@ package com.example.tdl.mapper;
import com.example.tdl.domain.vo.*; import com.example.tdl.domain.vo.*;
import com.example.tdl.entity.TDLDataOffset;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
......
package com.example.tdl.util; package com.example.tdl.util;
import com.example.tdl.domain.vo.AlarmRuleVo; import com.example.tdl.domain.vo.AlarmRuleVo;
import com.example.tdl.domain.vo.ResultAlarmVo;
import com.example.tdl.domain.vo.ResultBoschAlarmVo; import com.example.tdl.domain.vo.ResultBoschAlarmVo;
import com.example.tdl.entity.TDLDataOffset; import com.example.tdl.entity.TDLDataOffset;
import com.example.tdl.service.AlarmService;
import com.example.tdl.service.BoschAlarmService; import com.example.tdl.service.BoschAlarmService;
import com.example.tdl.service.redis.AlarmRedisService; import com.example.tdl.service.redis.AlarmRedisService;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class AlarmRule { public class AlarmRule {
private final static Logger logger = LoggerFactory.getLogger(AlarmRule.class);
private static Gson gson = new Gson(); private static Gson gson = new Gson();
...@@ -96,4 +99,38 @@ public class AlarmRule { ...@@ -96,4 +99,38 @@ public class AlarmRule {
alarmRedisService.hmSet(topic,"TDL-" +TDLSN+"_" +alias,gson.toJson(alarmRuleVos)); alarmRedisService.hmSet(topic,"TDL-" +TDLSN+"_" +alias,gson.toJson(alarmRuleVos));
} }
} }
public static void updateOffset(String topic, String TDLSN, String alias, Double offset,
AlarmRedisService alarmRedisService) {
try {
String hashKey = "TDL-" + TDLSN + "_" + alias;
/* Get alarm rule from redis */
Object hashValueObject = alarmRedisService.getHash(topic, hashKey);
List<AlarmRuleVo> alarmRuleList = gson.fromJson(hashValueObject.toString(),
new TypeToken<ArrayList<AlarmRuleVo>>() {}.getType());
/* Update offset */
if (alarmRuleList != null && alarmRuleList.size() > 0) {
alarmRuleList.get(0).setOffset(offset);
/* Write back to redis */
alarmRedisService.hmSet(topic, hashKey, gson.toJson(alarmRuleList));
}
} catch (JsonSyntaxException e) {
logger.error("Update offset(" + alias + ") JsonSyntaxException: " + e.getMessage());
}
}
public static void updateOffset(String topic, String TDLSN, TDLDataOffset offset,
AlarmRedisService alarmRedisService) {
if (offset == null)
return;
/* 更新温度校准值 */
if (offset.getTempOffset() != null) {
updateOffset(topic, TDLSN, "T", offset.getTempOffset(), alarmRedisService);
}
/* 更新湿度校准值 */
if (offset.getHumidityOffset() != null) {
updateOffset(topic, TDLSN, "h", offset.getHumidityOffset(), alarmRedisService);
}
}
} }
...@@ -7,7 +7,9 @@ import com.example.tdl.domain.dto.CommFeedback; ...@@ -7,7 +7,9 @@ import com.example.tdl.domain.dto.CommFeedback;
import com.example.tdl.domain.vo.*; import com.example.tdl.domain.vo.*;
import com.example.tdl.entity.TDLDataOffset; import com.example.tdl.entity.TDLDataOffset;
import com.example.tdl.service.TDLDeviceService; import com.example.tdl.service.TDLDeviceService;
import com.example.tdl.service.redis.AlarmRedisService;
import com.example.tdl.service.redis.TokenRedisService; import com.example.tdl.service.redis.TokenRedisService;
import com.example.tdl.util.AlarmRule;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
...@@ -20,7 +22,6 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -20,7 +22,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import retrofit2.http.HTTP;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -41,6 +42,8 @@ public class TDLDeviceController { ...@@ -41,6 +42,8 @@ public class TDLDeviceController {
@Autowired @Autowired
private TokenRedisService tokenRedisService; private TokenRedisService tokenRedisService;
@Autowired
private AlarmRedisService alarmRedisService;
@Autowired @Autowired
private I18nController i18n; private I18nController i18n;
...@@ -292,8 +295,18 @@ public class TDLDeviceController { ...@@ -292,8 +295,18 @@ public class TDLDeviceController {
String offset = gson.toJson(updateTDLOffsetVo.getOffset(), TDLDataOffset.class); String offset = gson.toJson(updateTDLOffsetVo.getOffset(), TDLDataOffset.class);
int a = tdlDeviceService.updateTDLOffset(updateTDLOffsetVo.getTdlSN(), offset); int a = tdlDeviceService.updateTDLOffset(updateTDLOffsetVo.getTdlSN(), offset);
if (a>=0){ if (a>=0){
/* 更新Redis里面的offset信息 */
ResultTDLDeviceVo resultTDLDeviceVo = tdlDeviceService.getByTDLSN(updateTDLOffsetVo.getTdlSN());
if (resultTDLDeviceVo.getGatewaySN() != null && resultTDLDeviceVo.getGatewayType() != null) {
String topic = "TDL/"+resultTDLDeviceVo.getGatewayType()+"/" +resultTDLDeviceVo.getGatewaySN()+"/Data";
AlarmRule.updateOffset(topic, updateTDLOffsetVo.getTdlSN(),
updateTDLOffsetVo.getOffset(), alarmRedisService);
fb.setCode(1); fb.setCode(1);
fb.setMessage(i18n.getMessage(request,"updateTDLOffsetSuccess")); fb.setMessage(i18n.getMessage(request,"updateTDLOffsetSuccess"));
} else {
fb.setCode(0);
fb.setMessage(i18n.getMessage(request,"TDLNotBoundGateway"));
}
}else{ }else{
fb.setCode(0); fb.setCode(0);
fb.setMessage(i18n.getMessage(request,"updateTDLOffsetFailure")); fb.setMessage(i18n.getMessage(request,"updateTDLOffsetFailure"));
......
...@@ -212,6 +212,7 @@ updateTDLCompanyFailure=Failed to allocate the sensor to the company ...@@ -212,6 +212,7 @@ updateTDLCompanyFailure=Failed to allocate the sensor to the company
wrongTDLOffset=Offset of sensor cannot be empty wrongTDLOffset=Offset of sensor cannot be empty
updateTDLOffsetSuccess=Update TDL offset successfully updateTDLOffsetSuccess=Update TDL offset successfully
updateTDLOffsetFailure=Update TDL offset failed updateTDLOffsetFailure=Update TDL offset failed
TDLNotBoundGateway=TDL not bound to gateway
warehouseName=Warehouse's name must not be null warehouseName=Warehouse's name must not be null
property=Warehouse's property must not be null property=Warehouse's property must not be null
wrongProperty=Please enter the correct property wrongProperty=Please enter the correct property
......
...@@ -240,6 +240,7 @@ updateTDLCompanyFailure=\u4F20\u611F\u5668\u5206\u914D\u516C\u53F8\u5931\u8D25 ...@@ -240,6 +240,7 @@ updateTDLCompanyFailure=\u4F20\u611F\u5668\u5206\u914D\u516C\u53F8\u5931\u8D25
wrongTDLOffset=\u4F20\u611F\u5668\u6821\u51C6\u503C\u4E0D\u80FD\u4E3A\u7A7A wrongTDLOffset=\u4F20\u611F\u5668\u6821\u51C6\u503C\u4E0D\u80FD\u4E3A\u7A7A
updateTDLOffsetSuccess=\u4F20\u611F\u5668\u4FEE\u6539\u6821\u51C6\u503C\u6210\u529F updateTDLOffsetSuccess=\u4F20\u611F\u5668\u4FEE\u6539\u6821\u51C6\u503C\u6210\u529F
updateTDLOffsetFailure=\u4F20\u611F\u5668\u4FEE\u6539\u6821\u51C6\u503C\u5931\u8D25 updateTDLOffsetFailure=\u4F20\u611F\u5668\u4FEE\u6539\u6821\u51C6\u503C\u5931\u8D25
TDLNotBoundGateway=\u4F20\u611F\u5668\u672A\u7ED1\u5B9A\u7F51\u5173
#\u4ED3\u5E93 #\u4ED3\u5E93
warehouseName = \u4ED3\u5E93\u540D\u4E0D\u80FD\u4E3A\u7A7A warehouseName = \u4ED3\u5E93\u540D\u4E0D\u80FD\u4E3A\u7A7A
property =\u4ED3\u5E93\u5C5E\u6027\u4E0D\u80FD\u4E3A\u7A7A property =\u4ED3\u5E93\u5C5E\u6027\u4E0D\u80FD\u4E3A\u7A7A
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment