Merge branch 'feature-BOX一期' of http://47.99.132.106:10081/wulin/qiuguo-iot into feature-BOX一期
This commit is contained in:
commit
e9ac89e0da
@ -5,6 +5,7 @@ package com.qiuguo.iot.data.service.user;
|
|||||||
import com.qiuguo.iot.base.utils.StringUtils;
|
import com.qiuguo.iot.base.utils.StringUtils;
|
||||||
import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity;
|
import com.qiuguo.iot.data.entity.user.UserHandlingDeviceEntity;
|
||||||
import com.qiuguo.iot.data.request.user.UserHandlingDeviceRequest;
|
import com.qiuguo.iot.data.request.user.UserHandlingDeviceRequest;
|
||||||
|
import java.util.List;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery;
|
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.api.crud.entity.QueryParamEntity;
|
||||||
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -159,6 +162,13 @@ public class UserHandlingDeviceService extends GenericReactiveCrudService<UserHa
|
|||||||
.and("id", id)
|
.and("id", id)
|
||||||
.fetchOne();
|
.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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
|
||||||
|
}
|
||||||
@ -19,6 +19,15 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public interface TuyaDeviceConnector {
|
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")
|
@GET("/v1.0/iot-03/device-categories")
|
||||||
JSONArray categories();
|
JSONArray categories();
|
||||||
|
|
||||||
|
|||||||
@ -4,12 +4,20 @@ 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;
|
||||||
import com.qiuguo.iot.data.service.user.UserHandlingDeviceService;
|
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.query.TuyaQuery;
|
||||||
|
import com.qiuguo.iot.third.resp.TuyaResponse;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
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;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,21 +56,93 @@ public class TuyaDeviceService {
|
|||||||
return Mono.just(byid.getJSONObject(0).getString("category"));
|
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(
|
Mono<UserHandlingDeviceEntity> userHandlingDeviceEntityMono = userHandlingDeviceService.selectUserHandlingDeviceById(
|
||||||
query.getUserHandlingDeviceId());
|
query.getUserHandlingDeviceId());
|
||||||
|
TuyaResponse tuyaResponse = new TuyaResponse();
|
||||||
|
tuyaResponse.setCode(500);
|
||||||
return userHandlingDeviceEntityMono.flatMap(data -> {
|
return userHandlingDeviceEntityMono.flatMap(data -> {
|
||||||
JSONArray jsonArray = tuyaDeviceConnector.getByid(query.getDeviceId());
|
JSONArray jsonArray = tuyaDeviceConnector.getByid(query.getDeviceId());
|
||||||
if (ObjectUtils.isEmpty(jsonArray)) {
|
if (ObjectUtils.isEmpty(jsonArray)) {
|
||||||
return Mono.just(false);
|
tuyaResponse.setMessage("查询不到设备");
|
||||||
|
return Mono.just(tuyaResponse);
|
||||||
}
|
}
|
||||||
JSONObject deviceInfo = jsonArray.getJSONObject(0);
|
JSONObject deviceInfo = jsonArray.getJSONObject(0);
|
||||||
if (Objects.equals(deviceInfo.getBoolean("is_online"), false) && !Objects.equals(data.getMatchingFields(),
|
if (Objects.equals(deviceInfo.getBoolean(TuyaCategoryEnum.getColumn(data.getCategoryCode())), false)
|
||||||
"开")) {
|
&& !Objects.equals(data.getMatchingFields(), "开")) {
|
||||||
return Mono.just(false);
|
tuyaResponse.setMessage("设备未开机");
|
||||||
|
return Mono.just(tuyaResponse);
|
||||||
}
|
}
|
||||||
JSONObject commands = new JSONObject();
|
JSONObject commands = new JSONObject();
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
@ -78,8 +158,22 @@ public class TuyaDeviceService {
|
|||||||
jsonObject.put("value", data.getValue());
|
jsonObject.put("value", data.getValue());
|
||||||
}
|
}
|
||||||
commands.put("commands", Arrays.asList(jsonObject));
|
commands.put("commands", Arrays.asList(jsonObject));
|
||||||
Boolean aBoolean = tuyaDeviceConnector.controlDevice(query.getDeviceId(), commands);
|
JSONArray deviceStatus = tuyaDeviceConnector.getDeviceStatus(query.getDeviceId());
|
||||||
return Mono.just(aBoolean);
|
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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package com.qiuguo.iot.user.api.controller.tuya;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.qiuguo.iot.third.query.TuyaQuery;
|
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.TuyaDeviceConnector;
|
||||||
import com.qiuguo.iot.third.service.TuyaDeviceService;
|
import com.qiuguo.iot.third.service.TuyaDeviceService;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -26,8 +27,7 @@ public class TuyaDeviceController {
|
|||||||
private TuyaDeviceService tuyaDeviceService;
|
private TuyaDeviceService tuyaDeviceService;
|
||||||
|
|
||||||
@GetMapping("/set")
|
@GetMapping("/set")
|
||||||
public Mono<Boolean> set(TuyaQuery query) {
|
public Mono<TuyaResponse> set(TuyaQuery query) {
|
||||||
|
|
||||||
return tuyaDeviceService.controlDevice(query);
|
return tuyaDeviceService.controlDevice(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,17 +18,22 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
@SpringBootTest(classes = IotBoxUserApiApplication.class)
|
@SpringBootTest(classes = IotBoxUserApiApplication.class)
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class UserTest {
|
public class UserTest {
|
||||||
|
//spring.profiles.active=dev
|
||||||
public String deviceId = "6c41ec425b5af5b81bxgas";
|
public String deviceId = "6c29b49edea00eb541rg3n";
|
||||||
|
|
||||||
public String spaceId = "163208675";
|
public String spaceId = "163208675";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TuyaDeviceConnector tuyaDeviceConnector;
|
private TuyaDeviceConnector tuyaDeviceConnector;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void 获取设备状态() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void 查询所有分类() {
|
public void 查询所有分类() {
|
||||||
String key = "dj";
|
String key = "sd";
|
||||||
JSONArray aa = tuyaDeviceConnector.categories();
|
JSONArray aa = tuyaDeviceConnector.categories();
|
||||||
for (Object o : aa) {
|
for (Object o : aa) {
|
||||||
JSONObject jsonObject = (JSONObject) o;
|
JSONObject jsonObject = (JSONObject) o;
|
||||||
@ -88,15 +93,15 @@ public class UserTest {
|
|||||||
public void 控制设备动作() {
|
public void 控制设备动作() {
|
||||||
JSONObject commands = new JSONObject();
|
JSONObject commands = new JSONObject();
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.put("code", "switch_led");
|
jsonObject.put("code", "power");
|
||||||
jsonObject.put("value", false);
|
jsonObject.put("value", true);
|
||||||
JSONObject js2 = new JSONObject();
|
JSONObject js2 = new JSONObject();
|
||||||
js2.put("code", "work_mode");
|
js2.put("code", "power");
|
||||||
js2.put("value", "colour");
|
js2.put("value", true);
|
||||||
JSONObject js3 = new JSONObject();
|
JSONObject js3 = new JSONObject();
|
||||||
js3.put("code", "bright_value_v2");
|
js3.put("code", "bright_value_v2");
|
||||||
js3.put("value", 10);
|
js3.put("value", 10);
|
||||||
commands.put("commands", Arrays.asList( jsonObject));
|
commands.put("commands", Arrays.asList( js2));
|
||||||
|
|
||||||
Object controlDevice = tuyaDeviceConnector.controlDevice(deviceId,commands);
|
Object controlDevice = tuyaDeviceConnector.controlDevice(deviceId,commands);
|
||||||
System.out.println(controlDevice);
|
System.out.println(controlDevice);
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
server:
|
server:
|
||||||
port: 8080
|
port: 8081
|
||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: qiuguo-iot-box-websocket
|
name: qiuguo-iot-box-websocket
|
||||||
Loading…
x
Reference in New Issue
Block a user