廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package Optimize;
 
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.UUID;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
 
import ng.db.*;
 
 public  class OptimizeTag  implements Runnable {
 private    String id;
 private      com.jacob.activeX.ActiveXComponent com;
 private    com.jacob.com.Dispatch dispatch;
 private    int running;
 private    java.util.Date date,lastdate;
 private    OptimizationInput input;
 private    java.lang.Thread thread;
 public     String flag;
 public     Object Token;
 private   static  Semaphore sem;
 
 private    Dictionary<String,Object> dic=new  Hashtable<String,Object>();
 
 
 
 public Object getProperty(String s){
     synchronized(dic){
         return dic.get(s);
     }
 }
 
 public void setProperty(String s,Object obj){
     synchronized(dic){
         dic.put(s,obj);
     }
 }
 
 
 
 public OptimizationInput  getInput(){
     return input;
 }
 
 public JSONObject infoToJson() throws JSONException{
     JSONObject obj=new JSONObject();
     obj.put("id", id);
     String s="free";
     if(running==1){
         s="running";
     }
     if(running==2){
         s="complated";
     }
     obj.put("state", s);
     obj.put("createTime",NGFunction.dateFormat("yyyy-MM-dd HH:mm:ss", date));
     obj.put("lastTime",NGFunction.dateFormat("yyyy-MM-dd HH:mm:ss",this.lastdate));
     s="";
     OptimizationInput in=this.input;
     if(in!=null){
         s=in.GongCheng;
     }
     obj.put("lastGongCheng", s);
     return obj;
 }
 
  boolean getExe(){
     
     try {
            if(sem.tryAcquire(0, TimeUnit.MILLISECONDS)){
                return true;
            }
            return false;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
 }
 
 public boolean BeginOptimize(OptimizationInput input){
       if(sem==null){
           sem=new Semaphore(1);
       }
      
       if(running==0 || running==2){
           if(getExe()){
           running=1;
           this.input=input;
           thread=new Thread(this);
           thread.start();
           return true;
           }
       }
      return false;
    }
    
    public String saveOpt(String Gongcheng, String BtContent, String type, String path){
         this.lastdate=new java.util.Date();
         return Dispatch.call(this.dispatch,"saveOpt",Gongcheng,BtContent,type,path).toString();
    }
    
    public Object[] getResultByBT(String Gongcheng, String BtContent){
        String s= Dispatch.call(this.dispatch, "btToResult",Gongcheng,BtContent).toString();
         try {
            JSONArray obj=new JSONArray(s);
            JSONObject    ob=obj.getJSONObject(1);
            OptimizationResult a=new OptimizationResult(ob);
            JSONObject b=obj.getJSONObject(0);
            JSONArray ar=new JSONArray();
            Object[] ret=new Object[2];
            ret[0]=b;
            ret[1]=a;
            return ret;
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return null;
    }
    
    
    public static OptimizeTag create(){
        ActiveXComponent dotnetCom = new ActiveXComponent("CLSID:97D686F9-F577-4E41-B5BF-CB8CC5BC73CB");
        Dispatch myCom = (Dispatch) dotnetCom.getObject();
        OptimizeTag tag=new OptimizeTag();
        tag.id=null;
        tag.com=dotnetCom;
        tag.dispatch=myCom;
 
        tag.date=new java.util.Date();
         tag.lastdate=new java.util.Date();
        return tag;
    }
    
    public static OptimizeTag create(String id){
        ActiveXComponent dotnetCom = new ActiveXComponent("CLSID:97D686F9-F577-4E41-B5BF-CB8CC5BC73CB");
        Dispatch myCom = (Dispatch) dotnetCom.getObject();
        OptimizeTag tag=new OptimizeTag();
        tag.id=id;
        tag.com=dotnetCom;
        tag.dispatch=myCom;
        tag.date=new java.util.Date();
        tag.lastdate=new java.util.Date();
        return tag;
    }
    public OptimizationState getState() throws JSONException{
         this.lastdate=new java.util.Date();
         String s=  Dispatch.call(this.dispatch,"getState").toString();
         if("null".equals(s))
             return null;
         
         JSONObject obj=new JSONObject(s);
         
         OptimizationState r=new  OptimizationState(obj);
         
         if(this.running==1){
             r.State="busy";
         }
         return r;
    }
    
    public OptimizationResult getResult() throws JSONException{
         this.lastdate=new java.util.Date();
        if(running!=2)
            return null;
         String s=  Dispatch.call(this.dispatch,"getResult").toString();
           if("null".equals(s))
                 return null;
         JSONObject obj=new JSONObject(s);
         return new OptimizationResult(obj);
    }
    
    
    public String getId(){
        return this.id;
    }
 
    @Override
    public void run() {
         this.lastdate=new java.util.Date();
        this.running=1;
        // TODO Auto-generated method stub
      String s;
    try {
        Dispatch.call(this.dispatch,"Optima",this.input.toJson().toString()).toString();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        sem.release();
        this.running=2;
        this.thread=null;
    }
    
    
}