Subversion Repositories Projects

Rev

Rev 92 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 92 Rev 129
1
/********************************************************************************************************************************
1
/********************************************************************************************************************************
2
 *                                                                    
2
 *                                                                    
3
 * Abstaction Layer to Communicate via J2ME and Bluetooth with the FlightCtrl of the MikroKopter Project (www.mikrokopter.de )  
3
 * Abstaction Layer to Communicate via J2ME and Bluetooth with the FlightCtrl of the MikroKopter Project (www.mikrokopter.de )  
4
 *                                                
4
 *                                                
5
 * Author:        Marcus -LiGi- Bueschleb          
5
 * Author:        Marcus -LiGi- Bueschleb          
6
 *
6
 *
7
 * see README for further Infos
7
 * see README for further Infos
8
 *
8
 *
9
 *
9
 *
10
 *******************************************************************************************************************************/
10
 *******************************************************************************************************************************/
11
package org.ligi;
11
package org.ligi;
12
//import javax.microedition.io.*;
12
//import javax.microedition.io.*;
13
import java.io.*;
13
import java.io.*;
14
 
14
 
15
 
15
 
16
public class MKCommunicator
16
public class MKCommunicator
17
    implements Runnable
17
    implements Runnable
18
{
18
{
19
    /***************** Section: public Attributes **********************************************/
19
    /***************** Section: public Attributes **********************************************/
20
    public boolean connected=false; // flag for the connection state
20
    public boolean connected=false; // flag for the connection state
21
    public boolean fatal=false; // flag which is set when an error is so fatal that reconnecting won't be tried - e.g. unknown version number.
21
    public boolean fatal=false; // flag which is set when an error is so fatal that reconnecting won't be tried - e.g. unknown version number.
22
 
22
 
23
 
23
 
24
    public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes
24
    public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes
25
   
25
   
26
    public MKLCD LCD;
26
    public MKLCD LCD;
27
    public MKVersion version;
27
    public MKVersion version;
28
    public MKDebugData debug_data;
28
    public MKDebugData debug_data;
29
    public MKStickData stick_data;
29
    public MKStickData stick_data;
30
    public MKParamsParser params;
30
    public MKParamsParser params;
31
    public MKWatchDog watchdog;
31
    public MKWatchDog watchdog;
32
    //    public MKProxy proxy=null;
32
    //    public MKProxy proxy=null;
33
 
33
 
34
    public long connection_start_time=-1;
34
    public long connection_start_time=-1;
35
   
35
   
36
 
36
 
37
    /****************** Section: private Attributes **********************************************/
37
    /****************** Section: private Attributes **********************************************/
38
    ////    private javax.microedition.io.StreamConnection connection;
38
    ////    private javax.microedition.io.StreamConnection connection;
39
    private java.net.Socket connection;
39
    private java.net.Socket connection;
40
    private java.io.InputStream reader;    
40
    private java.io.InputStream reader;    
41
    private java.io.OutputStream writer;    
41
    private java.io.OutputStream writer;    
42
 
42
 
43
   
43
   
44
    // temp - to be removed
44
    // temp - to be removed
45
    String p_msg="--";
45
    String p_msg="--";
46
    public String msg="BT_INIT";
46
    public String msg="BT_INIT";
47
 
47
 
48
    public int debug_data_count=0;
48
    public int debug_data_count=0;
49
    public int version_data_count=0;
49
    public int version_data_count=0;
50
    public int other_data_count=0;
50
    public int other_data_count=0;
51
    public int lcd_data_count=0;
51
    public int lcd_data_count=0;
52
    public int params_data_count=0;
52
    public int params_data_count=0;
53
 
53
 
54
 
54
 
55
 
55
 
56
    String name;
56
    String name;
57
    DUBwise root;
57
    DUBwise root;
58
    //    DUBwiseDebug debug;
58
    //    DUBwiseDebug debug;
59
 
59
 
60
   
60
   
61
    /******************  Section: public Methods ************************************************/
61
    /******************  Section: public Methods ************************************************/
62
    public MKCommunicator(DUBwise root_) //,DUBwiseDebug debug_)   
62
    public MKCommunicator(DUBwise root_) //,DUBwiseDebug debug_)   
63
    {
63
    {
64
 
64
 
65
        root=root_;
65
        root=root_;
66
        version=new MKVersion();
66
        version=new MKVersion();
67
        debug_data=new MKDebugData();
67
        debug_data=new MKDebugData();
68
        stick_data=new MKStickData();
68
        stick_data=new MKStickData();
69
        params=new MKParamsParser();
69
        params=new MKParamsParser();
70
        LCD= new MKLCD(this);
70
        LCD= new MKLCD(this);
71
        watchdog=new MKWatchDog(this);
71
        watchdog=new MKWatchDog(this);
72
        new Thread( this ).start(); // fire up main Thread 
72
        new Thread( this ).start(); // fire up main Thread 
73
    }
73
    }
74
 
74
 
75
 
75
 
76
    /*    public void do_proxy(String proxy_url)
76
    /*    public void do_proxy(String proxy_url)
77
    {
77
    {
78
        proxy=new MKProxy(proxy_url);
78
        proxy=new MKProxy(proxy_url);
79
        }*/
79
        }*/
80
 
80
 
81
    //  URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control
81
    //  URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control
82
    public void connect_to(String _url,String _name)
82
    public void connect_to(String _url,String _name)
83
    {
83
    {
84
        mk_url=_url; // remember URL for connecting / reconnecting later
84
        mk_url=_url; // remember URL for connecting / reconnecting later
85
        name=_name;
85
        name=_name;
86
        force_disconnect=false;
86
        force_disconnect=false;
87
        connected=false;
87
        connected=false;
88
    }
88
    }
89
 
89
 
90
    public boolean ready()
90
    public boolean ready()
91
    {
91
    {
92
        return (connected&&(version.major!=-1));
92
        return (connected&&(version.major!=-1));
93
    }
93
    }
94
 
94
 
95
    /******************  Section: private Methods ************************************************/
95
    /******************  Section: private Methods ************************************************/
96
    private void connect()
96
    private void connect()
97
    {
97
    {
98
        System.out.println("trying to connect to" + mk_url);
98
        System.out.println("trying to connect to" + mk_url);
99
        try{
99
        try{
100
            //// connection = (StreamConnection) Connector.open(mk_url);
100
            //// connection = (StreamConnection) Connector.open(mk_url);
101
            // old call
101
            // old call
102
            // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE);
102
            // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE);
103
 
103
 
104
            connection = new java.net.Socket("10.0.2.2",54321);
104
            connection = new java.net.Socket("10.0.2.2",54321);
105
            reader=connection.getInputStream();
105
            reader=connection.getInputStream();
106
            writer=connection.getOutputStream();
106
            writer=connection.getOutputStream();
107
 
107
 
108
            //
108
            //
109
            String magic="\rmk-mode\r";
109
            String magic="\rmk-mode\r";
110
            writer.write(magic.getBytes());
110
            writer.write(magic.getBytes());
111
            writer.flush();
111
            writer.flush();
112
            //
112
            //
113
 
113
 
114
 
114
 
115
            connection_start_time=System.currentTimeMillis();
115
            connection_start_time=System.currentTimeMillis();
116
            connected=true; // if we get here everything seems to be OK
116
            connected=true; // if we get here everything seems to be OK
117
            get_version();
117
            get_version();
118
            lcd_data_count=0;
118
            lcd_data_count=0;
119
            debug_data_count=0;
119
            debug_data_count=0;
120
            version_data_count=0;
120
            version_data_count=0;
121
 
121
 
122
 
122
 
123
        }
123
        }
124
        catch (Exception ex)
124
        catch (Exception ex)
125
            {
125
            {
126
                // TODO difference fatal errors from those which will lead to reconnection
126
                // TODO difference fatal errors from those which will lead to reconnection
127
                msg="Problem connecting" + "\n" + ex;
127
                msg="Problem connecting" + "\n" + ex;
128
                System.out.println("problem connecting " + ex);
128
                System.out.println("problem connecting " + ex);
129
            }  
129
            }  
130
 
130
 
131
 
131
 
132
 
132
 
133
    }
133
    }
134
 
134
 
135
    public int[] Decode64(int[] in_arr, int offset,int len)
135
    public int[] Decode64(int[] in_arr, int offset,int len)
136
    {
136
    {
137
        int ptrIn=offset;      
137
        int ptrIn=offset;      
138
        int a,b,c,d,x,y,z;
138
        int a,b,c,d,x,y,z;
139
        int ptr=0;
139
        int ptr=0;
140
       
140
       
141
        int[] out_arr=new int[len];
141
        int[] out_arr=new int[len];
142
 
142
 
143
        while(len!=0)
143
        while(len!=0)
144
            {
144
            {
145
                a = in_arr[ptrIn++] - '=';
145
                a = in_arr[ptrIn++] - '=';
146
                b = in_arr[ptrIn++] - '=';
146
                b = in_arr[ptrIn++] - '=';
147
                c = in_arr[ptrIn++] - '=';
147
                c = in_arr[ptrIn++] - '=';
148
                d = in_arr[ptrIn++] - '=';
148
                d = in_arr[ptrIn++] - '=';
149
                //if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
149
                //if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
150
 
150
 
151
                x = (a << 2) | (b >> 4);
151
                x = (a << 2) | (b >> 4);
152
                y = ((b & 0x0f) << 4) | (c >> 2);
152
                y = ((b & 0x0f) << 4) | (c >> 2);
153
                z = ((c & 0x03) << 6) | d;
153
                z = ((c & 0x03) << 6) | d;
154
 
154
 
155
                if((len--)!=0) out_arr[ptr++] = x; else break;
155
                if((len--)!=0) out_arr[ptr++] = x; else break;
156
                if((len--)!=0) out_arr[ptr++] = y; else break;
156
                if((len--)!=0) out_arr[ptr++] = y; else break;
157
                if((len--)!=0) out_arr[ptr++] = z; else break;
157
                if((len--)!=0) out_arr[ptr++] = z; else break;
158
            }
158
            }
159
       
159
       
160
        return out_arr;
160
        return out_arr;
161
 
161
 
162
    }
162
    }
163
 
163
 
164
    // FC - Function Mappers
164
    // FC - Function Mappers
165
 
165
 
166
    // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives
166
    // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives
167
    public void get_version()
167
    public void get_version()
168
    {
168
    {
169
        send_command(0,'v',new int[0]);
169
        send_command(0,'v',new int[0]);
170
    }
170
    }
171
 
171
 
172
    // send a MotorTest request - params are the speed for each Motor
172
    // send a MotorTest request - params are the speed for each Motor
173
    public void motor_test(int[] params)
173
    public void motor_test(int[] params)
174
    {
174
    {
175
        send_command(0,'t',params);
175
        send_command(0,'t',params);
176
    }
176
    }
177
 
177
 
178
    public void send_keys(int[] params)
178
    public void send_keys(int[] params)
179
    {
179
    {
180
        send_command(0,'k',params);
180
        send_command(0,'k',params);
181
    }
181
    }
182
 
182
 
183
    // get params
183
    // get params
184
    public void get_params(int id)
184
    public void get_params(int id)
185
    {
185
    {
186
        int[] params=new int[1];
186
        int[] params=new int[1];
187
        params[0]=id;
187
        params[0]=id;
188
 
188
 
189
        while(sending)
189
        while(sending)
190
            {try { Thread.sleep(50); }
190
            {try { Thread.sleep(50); }
191
            catch (Exception e)  {   }
191
            catch (Exception e)  {   }
192
            }
192
            }
193
 
193
 
194
        send_command(0,'q',params);
194
        send_command(0,'q',params);
195
    }
195
    }
196
 
196
 
197
 
197
 
198
    public void get_debug_name(int id)
198
    public void get_debug_name(int id)
199
    {
199
    {
200
        int[] params=new int[1];
200
        int[] params=new int[1];
201
        params[0]=id;
201
        params[0]=id;
202
 
202
 
203
        while(sending)
203
        while(sending)
204
            {try { Thread.sleep(50); }
204
            {try { Thread.sleep(50); }
205
            catch (Exception e)  {   }
205
            catch (Exception e)  {   }
206
            }
206
            }
207
 
207
 
208
        send_command(0,'a',params);
208
        send_command(0,'a',params);
209
    }
209
    }
-
 
210
 
-
 
211
 
-
 
212
 
-
 
213
    public void trigger_debug_data()
-
 
214
    {
-
 
215
        int[] params=new int[1];
-
 
216
        params[0]=0;
-
 
217
 
-
 
218
        while(sending)
-
 
219
            {try { Thread.sleep(50); }
-
 
220
            catch (Exception e)  {   }
-
 
221
            }
-
 
222
 
-
 
223
        send_command(0,'c',params);
-
 
224
    }
-
 
225
 
210
 
226
 
211
 
227
 
212
 
228
 
213
 
229
 
214
    public void trigger_LCD(int key)
230
    public void trigger_LCD(int key)
215
    {
231
    {
216
        if (sending) return;
232
        if (sending) return;
217
 
233
 
218
       
234
       
219
        int[] params=new int[3];
235
        int[] params=new int[3];
220
        params[0]=key;
236
        params[0]=key;
221
        params[1]=0;
237
        params[1]=0;
222
        params[2]=0;
238
        params[2]=0;
223
       
239
       
224
        send_command(0,'h',params);
240
        send_command(0,'h',params);
225
       
241
       
226
    }
242
    }
227
 
243
 
228
 
244
 
229
    public void write_params()
245
    public void write_params()
230
    {
246
    {
231
        while(sending)
247
        while(sending)
232
            {try { Thread.sleep(50); }
248
            {try { Thread.sleep(50); }
233
            catch (Exception e)  {   }
249
            catch (Exception e)  {   }
234
            }
250
            }
235
 
251
 
236
        send_command(0,(char)('l'+params.act_paramset),params.field[params.act_paramset]);
252
        send_command(0,(char)('l'+params.act_paramset),params.field[params.act_paramset]);
237
    }
253
    }
238
 
254
 
239
 
255
 
240
    boolean sending=false;
256
    boolean sending=false;
241
 
257
 
242
    // send command to FC ( add crc and pack into pseudo Base64
258
    // send command to FC ( add crc and pack into pseudo Base64
243
    public void send_command(int modul,char cmd,int[] params)
259
    public void send_command(int modul,char cmd,int[] params)
244
    {
260
    {
245
        sending=true;
261
        sending=true;
246
        char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc
262
        char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc
247
        send_buff[0]='#';
263
        send_buff[0]='#';
248
        send_buff[1]=(char)modul;
264
        send_buff[1]=(char)modul;
249
        send_buff[2]=cmd;
265
        send_buff[2]=cmd;
250
       
266
       
251
        for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++)
267
        for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++)
252
            {
268
            {
253
                int a = (param_pos*3<params.length)?params[param_pos*3]:0;
269
                int a = (param_pos*3<params.length)?params[param_pos*3]:0;
254
                int b = ((param_pos*3+1)<params.length)?params[param_pos*3+1]:0;
270
                int b = ((param_pos*3+1)<params.length)?params[param_pos*3+1]:0;
255
                int c = ((param_pos*3+2)<params.length)?params[param_pos*3+2]:0;
271
                int c = ((param_pos*3+2)<params.length)?params[param_pos*3+2]:0;
256
 
272
 
257
                send_buff[3+param_pos*4] =  (char)((a >> 2)+'=' );
273
                send_buff[3+param_pos*4] =  (char)((a >> 2)+'=' );
258
                send_buff[3+param_pos*4+1] = (char)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)));
