Merge branch 'feature-BOX一期' of http://47.99.132.106:10081/wulin/qiuguo-iot into feature-BOX一期

This commit is contained in:
wulin 2023-09-27 19:53:12 +08:00
commit 679532bcba

View File

@ -1,5 +1,6 @@
package com.qiuguo.iot.third.service;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity;
@ -14,6 +15,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@ -27,6 +29,7 @@ import reactor.core.publisher.Mono;
* @since 2023/9/19 15:13
*/
@Service
@Slf4j
public class TuyaDeviceService {
@Autowired
@ -124,6 +127,12 @@ public class TuyaDeviceService {
//
// }
//
public static void main(String[] args) {
int aa = 1000;
double a = 10 / (double) 1000;
System.out.println(a);
}
/**
* 控制设备
*/
@ -144,6 +153,12 @@ public class TuyaDeviceService {
tuyaResponse.setMessage("设备未开机");
return Mono.just(tuyaResponse);
}
JSONArray deviceStatus = tuyaDeviceConnector.getDeviceStatus(query.getDeviceId());
if (ObjectUtils.isEmpty(deviceStatus)) {
tuyaResponse.setMessage("设备信息异常");
return Mono.just(tuyaResponse);
}
List<JSONObject> javaList = deviceStatus.toJavaList(JSONObject.class);
JSONObject commands = new JSONObject();
JSONObject jsonObject = new JSONObject();
String type = data.getType();
@ -154,19 +169,37 @@ public class TuyaDeviceService {
jsonObject.put("value", Integer.parseInt(query.getValue()));
} else if (type.equalsIgnoreCase("Enum")) {
jsonObject.put("value", data.getValue());
} else {
jsonObject.put("value", data.getValue());
} else if (type.equalsIgnoreCase("auto_add_integer")) {
JSONObject code = javaList.stream()
.filter(statu -> data.getCode().equalsIgnoreCase(statu.getString("code"))).findFirst()
.orElse(new JSONObject());
double codeValue = code.getInteger("value");
int max = data.getMax();
double cut = max * 0.2;
double value = codeValue + cut;
int intValue = (int) value;
jsonObject.put("value", intValue >= max ? max - 1 : intValue);
} else if (type.equalsIgnoreCase("auto_reduce_integer")) {
JSONObject code = javaList.stream()
.filter(statu -> data.getCode().equalsIgnoreCase(statu.getString("code"))).findFirst()
.orElse(new JSONObject());
double codeValue = code.getInteger("value");
int min = data.getMin();
double cut = data.getMax() * 0.2;
double value = codeValue - cut;
int intValue = (int) value;
jsonObject.put("value", intValue <= min ? min + 1 : intValue);
}
commands.put("commands", Arrays.asList(jsonObject));
JSONArray deviceStatus = tuyaDeviceConnector.getDeviceStatus(query.getDeviceId());
if (!ObjectUtils.isEmpty(deviceStatus)) {
List<JSONObject> javaList = deviceStatus.toJavaList(JSONObject.class);
if (javaList.contains(jsonObject)) {
tuyaResponse.setMessage("已在当前状态");
tuyaResponse.setCode(200);
return Mono.just(tuyaResponse);
}
if (javaList.contains(jsonObject)) {
tuyaResponse.setMessage("已在当前状态");
tuyaResponse.setCode(200);
return Mono.just(tuyaResponse);
}
javaList.add(jsonObject);
List<JSONObject> collect = javaList.stream().filter(jl -> !ObjectUtils.isEmpty(jl.get("value")))
.filter(jl -> !jl.getString("value").startsWith("{"))
.filter(jl -> !jl.getString("value").startsWith("[")).collect(Collectors.toList());
commands.put("commands", collect);
Boolean result = tuyaDeviceConnector.controlDevice(query.getDeviceId(), commands);
if (result) {
tuyaResponse.setMessage("ok");
@ -175,7 +208,8 @@ public class TuyaDeviceService {
tuyaResponse.setMessage("请求失败");
}
return Mono.just(tuyaResponse);
}).onErrorResume(ex->{
}).onErrorResume(ex -> {
log.error("TuyaDeviceService is error:{}",ex);
tuyaResponse.setMessage("设备不支持此操作");
return Mono.just(tuyaResponse);
});