services.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. const express = require("express");
  2. const execSync = require('child_process').execSync;
  3. var parser = require('xml2json');
  4. const fs = require('fs');
  5. const redis = require('redis');
  6. var cors = require('cors');
  7. const client = redis.createClient();
  8. const app = express();
  9. app.use(cors());
  10. const port = 3001;
  11. //client.connect('127.0.0.1', 6379);
  12. const jsn = require("./base.json");
  13. const di = require("./di.json");
  14. const ia = require("./ia.json");
  15. const mach = require("./machinery.json");
  16. const mt = require("./mt.json");
  17. let ns_array = [];
  18. //console.log(typeof jsn);
  19. //console.log(jsn.UANodeSet.UAObject.find( record => record.NodeId === "i=84"));
  20. client.on('connect', function(){
  21. console.log('Connected.');
  22. });
  23. async function redis_set(shorty, data){
  24. await client.json.set(shorty,'.',data);
  25. }
  26. function inverse(dev){
  27. if("IsForward" in dev){
  28. if(dev.IsForward === "false"){
  29. return true;
  30. }else{
  31. return false;
  32. }
  33. }else{
  34. return false;
  35. }
  36. }
  37. function forward(dev){
  38. var b = true;
  39. if("IsForward" in dev){
  40. if(dev.IsForward === "false"){
  41. b = false;
  42. }
  43. }else{
  44. b = true;
  45. }
  46. return b;
  47. }
  48. function sortNS(files){
  49. let nsets = [];
  50. for(let x = 0; x<files.length; x++){
  51. if("RequiredModel" in files[x].UANodeSet.Models.Model){
  52. if(Array.isArray(files[x].UANodeSet.Models.Model.RequiredModel)){
  53. var len = files[x].UANodeSet.Models.Model.RequiredModel.length;
  54. for(let y = 0; y < len; y++){
  55. files[x].UANodeSet.Models.Model.RequiredModel[y]
  56. }
  57. }else{
  58. }
  59. }else{
  60. nsets.push(files[x].UANodeSet.Models.Model.ModelUri);
  61. }
  62. }
  63. }
  64. function get_Children(jsnob, nid, ret_obj){
  65. const entities = ['UAObjectType','UAObject','UAVariableType','UAVariable', 'UADataType', 'UAReferenceType', 'UAMethod'];
  66. const set = jsnob.UANodeSet;
  67. for(let i = 0; i< entities.length; i++){
  68. if(entities[i] in set){}
  69. }
  70. try{
  71. if(Array.isArray(jsnob.UANodeSet.UAObject)){
  72. for (let i = 0; i < jsnob.UANodeSet.UAObject.length;i++){
  73. if(Array.isArray(jsnob.UANodeSet.UAObject[i].References.Reference)){
  74. for (let x = 0; x < jsnob.UANodeSet.UAObject[i].References.Reference.length;x++){
  75. if(jsnob.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
  76. if(inverse(jsnob.UANodeSet.UAObject[i].References.Reference[x])){
  77. let zobj= overwriteNode(jsnob.UANodeSet.UAObject[i], nid, "UAObject", getIndexArray(jsnob));
  78. /*let zobj = {
  79. id: jsnob.UANodeSet.UAObject[i].NodeId,
  80. text: jsnob.UANodeSet.UAObject[i].DisplayName,
  81. ReferenceType: jsnob.UANodeSet.UAObject[i].References.Reference[x].ReferenceType,
  82. IsForward: forward(jsnob.UANodeSet.UAObject[i].References.Reference[x]),
  83. }*/
  84. if(!checkDouble(zobj,ret_obj)){
  85. ret_obj.push(zobj);
  86. }
  87. }
  88. }
  89. }
  90. }else{
  91. if(jsnob.UANodeSet.UAObject[i].References.Reference.$t === nid){
  92. //console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject[i].References.Reference) +jsnob.UANodeSet.UAObject[i].References.Reference.ReferenceType);
  93. if(inverse(jsnob.UANodeSet.UAObject[i].References.Reference)){
  94. let zobj= overwriteNode(jsnob.UANodeSet.UAObject[i], nid, "UAObject", getIndexArray(jsnob));
  95. /*let zobj = {
  96. id: jsnob.UANodeSet.UAObject[i].NodeId,
  97. text: jsnob.UANodeSet.UAObject[i].DisplayName,
  98. ReferenceType: jsnob.UANodeSet.UAObject[i].References.Reference.ReferenceType,
  99. IsForward: forward(jsnob.UANodeSet.UAObject[i].References.Reference),
  100. }*/
  101. if(!checkDouble(zobj,ret_obj)){
  102. ret_obj.push(zobj);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }else{
  109. if(Array.isArray(jsnob.UANodeSet.UAObject.References.Reference)){
  110. for (let x = 0; x < jsnob.UANodeSet.UAObject.References.Reference.length;x++){
  111. if(jsnob.UANodeSet.UAObject.References.Reference[x].$t === nid){
  112. //console.log(jsnob.UANodeSet.UAObject.BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference[x]) +jsnob.UANodeSet.UAObject.References.Reference[x].ReferenceType);
  113. if(inverse(jsnob.UANodeSet.UAObject.References.Reference[x])){
  114. let zobj= overwriteNode(jsnob.UANodeSet.UAObject, nid, "UAObject", getIndexArray(jsnob));
  115. /*let zobj = {
  116. id: jsnob.UANodeSet.UAObject.NodeId,
  117. text: jsnob.UANodeSet.UAObject[i].DisplayName,
  118. ReferenceType: jsnob.UANodeSet.UAObject.References.Reference[x].ReferenceType,
  119. IsForward: forward(jsnob.UANodeSet.UAObject.References.Reference[x]),
  120. }*/
  121. if(!checkDouble(zobj,ret_obj)){
  122. ret_obj.push(zobj);
  123. }
  124. }
  125. }
  126. }
  127. }else{
  128. if(jsnob.UANodeSet.UAObject.References.Reference.$t === nid){
  129. //console.log(jsnob.UANodeSet.UAObject.BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference) +jsnob.UANodeSet.UAObject.References.Reference.ReferenceType);
  130. if(inverse(jsnob.UANodeSet.UAObject.References.Reference)){
  131. let zobj= overwriteNode(jsnob.UANodeSet.UAObject, nid, "UAObject", getIndexArray(jsnob));
  132. /*let zobj = {
  133. id: jsnob.UANodeSet.UAObject.NodeId,
  134. text: jsnob.UANodeSet.UAObject.DisplayName,
  135. ReferenceType: jsnob.UANodeSet.UAObject.References.Reference.ReferenceType,
  136. IsForward: forward(jsnob.UANodeSet.UAObject.References.Reference),
  137. }*/
  138. if(!checkDouble(zobj,ret_obj)){
  139. ret_obj.push(zobj);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }catch(err){
  146. console.log("NOK");
  147. }
  148. }
  149. function getAllChildren(nid, ret_obj){
  150. for(let ix = 0; ix < ns_array.length; ix++){
  151. get_Children(ns_array[ix][0], nid, ret_obj);
  152. }
  153. }
  154. function getIndexObj(jsnob, ns){
  155. var uri = ns.UANodeSet.Models.Model.ModelUri;
  156. if(uri == "http://opcfoundation.org/UA/"){
  157. return 0;
  158. }
  159. console.log(uri);
  160. let uris = {};
  161. if(jsnob.UANodeSet.NamespaceUris != undefined){
  162. uris = jsnob.UANodeSet.NamespaceUris.Uri;
  163. console.log(uris);
  164. }else{
  165. return 0;
  166. }
  167. for(let i = 0; i < uris.length; i++){
  168. console.log(uris[i]);
  169. if(uris[i] == uri){
  170. return i+1;
  171. }
  172. }
  173. return undefined;
  174. }
  175. function getIndexName(jsnob, name){
  176. if(name == "http://opcfoundation.org/UA/"){
  177. return 0;
  178. }
  179. let uris = {};
  180. if(jsnob.UANodeSet.NamespaceUris != undefined){
  181. uris = jsnob.UANodeSet.NamespaceUris.Uri;
  182. }else{
  183. return 0;
  184. }
  185. for(let i = 0; i < uris.length; i++){
  186. if(uris[i] == name){
  187. return i+1;
  188. }
  189. }
  190. return undefined;
  191. }
  192. function getIndexArray(tobj){
  193. for(let i = 0; i < ns_array.length;i++){
  194. console.log(ns_array[i][1]);
  195. if(tobj.UANodeSet.Models.Model.ModelUri === ns_array[i][0].UANodeSet.Models.Model.ModelUri){
  196. return i;
  197. }
  198. }
  199. return undefined;
  200. }
  201. function getNode(jsnob, ns, id){
  202. let uris = {};
  203. var ns_uri = '';
  204. if(jsnob.UANodeSet.NamespaceUris){
  205. uris = jsnob.UANodeSet.NamespaceUris.Uri;
  206. ns_uri = uris[ns-1];
  207. }
  208. for(let i = 0; i < ns_array.length; i++){
  209. if(ns_array[i][0].UANodeSet.Models.Model.ModelUri == ns_uri){
  210. return i;
  211. }
  212. }
  213. return -1;
  214. }
  215. function overwriteNode(nobj, nid, nc, ns){
  216. const node = {
  217. id:nobj.NodeId,
  218. text:nobj.DisplayName,
  219. children: true,
  220. data:{
  221. browsename: nobj.BrowseName,
  222. namespace: ns,
  223. nodeclass: nc,
  224. parentNode: nid,
  225. parentid: "ParentNodeId" in nobj ? nobj.ParentNodeId : null,
  226. description: "Description" in nobj ? nobj.Description : null,
  227. notifier: "EventNotifier" in nobj ? nobj.EventNotifier : null, //Object
  228. isabstract: "IsAbstract" in nobj ? nobj.IsAbstract : null, //ObjectType
  229. datatype: "DataType" in nobj ? nobj.DataType : null, //Variable
  230. value: "Value" in nobj ? nobj.Value : null, //Variable
  231. arraydimensions: "ArrayDimensions" in nobj ? nobj.ArrayDimensions : null, //Variable
  232. references: "References" in nobj ? nobj.References : null
  233. }
  234. }
  235. return node;
  236. }
  237. function searchNode(id){
  238. const entities = ['UAObjectType','UAObject','UAVariableType','UAVariable', 'UADataType', 'UAReferenceType', 'UAMethod']; //7
  239. var nid = "";
  240. var jsnob = {};
  241. if(id.includes("ns=")){
  242. var ns = id.substring(id.length - 4);
  243. const ns1 = ns.split(";");
  244. var nsidx = parseInt(ns1[0]);
  245. jsnob = ns_array[nsidx][0];
  246. }else{
  247. nid = id;
  248. jsnob = ns_array[0][0];
  249. }
  250. const set = jsnob.UANodeSet;
  251. for(let i = 0; i< entities.length; i++){
  252. if(entities[i] in set){
  253. console.log(set.Models.Model.ModelUri+" has a "+entities[i]);
  254. if(Array.isArray(set[entities[i]])){
  255. for(let x = 0; x < set[entities[i]].length;x++){
  256. if(set[entities[i]][x].NodeId === nid){
  257. let nobj = set[entities[i]][x];
  258. const node = overwriteNode(nobj, null, entities[i], getIndexArray(jsnob));
  259. return node;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. return null;
  266. }
  267. async function redis_get(){
  268. try{
  269. const val = await client.json.get('mt',{
  270. path:'.UANodeSet.Models.Model.Version',
  271. });
  272. console.log(`Value is: ${val}`);}catch(err){
  273. console.log("NOK");
  274. }
  275. }
  276. async function redis_flush(){
  277. await client.sendCommand(['FLUSHALL']);
  278. }
  279. function p2json(name, shorty){
  280. try{
  281. const nn = name+".xml";
  282. var nnn = shorty+".json";
  283. fs.readFile( nn, function(err, data){
  284. //fs.readFile( "Opc.Ua.NodeSet2.xml", function(err, data){
  285. let jsn = parser.toJson(data);
  286. fs.writeFileSync(nnn, jsn);
  287. });
  288. }catch(err){
  289. console.log('not work');}
  290. }
  291. app.get('/setredis/:ident',(req, res) =>{
  292. try{
  293. var shorty = req.params["ident"];
  294. const id = './'+shorty+".json";
  295. const fl = require(id);
  296. redis_set(shorty, fl);
  297. res.json({result:"OK"});
  298. }catch(err){
  299. console.log("NOK");
  300. res.json({result:"NOK"});
  301. }
  302. })
  303. app.get('/requirejson/:ident/:name',(req, res) =>{
  304. try{
  305. var namejs = req.params["name"];
  306. var shorty = req.params["ident"];
  307. const id = "./"+namejs+".json";
  308. const fl = require(id);
  309. for(let i = 0; i < ns_array.length; i++){
  310. if(ns_array[i][1] === shorty){
  311. return res.json({result:"Array already includes element."})
  312. }
  313. }
  314. ns_array.push([fl, shorty, fl.UANodeSet.Models.Model.ModelUri]);
  315. res.json({result:"OK"});
  316. }catch(err){
  317. console.log("NOK");
  318. res.json({result:"NOK", error: "Json file may not be available. Call list json files."});
  319. }
  320. })
  321. app.get('/shownsarray',(req, res) =>{
  322. try{ let str = '';
  323. for(let i = 0; i < ns_array.length; i++){
  324. str += '('+i+': '+ns_array[i][1]+')';
  325. }
  326. res.json({result:str});
  327. console.log(ns_array);
  328. }catch(err){
  329. console.log("NOK");
  330. res.json({result:"NOK", error: "Json file may not be available. Call list json files."});
  331. }
  332. })
  333. app.get('/getredis',(req, res) =>{
  334. try{
  335. redis_get();
  336. res.json({result:"OK"});
  337. }catch(err){
  338. console.log("NOK");
  339. res.json({result:"NOK"});
  340. }
  341. })
  342. app.get('/get_root',(req, res) =>{
  343. try{
  344. let rt = jsn.UANodeSet.UAObject.find( record => record.NodeId === "i=84");
  345. let resj = {
  346. id: rt.NodeId,
  347. parent: "#",
  348. text: rt.DisplayName};
  349. res.json(resj);
  350. }catch(err){
  351. console.log("NOK");
  352. res.json({result:"NOK"});
  353. }
  354. })
  355. app.get('/get_children/base/:id',(req, res) =>{
  356. var nid = req.params["id"];
  357. try{
  358. if(Array.isArray(jsn.UANodeSet.UAObject)){
  359. for (let i = 0; i < jsn.UANodeSet.UAObject.length;i++){
  360. if(Array.isArray(jsn.UANodeSet.UAObject[i].References.Reference)){
  361. for (let x = 0; x < jsn.UANodeSet.UAObject[i].References.Reference.length;x++){
  362. if(jsn.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
  363. console.log(jsn.UANodeSet.UAObject[i].BrowseName + inverse(jsn.UANodeSet.UAObject[i].References.Reference[x]) +jsn.UANodeSet.UAObject[i].References.Reference[x].ReferenceType);
  364. }
  365. }
  366. }else{
  367. if(jsn.UANodeSet.UAObject[i].References.Reference.$t === nid){
  368. console.log(jsn.UANodeSet.UAObject[i].BrowseName + inverse(jsn.UANodeSet.UAObject[i].References.Reference) +jsn.UANodeSet.UAObject[i].References.Reference.ReferenceType);
  369. }
  370. }
  371. }
  372. }else{
  373. if(Array.isArray(jsn.UANodeSet.UAObject.References.Reference)){
  374. for (let x = 0; x < jsn.UANodeSet.UAObject.References.Reference.length;x++){
  375. if(jsn.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
  376. console.log(jsn.UANodeSet.UAObject[i].BrowseName + inverse(jsn.UANodeSet.UAObject.References.Reference[x]) +jsn.UANodeSet.UAObject.References.Reference[x].ReferenceType);
  377. }
  378. }
  379. }else{
  380. if(jsn.UANodeSet.UAObject.References.Reference.$t === nid){
  381. console.log(jsn.UANodeSet.UAObject.BrowseName + inverse(jsn.UANodeSet.UAObject.References.Reference) +jsn.UANodeSet.UAObject.References.Reference.ReferenceType);
  382. }
  383. }
  384. }
  385. console.log('');
  386. res.json({result:"OK"});
  387. }catch(err){
  388. console.log("NOK");
  389. res.json({result:"NOK"});
  390. }
  391. })
  392. app.get('/get_children/all/:ns/:id',(req, res) =>{
  393. var ret_obj = [];
  394. var ns = req.params["ns"];
  395. var id = req.params["id"];
  396. var nid = 'ns='+ns+';'+'i='+id;
  397. console.log(nid);
  398. get_Children(di, nid, ret_obj);
  399. get_Children(ia, nid, ret_obj);
  400. get_Children(mach, nid, ret_obj);
  401. get_Children(mt, nid, ret_obj);
  402. res.json(ret_obj);
  403. })
  404. app.get('/getNode/all/:ns/:id',(req, res) =>{
  405. var ret_obj = [];
  406. var ns = req.params["ns"];
  407. var id = req.params["id"];
  408. var nid = 'ns='+ns+';'+'i='+id;
  409. console.log(nid);
  410. get_Children(di, nid, ret_obj);
  411. get_Children(ia, nid, ret_obj);
  412. get_Children(mach, nid, ret_obj);
  413. get_Children(mt, nid, ret_obj);
  414. let node = {
  415. id:nid,
  416. text: "home",
  417. children:ret_obj
  418. }
  419. res.json(node);
  420. })
  421. app.get('/flushall',(req, res) =>{
  422. redis_flush();
  423. res.json({result:"OK"});
  424. })
  425. app.get('/p2json/:nodeset/:ident',(req, res) =>{
  426. var name = req.params["nodeset"];
  427. var shorty = req.params["ident"];
  428. p2json(name, shorty);
  429. res.json({result:"OK"});
  430. })
  431. app.get('/list_projects', (req, res) => { //reacts to requests -> /list_projects
  432. const output = execSync("cd .. && cd Projects && ls", { encoding: 'utf-8' });
  433. var out = output.split("\n");
  434. if(out[out.length-1] === ""){
  435. out.splice(-1,1);
  436. }
  437. console.log("Response: ", out);
  438. res.json({result:out});
  439. })
  440. app.get('/list_nodesets', (req, res) => { //reacts to requests -> /list_nodesets
  441. const output = execSync("cd .. && cd nodesets && ls", { encoding: 'utf-8' });
  442. var out = output.split("\n");
  443. var result = [];
  444. for(let i = 0; i < out.length; i++){
  445. if(out[i].includes(".xml")){
  446. result.push(out[i]);
  447. }
  448. }
  449. console.log("Response: ", result);
  450. res.json({result:result});
  451. })
  452. app.get('/new_project', (req, res) => { //reacts to requests -> /new_project?name=myproject
  453. const name = req.query.name; //project name
  454. const output = execSync("cd .. && cd Projects && ls", { encoding: 'utf-8' });
  455. var out = output.split("\n");
  456. var result = "";
  457. var err = "Error - Project already exists.";
  458. for(let i = 0; i < out.length; i++){
  459. if(out[i] === name){
  460. return res.json({result:err});
  461. }
  462. }
  463. const dir = execSync("cd .. && cd Projects && mkdir "+name, { encoding: 'utf-8' });
  464. var cp = execSync("sudo cp /home/pi/ModelingTool/nodesets/Opc.Ua.NodeSet2.xml /home/pi/ModelingTool/Projects/"+name+"/Opc.Ua.NodeSet2.xml", { encoding: 'utf-8' });
  465. cp = execSync("sudo cp /home/pi/ModelingTool/nodesets/Opc.Ua.NodeSet2.json /home/pi/ModelingTool/Projects/"+name+"/Opc.Ua.NodeSet2.json", { encoding: 'utf-8' });
  466. const cur_dir = execSync("cd .. && cd Projects && cd "+name+" && ls", { encoding: 'utf-8' });
  467. console.log("New Project was created.");
  468. res.json({result:cur_dir});
  469. })
  470. app.get('/save_project', (req, res) => { //reacts to requests -> /save_project?name=myproject
  471. const name = req.query.name; //project name
  472. const output = execSync("sudo cp /var/www/html/root.json /home/pi/ModelingTool/Projects/"+name+"/"+name+".json", { encoding: 'utf-8' });
  473. console.log("Project "+name+" was saved.");
  474. res.json({result:"Project saved"});
  475. })
  476. app.get('/load_json', (req, res) => { //reacts to requests -> /load_json?name=myproject
  477. const name = req.query.name; //project name
  478. var output = execSync("cd .. && cd Projects && ls", { encoding: 'utf-8' });
  479. var out = output.split("\n");
  480. var result = "";
  481. var err = "Error - Project or file not available.";
  482. for(let i = 0; i < out.length; i++){
  483. if(out[i] === name){
  484. var output1 = execSync("cd .. && cd Projects && cd "+name+" && ls", { encoding: 'utf-8' });
  485. var out1 = output1.split("\n");
  486. var chk = name+".json"
  487. for(let y = 0; y < out1.length; y++){
  488. if(out1[y] === chk){
  489. result = execSync("sudo cp /home/pi/ModelingTool/Projects/"+name+"/"+name+".json /var/www/html/root.json", { encoding: 'utf-8' });
  490. console.log("Project has been loaded.");
  491. return res.json({result:"Project loaded"});
  492. }
  493. }
  494. }
  495. }
  496. res.json({result:err});
  497. })
  498. app.get('/root', (req, res) => { //reacts to requests -> /root
  499. console.log("Return root.");
  500. 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}]}]);
  501. })
  502. function isChildRef(obj){
  503. if('IsForward' in obj && obj.IsForward === 'false'){
  504. return false;
  505. }
  506. if('IsForward' in obj && obj.IsForward === 'true'){
  507. switch (obj.ReferenceType) {
  508. case 'Organizes': return true;
  509. case 'HasComponent': return true;
  510. case 'HasProperty': return true;
  511. case 'HasAddIn': return true;
  512. default: return false;
  513. }
  514. }
  515. switch (obj.ReferenceType) {
  516. case 'Organizes': return true;
  517. case 'HasComponent': return true;
  518. case 'HasProperty': return true;
  519. case 'HasAddIn': return true;
  520. default: return false;
  521. }
  522. return false;
  523. }
  524. function checkDouble(item, obj){
  525. for(let i = 0; i < obj.length; i++){
  526. if(item.id === obj[i].id){
  527. return true;
  528. }
  529. }
  530. return false;
  531. }
  532. app.get('/lazy', (req, res) => {
  533. //console.log(req);
  534. var ret_obj = [];
  535. let node = {};
  536. if(req.query.id == "#"){
  537. //res.json([{id:"i=84",text:"Root",data:{nodeclass:"Object"},children:true}]);
  538. 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}]}]);
  539. }
  540. var nid = req.query.id;
  541. node = searchNode(nid);
  542. if(node.data.references != null){
  543. console.log("Here:"+node.data.references);
  544. if(Array.isArray(node.data.references.Reference)){
  545. console.log("And:"+node.data.references.Reference);
  546. for(let x = 0; x < node.data.references.Reference.length; x++ ){
  547. if(isChildRef(node.data.references.Reference[x])){
  548. var cnode = searchNode(node.data.references.Reference[x].$t);
  549. if(!checkDouble(cnode,ret_obj)){
  550. ret_obj.push(cnode);
  551. }
  552. }
  553. }
  554. }else{
  555. console.log("But:"+node.data.references);
  556. if(isChildRef(node.data.references)){
  557. var cnode = searchNode(node.data.references.$t);
  558. if(!checkDouble(cnode,ret_obj)){
  559. ret_obj.push(cnode);
  560. }
  561. }
  562. }
  563. }
  564. getAllChildren(nid, ret_obj);
  565. console.log(ret_obj);
  566. res.json(ret_obj);
  567. });
  568. app.get('/index/:ns1/:ns2', (req, res) => {
  569. var n1 = req.params["ns1"];
  570. var n2 = req.params["ns2"];
  571. var num = getIndexObj(ns_array[n1][0],ns_array[n2][0]);
  572. console.log(getNode(ns_array[3][0],2,15048));
  573. res.send("Index of "+ns_array[n2][1]+' in '+ns_array[n1][1]+' is '+num);
  574. });
  575. app.get('/test/:id', (req, res) => { //test
  576. var n1 = parseInt(req.params["id"]);
  577. console.log(ns_array[n1][1],);
  578. var ret = searchNode("i=85");
  579. console.log(ret);
  580. res.send("All good");
  581. //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}]}]);
  582. })
  583. app.get('/require/all', (req, res) => { //test
  584. ns_array.push([jsn, "base", jsn.UANodeSet.Models.Model.ModelUri]);
  585. ns_array.push([di, "di", di.UANodeSet.Models.Model.ModelUri]);
  586. ns_array.push([ia, "ia", ia.UANodeSet.Models.Model.ModelUri]);
  587. ns_array.push([mach, "mach", mach.UANodeSet.Models.Model.ModelUri]);
  588. ns_array.push([mt, "mt", mt.UANodeSet.Models.Model.ModelUri]);
  589. res.json({result:"OK"});
  590. })
  591. app.listen(port, () => {
  592. console.log("Server listening at http://localhost:", port);
  593. })