等光控制

This commit is contained in:
weiyachao 2023-09-27 18:10:41 +08:00
parent bcecf37822
commit e50debd590

View File

@ -1,5 +1,6 @@
package com.qiuguo.iot.third.service; package com.qiuguo.iot.third.service;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity; import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity;
@ -14,6 +15,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@ -27,6 +29,7 @@ import reactor.core.publisher.Mono;
* @since 2023/9/19 15:13 * @since 2023/9/19 15:13
*/ */
@Service @Service
@Slf4j
public class TuyaDeviceService { public class TuyaDeviceService {
@Autowired @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("设备未开机"); tuyaResponse.setMessage("设备未开机");
return Mono.just(tuyaResponse); 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 commands = new JSONObject();
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
String type = data.getType(); String type = data.getType();
@ -154,19 +169,37 @@ public class TuyaDeviceService {
jsonObject.put("value", Integer.parseInt(query.getValue())); jsonObject.put("value", Integer.parseInt(query.getValue()));
} else if (type.equalsIgnoreCase("Enum")) { } else if (type.equalsIgnoreCase("Enum")) {
jsonObject.put("value", data.getValue()); jsonObject.put("value", data.getValue());
} else { } else if (type.equalsIgnoreCase("auto_add_integer")) {
jsonObject.put("value", data.getValue()); 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)); if (javaList.contains(jsonObject)) {
JSONArray deviceStatus = tuyaDeviceConnector.getDeviceStatus(query.getDeviceId()); tuyaResponse.setMessage("已在当前状态");
if (!ObjectUtils.isEmpty(deviceStatus)) { tuyaResponse.setCode(200);
List<JSONObject> javaList = deviceStatus.toJavaList(JSONObject.class); 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); Boolean result = tuyaDeviceConnector.controlDevice(query.getDeviceId(), commands);
if (result) { if (result) {
tuyaResponse.setMessage("ok"); tuyaResponse.setMessage("ok");
@ -175,7 +208,8 @@ public class TuyaDeviceService {
tuyaResponse.setMessage("请求失败"); tuyaResponse.setMessage("请求失败");
} }
return Mono.just(tuyaResponse); return Mono.just(tuyaResponse);
}).onErrorResume(ex->{ }).onErrorResume(ex -> {
log.error("TuyaDeviceService is error:{}",ex);
tuyaResponse.setMessage("设备不支持此操作"); tuyaResponse.setMessage("设备不支持此操作");
return Mono.just(tuyaResponse); return Mono.just(tuyaResponse);
}); });