things api and db

This commit is contained in:
imhao183
2020-05-01 12:39:29 +08:00
committed by heavyrain2012
parent e771794bc0
commit c741b4e1da
11 changed files with 117 additions and 61 deletions
+2 -2
View File
@@ -107,8 +107,8 @@
<write-delay-seconds>0</write-delay-seconds>
</map-store>
</map>
<map name="device">
<map name="devices">
<!-- 7 days -->
<time-to-live-seconds>604800</time-to-live-seconds>
<eviction-policy>LRU</eviction-policy>
+11 -4
View File
@@ -5,11 +5,18 @@ DROP TABLE IF EXISTS `t_device`;
CREATE TABLE `t_device` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`_uid` varchar(64) NOT NULL,
`_owner` varchar(64) DEFAULT '',
`_token` varchar(64) DEFAULT '',
`_state` tinyint DEFAULT 0,
`_extra` TEXT DEFAULT NULL,
`_dt` bigint(20) NOT NULL,
UNIQUE INDEX `device_uid_index` (`_uid` ASC),
INDEX `device_owner_index` (`_owner` ASC)
)
UNIQUE INDEX `device_uid_index` (`_uid` ASC)
);
DROP TABLE IF EXISTS `t_user_device`;
CREATE TABLE `t_user_device` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`_uid` varchar(64) NOT NULL,
`_did` varchar(64) DEFAULT '',
INDEX `user_device_uid_index` (`_uid` ASC),
INDEX `user_device_did_index` (`_did` ASC)
);
@@ -5,15 +5,24 @@ DROP TABLE IF EXISTS `t_device`;
CREATE TABLE `t_device` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`_uid` varchar(64) NOT NULL,
`_owner` varchar(64) DEFAULT '',
`_token` varchar(64) DEFAULT '',
`_state` tinyint DEFAULT 0,
`_extra` TEXT DEFAULT NULL,
`_dt` bigint(20) NOT NULL,
UNIQUE INDEX `device_uid_index` (`_uid` ASC),
INDEX `device_owner_index` (`_owner` ASC)
UNIQUE INDEX `device_uid_index` (`_uid` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
DROP TABLE IF EXISTS `t_user_device`;
CREATE TABLE `t_user_device` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`_uid` varchar(64) NOT NULL,
`_did` varchar(64) DEFAULT '',
UNIQUE INDEX `user_device_uid_index` (`_uid` ASC, `_did` ASC),
INDEX `user_device_did_index` (`_did` ASC)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
@@ -21,6 +21,7 @@ public interface APIPath {
String Create_Robot = "/admin/robot/create";
String CreateOrUpdate_Device = "/admin/device/create";
String Get_Device = "/admin/device/get";
String Get_User_Devices = "/admin/device/user_devices";
String User_Get_Token = "/admin/user/get_token";
String User_Update_Block_Status = "/admin/user/update_block_status";
String User_Get_Info = "/admin/user/get_info";
@@ -15,13 +15,11 @@ import java.util.List;
public class ForwardDeviceMessageData {
private String deviceId;
private String owner;
private int type;
private byte[] data;
public ForwardDeviceMessageData(String deviceId, String owner, int type, byte[] data) {
public ForwardDeviceMessageData(String deviceId, int type, byte[] data) {
this.deviceId = deviceId;
this.owner = owner;
this.type = type;
this.data = data;
}
@@ -34,14 +32,6 @@ public class ForwardDeviceMessageData {
this.deviceId = deviceId;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getType() {
return type;
}
@@ -12,30 +12,19 @@ package cn.wildfirechat.pojos;
import cn.wildfirechat.proto.WFCMessage;
import io.netty.util.internal.StringUtil;
import java.util.List;
public class InputCreateDevice {
private String deviceId;
private String owner;
private List<String> owners;
private String extra;
public WFCMessage.Device toDevice() {
WFCMessage.Device.Builder builder = WFCMessage.Device.newBuilder();
if (!StringUtil.isNullOrEmpty(deviceId))
builder.setUid(deviceId);
if (!StringUtil.isNullOrEmpty(owner))
builder.setOwner(owner);
if (!StringUtil.isNullOrEmpty(extra))
builder.setExtra(extra);
builder.setState(0);
return builder.build();
public List<String> getOwners() {
return owners;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
public void setOwners(List<String> owners) {
this.owners = owners;
}
public String getExtra() {
@@ -10,12 +10,14 @@ package cn.wildfirechat.pojos;
import cn.wildfirechat.proto.WFCMessage;
import java.util.List;
public class OutputDevice {
private int state;
private String deviceId;
private String token;
private String secret;
private String owner;
private List<String> owners;
private String extra;
public OutputDevice(String deviceId, String token, String secret) {
@@ -56,12 +58,12 @@ public class OutputDevice {
this.state = state;
}
public String getOwner() {
return owner;
public List<String> getOwners() {
return owners;
}
public void setOwner(String owner) {
this.owner = owner;
public void setOwners(List<String> owners) {
this.owners = owners;
}
public String getExtra() {
@@ -0,0 +1,23 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package cn.wildfirechat.pojos;
import java.util.List;
public class OutputDeviceList {
private List<OutputDevice> devices;
public List<OutputDevice> getDevices() {
return devices;
}
public void setDevices(List<OutputDevice> devices) {
this.devices = devices;
}
}
@@ -102,7 +102,7 @@
</map-store>
</map>
<map name="device">
<map name="devices">
<!-- 7 days -->
<time-to-live-seconds>604800</time-to-live-seconds>
<eviction-policy>LRU</eviction-policy>
+45 -17
View File
@@ -73,24 +73,7 @@ public class Main {
System.exit(-1);
}
InputCreateDevice createDevice = new InputCreateDevice();
createDevice.setDeviceId("deviceId1");
createDevice.setOwner("userId1");
IMResult<OutputCreateDevice> resultCreateDevice = UserAdmin.createOrUpdateDevice(createDevice);
if (resultCreateDevice != null && resultCreateDevice.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("Create device " + resultCreateDevice.getResult().getDeviceId() + " success");
} else {
System.out.println("Create device failure");
System.exit(-1);
}
IMResult<OutputDevice> getDevice = UserAdmin.getDevice("deviceId1");
if (getDevice != null && getDevice.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && getDevice.getResult().getDeviceId().equals("deviceId1") && getDevice.getResult().getOwner().equals("userId1")) {
System.out.println("Get device " + resultCreateDevice.getResult().getDeviceId() + " success");
} else {
System.out.println("Get device failure");
System.exit(-1);
}
IMResult<InputOutputUserInfo> resultGetUserInfo1 = UserAdmin.getUserByName(userInfo.getName());
if (resultGetUserInfo1 != null && resultGetUserInfo1.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
@@ -876,4 +859,49 @@ public class Main {
System.exit(-1);
}
}
//***********************************************
//**** 物联网相关的API,仅专业版支持
//***********************************************
static void testDevice() throws Exception {
InputCreateDevice createDevice = new InputCreateDevice();
createDevice.setDeviceId("deviceId1");
createDevice.setOwners(Arrays.asList("opoGoG__", "userId1"));
IMResult<OutputCreateDevice> resultCreateDevice = UserAdmin.createOrUpdateDevice(createDevice);
if (resultCreateDevice != null && resultCreateDevice.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("Create device " + resultCreateDevice.getResult().getDeviceId() + " success");
} else {
System.out.println("Create device failure");
System.exit(-1);
}
IMResult<OutputDevice> getDevice = UserAdmin.getDevice("deviceId1");
if (getDevice != null && getDevice.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS && getDevice.getResult().getDeviceId().equals("deviceId1") && getDevice.getResult().getOwners().contains("opoGoG__")) {
System.out.println("Get device " + resultCreateDevice.getResult().getDeviceId() + " success");
} else {
System.out.println("Get device failure");
System.exit(-1);
}
IMResult<OutputDeviceList> getUserDevices = UserAdmin.getUserDevices("userId1");
if (getUserDevices != null && getUserDevices.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
boolean success = false;
for (OutputDevice outputDevice : getUserDevices.getResult().getDevices()) {
if (outputDevice.getDeviceId().equals("deviceId1")) {
success = true;
break;
}
}
if (success) {
System.out.println("Get user device success");
} else {
System.out.println("Get user device failure");
System.exit(-1);
}
} else {
System.out.println("Get device failure");
System.exit(-1);
}
}
}
@@ -84,4 +84,11 @@ public class UserAdmin {
return AdminHttpUtils.httpJsonPost(path, inputDeviceId, OutputDevice.class);
}
public static IMResult<OutputDeviceList> getUserDevices(String userId) throws Exception {
String path = APIPath.Get_User_Devices;
InputUserId inputUserId = new InputUserId();
inputUserId.setUserId(userId);
return AdminHttpUtils.httpJsonPost(path, inputUserId, OutputDeviceList.class);
}
}