ソースを参照

state before changing children function

DS 2 年 前
コミット
d0a91c3304
共有1 個のファイルを変更した313 個の追加30 個の削除を含む
  1. 313 30
      Backend/services.js

+ 313 - 30
Backend/services.js

@@ -3,10 +3,12 @@ const execSync = require('child_process').execSync;
 var parser = require('xml2json');
 const fs = require('fs');
 const redis = require('redis');
+var cors = require('cors');
 const client = redis.createClient();
 
 const app = express();
-const port = 3000;
+app.use(cors());
+const port = 3001;
 //client.connect('127.0.0.1', 6379);
 
 
@@ -19,7 +21,7 @@ const mt = require("./mt.json");
 let ns_array = [];
 
 
-console.log(typeof jsn);
+//console.log(typeof jsn);
 //console.log(jsn.UANodeSet.UAObject.find( record => record.NodeId === "i=84"));
 
 client.on('connect', function(){
@@ -31,15 +33,16 @@ async function redis_set(shorty, data){
 }
 
 function inverse(dev){
-	var str = '';
 	if("IsForward" in dev){
 		if(dev.IsForward === "false"){
-			str = "<-";
-			}
+			return true;
+			
 		}else{
-			str = "->";
+			return false;
 			}
-	return str;	
+	}else{
+		return false;
+	}
 }
 
 function forward(dev){
@@ -75,55 +78,84 @@ function sortNS(files){
 
 
 function get_Children(jsnob, nid, ret_obj){
+	const entities = ['UAObjectType','UAObject','UAVariableType','UAVariable', 'UADataType', 'UAReferenceType', 'UAMethod'];
+	const set = jsnob.UANodeSet;
+	for(let i = 0; i< entities.length; i++){
+		if(entities[i] in set){}
+	}
+	
 	try{
 		if(Array.isArray(jsnob.UANodeSet.UAObject)){
 			for (let i = 0; i < jsnob.UANodeSet.UAObject.length;i++){
 				if(Array.isArray(jsnob.UANodeSet.UAObject[i].References.Reference)){
 					for (let x = 0; x < jsnob.UANodeSet.UAObject[i].References.Reference.length;x++){
 						if(jsnob.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
-							console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject[i].References.Reference[x]) +jsnob.UANodeSet.UAObject[i].References.Reference[x].ReferenceType);
-							let zobj = {
+							if(inverse(jsnob.UANodeSet.UAObject[i].References.Reference[x])){
+							let zobj= overwriteNode(jsnob.UANodeSet.UAObject[i],  nid, "UAObject",  getIndexArray(jsnob));
+							/*let zobj = {
 								id: jsnob.UANodeSet.UAObject[i].NodeId,
+								text: jsnob.UANodeSet.UAObject[i].DisplayName, 
 								ReferenceType: jsnob.UANodeSet.UAObject[i].References.Reference[x].ReferenceType,
 								IsForward: forward(jsnob.UANodeSet.UAObject[i].References.Reference[x]),
-								}
-							ret_obj.push(zobj);
+								}*/
+							if(!checkDouble(zobj,ret_obj)){
+								ret_obj.push(zobj);
+							}
 							}
 						}
+					 }
 					}else{
 						if(jsnob.UANodeSet.UAObject[i].References.Reference.$t === nid){
-							console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject[i].References.Reference) +jsnob.UANodeSet.UAObject[i].References.Reference.ReferenceType);
-							let zobj = {
+							//console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject[i].References.Reference) +jsnob.UANodeSet.UAObject[i].References.Reference.ReferenceType);
+							if(inverse(jsnob.UANodeSet.UAObject[i].References.Reference)){
+							let zobj= overwriteNode(jsnob.UANodeSet.UAObject[i], nid, "UAObject", getIndexArray(jsnob));
+							/*let zobj = {
 								id: jsnob.UANodeSet.UAObject[i].NodeId,
+								text: jsnob.UANodeSet.UAObject[i].DisplayName, 
 								ReferenceType: jsnob.UANodeSet.UAObject[i].References.Reference.ReferenceType,
 								IsForward: forward(jsnob.UANodeSet.UAObject[i].References.Reference),
-								}
-							ret_obj.push(zobj);
+								}*/
+							if(!checkDouble(zobj,ret_obj)){
+								ret_obj.push(zobj);
+							}
+							}
 						}
 					}
 			}
 		}else{
 			if(Array.isArray(jsnob.UANodeSet.UAObject.References.Reference)){
 				for (let x = 0; x < jsnob.UANodeSet.UAObject.References.Reference.length;x++){
-					if(jsnob.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
-						console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference[x]) +jsnob.UANodeSet.UAObject.References.Reference[x].ReferenceType);
-						let zobj = {
+					if(jsnob.UANodeSet.UAObject.References.Reference[x].$t === nid){
+						//console.log(jsnob.UANodeSet.UAObject.BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference[x]) +jsnob.UANodeSet.UAObject.References.Reference[x].ReferenceType);
+						if(inverse(jsnob.UANodeSet.UAObject.References.Reference[x])){
+						let zobj= overwriteNode(jsnob.UANodeSet.UAObject, nid, "UAObject", getIndexArray(jsnob));
+						/*let zobj = {
 								id: jsnob.UANodeSet.UAObject.NodeId,
+								text: jsnob.UANodeSet.UAObject[i].DisplayName, 
 								ReferenceType: jsnob.UANodeSet.UAObject.References.Reference[x].ReferenceType,
 								IsForward: forward(jsnob.UANodeSet.UAObject.References.Reference[x]),
-								}
-							ret_obj.push(zobj);
+								}*/
+						if(!checkDouble(zobj,ret_obj)){
+								ret_obj.push(zobj);
+							}
+						}
 					}
 				}
 			}else{
 				if(jsnob.UANodeSet.UAObject.References.Reference.$t === nid){
-					console.log(jsnob.UANodeSet.UAObject.BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference) +jsnob.UANodeSet.UAObject.References.Reference.ReferenceType);
-					let zobj = {
+					//console.log(jsnob.UANodeSet.UAObject.BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference) +jsnob.UANodeSet.UAObject.References.Reference.ReferenceType);
+					if(inverse(jsnob.UANodeSet.UAObject.References.Reference)){
+					let zobj= overwriteNode(jsnob.UANodeSet.UAObject, nid, "UAObject", getIndexArray(jsnob));
+					/*let zobj = {
 								id: jsnob.UANodeSet.UAObject.NodeId,
+								text: jsnob.UANodeSet.UAObject.DisplayName, 
 								ReferenceType: jsnob.UANodeSet.UAObject.References.Reference.ReferenceType,
 								IsForward: forward(jsnob.UANodeSet.UAObject.References.Reference),
-								}
-							ret_obj.push(zobj);
+								}*/
+						if(!checkDouble(zobj,ret_obj)){
+								ret_obj.push(zobj);
+							}
+					}
 				}
 			}		
 		}
@@ -132,6 +164,135 @@ function get_Children(jsnob, nid, ret_obj){
 	}
 }
 
+function getAllChildren(nid, ret_obj){
+	for(let ix = 0; ix < ns_array.length; ix++){
+		get_Children(ns_array[ix][0], nid, ret_obj);
+	}
+}
+
+
+function getIndexObj(jsnob, ns){
+	var uri = ns.UANodeSet.Models.Model.ModelUri;
+	if(uri == "http://opcfoundation.org/UA/"){
+		return 0;
+	}
+	console.log(uri);
+	let uris = {};
+	if(jsnob.UANodeSet.NamespaceUris != undefined){
+		uris = jsnob.UANodeSet.NamespaceUris.Uri;
+		console.log(uris);
+	}else{
+		return 0;
+	}
+	for(let i = 0; i < uris.length; i++){
+		console.log(uris[i]);
+		if(uris[i] == uri){
+			return i+1;
+		}
+	}
+	return undefined;	
+}
+
+function getIndexName(jsnob, name){
+	if(name == "http://opcfoundation.org/UA/"){
+		return 0;
+	}
+	let uris = {};
+	if(jsnob.UANodeSet.NamespaceUris != undefined){
+		uris = jsnob.UANodeSet.NamespaceUris.Uri;
+	}else{
+		return 0;
+	}
+	for(let i = 0; i < uris.length; i++){
+		if(uris[i] == name){
+			return i+1;
+		}
+	}
+	return undefined;	
+}
+
+function getIndexArray(tobj){
+	for(let i = 0; i < ns_array.length;i++){
+		console.log(ns_array[i][1]);
+		if(tobj.UANodeSet.Models.Model.ModelUri === ns_array[i][0].UANodeSet.Models.Model.ModelUri){
+				return i;
+		}	
+	}
+	return undefined;	
+}
+
+
+
+function getNode(jsnob, ns, id){
+	let uris = {};
+	var ns_uri = '';
+	if(jsnob.UANodeSet.NamespaceUris){
+		uris = jsnob.UANodeSet.NamespaceUris.Uri;
+		ns_uri = uris[ns-1];
+	}
+	for(let i = 0; i < ns_array.length; i++){
+		if(ns_array[i][0].UANodeSet.Models.Model.ModelUri == ns_uri){
+			return i;
+		}
+	}
+	return -1;	
+}
+
+function overwriteNode(nobj, nid, nc, ns){
+	const node = {
+		id:nobj.NodeId,
+		text:nobj.DisplayName,
+		children: true,
+		data:{
+			browsename: nobj.BrowseName,
+			namespace: ns,
+			nodeclass: nc,
+			parentNode: nid,
+			parentid: "ParentNodeId" in nobj ? nobj.ParentNodeId : null,
+			description: "Description" in nobj ? nobj.Description : null,
+			notifier: "EventNotifier" in nobj ? nobj.EventNotifier : null, //Object
+			isabstract: "IsAbstract" in nobj ? nobj.IsAbstract : null, //ObjectType
+			datatype: "DataType" in nobj ? nobj.DataType : null, //Variable
+			value: "Value" in nobj ? nobj.Value : null, //Variable
+			arraydimensions: "ArrayDimensions" in nobj ? nobj.ArrayDimensions : null, //Variable
+			references: "References" in nobj ? nobj.References : null
+		}
+		
+	}
+	return node;
+}
+
+
+function searchNode(id){
+	const entities = ['UAObjectType','UAObject','UAVariableType','UAVariable', 'UADataType', 'UAReferenceType', 'UAMethod']; //7
+	var nid = "";
+	var jsnob = {};
+	if(id.includes("ns=")){
+		var ns = id.substring(id.length - 4);
+		const ns1 = ns.split(";");
+		var nsidx = parseInt(ns1[0]);
+		jsnob = ns_array[nsidx][0];
+	}else{
+		nid = id;
+		jsnob = ns_array[0][0];
+	}
+	const set = jsnob.UANodeSet;
+	for(let i = 0; i< entities.length; i++){
+		if(entities[i] in set){
+			console.log(set.Models.Model.ModelUri+" has a "+entities[i]);
+			if(Array.isArray(set[entities[i]])){
+				for(let x = 0; x < set[entities[i]].length;x++){
+					if(set[entities[i]][x].NodeId === nid){
+						let nobj = set[entities[i]][x];
+						const node = overwriteNode(nobj, null, entities[i], getIndexArray(jsnob));
+						return node;
+					}
+				}
+			}
+		}
+	}
+	return null;
+}
 
 async function redis_get(){
 	try{
@@ -143,6 +304,7 @@ async function redis_get(){
 		}
 }
 
+
 async function redis_flush(){
 	await client.sendCommand(['FLUSHALL']);
 }
@@ -162,6 +324,9 @@ function p2json(name, shorty){
 
 }
 
+
+
+
 app.get('/setredis/:ident',(req, res) =>{
 	try{
 		var shorty = req.params["ident"];
@@ -188,7 +353,7 @@ app.get('/requirejson/:ident/:name',(req, res) =>{
 				}
 			}
 
-		ns_array.push([fl, shorty]);	
+		ns_array.push([fl, shorty, fl.UANodeSet.Models.Model.ModelUri]);	
 		res.json({result:"OK"});
 		
 	}catch(err){
@@ -280,7 +445,8 @@ app.get('/get_children/all/:ns/:id',(req, res) =>{
 	var ret_obj = [];
 	var ns = req.params["ns"];
 	var id = req.params["id"];
-	var nid = ns+';'+id;
+	var nid = 'ns='+ns+';'+'i='+id;
+	console.log(nid);
 	get_Children(di, nid, ret_obj);
 	get_Children(ia, nid, ret_obj);
 	get_Children(mach, nid, ret_obj);
@@ -289,6 +455,24 @@ app.get('/get_children/all/:ns/:id',(req, res) =>{
 	res.json(ret_obj);
 })
 
+app.get('/getNode/all/:ns/:id',(req, res) =>{
+	var ret_obj = [];
+	var ns = req.params["ns"];
+	var id = req.params["id"];
+	var nid = 'ns='+ns+';'+'i='+id;
+	console.log(nid);
+	get_Children(di, nid, ret_obj);
+	get_Children(ia, nid, ret_obj);
+	get_Children(mach, nid, ret_obj);
+	get_Children(mt, nid, ret_obj);
+	let node = {
+		id:nid,
+		text: "home",
+		children:ret_obj
+	}
+	res.json(node);
+})
+
 
 app.get('/flushall',(req, res) =>{
 	redis_flush();
@@ -315,7 +499,6 @@ app.get('/list_projects', (req, res) => { //reacts to requests -> /list_projects
 	res.json({result:out});
 	})
 	
-	
 app.get('/list_nodesets', (req, res) => { //reacts to requests -> /list_nodesets
 	const output = execSync("cd .. && cd nodesets && ls", { encoding: 'utf-8' });
 	var out = output.split("\n");
@@ -385,10 +568,110 @@ app.get('/load_json', (req, res) => { //reacts to requests -> /load_json?name=my
 	
 	
 app.get('/root', (req, res) => { //reacts to requests -> /root
-	
 	console.log("Return root.");
-	res.json([{id:1,text:"Root node",children:[{id:2,text:"Child node 1",children:true},{id:3,text:"Child node 2"}]}]);
-	})
+	res.json([{id:"i=84",text:"Root",children:[{id:"i=85",text:"Objects",children:true},{id:"i=86",text:"Types", children:true},{id:"i=87",text:"View",children:false}]}]);
+})
+
+
+function isChildRef(obj){
+	if('IsForward' in obj && obj.IsForward === 'false'){
+		return false;
+	}
+	if('IsForward' in obj && obj.IsForward === 'true'){
+		switch (obj.ReferenceType) {
+			case 'Organizes': return true; 
+			case 'HasComponent': return true;
+			case 'HasProperty': return true;
+			case 'HasAddIn': return true;
+			default: return false;
+		}
+	}
+	switch (obj.ReferenceType) {
+			case 'Organizes': return true; 
+			case 'HasComponent': return true;
+			case 'HasProperty': return true;
+			case 'HasAddIn': return true;
+			default: return false;
+		}
+	
+
+	return false;
+}
+
+function checkDouble(item, obj){
+	for(let i = 0; i < obj.length; i++){
+		if(item.id === obj[i].id){
+			return true;
+		}
+	}
+	return false;
+}
+
+app.get('/lazy', (req, res) => { 
+	//console.log(req);
+	var ret_obj = [];
+	let node = {};
+	if(req.query.id == "#"){
+		//res.json([{id:"i=84",text:"Root",data:{nodeclass:"Object"},children:true}]);
+		res.json([{id:"i=84",text:"Root",data:{nodeclass:"Object"},children:[{id:"i=85",text:"Objects",data:{nodeclass:"Objects"},children:true},{id:"i=86",text:"Types", data:{nodeclass:"Objects"},children:true},{id:"i=87",text:"View",data:{nodeclass:"Objects"},children:true}]}]);
+	}
+	var nid = req.query.id;
+	node = searchNode(nid);
+
+	if(node.data.references != null){
+		console.log("Here:"+node.data.references);
+		if(Array.isArray(node.data.references.Reference)){
+			console.log("And:"+node.data.references.Reference);
+			for(let x = 0; x < node.data.references.Reference.length; x++ ){
+				if(isChildRef(node.data.references.Reference[x])){
+					var cnode = searchNode(node.data.references.Reference[x].$t);
+					if(!checkDouble(cnode,ret_obj)){
+								ret_obj.push(cnode);
+							}
+				}
+			}
+			
+		}else{
+			console.log("But:"+node.data.references);
+			if(isChildRef(node.data.references)){
+					var cnode = searchNode(node.data.references.$t);
+					if(!checkDouble(cnode,ret_obj)){
+								ret_obj.push(cnode);
+							}
+				}
+		}
+	}
+	getAllChildren(nid, ret_obj);
+	console.log(ret_obj);
+	res.json(ret_obj);
+});
+
+app.get('/index/:ns1/:ns2', (req, res) => { 
+	var n1 = req.params["ns1"];
+	var n2 = req.params["ns2"];
+	var num = getIndexObj(ns_array[n1][0],ns_array[n2][0]);
+	console.log(getNode(ns_array[3][0],2,15048));
+	res.send("Index of "+ns_array[n2][1]+' in '+ns_array[n1][1]+' is '+num);
+});
+
+
+app.get('/test/:id', (req, res) => { //test
+	var n1 = parseInt(req.params["id"]);
+	console.log(ns_array[n1][1],);
+	var ret = searchNode("i=85");
+	console.log(ret);
+	res.send("All good");
+	//res.json([{id:"i=84",text:"Root",children:[{id:"i=85",text:"Objects",children:true},{id:"i=86",text:"Types", children:true},{id:"i=87",text:"View",children:false}]}]);
+})
+
+app.get('/require/all', (req, res) => { //test
+	ns_array.push([jsn, "base", jsn.UANodeSet.Models.Model.ModelUri]);
+	ns_array.push([di, "di", di.UANodeSet.Models.Model.ModelUri]);
+	ns_array.push([ia, "ia", ia.UANodeSet.Models.Model.ModelUri]);
+	ns_array.push([mach, "mach", mach.UANodeSet.Models.Model.ModelUri]);
+	ns_array.push([mt, "mt", mt.UANodeSet.Models.Model.ModelUri]);	
+	res.json({result:"OK"});
+})
 	
 	
 app.listen(port, () => {