Browse Source

优化pom文件
添加ws协议服务端脚本

tangfudong 2 years ago
parent
commit
9b4d042b6e

+ 1 - 16
iot-components/iot-websocket-component/pom.xml

@@ -5,7 +5,7 @@
     <parent>
         <artifactId>iot-components</artifactId>
         <groupId>cc.iotkit</groupId>
-        <version>0.4.3-SNAPSHOT</version>
+        <version>0.4.2-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 
@@ -28,26 +28,11 @@
             <artifactId>slf4j-api</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.luaj</groupId>
-            <artifactId>luaj-jse</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>cc.iotkit</groupId>
-            <artifactId>iot-common</artifactId>
-        </dependency>
-
         <dependency>
             <groupId>cc.iotkit</groupId>
             <artifactId>iot-component-base</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>cc.iotkit</groupId>
-            <artifactId>iot-data-service</artifactId>
-        </dependency>
-
     </dependencies>
 
     <build>

+ 1 - 1
iot-components/iot-websocket-component/src/main/java/cc/iotkit/comp/websocket/server/WebSocketServerVerticle.java

@@ -151,7 +151,7 @@ public class WebSocketServerVerticle extends AbstractDeviceVerticle {
 
     @Override
     public DeviceMessage send(DeviceMessage message) {
-        ServerWebSocket wsClient = wsClients.get(getDeviceKey(message.getProductKey(),message.getDeviceName()));
+        ServerWebSocket wsClient = wsClients.get(message.getDeviceName());
         Object obj = message.getContent();
         if (!(obj instanceof Map)) {
             throw new BizException("message content is not Map");

+ 114 - 0
iot-components/iot-websocket-component/src/main/resources/server-component.js

@@ -0,0 +1,114 @@
+var mid=1;
+
+function getMid(){
+	mid++;
+	if(mid>10000){
+		mid=1;
+	}
+	return mid;
+};
+function getPkDn(deviceKey){
+	var arr=deviceKey.split("_");
+	return {
+		pk:arr[1],
+		dn:deviceKey
+	};
+}
+function register(data){
+	var device=getPkDn(data.data.deviceName)
+	var subDevicesList=data.data.subDevices
+	var subDevices=[]
+	if(subDevicesList!=undefined&&subDevicesList.length>0){
+		apiTool.log("device:"+subDevicesList);
+		for (var i = 0; i < subDevicesList.length; i++) {
+			var deviceKey=subDevicesList[i]
+			var subDevice=getPkDn(deviceKey)
+			subDevices.push({
+				productKey:subDevice.pk,
+				deviceName:subDevice.dn,
+				model:''
+			})
+		}
+	}
+
+	var reply=
+		{
+			productKey:device.pk,
+			deviceName:device.dn,
+			mid:"0",
+			content:{
+				id:data.id,
+				type:data.type,
+				result:'success'
+			}
+		};
+	var data={
+		productKey:device.pk,
+		deviceName:device.dn
+	}
+	if(subDevices.length>0){
+		data['subDevices']=subDevices
+	}
+	apiTool.log("subDevices:"+JSON.stringify(data));
+	return {
+		type:"register",
+		data:{
+			productKey:device.pk,
+			deviceName:device.dn,
+			subDevices:subDevices
+		},
+		action:{
+			type:"ack",
+			content:JSON.stringify(reply)
+		}
+	};
+}
+
+function online(data){
+	apiTool.log("data:"+JSON.stringify(data));
+	var device=getPkDn(data.data.deviceName)
+	return {
+		type:"state",
+		data:{
+			productKey:device.pk,
+			deviceName:device.dn,
+			state:data.type
+		}
+	};
+}
+
+function offline(data){
+	var device=getPkDn(data.deviceKey)
+	return {
+		type:"state",
+		data:{
+			productKey:device.pk,
+			deviceName:device.dn,
+			state:data.type
+		}
+	};
+}
+
+//必须提供onReceive方法
+this.onReceive=function(head,type,payload){
+	var data=JSON.parse(payload)
+	if(data.type=="register"){
+		apiTool.log("data:"+payload);
+		return register(data)
+	}else if(data.type=="online"){
+		return online(data);
+	}else if(data.type=="offline"){
+		return offline(data);
+	}
+	return {
+		productKey:"",
+		deviceName:"",
+		mid:0,
+		content:{
+		}
+	}
+};
+
+this.onRegistered=function (data,status) {
+	apiTool.log("onRegistered调用");
+}

+ 1 - 0
iot-components/pom.xml

@@ -19,6 +19,7 @@
         <module>iot-component-base</module>
         <module>iot-http-biz-component</module>
         <module>iot-component-tcp</module>
+        <module>iot-DLT645-component</module>
         <module>iot-websocket-component</module>
         <!--        <module>iot-ctwing-component</module>-->
     </modules>