274
                send_buff[3+param_pos*4+1] = (char)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)));
259
                send_buff[3+param_pos*4+2] = (char)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)));
275
                send_buff[3+param_pos*4+2] = (char)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)));
260
                send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f));
276
                send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f));
261
 
277
 
262
                //send_buff[3+foo]='=';
278
                //send_buff[3+foo]='=';
263
            }
279
            }
264
 
280
 
265
        /*      for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++)
281
        /*      for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++)
266
                {
282
                {
267
                int a = (foo<params.length) params[foo];
283
                int a = (foo<params.length) params[foo];
268
                int a = params[foo];
284
                int a = params[foo];
269
               
285
               
270
                //send_buff[3+foo]='=';
286
                //send_buff[3+foo]='=';
271
                }
287
                }
272
        */
288
        */
273
        try
289
        try
274
            {
290
            {
275
                int tmp_crc=0;
291
                int tmp_crc=0;
276
                for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++)
292
                for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++)
277
                    {
293
                    {
278
                        tmp_crc+=(int)send_buff[tmp_i];
294
                        tmp_crc+=(int)send_buff[tmp_i];
279
                        writer.write(send_buff[tmp_i]);
295
                        writer.write(send_buff[tmp_i]);
280
                    }
296
                    }
281
                tmp_crc%=4096;
