Commit 547fa2b5 by fz

1.添加自动化管理service

2.添加自动化相关参数实体类
3.整理无用包引用
4.SendRequestService添加put,delete方法
1 parent 639fc527
......@@ -10,11 +10,7 @@ import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
/**
* 对接涂鸦用户模块
......
......@@ -2,8 +2,6 @@ package cn.com.glsx.modules.model.param;
import lombok.Data;
import java.util.List;
/**
* @author zhouhaibao
* @date 2021/3/4 10:10
......
package cn.com.glsx.modules.model.param;
import lombok.Data;
@Data
public class Action {
/*目标 ID*/
private String entity_id;
/*设备动作
entity_id:设备 ID
action_executor :dpIssue
executor_property:标准指令
禁用自动化,
entity_id:自动化id
action_executor:ruleDisable
executor_property:不填
启用自动化
entity_id:自动化id
action_executor:ruleEnable
executor_property:不填
触发自动化或场景
entity_id :自动化 ID 或场景 ID
action_executo:ruleTrigger
executor_property:不填
*/
private String executor_property;
/*执行动作类别*/
private String action_executor;
}
package cn.com.glsx.modules.model.param;
import lombok.Data;
import java.util.List;
@Data
public class Automation {
/*背景图片*/
private String background;
/*匹配类型*/
private Integer match_type;
/*自动化名称*/
private String name;
/*条件列表*/
private List<Condition> conditions;
/*动作列表*/
private List<Action> actions;
/*自定义条件规则*/
private String condition_rule;
/*前置条件,优先级最高*/
private List<Precondition> preconditions;
}
package cn.com.glsx.modules.model.param;
import lombok.Data;
@Data
public class Condition {
/*条件类型。目前支持以下类型:
设备状态条件: entity_type=1
外部条件: entity_type=15
天气条件:entityType=3
定时条件: entity_type=6*/
private Integer entity_type;
/*条件排序,从 1 开始*/
private Integer order_num;
/* 非必填==执行动作实体:
entity_type=1 :取值为设备 ID,必填。
entity_type=15:取值由系统自动生成。
entit_type=3:取值为对应城市的 ID。
entity_type=6:取值固定为 timer*/
private String entity_id;
private Display display;
}
package cn.com.glsx.modules.model.param;
import lombok.Data;
/*设备,外部,天气条件实体类*/
@Data
public class Display {
/*设备的 statusCode*/
private String code;
/*操作符
type operator
Boolean “==”
Integer “>”,“==”,“<”
Enum “==”
* */
private String operator;
/*Code 的取值*/
private String value;
}
package cn.com.glsx.modules.model.param;
import lombok.Data;
@Data
public class Precondition {
/*当前仅支持时间段限制,固定为timeCheck*/
private String cond_type;
/*条件展示*/
private PreconditionDisplay display;
}
package cn.com.glsx.modules.model.param;
import lombok.Data;
@Data
public class PreconditionDisplay {
/*开始时间,默认为当天的某个时间点,24小时制。示例:10:00*/
private String start;
/*结束时间,24小时制。示例:22:00*/
private String end;
/*由 0 和 1 组成的 7 位数字。
0 表示不执行,1 表示执行。
第 1 位为周日,依次表示周一至周六。
例如,0011000表示每周二,周三执行*/
private String loops;
/*时区 ID,示例:Asia/Shanghai*/
private String timezone_id;
}
package cn.com.glsx.modules.model.param;
import lombok.Data;
@Data
public class TimeDisplay {
/*触发日期,格式为yyyyMMdd,例如20191125*/
private String date;
/*触发时间,24 小时制,示例:14:00*/
private String time;
/*由 0 和 1 组成的 7 位数字。
0 表示不执行,1 表示执行。
第 1 位为周日,依次表示周一至周六。
例如,0011000表示每周二,周三执行*/
private String loops;
/*时区 ID,示例:Asia/Shanghai*/
private String timezone_id;
}
package cn.com.glsx.modules.model.param;
import lombok.Data;
@Data
public class memberInfo {
/** 国家码*/
private String country_code;
/** 成员账号*/
private String member_account;
/** 是否为管理员*/
private boolean admin;
/** 成员名字*/
private String name;
}
......@@ -2,11 +2,13 @@ package cn.com.glsx.modules.service;
import cn.com.glsx.modules.model.param.HomeInfo;
import cn.com.glsx.modules.model.param.ResponseBody;
import cn.com.glsx.modules.model.param.memberInfo;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
......@@ -20,19 +22,18 @@ import java.util.List;
public class HomeService {
/*
PUT /v1.0/homes/{home_id} 修改家庭
DELETE /v1.0/homes/{home_id} 删除家庭
GET /v1.0/homes/{home_id}/devices 查询家庭下的设备列表
POST /v1.0/homes/{home_id}/members 添加家庭成员
PUT /v1.0/homes/{home_id}/members/{uid} 设置成员权限
DELETE /v1.0/homes/{home_id}/members/{uid} 删除家庭成员
GET /v1.0/homes/{home_id}/members 查询家庭成员
GET /v1.0/users/{uid}/homes 查询⽤户所在的家庭列表
GET /v1.0/homes/{home_id}/rooms 查询家庭下的房间列表
GET /v1.0/homes/{home_id}/rooms/{room_id}/devices 查询家庭房间设备
POST /v1.0/homes/{home_id}/rooms/{room_id}/devices 新增家庭下房间内的设备
PUT /v1.0/homes/{home_id}/rooms/{room_id}/devices 修改家庭下房间内的设备
DELETE /v1.0/homes/{home_id}/rooms/{room_id}/devices 删除家庭下房间内的设备*/
DELETE /v1.0/homes/{home_id}/rooms/{room_id}/devices 删除家庭下房间内的设备
DELETE /v1.0/homes/{home_id} 删除家庭
GET /v1.0/homes/{home_id}/rooms 查询家庭下的房间列表
GET /v1.0/homes/{home_id}/rooms/{room_id}/devices 查询家庭房间设备*/
/********************************创建默认家庭,默认成员,设置成员权限********************************/
@Value("${tuya.baseUrl}")
private String baseUrl;
......@@ -40,6 +41,7 @@ public class HomeService {
private SendRequestService requestService;
/**
* POST /v1.0/home/create-home
* 添加家庭,给⽤户添加⼀个家庭以及家庭下的房间。
* 说明:可以只添加家庭,房间留空。
*
......@@ -57,5 +59,81 @@ public class HomeService {
log.info("回调:" + responseBody);
}
//GET /v1.0/homes/{home_id} 查询家庭
/**
* POST /v1.0/homes/{home_id}/members
* 添加家庭成员
* 说明:给某个家庭添加家庭成员
*
* @param home_id 家庭 ID
* @param app_schema 账号标识
* @param member 成员对象
*/
public void addHomeMember(String home_id, String app_schema,memberInfo member) {
HashMap<String, Object> params = new HashMap<>();
params.put("app_schema", app_schema);
params.put("member", member);
ResponseBody responseBody = requestService.sendRequestPost(baseUrl + "/homes/"+home_id+"/members", JSONObject.toJSONString(params));
log.info("回调:" + responseBody);
}
/**
* PUT /v1.0/homes/{home_id}/members/{uid}
* 设置成员权限
* 说明:为家庭成员设置管理员和非管理员的角色权限,修改家庭成员的名字
*
* @param home_id 家庭 ID
* @param uid 涂鸦用户 ID
* @param admin 家庭管理员标示。
* true:管理员
* false:非管理员
*/
public void editMemberPermission(String home_id, String uid,boolean admin) {
HashMap<String, Object> params = new HashMap<>();
params.put("admin", admin);
ResponseBody responseBody = requestService.sendRequestPut(baseUrl + "/homes/"+home_id+"/members/"+uid, JSONObject.toJSONString(params));
log.info("回调:" + responseBody);
}
/********************************其他接口暂未使用********************************/
/**
* POST /v1.0/homes/{home_id}/rooms/{room_id}/devices
* 说明:新增家庭下房间内的设备
*
* @param home_id 家庭 ID
* @param room_id 房间 ID
* @param device_ids 新增的设备 ID
*/
public void addHomeRoomDevices(Long home_id, Long room_id,List<String> device_ids) {
HashMap<String, Object> params = new HashMap<>();
params.put("device_ids", device_ids);
ResponseBody responseBody = requestService.sendRequestPost(baseUrl + "/homes/"+home_id+"/rooms/"+room_id+"/devices", JSONObject.toJSONString(params));
log.info("回调:" + responseBody);
}
/**
* GET /v1.0/homes/{home_id}
* 查询家庭
* 根据家庭 ID查询家庭信息
*
* @param home_id 家庭 ID
*/
public void queryHome(Long home_id) {
ResponseBody responseBody = requestService.sendRequestGet(baseUrl + "/homes/"+home_id);
log.info("回调:" + responseBody);
}
/**
* GET /v1.0/homes/{home_id}/devices
* 查询家庭下的设备列表
* 根据家庭 ID查询家庭下的设备列表
*
* @param home_id 家庭 ID
*/
public void queryHomeDevices(Long home_id) {
ResponseBody responseBody = requestService.sendRequestGet(baseUrl + "/homes/"+home_id+"/devices");
log.info("回调:" + responseBody);
}
}
package cn.com.glsx.modules.service;
import cn.com.glsx.modules.model.param.Automation;
import cn.com.glsx.modules.model.param.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @author fengzhi
* @date 2021/3/5 17:26
* @description 场景自动化
*/
@Service
@Slf4j
public class ScenesService {
/*
GET /v1.0/homes/{home_id}/scenes 查询家庭下的场景列表
GET /v1.0/scenes/default-pictures 查询默认场景图片列表
POST /v1.0/homes/{home_id}/scenes/{scene_id}/trigger 触发场景
POST /v1.0/homes/{home_id}/scenes 添加场景
DELETE /v1.0/homes/{home_id}/scenes/{scene_id} 删除场景
PUT /v1.0/homes/{home_id}/scenes/{scene_id} 修改场景
GET /v1.0/homes/{home_id}/scene/devices 查询家庭下支持场景的设备列表
POST /v1.0/devices/{device_id}/scenes/{scene_id} 场景绑定
DELETE /v1.0/devices/{device_id}/scenes/{scene_id} 场景解绑
GET /v1.0/devices/{device_id}/scenes 查询设备已绑定的场景列表
DELETE /v1.0/homes/{home_id}/automations
/{automation_id} 删除自动化
GET /v1.0/homes/{home_id}/automations 查询自动化列表
GET /v1.0/homes/{home_id}/automations
/{automation_id} 查询家庭下的某个自动化
POST /v1.0/homes/{home_id}/automations
/{automation_id}/conditions/trigger 触发家庭下某个自动化的外部条件
GET /v1.0/homes/{home_id}/automation
/devices 查询家庭下支持自动化的设备列表
GET /v1.0/homes/automation/weather/conditions 查询自动化支持的天气条件
GET /v1.0/homes/{home_id}/enable-linkage/codes 查询家庭下支持的动作列表
PUT /v1.0/homes/{home_id}/automations
/{automation_id}/actions/enable 启用自动化
PUT /v1.0/homes/{home_id}/automations
/{automation_id}/actions/disable 停用自动化*/
/********************************创建,修改自动化********************************/
@Value("${tuya.baseUrl}")
private String baseUrl;
@Autowired
private SendRequestService requestService;
/**
* POST /v1.0/homes/{home_id}/automations
* 添加自动化
* 说明:为家庭添加自动化功能。
*
* @param home_id 用户家庭 ID
* @param automation 自动化对象参数
*/
public void addAutomations(String home_id, Automation automation) {
ResponseBody responseBody = requestService.sendRequestPost(baseUrl + "/homes/"+home_id+"/automations", JSONObject.toJSONString(automation));
log.info("回调:" + responseBody);
}
/**
* PUT /v1.0/homes/{home_id}/automations/{automation_id}
* 修改自动化
* 说明:修改家庭的自动化配置。
*
* @param home_id 用户家庭 ID
* @param automation_id 自动化 ID
* @param automation 自动化对象参数
*/
public void editAutomations(String home_id,String automation_id,Automation automation) {
ResponseBody responseBody = requestService.sendRequestPut(baseUrl + "/homes/"+home_id+"/automations/"+automation_id, JSONObject.toJSONString(automation));
log.info("回调:" + responseBody);
}
}
......@@ -88,6 +88,42 @@ public class SendRequestService {
* @param requesturl 请求链接
* @return
*/
public ResponseBody sendRequestPut(String requesturl,String requestBody) {
ResponseBody responseBody = null;
String accessToken = accessTokenService.getAccessToken();
if(StringUtils.isBlank(accessToken)){
log.info("获取token失败");
return responseBody;
}
Map<String, String> headerParams = HeaderParamUtils.buildHeaderParams(client_id, secret, accessToken);
try {
JSONObject jsonObject = HttpUtil.sendPut(requesturl, requestBody, headerParams);
responseBody = JSON.toJavaObject(jsonObject, ResponseBody.class);
} catch (IOException e) {
e.printStackTrace();
log.info("请求用户信息接口异常,异常信息{}", e);
}
return responseBody;
}
public ResponseBody sendRequestDelete(String requesturl) {
ResponseBody responseBody = null;
String accessToken = accessTokenService.getAccessToken();
if(StringUtils.isBlank(accessToken)){
log.info("获取token失败");
return responseBody;
}
Map<String, String> headerParams = HeaderParamUtils.buildHeaderParams(client_id, secret, accessToken);
try {
JSONObject jsonObject = HttpUtil.sendDelete(requesturl, headerParams);
responseBody = JSON.toJavaObject(jsonObject, ResponseBody.class);
} catch (IOException e) {
e.printStackTrace();
log.info("请求用户信息接口异常,异常信息{}", e);
}
return responseBody;
}
public ResponseBody sendRequestGetOA(String requesturl) {
ResponseBody responseBody = null;
Map<String, String> headerParams = HeaderParamUtils.buildHeaderParams(client_id, secret);
......
......@@ -5,7 +5,6 @@ import cn.com.glsx.modules.model.param.UserBo;
import cn.com.glsx.modules.model.param.UserInfo;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......
......@@ -6,15 +6,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.NoHttpResponseException;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpRequestRetryHandler;
......@@ -52,12 +44,7 @@ import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class HttpUtil {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!