From 9bb3a636cb1d3bad775ee43d075866ca5a8ddded Mon Sep 17 00:00:00 2001 From: lau572 <1010031226@qq.com> Date: Wed, 27 Dec 2023 08:42:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=83=85=E6=8A=A5=E6=9D=BF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/IDGenerator.java | 83 +++++++++++++++++ .../com/ruoyi/common/utils/RandomUtil.java | 46 ++++++++++ .../common/utils/SnowflakeIdGenerator.java | 90 +++++++++++++++++++ .../ruoyi/common/utils/http/HttpUtils.java | 5 +- .../IotBoardTemplateController.java | 30 +++++++ ruoyi-ui/src/api/information/api.js | 11 ++- .../src/views/information/board/index.vue | 5 +- 7 files changed, 266 insertions(+), 4 deletions(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/IDGenerator.java create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/RandomUtil.java create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/SnowflakeIdGenerator.java diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/IDGenerator.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/IDGenerator.java new file mode 100644 index 00000000..2af3e0c6 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/IDGenerator.java @@ -0,0 +1,83 @@ +/* + * + * * Copyright 2020 http://www.hswebframework.org + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + * + */ + +package com.ruoyi.common.utils; + + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * ID生成器,用于生成ID + * + * @author zhouhao + * @since 3.0 + */ +@FunctionalInterface +public interface IDGenerator { + T generate(); + + /** + * 空ID生成器 + */ + IDGenerator NULL = () -> null; + + @SuppressWarnings("unchecked") + static IDGenerator getNullGenerator() { + return (IDGenerator) NULL; + } + + /** + * 使用UUID生成id + */ + IDGenerator UUID = () -> java.util.UUID.randomUUID().toString(); + + /** + * 随机字符 + */ + IDGenerator RANDOM = RandomUtil::randomChar; + + /** + * md5(uuid()+random()) + */ + IDGenerator MD5 = () -> { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(UUID.generate().concat(RandomUtil.randomChar()).getBytes()); + return new BigInteger(1, md.digest()).toString(16); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + }; + + /** + * 雪花算法 + */ + IDGenerator SNOW_FLAKE = SnowflakeIdGenerator.getInstance()::nextId; + + /** + * 雪花算法转String + */ + IDGenerator SNOW_FLAKE_STRING = () -> String.valueOf(SNOW_FLAKE.generate()); + + /** + * 雪花算法的16进制 + */ + IDGenerator SNOW_FLAKE_HEX = () -> Long.toHexString(SNOW_FLAKE.generate()); +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RandomUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RandomUtil.java new file mode 100644 index 00000000..185ff016 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RandomUtil.java @@ -0,0 +1,46 @@ +package com.ruoyi.common.utils; + +import java.util.Random; + + +/** + * 随机数工具,用于产生随机数,随机密码等 + */ +public class RandomUtil { + private static final Random random = new Random(); + + public static Random getRandom() { + return random; + } + + private static char[] chars = { + 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', + 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', + 'V', 'W', 'X', 'Y', 'Z' + }; + + /** + * 随机生成由0-9a-zA-Z组合而成的字符串 + * + * @param len 字符串长度 + * @return 生成结果 + */ + public static String randomChar(int len) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < len; i++) { + sb.append(chars[random.nextInt(chars.length)]); + } + return sb.toString(); + } + + public static String randomChar() { + return randomChar(8); + } + +} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/SnowflakeIdGenerator.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/SnowflakeIdGenerator.java new file mode 100644 index 00000000..2d324a69 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/SnowflakeIdGenerator.java @@ -0,0 +1,90 @@ +package com.ruoyi.common.utils; + +import lombok.extern.slf4j.Slf4j; + + +import java.util.*; + +@Slf4j +public class SnowflakeIdGenerator { + + private long workerId; + private long dataCenterId; + private long sequence = 0L; + + private long twepoch = 1288834974657L; + + private long workerIdBits = 5L; + private long datacenterIdBits = 5L; + private long maxWorkerId = -1L ^ (-1L << workerIdBits); + private long maxDataCenterId = -1L ^ (-1L << datacenterIdBits); + private long sequenceBits = 12L; + + private long workerIdShift = sequenceBits; + private long datacenterIdShift = sequenceBits + workerIdBits; + private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; + private long sequenceMask = -1L ^ (-1L << sequenceBits); + + private long lastTimestamp = -1L; + + private static final SnowflakeIdGenerator generator; + + static { + Random random = new Random(); + long workerId = Long.getLong("id-worker", random.nextInt(31)); + long dataCenterId = Long.getLong("id-datacenter", random.nextInt(31)); + generator = new SnowflakeIdGenerator(workerId, dataCenterId); + } + + public static SnowflakeIdGenerator getInstance() { + return generator; + } + + public SnowflakeIdGenerator(long workerId, long dataCenterId) { + // sanity check for workerId + if (workerId > maxWorkerId || workerId < 0) { + throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); + } + if (dataCenterId > maxDataCenterId || dataCenterId < 0) { + throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDataCenterId)); + } + this.workerId = workerId; + this.dataCenterId = dataCenterId; + log.info("worker starting. timestamp left shift {}, datacenter id bits {}, worker id bits {}, sequence bits {}, workerid {}", timestampLeftShift, datacenterIdBits, workerIdBits, sequenceBits, workerId); + } + + public synchronized long nextId() { + long timestamp = timeGen(); + + if (timestamp < lastTimestamp) { + log.error("clock is moving backwards. Rejecting requests until {}.", lastTimestamp); + throw new UnsupportedOperationException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); + } + + if (lastTimestamp == timestamp) { + sequence = (sequence + 1) & sequenceMask; + if (sequence == 0) { + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0L; + } + + lastTimestamp = timestamp; + + return ((timestamp - twepoch) << timestampLeftShift) | (dataCenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence; + } + + protected long tilNextMillis(long lastTimestamp) { + long timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + + protected long timeGen() { + return System.currentTimeMillis(); + } + +} \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java index e4e0c573..ed6859e5 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java @@ -17,6 +17,8 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; + +import com.ruoyi.common.utils.IDGenerator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ruoyi.common.constant.Constants; @@ -138,7 +140,8 @@ public class HttpUtils conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("Accept-Charset", "utf-8"); - conn.setRequestProperty("contentType", "utf-8"); + conn.setRequestProperty("Content-Type", " application/json;charset=UTF-8"); + conn.setRequestProperty("Message-ID", IDGenerator.SNOW_FLAKE_STRING.generate()); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(conn.getOutputStream()); diff --git a/ruoyi-system/src/main/java/com/zc/controller/IotBoardTemplateController.java b/ruoyi-system/src/main/java/com/zc/controller/IotBoardTemplateController.java index 21248749..56b84bbe 100644 --- a/ruoyi-system/src/main/java/com/zc/controller/IotBoardTemplateController.java +++ b/ruoyi-system/src/main/java/com/zc/controller/IotBoardTemplateController.java @@ -1,11 +1,15 @@ package com.zc.controller; import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.common.utils.IDGenerator; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.zc.domain.IotBoardTemplate; import com.zc.service.IIotBoardTemplateService; @@ -13,7 +17,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 情报板模板Controller @@ -68,5 +74,29 @@ public class IotBoardTemplateController extends BaseController return toAjax(iotBoardTemplateService.deleteSdVmsTemplateByIds(ids)); } + @PostMapping("/deviceControl") + public AjaxResult deviceControl(){ + + String deviceId = "65535"; + String function = "11"; + //拼接请求路径 + String url = "http://10.0.81.201:8081/iot/device/functions/" + deviceId + "/" + function; + + Map params = new HashMap<>(); + + params.put("1","65535"); + params.put("2","play033.lst"); + + + //请求参数 + Gson gson = new Gson(); + String jsonMessage = gson.toJson(params); + + //发起post请求 + String result = HttpUtils.sendPost(url,jsonMessage); + System.out.println(result); + return null; + } + } diff --git a/ruoyi-ui/src/api/information/api.js b/ruoyi-ui/src/api/information/api.js index b474671a..d51d9299 100644 --- a/ruoyi-ui/src/api/information/api.js +++ b/ruoyi-ui/src/api/information/api.js @@ -78,4 +78,13 @@ export function getBoardContentData(query) { method: 'get', params: query }) -} \ No newline at end of file +} + +// 情报板控制 +export function deviceControl(data) { + return request({ + url: '/system/template/deviceControl', + method: 'post', + data: data + }) +} diff --git a/ruoyi-ui/src/views/information/board/index.vue b/ruoyi-ui/src/views/information/board/index.vue index e64f1356..0049641d 100644 --- a/ruoyi-ui/src/views/information/board/index.vue +++ b/ruoyi-ui/src/views/information/board/index.vue @@ -221,7 +221,7 @@ import addinfo from './addinfo' import editInfo from './editInfo' import boardData from './boardData' import { getUserDeptId } from '@/api/system/user' -import { listTunnels, devicessize, information, getBoardInfo, getBoardEditInfo, getIotBoardList, getBoardContentData } from '@/api/information/api.js' +import { listTunnels, devicessize, information, getBoardInfo, getBoardEditInfo, getIotBoardList, getBoardContentData,deviceControl } from '@/api/information/api.js' import { uploadBoardEditInfo, getAllVmsTemplate, addTemplate, addTemplateContent, getBoardContent, deleteTemplate, splicingBoard } from '@/api/board/template' // 对象深拷贝 @@ -3186,7 +3186,8 @@ export default { objectData: JSON.stringify(objAll) } console.log(param, 'param') - splicingBoard(param) + deviceControl(param) + // splicingBoard(param) .then(res => { console.log(res, '返回结果') loading.close()