297
                tmp_crc%=4096;
282
                writer.write( (char)(tmp_crc/64 + '='));
298
                writer.write( (char)(tmp_crc/64 + '='));
283
                writer.write( (char)(tmp_crc%64 + '='));
299
                writer.write( (char)(tmp_crc%64 + '='));
284
                writer.write('\r');
300
                writer.write('\r');
285
                writer.flush();
301
                writer.flush();
286
            }
302
            }
287
        catch (Exception e)
303
        catch (Exception e)
288
            { // problem sending data to FC
304
            { // problem sending data to FC
289
            }
305
            }
290
 
306
 
291
        sending=false;
307
        sending=false;
292
    }
308
    }
293
 
309
 
294
 
310
 
295
    public void process_data(int[] data,int len)
311
    public void process_data(int[] data,int len)
296
    {
312
    {
297
 
313
 
298
        switch((char)data[2])
314
        switch((char)data[2])
299
            {
315
            {
300
           
316
           
301
            case 'D': // debug Data
317
            case 'D': // debug Data
302
                debug_data_count++;
318
                debug_data_count++;
303
                debug_data.set_by_mk_data(Decode64(data,3,len-3),version);
319
                debug_data.set_by_mk_data(Decode64(data,3,len-3),version);
304
                break;
320
                break;
305
 
321
 
306
            case 'A': // debug Data Names
322
            case 'A': // debug Data Names
307
                //debug_data_count++;
323
                //debug_data_count++;
308
                debug_data.set_names_by_mk_data(data[1]-'0',Decode64(data,3,len-3));
324
                debug_data.set_names_by_mk_data(data[1]-'0',Decode64(data,3,len-3));
309
                break;
325
                break;
310
               
326
               
311
            case 'V': // Version Info
327
            case 'V': // Version Info
312
                version_data_count++;
328
                version_data_count++;
313
                version.set_by_mk_data(Decode64(data,3,6));
329
                version.set_by_mk_data(Decode64(data,3,6));
314
                break;
330
                break;
315
               
331
               
316
            case '0':
332
            case '0':
317
            case '1':
333
            case '1':
318
            case '2':
334
            case '2':
319
            case '3':
335
            case '3':
320
                lcd_data_count++;
336
                lcd_data_count++;
321
                LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0');
337
                LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0');
322
 
338
 
323
                break;
339
                break;
324
            case '4':
340
            case '4':
325
                stick_data.set_by_mk_data(Decode64(data,3,20));
341
                stick_data.set_by_mk_data(Decode64(data,3,20));
326
                String tmp_s="";
342
                String tmp_s="";
327
                for (int tmp_c=0;tmp_c<10;tmp_c++)
343
                for (int tmp_c=0;tmp_c<10;tmp_c++)
328
                    tmp_s+="s"+tmp_c+"v"+stick_data.stick[tmp_c]+" ";
344
                    tmp_s+="s"+tmp_c+"v"+stick_data.stick[tmp_c]+" ";
329
                root.log(tmp_s);
345
                root.log(tmp_s);
330
                break;
346
                break;
331
 
347
 
332
            case 'L':
348
            case 'L':
333
            case 'M':
349
            case 'M':
334
            case 'N':
350
            case 'N':
335
            case 'O':
351
            case 'O':
336
            case 'P':
352
            case 'P':
337
                params.set_by_mk_data((int)(data[2]-'L'),Decode64(data,3,len-3),version);
353
                params.set_by_mk_data((int)(data[2]-'L'),Decode64(data,3,len-3),version);
338
                params_data_count++;
354
                params_data_count++;
339
                break;
355
                break;
340
 
356
 
341
            default:
357
            default:
342
                other_data_count++;
358
                other_data_count++;
343
                root.log("got other data:" + (char)data[2] + "=>" + (byte)data[2]);
359
                root.log("got other data:" + (char)data[2] + "=>" + (byte)data[2]);
344
 
360
 
345
 
361
 
346
                String tmp_str="";
362
                String tmp_str="";
347
                for (int tmp_i=0;tmp_i<len;tmp_i++)
363
                for (int tmp_i=0;tmp_i<len;tmp_i++)
348
                    tmp_str+=(char)data[tmp_i];
364
                    tmp_str+=(char)data[tmp_i];
349
                root.log(tmp_str);
365
                root.log(tmp_str);
350
                break;
366
                break;
351
 
367
 
352
            }
368
            }
353
 
369
 
354
 
370
 
355
 
371
 
356
       
372
       
357
    }
373
    }
