Subversion Repositories Projects

Rev

Rev 216 | Rev 222 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**************************************
 *
 * WatchDog for MK-Connection
 *                      
 * Author:        Marcus -LiGi- Bueschleb
 *
 * see README for further Infos
 *
 *

 **************************************/

package org.ligi.ufo;

public class MKWatchDog
    implements Runnable,DUBwiseDefinitions
{
    MKCommunicator mk=null;

    int debug_data_count_buff=-123;

    public MKWatchDog(MKCommunicator _mk)
    {

        mk=_mk;
        new Thread( this ).start(); // fire up main Thread
    }


    public int act_paramset=0;
    int conn_check_timeout=0;


    public byte resend_timeout=0;
    int last_count=0;


    public boolean resend_check(int ref_count)
    {
        if (( last_count!=ref_count)||(resend_timeout<0))
            {
                if (resend_timeout<0) mk.stats.resend_count++;
                last_count=ref_count;
                resend_timeout=20;
                return true;
            }
        else
            resend_timeout--;
       
        return false;
    }

//#ifdef android
//    public final static int BASE_SLEEP=50;
//#else
    public final static int BASE_SLEEP=10;
//#endif

    public void run()
    {
        mk.log("starting Watchdog");           
        // get all params
        int act_debug_name=0;
        //      int sleeper=BASE_SLEEP;
        while(true)
            {
                try {
                    Thread.sleep(BASE_SLEEP);
                    //              sleeper=BASE_SLEEP;
                    if (mk.connected&&(!mk.force_disconnect))
                        {

                            mk.log("watchdog pre main loop");          
                            if (mk.init_bootloader)
                                {
                                    mk.jump_bootloader();
                                    mk.init_bootloader=false;
                                }
                            else if  ( mk.version.major==-1 )
                                    mk.get_version();
                            else if (mk.ufo_prober.is_navi()&&(mk.error_str==null))
                                    mk.get_error_str();

                            else if (mk.ufo_prober.is_mk()&&(mk.params.last_parsed_paramset==-1))
                                {
                                    mk.get_params(0xFF-1);
                                    Thread.sleep(150);

                                    act_paramset=0; // warning -  if dropped problem
                                }
                            else   switch (mk.user_intent)
                                {
                                case USER_INTENT_PARAMS:

                                    if ((act_paramset<5))
                                    {

                                        if (resend_timeout==0)  {
                                            mk.get_params(act_paramset);
                                            resend_timeout=120;
                                        }
                                       
                                        if(mk.params.field[act_paramset]!=null)
                                            {
                                                mk.get_params(++act_paramset);
                                                resend_timeout=120;
                                            }
                                        else
                                            resend_timeout--;
                                            /*
                                                //                                              act_paramset++;
                                        else
                                            mk.get_params(act_paramset);
                                       
                                        sleeper+=1200;
                                            */

                                    }
                                    break;
                                case USER_INTENT_RAWDEBUG:
                                     if (act_debug_name<32)
                                         {                                           

                                             if (resend_timeout==0)  {
                                                 mk.get_debug_name(act_debug_name);
                                                 resend_timeout=50;
                                             }
                                             
                                             
                                             //sleeper+=100;
                                             if (mk.debug_data.got_name[act_debug_name])
                                                 {
                                                     mk.get_debug_name(++act_debug_name);
                                                     resend_timeout=50;
                                                 }
                                             else
                                                 resend_timeout--;

                                         }
                                     else
                                         if (!(mk.debug_data.got_name[0]))
                                             act_debug_name=0;

                                     break;

                                case USER_INTENT_RCDATA:
                                    if ( resend_check(mk.stats.stick_data_count) )
                                        mk.trigger_rcdata();
                                    break;

                                case USER_INTENT_EXTERNAL_CONTROL:
                                    if (resend_check(mk.stats.external_control_confirm_frame_count))
                                            mk.send_extern_control();

                                    break;

                                   
                                case USER_INTENT_LCD:
                                    if (resend_check(mk.stats.lcd_data_count))
                                            mk.LCD.trigger_LCD();

                                    break;

                                default:
                                    //                              mk.log("uncactched intent " +mk.root.canvas.user_intent );         
                                    break;
                                }

                            //                  if ((!mk.ufo_prober.is_incompatible()))
                            {
                               
                                mk.log("watchdog after main loop");            
                                if (debug_data_count_buff==mk.stats.debug_data_count)
                                    {
                                        mk.log("timeout:" +conn_check_timeout );               
                                        conn_check_timeout++;
                                        if  (conn_check_timeout==100)
                                            {
                                                mk.log("disconnecting");               
                                                mk.close_connections(false);
                                                conn_check_timeout=0;
                                            }
                                        debug_data_count_buff=mk.stats.debug_data_count;
                                    }
                                else
                                    conn_check_timeout=0;
                               


                                /*else  if ((mk.root.canvas.user_intent==USER_INTENT_RAWDEBUG) || (mk.root.canvas.user_intent==USER_INTENT_GRAPH) )
                                  {
                                  mk.trigger_debug();
                                  try { Thread.sleep(100); }
                                  catch (Exception e)  {   }
                                  }
                                */
 

               
           



                                /*


                                if (debug_data_count_buff==mk.debug_data_count)
                                {
                                //                              mk.close_connections(false);
                                }

                                */


                                /*if (mk.version.major==-1)
                                  mk.get_version();
                                  else
                                */
 
                                /*for ( int cnt=0;cnt<5;cnt++)
                                  if (mk.params.field[cnt]==null)
                                  {
                                  mk.get_params(cnt+1);
                                  break;
                                  }*/

                                /*
                                  for (int c=0;c<32;c++)
                                  if (!mk.debug_data.got_name[c])
                                  {
                                  mk.get_debug_name(c);
                                  break;
                                  }
                                */


                       
                            }
                       
                        }
               

                } // 3000
                catch (Exception e)  {                 
                    mk.log("err in watchdog:");          
                    mk.log(e.toString());                

                }
            }


        //      mk.log("watchdog quit");               
    }

   
}