skyeye-sdk-js 1.1.9 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/SkyEyeClient.ts +97 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skyeye-sdk-js",
3
- "version": "1.1.9",
3
+ "version": "1.2.0",
4
4
  "description": "gRPC to SkyEye",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -809,37 +809,105 @@ export class SkyEyeClient extends JSONTransmissionClient {
809
809
  return this.port;
810
810
  }
811
811
 
812
- public initSkyEyeAndRun(pathSkyEye: string, fileName: string, port: string, skyeyeDir: string) {
813
- console.log('runExample');
814
- cp.exec(skyeyeDir + " -q " + port + "\n", (err: any, stdout: any, stderr: any) => {
815
- // console.log('stdout: ' + stdout);
816
- // console.log('stderr: ' + stderr);
817
- });
818
- setTimeout(() => {
819
- client = new JSONTransmissionClient('127.0.0.1:' + port, grpc.credentials.createInsecure());
820
- // console.log('client:' + client);
821
- const Jr = new JSONRequest();
822
- Jr.setRequest("{\"request\": {\"name\": \"chdir\", \"args\": {\"path\":\"" + pathSkyEye + "\"}}}");
823
- client.callSkyEye(Jr, (error: ServiceError | null, response: JSONResponse) => {
824
- console.log("chdir res:" + response + error);
825
-
826
- Jr.setRequest("{\"request\": {\"name\": \"run_script\", \"args\": {\"filename\":\"" + fileName + "\"}}}");
827
- client.callSkyEye(Jr, (error: ServiceError | null, response: JSONResponse) => {
828
- console.log("run_script res:" + response + error);
829
- // this.runCommand()
830
- return "1";
831
- });
832
- });
833
- }, 2000);
812
+ // public initSkyEyeAndRun(pathSkyEye: string, fileName: string, port: string, skyeyeDir: string) {
813
+ // console.log('runExample');
814
+ // cp.exec(skyeyeDir + " -q " + port + "\n", (err: any, stdout: any, stderr: any) => {
815
+ // if(err){
816
+ // console.log("initSkyEyeAndRun error:",err)
817
+ // return
818
+ // }
819
+ // console.log('initSkyEyeAndRun stdout: ' ,stdout);
820
+ // console.log('initSkyEyeAndRun stderr: ' , stderr);
821
+ // });
822
+ // setTimeout(() => {
823
+ // client = new JSONTransmissionClient('127.0.0.1:' + port, grpc.credentials.createInsecure());
824
+ // // console.log('client:' + client);
825
+ // const Jr = new JSONRequest();
826
+ // Jr.setRequest("{\"request\": {\"name\": \"chdir\", \"args\": {\"path\":\"" + pathSkyEye + "\"}}}");
827
+ // client.callSkyEye(Jr, (error: ServiceError | null, response: JSONResponse) => {
828
+ // console.log("chdir res:" + response + error);
829
+
830
+ // Jr.setRequest("{\"request\": {\"name\": \"run_script\", \"args\": {\"filename\":\"" + fileName + "\"}}}");
831
+ // client.callSkyEye(Jr, (error: ServiceError | null, response: JSONResponse) => {
832
+ // console.log("run_script res:" + response + error);
833
+ // // this.runCommand()
834
+ // return "1";
835
+ // });
836
+ // });
837
+ // }, 2000);
838
+ // }
839
+
840
+
841
+ private childProcess: cp.ChildProcess | null = null;
842
+
843
+ public async initSkyEyeAndRun(pathSkyEye: string, fileName: string, port: string, skyeyeDir: string) {
844
+ try {
845
+ console.log('runExample');
846
+ this.childProcess = await this.startSkyEye(port, skyeyeDir);
847
+ await this.setWorkingDirectory(pathSkyEye, port);
848
+ await this.runScript(fileName, port);
849
+ return "1";
850
+ } catch (error) {
851
+ console.error("initSkyEyeAndRun error:", error);
852
+ if (this.childProcess) {
853
+ console.log('Terminating SkyEye child process...');
854
+ this.childProcess.kill(); // 手动终止子进程
855
+ }
856
+ throw error;
857
+ }
834
858
  }
835
859
 
860
+ private startSkyEye(port: string, skyeyeDir: string): Promise<cp.ChildProcess> {
861
+ return new Promise((resolve, reject) => {
862
+ const childProcess = cp.exec(`${skyeyeDir} -q ${port}`, (err: any, stdout: any, stderr: any) => {
863
+ if (err) {
864
+ reject(err);
865
+ } else {
866
+ console.log('initSkyEyeAndRun stdout: ', stdout);
867
+ console.log('initSkyEyeAndRun stderr: ', stderr);
868
+ }
869
+ });
836
870
 
837
- //与skyeye长连接
838
-
839
-
840
-
841
-
842
-
843
- }
871
+ childProcess.on('exit', (code, signal) => {
872
+ console.log('SkyEye child process exited with code', code, 'and signal', signal);
873
+ });
844
874
 
875
+ resolve(childProcess);
876
+ });
877
+ }
845
878
 
879
+
880
+ private setWorkingDirectory(pathSkyEye: string, port: string): Promise<void> {
881
+ const client = new JSONTransmissionClient(`127.0.0.1:${port}`, grpc.credentials.createInsecure());
882
+ const Jr = new JSONRequest();
883
+ Jr.setRequest(`{"request": {"name": "chdir", "args": {"path":"${pathSkyEye}"}}}`);
884
+
885
+ return new Promise((resolve, reject) => {
886
+ client.callSkyEye(Jr, (error: ServiceError | null, response: JSONResponse) => {
887
+ if (error) {
888
+ reject(error);
889
+ } else {
890
+ console.log("chdir res:", response);
891
+ resolve();
892
+ }
893
+ });
894
+ });
895
+ }
896
+
897
+ private runScript(fileName: string, port: string): Promise<void> {
898
+ const client = new JSONTransmissionClient(`127.0.0.1:${port}`, grpc.credentials.createInsecure());
899
+ const Jr = new JSONRequest();
900
+ Jr.setRequest(`{"request": {"name": "run_script", "args": {"filename":"${fileName}"}}}`);
901
+
902
+ return new Promise((resolve, reject) => {
903
+ client.callSkyEye(Jr, (error: ServiceError | null, response: JSONResponse) => {
904
+ if (error) {
905
+ reject(error);
906
+ } else {
907
+ console.log("run_script res:", response);
908
+ resolve();
909
+ }
910
+ });
911
+ });
912
+ }
913
+ }