358
 
374
 
359
    String o_msg="";
375
    String o_msg="";
360
 
376
 
361
    public boolean force_disconnect=true;
377
    public boolean force_disconnect=true;
362
 
378
 
363
    public void close_connections(boolean force)
379
    public void close_connections(boolean force)
364
    {
380
    {
365
        //      if ((!force)&&root.canvas.do_vibra) root.vibrate(500);
381
        //      if ((!force)&&root.canvas.do_vibra) root.vibrate(500);
366
        force_disconnect=force;
382
        force_disconnect=force;
367
        try{ reader.close(); }
383
        try{ reader.close(); }
368
        catch (Exception inner_ex) { }
384
        catch (Exception inner_ex) { }
369
 
385
 
370
        try{ writer.close(); }
386
        try{ writer.close(); }
371
        catch (Exception inner_ex) { }
387
        catch (Exception inner_ex) { }
372
       
388
       
373
        try{ connection.close(); }
389
        try{ connection.close(); }
374
        catch (Exception inner_ex) { }
390
        catch (Exception inner_ex) { }
375
       
391
       
376
        connected=false;
392
        connected=false;
377
    }
393
    }
378
 
394
 
379
    public boolean run=true;
395
    public boolean run=true;
380
    // Thread to recieve data from Connection
396
    // Thread to recieve data from Connection
