修改响应

This commit is contained in:
weiyachao 2023-09-26 20:07:52 +08:00
parent e22724b07f
commit 4808589cad
9 changed files with 200 additions and 24 deletions

View File

@ -5,6 +5,7 @@ package com.qiuguo.iot.data.service.user;
import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity;
import com.qiuguo.iot.data.request.user.UserHandlingDeviceRequest;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery;
@ -14,6 +15,8 @@ import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
@ -159,6 +162,13 @@ public class UserHandlingDeviceService extends GenericReactiveCrudService<UserHa
.and("id", id)
.fetchOne();
}
public Flux<UserHandlingDeviceEntity> selectByIds(List<Long> ids){
if (ObjectUtils.isEmpty(ids)) {
return Flux.empty();
}
return createQuery().and("is_delete", 0).in("id", ids).fetch();
}

View File

@ -0,0 +1,43 @@
package com.qiuguo.iot.third.enums;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
/**
* XXX
*
* @author weiyachao
* @since 2023/9/26 17:30
*/
@Getter
@ToString
@AllArgsConstructor
public enum TuyaCategoryEnum {
DJ("dj", "光源", "switch_led"),
SD("sd", "扫地机器人", "power"),
;
private final String code;
private final String name;
private final String switchColumn;
public static String getColumn(String code) {
TuyaCategoryEnum[] values = values();
for (TuyaCategoryEnum value : values) {
if (Objects.equals(value.code, code)) {
return value.switchColumn;
}
}
return null;
}
}

View File

@ -0,0 +1,19 @@
package com.qiuguo.iot.third.resp;
import java.io.Serializable;
import lombok.Data;
/**
* XXX
*
* @author weiyachao
* @since 2023/9/26 17:19
*/
@Data
public class TuyaResponse implements Serializable {
private Integer code;
private String message;
}

View File

