IAMHERE 11 months ago
parent
commit
db86bf766c
  1. 83
      ruoyi-common/src/main/java/com/ruoyi/common/utils/IDGenerator.java
  2. 46
      ruoyi-common/src/main/java/com/ruoyi/common/utils/RandomUtil.java
  3. 90
      ruoyi-common/src/main/java/com/ruoyi/common/utils/SnowflakeIdGenerator.java
  4. 5
      ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
  5. 30
      ruoyi-system/src/main/java/com/zc/controller/IotBoardTemplateController.java
  6. 9
      ruoyi-ui/src/api/information/api.js
  7. 5
      ruoyi-ui/src/views/information/board/index.vue

83
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> {
T generate();
/**
* 空ID生成器
*/
IDGenerator<?> NULL = () -> null;
@SuppressWarnings("unchecked")
static <T> IDGenerator<T> getNullGenerator() {
return (IDGenerator) NULL;
}
/**
* 使用UUID生成id
*/
IDGenerator<String> UUID = () -> java.util.UUID.randomUUID().toString();
/**
* 随机字符
*/
IDGenerator<String> RANDOM = RandomUtil::randomChar;
/**
* md5(uuid()+random())
*/
IDGenerator<String> 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<Long> SNOW_FLAKE = SnowflakeIdGenerator.getInstance()::nextId;
/**
* 雪花算法转String
*/
IDGenerator<String> SNOW_FLAKE_STRING = () -> String.valueOf(SNOW_FLAKE.generate());
/**
* 雪花算法的16进制
*/
IDGenerator<String> SNOW_FLAKE_HEX = () -> Long.toHexString(SNOW_FLAKE.generate());
}

46
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);
}
}

90
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();
}
}

5
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());

30
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<String,Object> 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;
}
}

9
ruoyi-ui/src/api/information/api.js

@ -79,3 +79,12 @@ export function getBoardContentData(query) {
params: query
})
}
// 情报板控制
export function deviceControl(data) {
return request({
url: '/system/template/deviceControl',
method: 'post',
data: data
})
}

5
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()

Loading…
Cancel
Save