Merge remote-tracking branch 'origin/dev-V0.5.0' into dev-V0.5.0

V0.5.x
tangfudong 2023-12-21 16:27:35 +08:00
commit 70c8d69444
8 changed files with 44 additions and 29 deletions

View File

@ -158,15 +158,3 @@ http://iotkit-open-source.gitee.io/document/
![输入图片说明](doc/ma.png)
#### 开源生态
1. [mybatis-plus版数据服务](https://gitee.com/openiot/iotkit-parent-mybatis)直接替换iot-data-serviceImpl-rdb就能从jpa切换成mybatis-plus
### 项目规划
见:《[开源项目管理](https://open-iita.feishu.cn/base/G4owbWqkbaoI0LsYVnWcreXVnnc)》
![输入图片说明](https://foruda.gitee.com/images/1686571717085038365/f1bd636b_504690.png "proj.png")

View File

@ -50,6 +50,7 @@ public class ThingServiceImpl implements IThingService {
@Override
public ActionResult post(String pluginId, IDeviceAction action) {
try {
log.info("receive plugin:{}, action:{}", pluginId, action);
String deviceName = action.getDeviceName();
//添加设备路由
@ -160,10 +161,6 @@ public class ThingServiceImpl implements IThingService {
if (device != null) {
log.info("device already registered");
device.setModel(register.getModel());
device.setProductKey(register.getProductKey());
device.setSecret(RandomStringUtils.randomAlphabetic(16));
deviceInfoData.save(device);
} else {
//不存在,注册新设备
device = new DeviceInfo();
@ -237,6 +234,7 @@ public class ThingServiceImpl implements IThingService {
message.setData(new HashMap<>(0));
}
log.info("publish thingModel msg:{}", message);
producer.publish(Constants.THING_MODEL_MESSAGE_TOPIC, message);
} catch (Throwable e) {
log.error("send thing model message error", e);

View File

@ -1,6 +1,5 @@
package cc.iotkit.plugin.main.script;
import cc.iotkit.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.locks.LockSupport;
@ -34,7 +33,6 @@ public class Chan<T> {
if (t == null) {
return;
}
log.debug("put message data:{}", JsonUtils.toJsonString(data));
LockSupport.unpark(t);
}

View File

@ -60,7 +60,6 @@ public class ScriptClientVerticle extends AbstractVerticle {
public String send(DataPackage data) {
Buffer buffer = DataEncoder.encode(data);
log.info("send data:{}", HexUtil.encodeHexStr(buffer.getBytes()));
socket.write(buffer);
Chan<DataPackage> chan = Chan.getInstance();
DataPackage receiver = chan.get(data.getMid());
@ -74,7 +73,6 @@ public class ScriptClientVerticle extends AbstractVerticle {
}
public void handle(Buffer buffer) {
log.info("receive server data:{}", buffer.toString());
DataPackage data = DataDecoder.decode(buffer);
Chan<DataPackage> chan = Chan.getInstance();
chan.put(data);

View File

@ -1,5 +1,6 @@
package cc.iotkit.plugin.main.script;
import cc.iotkit.common.utils.CodecUtil;
import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.data.manager.IPluginInfoData;
import cc.iotkit.model.plugin.PluginInfo;
@ -15,12 +16,14 @@ import io.vertx.core.parsetools.RecordParser;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@ -30,6 +33,8 @@ public class ScriptVerticle extends AbstractVerticle {
private static final Map<String, IScriptEngine> PLUGIN_SCRIPT_ENGINES = new HashMap<>();
private final Map<String, String> pluginScripts = new HashMap<>();
@Setter
private long keepAliveTimeout = Duration.ofSeconds(30).toMillis();
@ -46,6 +51,9 @@ public class ScriptVerticle extends AbstractVerticle {
return null;
}
//缓存脚本md5用于判断是否需要更新脚本
pluginScripts.put(pluginId, CodecUtil.md5Str(script));
IScriptEngine jsEngine = ScriptEngineFactory.getJsEngine(script);
PLUGIN_SCRIPT_ENGINES.put(pluginId, ScriptEngineFactory.getJsEngine(script));
return jsEngine;
@ -58,6 +66,27 @@ public class ScriptVerticle extends AbstractVerticle {
return PLUGIN_SCRIPT_ENGINES.get(pluginId);
}
@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
public void checkScriptUpdate() {
//定时检查脚本是否需要更新
pluginScripts.forEach((k, v) -> {
PluginInfo pluginInfo = pluginInfoData.findByPluginId(k);
if (pluginInfo == null) {
return;
}
String md5 = CodecUtil.md5Str(pluginInfo.getScript());
if (v.equals(md5)) {
return;
}
IScriptEngine scriptEngine = PLUGIN_SCRIPT_ENGINES.get(k);
if (scriptEngine == null) {
return;
}
pluginScripts.put(k, md5);
scriptEngine.setScript(pluginInfo.getScript());
});
}
@Override
public void start() {
initTcpServer();
@ -112,9 +141,9 @@ public class ScriptVerticle extends AbstractVerticle {
String pluginId = data.getPluginId();
clientMap.put(pluginId, client);
IScriptEngine scriptEngine = getScriptEngine(pluginId);
if(scriptEngine==null){
if (scriptEngine == null) {
data.setResult("");
}else {
} else {
//调用执行脚本方法返回结果
String result = scriptEngine.invokeMethod(data.getMethod(), data.getArgs());
data.setResult(result);

View File

@ -72,12 +72,9 @@ public class VertxTcpClient {
}
public void sendMessage(Buffer buffer) {
log.info("wirte data:{}", buffer.toString());
socket.write(buffer, r -> {
keepAlive();
if (r.succeeded()) {
log.info("client msg send success");
} else {
if (!r.succeeded()) {
log.error("client msg send failed", r.cause());
}
});

View File

@ -18,6 +18,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -26,6 +27,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableTransactionManagement
@EnableWebMvc
@EnableJpaAuditing
@EnableScheduling
public class Application implements SpringBootstrap {
public static void main(String[] args) {

View File

@ -33,10 +33,11 @@ import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Slf4j
public class VirtualManager {
@ -57,7 +58,11 @@ public class VirtualManager {
@Autowired
private MqProducer<ThingModelMessage> producer;
@Scheduled(initialDelay = 8000)
public VirtualManager(){
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorService.schedule(this::init, 8, TimeUnit.SECONDS);
}
public void init() {
List<VirtualDevice> virtualDevices = getAllVirtualDevices();
for (VirtualDevice virtualDevice : virtualDevices) {