@ -19,6 +19,15 @@ import java.util.List;
*/
public interface TuyaDeviceConnector {
// 获取品类所支持的指令集
@GET("/v1.0/iot-03/categories/{category}/functions")
JSONArray getCategoryFunctions(@Path("category") String category);
// 获取设备状态
@GET("/v1.0/iot-03/devices/{device_id}/status")
JSONArray getDeviceStatus(@Path("device_id") String deviceId);
// 获取所有分类
@GET("/v1.0/iot-03/device-categories")
JSONArray categories();

View File

@ -4,12 +4,20 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity;
import com.qiuguo.iot.data.service.user.UserHandlingDeviceService;
import com.qiuguo.iot.third.enums.TuyaCategoryEnum;
import com.qiuguo.iot.third.query.TuyaQuery;
import com.qiuguo.iot.third.resp.TuyaResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
@ -48,21 +56,93 @@ public class TuyaDeviceService {
return Mono.just(byid.getJSONObject(0).getString("category"));
}
/**
* 获取设备状态
*/
public Mono<JSONArray> getDeviceStatus(String deviceId) {
return Mono.just(tuyaDeviceConnector.getDeviceStatus(deviceId));
}
// /**
// * 设备控制
// */
// public Mono<TuyaResponse> controlDevice(List<TuyaQuery> list) {
// TuyaResponse tuyaResponse = new TuyaResponse();
// tuyaResponse.setCode(500);
// if (ObjectUtils.isEmpty(list)) {
// tuyaResponse.setMessage("参数为空");
// return Mono.just(tuyaResponse);
// }
// Map<Long, TuyaQuery> queryMap = list.stream()
// .collect(Collectors.toMap(TuyaQuery::getUserHandlingDeviceId, Function.identity()));
// Flux<UserHandlingDeviceEntity> selectByIds = userHandlingDeviceService.selectByIds(
// list.stream().map(TuyaQuery::getUserHandlingDeviceId).collect(Collectors.toList()));
//
// return selectByIds.flatMapSequential(entity -> {
// try {
// TuyaQuery query = queryMap.get(entity.getId());
// JSONArray jsonArray = tuyaDeviceConnector.getByid(query.getDeviceId());
// if (ObjectUtils.isEmpty(jsonArray)) {
// tuyaResponse.setMessage("查询不到设备");
// return Mono.just(tuyaResponse);
// }
// JSONObject deviceInfo = jsonArray.getJSONObject(0);
// if (Objects.equals(deviceInfo.getBoolean(TuyaCategoryEnum.getColumn(entity.getCategoryCode())), false)
// && !Objects.equals(entity.getMatchingFields(), "")) {
// tuyaResponse.setMessage("设备未开机");
// return Mono.just(tuyaResponse);
// }
// JSONObject commands = new JSONObject();
// List<JSONObject> queryList = new ArrayList<>();
// JSONObject jsonObject = new JSONObject();
// String type = entity.getType();
// jsonObject.put("code", entity.getCode());
// if (type.equalsIgnoreCase("boolean")) {
// jsonObject.put("value", Boolean.parseBoolean(entity.getValue()));
// } else if (type.equalsIgnoreCase("Integer")) {
// int value = Integer.parseInt(entity.getValue());
// jsonObject.put("value", value <= entity.getMin() ? entity.getMin()
// : value >= entity.getMax() ? entity.getMax() : value);
// } else if (type.equalsIgnoreCase("Enum")) {
// jsonObject.put("value", entity.getValue());
// } else {
// jsonObject.put("value", entity.getValue());
// }
// queryList.add(jsonObject);
// commands.put("commands", queryList);
// return Mono.just(true);
// } catch (Exception ex) {
// tuyaResponse.setMessage("网络异常");
// return Mono.just(tuyaResponse);
// }
// }).onErrorResume(throwable -> {
// // 处理异常情况返回 Mono.just(false)
// return Mono.just(false);
// });
//
//
//
// }
//
/**
* 控制设备
*/
public Mono<Boolean> controlDevice(TuyaQuery query) {
public Mono<TuyaResponse> controlDevice(TuyaQuery query) {
Mono<UserHandlingDeviceEntity> userHandlingDeviceEntityMono = userHandlingDeviceService.selectUserHandlingDeviceById(
query.getUserHandlingDeviceId());
TuyaResponse tuyaResponse = new TuyaResponse();
tuyaResponse.setCode(500);
return userHandlingDeviceEntityMono.flatMap(data -> {
JSONArray jsonArray = tuyaDeviceConnector.getByid(query.getDeviceId());
if (ObjectUtils.isEmpty(jsonArray)) {
return Mono.just(false);
tuyaResponse.setMessage("查询不到设备");
return Mono.just(tuyaResponse);
}
JSONObject deviceInfo = jsonArray.getJSONObject(0);
if (Objects.equals(deviceInfo.getBoolean("is_online"), false) && !Objects.equals(data.getMatchingFields(),
"")) {
return Mono.just(false);
if (Objects.equals(deviceInfo.getBoolean(TuyaCategoryEnum.getColumn(data.getCategoryCode())), false)
&& !Objects.equals(data.getMatchingFields(), "")) {
tuyaResponse.setMessage("设备未开机");
return Mono.just(tuyaResponse);
}
JSONObject commands = new JSONObject();
JSONObject jsonObject = new JSONObject();
@ -71,15 +151,29 @@ public class TuyaDeviceService {
if (type.equalsIgnoreCase("boolean")) {
jsonObject.put("value", Boolean.parseBoolean(data.getValue()));
} else if (type.equalsIgnoreCase("Integer")) {
jsonObject.put("value", Integer.parseInt(data.getValue()));
jsonObject.put("value", Integer.parseInt(query.getValue()));
} else if (type.equalsIgnoreCase("Enum")) {
jsonObject.put("value", data.getValue());
} else {
jsonObject.put("value", data.getValue());
}
commands.put("commands", Arrays.asList(jsonObject));
Boolean aBoolean = tuyaDeviceConnector.controlDevice(query.getDeviceId(), commands);
return Mono.just(aBoolean);
JSONArray deviceStatus = tuyaDeviceConnector.getDeviceStatus(query.getDeviceId());
if (!ObjectUtils.isEmpty(deviceStatus)) {
List<JSONObject> javaList = deviceStatus.toJavaList(JSONObject.class);
if (javaList.contains(jsonObject)) {
tuyaResponse.setMessage("已在当前状态");
return Mono.just(tuyaResponse);
}
}
Boolean result = tuyaDeviceConnector.controlDevice(query.getDeviceId(), commands);
if (result) {
tuyaResponse.setMessage("ok");
tuyaResponse.setCode(200);
} else {
tuyaResponse.setMessage("请求失败");
}
return Mono.just(tuyaResponse);
});
}
}

View File

@ -28,11 +28,7 @@
<artifactId>iot-third</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.qiuguo.iot</groupId>
<artifactId>iot-rabbit</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -2,6 +2,7 @@ package com.qiuguo.iot.user.api.controller.tuya;
import com.alibaba.fastjson.JSONObject;
import com.qiuguo.iot.third.query.TuyaQuery;
import com.qiuguo.iot.third.resp.TuyaResponse;
import com.qiuguo.iot.third.service.TuyaDeviceConnector;
import com.qiuguo.iot.third.service.TuyaDeviceService;
import java.util.Arrays;
@ -26,8 +27,7 @@ public class TuyaDeviceController {
private TuyaDeviceService tuyaDeviceService;
@GetMapping("/set")
public Mono<Boolean> set(TuyaQuery query) {
public Mono<TuyaResponse> set(TuyaQuery query) {
return tuyaDeviceService.controlDevice(query);
}

View File

@ -18,17 +18,22 @@ import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = IotBoxUserApiApplication.class)
@Slf4j
public class UserTest {
public String deviceId = "6c41ec425b5af5b81bxgas";
//spring.profiles.active=dev
public String deviceId = "6c29b49edea00eb541rg3n";
public String spaceId = "163208675";
@Autowired
private TuyaDeviceConnector tuyaDeviceConnector;
@Test
public void 获取设备状态() {
}
@Test
public void 查询所有分类() {
String key = "dj";
String key = "sd";
JSONArray aa = tuyaDeviceConnector.categories();
for (Object o : aa) {
JSONObject jsonObject = (JSONObject) o;
@ -88,15 +93,15 @@ public class UserTest {
public void 控制设备动作() {
JSONObject commands = new JSONObject();
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "switch_led");
jsonObject.put("value", false);
jsonObject.put("code", "power");
jsonObject.put("value", true);
JSONObject js2 = new JSONObject();
js2.put("code", "work_mode");
js2.put("value", "colour");
js2.put("code", "power");
js2.put("value", true);
JSONObject js3 = new JSONObject();
js3.put("code", "bright_value_v2");
js3.put("value", 10);
commands.put("commands", Arrays.asList( jsonObject));
commands.put("commands", Arrays.asList( js2));
Object controlDevice = tuyaDeviceConnector.controlDevice(deviceId,commands);
System.out.println(controlDevice);

View File

@ -1,5 +1,5 @@
server:
port: 8080
port: 8081
spring:
application:
name: qiuguo-iot-box-websocket