services.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. const client = redis.createClient();
  7. const app = express();
  8. const port = 3000;
  9. //client.connect('127.0.0.1', 6379);
  10. const jsn = require("./base.json");
  11. const di = require("./di.json");
  12. const ia = require("./ia.json");
  13. const mach = require("./machinery.json");
  14. const mt = require("./mt.json");
  15. let ns_array = [];
  16. console.log(typeof jsn);
  17. //console.log(jsn.UANodeSet.UAObject.find( record => record.NodeId === "i=84"));
  18. client.on('connect', function(){
  19. console.log('Connected.');
  20. });
  21. async function redis_set(shorty, data){
  22. await client.json.set(shorty,'.',data);
  23. }
  24. function inverse(dev){
  25. var str = '';
  26. if("IsForward" in dev){
  27. if(dev.IsForward === "false"){
  28. str = "<-";
  29. }
  30. }else{
  31. str = "->";
  32. }
  33. return str;
  34. }
  35. function forward(dev){
  36. var b = true;
  37. if("IsForward" in dev){
  38. if(dev.IsForward === "false"){
  39. b = false;
  40. }
  41. }else{
  42. b = true;
  43. }
  44. return b;
  45. }
  46. function sortNS(files){
  47. let nsets = [];
  48. for(let x = 0; x<files.length; x++){
  49. if("RequiredModel" in files[x].UANodeSet.Models.Model){
  50. if(Array.isArray(files[x].UANodeSet.Models.Model.RequiredModel)){
  51. var len = files[x].UANodeSet.Models.Model.RequiredModel.length;
  52. for(let y = 0; y < len; y++){
  53. files[x].UANodeSet.Models.Model.RequiredModel[y]
  54. }
  55. }else{
  56. }
  57. }else{
  58. nsets.push(files[x].UANodeSet.Models.Model.ModelUri);
  59. }
  60. }
  61. }
  62. function get_Children(jsnob, nid, ret_obj){
  63. try{
  64. if(Array.isArray(jsnob.UANodeSet.UAObject)){
  65. for (let i = 0; i < jsnob.UANodeSet.UAObject.length;i++){
  66. if(Array.isArray(jsnob.UANodeSet.UAObject[i].References.Reference)){
  67. for (let x = 0; x < jsnob.UANodeSet.UAObject[i].References.Reference.length;x++){
  68. if(jsnob.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
  69. console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject[i].References.Reference[x]) +jsnob.UANodeSet.UAObject[i].References.Reference[x].ReferenceType);
  70. let zobj = {
  71. id: jsnob.UANodeSet.UAObject[i].NodeId,
  72. ReferenceType: jsnob.UANodeSet.UAObject[i].References.Reference[x].ReferenceType,
  73. IsForward: forward(jsnob.UANodeSet.UAObject[i].References.Reference[x]),
  74. }
  75. ret_obj.push(zobj);
  76. }
  77. }
  78. }else{
  79. if(jsnob.UANodeSet.UAObject[i].References.Reference.$t === nid){
  80. console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject[i].References.Reference) +jsnob.UANodeSet.UAObject[i].References.Reference.ReferenceType);
  81. let zobj = {
  82. id: jsnob.UANodeSet.UAObject[i].NodeId,
  83. ReferenceType: jsnob.UANodeSet.UAObject[i].References.Reference.ReferenceType,
  84. IsForward: forward(jsnob.UANodeSet.UAObject[i].References.Reference),
  85. }
  86. ret_obj.push(zobj);
  87. }
  88. }
  89. }
  90. }else{
  91. if(Array.isArray(jsnob.UANodeSet.UAObject.References.Reference)){
  92. for (let x = 0; x < jsnob.UANodeSet.UAObject.References.Reference.length;x++){
  93. if(jsnob.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
  94. console.log(jsnob.UANodeSet.UAObject[i].BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference[x]) +jsnob.UANodeSet.UAObject.References.Reference[x].ReferenceType);
  95. let zobj = {
  96. id: jsnob.UANodeSet.UAObject.NodeId,
  97. ReferenceType: jsnob.UANodeSet.UAObject.References.Reference[x].ReferenceType,
  98. IsForward: forward(jsnob.UANodeSet.UAObject.References.Reference[x]),
  99. }
  100. ret_obj.push(zobj);
  101. }
  102. }
  103. }else{
  104. if(jsnob.UANodeSet.UAObject.References.Reference.$t === nid){
  105. console.log(jsnob.UANodeSet.UAObject.BrowseName + inverse(jsnob.UANodeSet.UAObject.References.Reference) +jsnob.UANodeSet.UAObject.References.Reference.ReferenceType);
  106. let zobj = {
  107. id: jsnob.UANodeSet.UAObject.NodeId,
  108. ReferenceType: jsnob.UANodeSet.UAObject.References.Reference.ReferenceType,
  109. IsForward: forward(jsnob.UANodeSet.UAObject.References.Reference),
  110. }
  111. ret_obj.push(zobj);
  112. }
  113. }
  114. }
  115. }catch(err){
  116. console.log("NOK");
  117. }
  118. }
  119. async function redis_get(){
  120. try{
  121. const val = await client.json.get('mt',{
  122. path:'.UANodeSet.Models.Model.Version',
  123. });
  124. console.log(`Value is: ${val}`);}catch(err){
  125. console.log("NOK");
  126. }
  127. }
  128. async function redis_flush(){
  129. await client.sendCommand(['FLUSHALL']);
  130. }
  131. function p2json(name, shorty){
  132. try{
  133. const nn = name+".xml";
  134. var nnn = shorty+".json";
  135. fs.readFile( nn, function(err, data){
  136. //fs.readFile( "Opc.Ua.NodeSet2.xml", function(err, data){
  137. let jsn = parser.toJson(data);
  138. fs.writeFileSync(nnn, jsn);
  139. });
  140. }catch(err){
  141. console.log('not work');}
  142. }
  143. app.get('/setredis/:ident',(req, res) =>{
  144. try{
  145. var shorty = req.params["ident"];
  146. const id = './'+shorty+".json";
  147. const fl = require(id);
  148. redis_set(shorty, fl);
  149. res.json({result:"OK"});
  150. }catch(err){
  151. console.log("NOK");
  152. res.json({result:"NOK"});
  153. }
  154. })
  155. app.get('/requirejson/:ident/:name',(req, res) =>{
  156. try{
  157. var namejs = req.params["name"];
  158. var shorty = req.params["ident"];
  159. const id = "./"+namejs+".json";
  160. const fl = require(id);
  161. for(let i = 0; i < ns_array.length; i++){
  162. if(ns_array[i][1] === shorty){
  163. return res.json({result:"Array already includes element."})
  164. }
  165. }
  166. ns_array.push([fl, shorty]);
  167. res.json({result:"OK"});
  168. }catch(err){
  169. console.log("NOK");
  170. res.json({result:"NOK", error: "Json file may not be available. Call list json files."});
  171. }
  172. })
  173. app.get('/shownsarray',(req, res) =>{
  174. try{ let str = '';
  175. for(let i = 0; i < ns_array.length; i++){
  176. str += '('+i+': '+ns_array[i][1]+')';
  177. }
  178. res.json({result:str});
  179. console.log(ns_array);
  180. }catch(err){
  181. console.log("NOK");
  182. res.json({result:"NOK", error: "Json file may not be available. Call list json files."});
  183. }
  184. })
  185. app.get('/getredis',(req, res) =>{
  186. try{
  187. redis_get();
  188. res.json({result:"OK"});
  189. }catch(err){
  190. console.log("NOK");
  191. res.json({result:"NOK"});
  192. }
  193. })
  194. app.get('/get_root',(req, res) =>{
  195. try{
  196. let rt = jsn.UANodeSet.UAObject.find( record => record.NodeId === "i=84");
  197. let resj = {
  198. id: rt.NodeId,
  199. parent: "#",
  200. text: rt.DisplayName};
  201. res.json(resj);
  202. }catch(err){
  203. console.log("NOK");
  204. res.json({result:"NOK"});
  205. }
  206. })
  207. app.get('/get_children/base/:id',(req, res) =>{
  208. var nid = req.params["id"];
  209. try{
  210. if(Array.isArray(jsn.UANodeSet.UAObject)){
  211. for (let i = 0; i < jsn.UANodeSet.UAObject.length;i++){
  212. if(Array.isArray(jsn.UANodeSet.UAObject[i].References.Reference)){
  213. for (let x = 0; x < jsn.UANodeSet.UAObject[i].References.Reference.length;x++){
  214. if(jsn.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
  215. console.log(jsn.UANodeSet.UAObject[i].BrowseName + inverse(jsn.UANodeSet.UAObject[i].References.Reference[x]) +jsn.UANodeSet.UAObject[i].References.Reference[x].ReferenceType);
  216. }
  217. }
  218. }else{
  219. if(jsn.UANodeSet.UAObject[i].References.Reference.$t === nid){
  220. console.log(jsn.UANodeSet.UAObject[i].BrowseName + inverse(jsn.UANodeSet.UAObject[i].References.Reference) +jsn.UANodeSet.UAObject[i].References.Reference.ReferenceType);
  221. }
  222. }
  223. }
  224. }else{
  225. if(Array.isArray(jsn.UANodeSet.UAObject.References.Reference)){
  226. for (let x = 0; x < jsn.UANodeSet.UAObject.References.Reference.length;x++){
  227. if(jsn.UANodeSet.UAObject[i].References.Reference[x].$t === nid){
  228. console.log(jsn.UANodeSet.UAObject[i].BrowseName + inverse(jsn.UANodeSet.UAObject.References.Reference[x]) +jsn.UANodeSet.UAObject.References.Reference[x].ReferenceType);
  229. }
  230. }
  231. }else{
  232. if(jsn.UANodeSet.UAObject.References.Reference.$t === nid){
  233. console.log(jsn.UANodeSet.UAObject.BrowseName + inverse(jsn.UANodeSet.UAObject.References.Reference) +jsn.UANodeSet.UAObject.References.Reference.ReferenceType);
  234. }
  235. }
  236. }
  237. console.log('');
  238. res.json({result:"OK"});
  239. }catch(err){
  240. console.log("NOK");
  241. res.json({result:"NOK"});
  242. }
  243. })
  244. app.get('/get_children/all/:ns/:id',(req, res) =>{
  245. var ret_obj = [];
  246. var ns = req.params["ns"];
  247. var id = req.params["id"];
  248. var nid = ns+';'+id;
  249. get_Children(di, nid, ret_obj);
  250. get_Children(ia, nid, ret_obj);
  251. get_Children(mach, nid, ret_obj);
  252. get_Children(mt, nid, ret_obj);
  253. res.json(ret_obj);
  254. })
  255. app.get('/flushall',(req, res) =>{
  256. redis_flush();
  257. res.json({result:"OK"});
  258. })
  259. app.get('/p2json/:nodeset/:ident',(req, res) =>{
  260. var name = req.params["nodeset"];
  261. var shorty = req.params["ident"];
  262. p2json(name, shorty);
  263. res.json({result:"OK"});
  264. })
  265. app.get('/list_projects', (req, res) => { //reacts to requests -> /list_projects
  266. const output = execSync("cd .. && cd Projects && ls", { encoding: 'utf-8' });
  267. var out = output.split("\n");
  268. if(out[out.length-1] === ""){
  269. out.splice(-1,1);
  270. }
  271. console.log("Response: ", out);
  272. res.json({result:out});
  273. })
  274. app.get('/list_nodesets', (req, res) => { //reacts to requests -> /list_nodesets
  275. const output = execSync("cd .. && cd nodesets && ls", { encoding: 'utf-8' });
  276. var out = output.split("\n");
  277. var result = [];
  278. for(let i = 0; i < out.length; i++){
  279. if(out[i].includes(".xml")){
  280. result.push(out[i]);
  281. }
  282. }
  283. console.log("Response: ", result);
  284. res.json({result:result});
  285. })
  286. app.get('/new_project', (req, res) => { //reacts to requests -> /new_project?name=myproject
  287. const name = req.query.name; //project name
  288. const output = execSync("cd .. && cd Projects && ls", { encoding: 'utf-8' });
  289. var out = output.split("\n");
  290. var result = "";
  291. var err = "Error - Project already exists.";
  292. for(let i = 0; i < out.length; i++){
  293. if(out[i] === name){
  294. return res.json({result:err});
  295. }
  296. }
  297. const dir = execSync("cd .. && cd Projects && mkdir "+name, { encoding: 'utf-8' });
  298. 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' });
  299. cp = execSync("sudo cp /home/pi/ModelingTool/nodesets/Opc.Ua.NodeSet2.json /home/pi/ModelingTool/Projects/"+name+"/Opc.Ua.NodeSet2.json", { encoding: 'utf-8' });
  300. const cur_dir = execSync("cd .. && cd Projects && cd "+name+" && ls", { encoding: 'utf-8' });
  301. console.log("New Project was created.");
  302. res.json({result:cur_dir});
  303. })
  304. app.get('/save_project', (req, res) => { //reacts to requests -> /save_project?name=myproject
  305. const name = req.query.name; //project name
  306. const output = execSync("sudo cp /var/www/html/root.json /home/pi/ModelingTool/Projects/"+name+"/"+name+".json", { encoding: 'utf-8' });
  307. console.log("Project "+name+" was saved.");
  308. res.json({result:"Project saved"});
  309. })
  310. app.get('/load_json', (req, res) => { //reacts to requests -> /load_json?name=myproject
  311. const name = req.query.name; //project name
  312. var output = execSync("cd .. && cd Projects && ls", { encoding: 'utf-8' });
  313. var out = output.split("\n");
  314. var result = "";
  315. var err = "Error - Project or file not available.";
  316. for(let i = 0; i < out.length; i++){
  317. if(out[i] === name){
  318. var output1 = execSync("cd .. && cd Projects && cd "+name+" && ls", { encoding: 'utf-8' });
  319. var out1 = output1.split("\n");
  320. var chk = name+".json"
  321. for(let y = 0; y < out1.length; y++){
  322. if(out1[y] === chk){
  323. result = execSync("sudo cp /home/pi/ModelingTool/Projects/"+name+"/"+name+".json /var/www/html/root.json", { encoding: 'utf-8' });
  324. console.log("Project has been loaded.");
  325. return res.json({result:"Project loaded"});
  326. }
  327. }
  328. }
  329. }
  330. res.json({result:err});
  331. })
  332. app.get('/root', (req, res) => { //reacts to requests -> /root
  333. console.log("Return root.");
  334. res.json([{id:1,text:"Root node",children:[{id:2,text:"Child node 1",children:true},{id:3,text:"Child node 2"}]}]);
  335. })
  336. app.listen(port, () => {
  337. console.log("Server listening at http://localhost:", port);
  338. })