_model.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. var fs = require('fs');
  2. var path = require('path');
  3. const net = require('net');
  4. var exec = require('child_process').exec;
  5. const {
  6. spawn
  7. } = require('child_process');
  8. const {
  9. fork
  10. } = require('child_process');
  11. const {
  12. Readable
  13. } = require('stream');
  14. const { execSync } = require('child_process');
  15. const io = require('socket.io')();
  16. var data;
  17. var cla;
  18. var size = 5;
  19. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. //Initialize codes.
  21. AutoLoadPlugins();
  22. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. //You can load the plugins in global field, if the next line codes are uncommented.
  24. AutoLoadGlobalPlugins();
  25. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. //Sample codes.
  27. exports.Test = function() {
  28. console.log("Hi, I'm a model test funciton");
  29. };
  30. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //Put you codes here
  32. exports.setCla = function(input){
  33. cla = input;
  34. };
  35. exports.getCla = function(){
  36. return cla;
  37. };
  38. exports.setSize = function(input){
  39. size = input;
  40. };
  41. exports.getSize = function(){
  42. return size;
  43. };
  44. exports.call = function(cb) {
  45. var input = ['D:/dcip/dcip/dcip/dcip/comx_sdk.node/unit/ADIGA/data/iga01.kms\n'];
  46. var iterator = input[Symbol.iterator]();
  47. const input_readable = Readable.from(iterator);
  48. var cp = spawn('D:/dcip/dcip/dcip/dcip/comx_sdk.node/unit/ADIGA/data/DE_Quadr_Wmi.exe');
  49. input_readable.pipe(cp.stdin);
  50. cp.stdout.on('data', data => {
  51. cb(data.toString());
  52. });
  53. };
  54. exports.getPID = function() {
  55. return process.pid;
  56. };
  57. exports.getCurrentDirectory2 = () => {
  58. var currentWorkingDirectory = process.cwd();
  59. var twoLevelsUpDirectory = path.join(currentWorkingDirectory, '..', '..', '..', '..');
  60. return twoLevelsUpDirectory;
  61. };
  62. exports.startSubProcess = function(cb, on_msg, on_msg01, cmd, cwds) {
  63. //let cmd = path.join("d:\\", 'QDebugTest.exe').replace(/\\/g, '/');
  64. //let cmd = path.join("d:\\", 'dcip\\dcip\\dcip\\dcip\\comx_sdk.node\\unit\\ERIGAS\\src\\QStructure.exe').replace(/\\/g, '/');
  65. //let cmd = "D:/dcip/dcip/dcip/dcip/comx_sdk.node/unit/ERIGAS/src/QStructure.exe";
  66. //let cp = spawn(cmd, ['hide'], {cwd : "D:\\Qt\\Qt-5.15.2\\bin"});
  67. let cp = spawn(cmd, ['hide'], {
  68. //cwd: "D:\\dcip\\dcip\\dcip\\dcip\\comx_sdk.node\\unit\\ERIGAS\\src"
  69. cwd: cwds
  70. });
  71. io.on('connection', client => {
  72. client.on('ready', () => {
  73. if (cb) {
  74. cb(cp.pid);
  75. }
  76. });
  77. client.on('message', msg => {
  78. if (on_msg) {
  79. on_msg(msg);
  80. }
  81. });
  82. client.on('message01', msg01 => {
  83. if (on_msg01) {
  84. on_msg01(msg01);
  85. }
  86. });
  87. });
  88. io.listen(3000);
  89. return io;
  90. };
  91. exports.setData = function(input){
  92. data = input;
  93. };
  94. exports.getData = function(){
  95. return data;
  96. };
  97. exports.getCurrentDirectory = () => {
  98. var currentWorkingDirectory = process.cwd();
  99. var twoLevelsUpDirectory = path.join(currentWorkingDirectory, '..', '..', '..', 'unit', 'variant', '/');
  100. return twoLevelsUpDirectory;
  101. };
  102. exports.write = function(fname, path) { //写文件
  103. var ws = fs.createWriteStream(path);
  104. ws.on('open', () => {
  105. console.log('打开流');
  106. });
  107. ws.write(fname);
  108. ws.on('close', () => {
  109. console.log('关闭流');
  110. });
  111. ws.end();
  112. };
  113. exports.spawn = function(path, cb) {
  114. var cp = spawn(path);
  115. cp.stdout.on('data', data => {
  116. cb(data.toString());
  117. });
  118. cp.stderr.on('data', data => {
  119. cb(data.toString());
  120. });
  121. };
  122. exports.spawns1 = function(path, processNumber, processNumber2, cb){
  123. let cp = spawn(path, [processNumber, processNumber2]);
  124. cp.stdout.on("data", data => {
  125. cb(data.toString());
  126. });
  127. cp.on('exit', code => {
  128. if(cb){
  129. //cb(true);
  130. }
  131. });
  132. };
  133. exports.exec = function(path, cb) {
  134. exec(path, (error, stdout, stderr) => {
  135. if (error) {
  136. //console.error(`exec error: ${error}`);
  137. cb(`exec error: ${error}`);
  138. return;
  139. }
  140. //console.log(`stdout: ${stdout}`);
  141. //console.error(`stderr: ${stderr}`);
  142. cb(`stdout: ${stdout}`);
  143. cb(`stderr: ${stderr}`);
  144. });
  145. };
  146. exports.execs = function(path, cb) {
  147. exec(path, (error, stdout, stderr)=>{
  148. if(error) {
  149. console.error(`exec error: ${error}`);
  150. if(cb){cb(false);}
  151. return;
  152. }
  153. cb(`stdout: ${stdout}`);
  154. cb(`stderr: ${stderr}`);
  155. if(cb){cb(true);}
  156. });
  157. };
  158. function runAbbExe(param, path) {
  159. return new Promise((resolve, reject) => {
  160. const exePath2 = path + 'src/MainPrj.exe';
  161. console.log(exePath2);
  162. exec(`${exePath2} ${param}`, (error, stdout, stderr) => {
  163. if (error) {
  164. reject(error);
  165. } else {
  166. resolve(stdout);
  167. }
  168. });
  169. });
  170. }
  171. // 并行执行 abb.exe 的函数
  172. exports.runAbbExeParallel = async function(path, sec_num) {
  173. const promises = [];
  174. for (let i = 0; i < sec_num; i++) {
  175. console.log(11111);
  176. const argv1 = path + "src/project1/project" + i;
  177. const argv2 = 0;
  178. const argv3 = 1;
  179. const argv4 = 1;
  180. const param = `${argv1} ${argv2} ${argv3} ${argv4}`;
  181. console.log(param);
  182. promises.push(runAbbExe(param, path));
  183. }
  184. const results = await Promise.allSettled(promises);
  185. results.forEach((result, index) => {
  186. if (result.status === 'fulfilled') {
  187. console.log(`Execution ${index} succeeded:`, result.value);
  188. //getstp();
  189. } else {
  190. console.log(`Execution ${index} failed:`, result.reason);
  191. }
  192. });
  193. var p = path + "src/Get_step.exe " + path + '/ ' + path + "/data/beam_shell " + path + '/data/beam_shell/M_shell.stp ' + data;
  194. getstp(p);
  195. };
  196. function getstp(path){
  197. exec(path, (error, stdout, stderr) => {
  198. if (error) {
  199. console.error(`exec error: ${error}`);
  200. //cb(`exec error: ${error}`);
  201. return;
  202. }
  203. console.log(`stdout: ${stdout}`);
  204. console.error(`stderr: ${stderr}`);
  205. //cb(`stdout: ${stdout}`);
  206. //cb(`stderr: ${stderr}`);
  207. });
  208. console.log('变体后处理完成!');
  209. return;
  210. }
  211. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. // AutoLoadPlugins Function Implement Start.
  213. function AutoLoadPlugins() {
  214. var plugin_dir = (__dirname + '/../addon/');
  215. if(!fs.existsSync(plugin_dir)) {
  216. return;
  217. }
  218. var files = fs.readdirSync(plugin_dir);
  219. files.forEach(function(filename){
  220. var filedir = path.join(plugin_dir, filename);
  221. var stats = fs.statSync(filedir);
  222. if(!stats.isDirectory()) {
  223. if(filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
  224. require(filedir);
  225. }
  226. if(filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
  227. require(filedir);
  228. }
  229. }
  230. });
  231. }
  232. function AutoLoadGlobalPlugins() {
  233. var plugin_dir = (process.env.COMX_SDK + 'addon/');
  234. if (!fs.existsSync(plugin_dir)) {
  235. return;
  236. }
  237. var files = fs.readdirSync(plugin_dir);
  238. files.forEach(function(filename) {
  239. var filedir = path.join(plugin_dir, filename);
  240. var stats = fs.statSync(filedir);
  241. if (!stats.isDirectory()) {
  242. if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
  243. require(filedir);
  244. }
  245. if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
  246. require(filedir);
  247. }
  248. }
  249. });
  250. }
  251. var g_wait_form = false;
  252. exports.wait = () => {
  253. var cmd = (unit.dir + '_entry.js');
  254. g_wait_form = fork(cmd, ['wait']);
  255. //g_wait_form.send('wait_cueline', 'test');
  256. };
  257. exports.cueline = (info) => {
  258. if (g_wait_form && g_wait_form.send) {
  259. //g_wait_form.send('wait_cueline', '' + info);
  260. //exports.ide_info(info);
  261. g_wait_form.send({
  262. type: 'wait_cueline',
  263. info: info
  264. });
  265. }
  266. };
  267. exports.stop = () => {
  268. var timeID = setInterval(() => {
  269. if (g_wait_form) {
  270. clearInterval(timeID);
  271. g_wait_form.send({
  272. type: "close",
  273. form: 'wait'
  274. });
  275. g_wait_form = false;
  276. }
  277. }, 100);
  278. };
  279. exports.updateGeometry = (geometry) => {
  280. if (g_wait_form) {
  281. g_wait_form.send({
  282. type: "setGeometry",
  283. geometry: geometry,
  284. form: 'wait'
  285. });
  286. }
  287. };
  288. //AutoLoadPlugins Function Implement End.
  289. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  291. // ide_info Function Implement Start.
  292. exports.ide_info = (msg) => {
  293. if (process.send) {
  294. process.send({
  295. type: 'debug',
  296. info: msg
  297. });
  298. }
  299. };
  300. //ide_info Function Implement End.
  301. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////