From c741b4e1da49973dc16e1a104d4a297b49fe0786 Mon Sep 17 00:00:00 2001 From: imhao183 Date: Fri, 1 May 2020 09:39:08 +0800 Subject: [PATCH] things api and db --- broker/config/hazelcast.xml | 4 +- .../migrate/h2/V27__create_device_table.sql | 15 +++-- .../mysql/V32__create_device_table.sql | 15 ++++- .../java/cn/wildfirechat/common/APIPath.java | 1 + .../pojos/ForwardDeviceMessageData.java | 12 +--- .../wildfirechat/pojos/InputCreateDevice.java | 25 +++----- .../cn/wildfirechat/pojos/OutputDevice.java | 12 ++-- .../wildfirechat/pojos/OutputDeviceList.java | 23 +++++++ distribution/src/main/resources/hazelcast.xml | 2 +- .../main/java/cn/wildfirechat/sdk/Main.java | 62 ++++++++++++++----- .../java/cn/wildfirechat/sdk/UserAdmin.java | 7 +++ 11 files changed, 117 insertions(+), 61 deletions(-) create mode 100755 common/src/main/java/cn/wildfirechat/pojos/OutputDeviceList.java diff --git a/broker/config/hazelcast.xml b/broker/config/hazelcast.xml index d7e02dff..19c38c3e 100755 --- a/broker/config/hazelcast.xml +++ b/broker/config/hazelcast.xml @@ -107,8 +107,8 @@ 0 - - + + 604800 LRU diff --git a/broker/migrate/h2/V27__create_device_table.sql b/broker/migrate/h2/V27__create_device_table.sql index a98e0f35..a704c6bc 100755 --- a/broker/migrate/h2/V27__create_device_table.sql +++ b/broker/migrate/h2/V27__create_device_table.sql @@ -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) +); diff --git a/broker/migrate/mysql/V32__create_device_table.sql b/broker/migrate/mysql/V32__create_device_table.sql index 738f4757..86580852 100755 --- a/broker/migrate/mysql/V32__create_device_table.sql +++ b/broker/migrate/mysql/V32__create_device_table.sql @@ -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; diff --git a/common/src/main/java/cn/wildfirechat/common/APIPath.java b/common/src/main/java/cn/wildfirechat/common/APIPath.java index 4ab50c91..2b70cc37 100644 --- a/common/src/main/java/cn/wildfirechat/common/APIPath.java +++ b/common/src/main/java/cn/wildfirechat/common/APIPath.java @@ -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"; diff --git a/common/src/main/java/cn/wildfirechat/pojos/ForwardDeviceMessageData.java b/common/src/main/java/cn/wildfirechat/pojos/ForwardDeviceMessageData.java index d3cca6df..c171cbbb 100644 --- a/common/src/main/java/cn/wildfirechat/pojos/ForwardDeviceMessageData.java +++ b/common/src/main/java/cn/wildfirechat/pojos/ForwardDeviceMessageData.java @@ -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; } diff --git a/common/src/main/java/cn/wildfirechat/pojos/InputCreateDevice.java b/common/src/main/java/cn/wildfirechat/pojos/InputCreateDevice.java index 89da7067..ab16ec6b 100755 --- a/common/src/main/java/cn/wildfirechat/pojos/InputCreateDevice.java +++ b/common/src/main/java/cn/wildfirechat/pojos/InputCreateDevice.java @@ -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 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 getOwners() { + return owners; } - - public String getOwner() { - return owner; - } - - public void setOwner(String owner) { - this.owner = owner; + public void setOwners(List owners) { + this.owners = owners; } public String getExtra() { diff --git a/common/src/main/java/cn/wildfirechat/pojos/OutputDevice.java b/common/src/main/java/cn/wildfirechat/pojos/OutputDevice.java index db39868e..95fad61a 100755 --- a/common/src/main/java/cn/wildfirechat/pojos/OutputDevice.java +++ b/common/src/main/java/cn/wildfirechat/pojos/OutputDevice.java @@ -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 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 getOwners() { + return owners; } - public void setOwner(String owner) { - this.owner = owner; + public void setOwners(List owners) { + this.owners = owners; } public String getExtra() { diff --git a/common/src/main/java/cn/wildfirechat/pojos/OutputDeviceList.java b/common/src/main/java/cn/wildfirechat/pojos/OutputDeviceList.java new file mode 100755 index 00000000..8010195f --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/pojos/OutputDeviceList.java @@ -0,0 +1,23 @@ +/* + * This file is part of the Wildfire Chat package. + * (c) Heavyrain2012 + * + * 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 devices; + + public List getDevices() { + return devices; + } + + public void setDevices(List devices) { + this.devices = devices; + } +} diff --git a/distribution/src/main/resources/hazelcast.xml b/distribution/src/main/resources/hazelcast.xml index 29659d4f..65295e75 100755 --- a/distribution/src/main/resources/hazelcast.xml +++ b/distribution/src/main/resources/hazelcast.xml @@ -102,7 +102,7 @@ - + 604800 LRU diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index 2a4ccf93..918000e8 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -73,24 +73,7 @@ public class Main { System.exit(-1); } - InputCreateDevice createDevice = new InputCreateDevice(); - createDevice.setDeviceId("deviceId1"); - createDevice.setOwner("userId1"); - IMResult 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 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 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 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 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 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); + } + + } } diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java b/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java index 789336fe..1a584243 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/UserAdmin.java @@ -84,4 +84,11 @@ public class UserAdmin { return AdminHttpUtils.httpJsonPost(path, inputDeviceId, OutputDevice.class); } + public static IMResult 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); + } + }