Rev 266 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
206 | ligi | 1 | /************************************** |
2 | * |
||
3 | * WatchDog for MK-Connection |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * |
||
7 | * see README for further Infos |
||
8 | * |
||
9 | * |
||
10 | |||
11 | **************************************/ |
||
12 | package org.ligi.ufo; |
||
13 | |||
14 | public class MKWatchDog |
||
15 | implements Runnable,DUBwiseDefinitions |
||
16 | { |
||
17 | MKCommunicator mk=null; |
||
18 | |||
266 | ligi | 19 | int bytes_in_count_buff=-123; |
206 | ligi | 20 | |
21 | public MKWatchDog(MKCommunicator _mk) |
||
22 | { |
||
23 | |||
24 | mk=_mk; |
||
25 | new Thread( this ).start(); // fire up main Thread |
||
26 | } |
||
27 | |||
28 | |||
29 | public int act_paramset=0; |
||
30 | int conn_check_timeout=0; |
||
31 | |||
32 | |||
33 | public byte resend_timeout=0; |
||
34 | int last_count=0; |
||
35 | |||
36 | |||
381 | ligi | 37 | |
38 | int last_fm_send=-1; |
||
219 | ligi | 39 | public boolean resend_check(int ref_count) |
40 | { |
||
41 | if (( last_count!=ref_count)||(resend_timeout<0)) |
||
42 | { |
||
43 | if (resend_timeout<0) mk.stats.resend_count++; |
||
44 | last_count=ref_count; |
||
45 | resend_timeout=20; |
||
46 | return true; |
||
47 | } |
||
48 | else |
||
49 | resend_timeout--; |
||
50 | |||
51 | return false; |
||
52 | } |
||
53 | |||
206 | ligi | 54 | //#ifdef android |
55 | // public final static int BASE_SLEEP=50; |
||
56 | //#else |
||
219 | ligi | 57 | public final static int BASE_SLEEP=10; |
206 | ligi | 58 | //#endif |
59 | |||
381 | ligi | 60 | int intitial_paramset_try=0; |
206 | ligi | 61 | public void run() |
62 | { |
||
63 | mk.log("starting Watchdog"); |
||
64 | // get all params |
||
65 | int act_debug_name=0; |
||
219 | ligi | 66 | // int sleeper=BASE_SLEEP; |
206 | ligi | 67 | while(true) |
68 | { |
||
69 | try { |
||
219 | ligi | 70 | Thread.sleep(BASE_SLEEP); |
71 | // sleeper=BASE_SLEEP; |
||
381 | ligi | 72 | if (mk.connected&&(!mk.force_disconnect))//&&(mk.bootloader_stage==BOOTLOADER_STAGE_NONE)) |
206 | ligi | 73 | { |
381 | ligi | 74 | // // mk.log("watchdog pre main loop"); |
75 | // if (mk.init_bootloader) |
||
76 | // { |
||
77 | // mk.jump_bootloader(); |
||
78 | // mk.init_bootloader=false; |
||
79 | // } |
||
80 | //else |
||
81 | if ( mk.version.major==-1 ) |
||
206 | ligi | 82 | mk.get_version(); |
381 | ligi | 83 | else if (mk.is_navi()&&(mk.error_str==null)) |
206 | ligi | 84 | mk.get_error_str(); |
85 | |||
381 | ligi | 86 | else if (mk.is_mk()&&(mk.params.last_parsed_paramset==-1)&&(intitial_paramset_try<7)) |
206 | ligi | 87 | { |
88 | mk.get_params(0xFF-1); |
||
219 | ligi | 89 | Thread.sleep(150); |
381 | ligi | 90 | intitial_paramset_try++; |
206 | ligi | 91 | act_paramset=0; // warning - if dropped problem |
92 | } |
||
93 | else switch (mk.user_intent) |
||
94 | { |
||
95 | case USER_INTENT_PARAMS: |
||
96 | |||
97 | if ((act_paramset<5)) |
||
98 | { |
||
99 | |||
100 | if (resend_timeout==0) { |
||
101 | mk.get_params(act_paramset); |
||
213 | ligi | 102 | resend_timeout=120; |
206 | ligi | 103 | } |
104 | |||
105 | if(mk.params.field[act_paramset]!=null) |
||
106 | { |
||
107 | mk.get_params(++act_paramset); |
||
213 | ligi | 108 | resend_timeout=120; |
206 | ligi | 109 | } |
110 | else |
||
111 | resend_timeout--; |
||
112 | /* |
||
113 | // act_paramset++; |
||
114 | else |
||
115 | mk.get_params(act_paramset); |
||
116 | |||
117 | sleeper+=1200; |
||
118 | */ |
||
119 | } |
||
120 | break; |
||
121 | case USER_INTENT_RAWDEBUG: |
||
122 | if (act_debug_name<32) |
||
123 | { |
||
124 | |||
125 | if (resend_timeout==0) { |
||
126 | mk.get_debug_name(act_debug_name); |
||
127 | resend_timeout=50; |
||
128 | } |
||
129 | |||
130 | |||
131 | //sleeper+=100; |
||
132 | if (mk.debug_data.got_name[act_debug_name]) |
||
133 | { |
||
134 | mk.get_debug_name(++act_debug_name); |
||
135 | resend_timeout=50; |
||
136 | } |
||
137 | else |
||
138 | resend_timeout--; |
||
139 | |||
140 | } |
||
216 | ligi | 141 | else |
142 | if (!(mk.debug_data.got_name[0])) |
||
143 | act_debug_name=0; |
||
206 | ligi | 144 | |
145 | break; |
||
146 | |||
147 | case USER_INTENT_RCDATA: |
||
219 | ligi | 148 | if ( resend_check(mk.stats.stick_data_count) ) |
149 | mk.trigger_rcdata(); |
||
206 | ligi | 150 | break; |
219 | ligi | 151 | |
152 | case USER_INTENT_EXTERNAL_CONTROL: |
||
153 | if (resend_check(mk.stats.external_control_confirm_frame_count)) |
||
222 | ligi | 154 | { |
219 | ligi | 155 | mk.send_extern_control(); |
222 | ligi | 156 | mk.send_extern_control(); |
157 | mk.send_extern_control(); |
||
158 | mk.send_extern_control(); |
||
159 | } |
||
219 | ligi | 160 | |
161 | break; |
||
162 | |||
206 | ligi | 163 | |
164 | case USER_INTENT_LCD: |
||
219 | ligi | 165 | if (resend_check(mk.stats.lcd_data_count)) |
206 | ligi | 166 | mk.LCD.trigger_LCD(); |
219 | ligi | 167 | |
206 | ligi | 168 | break; |
169 | |||
264 | ligi | 170 | case USER_INTENT_GPSOSD: |
381 | ligi | 171 | mk.set_gpsosd_interval(mk.primary_abo); |
264 | ligi | 172 | break; |
262 | ligi | 173 | case USER_INTENT_GRAPH: |
381 | ligi | 174 | mk.set_debug_interval(mk.primary_abo); |
262 | ligi | 175 | break; |
381 | ligi | 176 | |
177 | case USER_INTENT_FOLLOWME: |
||
178 | |||
179 | // once a second |
||
180 | if (last_fm_send!=(System.currentTimeMillis()/1000)) |
||
181 | { |
||
182 | last_fm_send=(int)(System.currentTimeMillis()/1000); |
||
183 | mk.send_follow_me(60); |
||
184 | } |
||
185 | |||
186 | |||
187 | |||
188 | break; |
||
189 | |||
206 | ligi | 190 | default: |
381 | ligi | 191 | // mk.log("uncactched intent " +mk.root.canvas.user_intent ) |
206 | ligi | 192 | break; |
193 | } |
||
194 | |||
381 | ligi | 195 | |
196 | |||
206 | ligi | 197 | |
266 | ligi | 198 | if (bytes_in_count_buff==mk.stats.bytes_in) |
199 | if ((conn_check_timeout++)*BASE_SLEEP>3000) |
||
200 | { |
||
201 | conn_check_timeout=0; |
||
202 | mk.close_connections(false); |
||
381 | ligi | 203 | |
266 | ligi | 204 | } |
381 | ligi | 205 | else |
206 | conn_check_timeout=0; |
||
266 | ligi | 207 | bytes_in_count_buff=mk.stats.debug_data_count; |
381 | ligi | 208 | |
206 | ligi | 209 | |
210 | } |
||
211 | |||
212 | |||
213 | } // 3000 |
||
214 | catch (Exception e) { |
||
215 | mk.log("err in watchdog:"); |
||
216 | mk.log(e.toString()); |
||
217 | |||
218 | } |
||
219 | } |
||
220 | |||
221 | |||
222 | // mk.log("watchdog quit"); |
||
223 | } |
||
224 | |||
225 | |||
226 | } |