381
    public void run()
397
    public void run()
382
    {
398
    {
383
        int[] data_set=new int[1024];
399
        int[] data_set=new int[1024];
384
        int input;
400
        int input;
385
        int pos=0;
401
        int pos=0;
386
        msg+="!!run started!!";
402
        msg+="!!run started!!";
387
        while(run)
403
        while(run)
388
            {
404
            {
389
                if (!connected)
405
                if (!connected)
390
                    {
406
                    {
391
                        if (!force_disconnect) connect();
407
                        if (!force_disconnect) connect();
392
                    }
408
                    }
393
                else
409
                else
394
                    try{
410
                    try{
395
               
411
               
396
                        pos=0;
412
                        pos=0;
397
                        input=0;
413
                        input=0;
398
                        // recieve data-set
414
                        // recieve data-set
399
                        while ((input != 13)&&(input != 10)) // &&(input!=-1))
415
                        while ((input != 13)&&(input != 10)) // &&(input!=-1))
400
                            {
416
                            {
401
 
417
 
402
                                input = reader.read() ;
418
                                input = reader.read() ;
403
                                //                              if (proxy!=null)
419
                                //                              if (proxy!=null)
404
                                //    proxy.writer.write(input);
420
                                //    proxy.writer.write(input);
405
                                //                              if (input==-1) throw new Exception("test");
421
                                //                              if (input==-1) throw new Exception("test");
406
                                if (input!=-1)
422
                                if (input!=-1)
407
                                    {
423
                                    {
408
                                        data_set[pos]=input;
424
                                        data_set[pos]=input;
409
                                        pos++;
425
                                        pos++;
410
                                    }
426
                                    }
411
                                //                      root.log("p "+pos + " i:" + (char)input + " ii: " + input);
427
                                //                      root.log("p "+pos + " i:" + (char)input + " ii: " + input);
412
                            }
428
                            }
413
                        /*
429
                        /*
414
                        if (proxy!=null)
430
                        if (proxy!=null)
415
                            {
431
                            {
416
                                proxy.writer.write('\r');
432
                                proxy.writer.write('\r');
417
                                proxy.writer.write('\n');
433
                                proxy.writer.write('\n');
418
                                proxy.writer.flush();
434
                                proxy.writer.flush();
419
                            }
435
                            }
420
                        */
436
                        */
421
                        try
437
                        try
422
                            {
438
                            {
423
                                if (pos>5)
439
                                if (pos>5)
424
                                    process_data(data_set,pos);
440
                                    process_data(data_set,pos);
425
                               
441
                               
426
                            }
442
                            }
427
                        catch (Exception ex)
443
                        catch (Exception ex)
428
                            {
444
                            {
429
                                root.log("Problem Parsinf data");
445
                                root.log("Problem Parsinf data");
430
                                root.log(ex.toString());
446
                                root.log(ex.toString());
431
                                // close the connection 
447
                                // close the connection 
432
                                //                          close_connections(false);
448
                                //                          close_connections(false);
433
                               
449
                               
434
                               
450
                               
435
                            }  
451
                            }  
436
                    }
452
                    }
437
                    catch (Exception ex)
453
                    catch (Exception ex)
438
                        {
454
                        {
439
                            root.log("Problem reading from MK -> closing conn");
455
                            root.log("Problem reading from MK -> closing conn");
440
                            root.log(ex.toString());
456
                            root.log(ex.toString());
441
                            // close the connection 
457
                            // close the connection 
442
                            //                      close_connections(false);
458
                            //                      close_connections(false);
443
 
459
 
444
 
460
 
445
                        }      
461
                        }      
446
   
462
   
447
                // sleep a bit to get someting more done
463
                // sleep a bit to get someting more done
448
                try { Thread.sleep(50); }
464
                try { Thread.sleep(50); }
449
                catch (Exception e)  {   }
465
                catch (Exception e)  {   }
450
       
466
       
451
            } // while
467
            } // while
452
 
468
 
453
 
469
 
454
    } // run()
470
    } // run()
455
 
471
 
456
 
472
 
457
}
473
}
458
 
474