2014-11-05 15 views
0

我是新的Android编程和新的funf传感器框架。根据下面给出的例子,我是按照指定的时间间隔安排探头。但创建的sqlite数据库是空的,我不认为数据是根本收集的。任何想法请。funf调度和数据收集

public class MainActivity extends Activity { 
    private static final String TAG = "APP"; 
    protected static final String PIPELINE_NAME = "default_pipeline"; 
    private FunfManager manager; 
    private Pipeline pipeline; 
    private Handler handler; 

    private ServiceConnection conn = new ServiceConnection() { 
     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      Log.e(TAG,"onServiceDisconnected"); 
      manager = null; 
     } 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.e(TAG,"onServiceConnected"); 
      manager = ((FunfManager.LocalBinder)service).getManager(); 
      pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME); 
      JsonObject jsonobject = (new JsonParser()).parse(getString(R.string.default_pipeline)).getAsJsonObject(); 
      if (pipeline == null){ 
       Log.e(TAG,"pipeling is null"); 
       manager.enablePipeline(PIPELINE_NAME); 
       boolean flag = manager.saveAndReload(PIPELINE_NAME, jsonobject); 
       Log.e(TAG,String.valueOf(flag)); 
       if (flag){ 
        pipeline = manager.getRegisteredPipeline(PIPELINE_NAME); 
        handler.postDelayed(new Runnable() { 
         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          Log.e(TAG,"will archive now"); 
          pipeline.onRun("archive",null); 
         } 
        }, 10000L); 
       } 

      } 
      else { 
       Log.e(TAG,"pipeling is NOT null"); 
      } 

     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.e(TAG,"onCreate"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     handler = new Handler(); 
     bindService(new Intent(this, FunfManager.class), conn, BIND_AUTO_CREATE); 
     waitForServiceConnection(3000); 
    } 

    @Override 
     protected void onDestroy() { 
     Log.e(TAG,"onDestroy"); 
     super.onDestroy(); 
     unbindService(conn); 
     } 

    public void waitForServiceConnection(long millisToWait) { 
     long time = System.currentTimeMillis(); 
     while (System.currentTimeMillis() < time + millisToWait) { 
      if (manager != null) { 
       break; 
      } else { 
       try { 
        Thread.sleep(100); 
       } 
       catch (InterruptedException e) { 

       } 
      } 
     } 
    } 
} 

我的strings.xml具有以下值预先指定

<string name="default_pipeline">{ 
       "name": "example", 
       "version":1, 
       "archive": { 
         "@schedule": {"interval": 60} 
       }, 
       "upload": { 
         "url": \"http://www.samplewebsite.com/uploadurl\", 
         "@schedule": {"interval": 60} 
       }, 
       "update": { 
         "url": \"http://www.samplewebsite.com/funfconfig\", 
         "@schedule": {"interval": 60} 
       }, 
       "data": [ 
      {"@type": "edu.mit.media.funf.probe.builtin.LocationProbe", 
         "@schedule": {"interval": 5} 
         } 
       ] 
     }</string> 

感谢。

更新:基于更新的onServiceConnected评论如下,但我仍然看不到数据库文件创建。我已完全卸载并重新安装了应用程序重新启动的手机。

private ServiceConnection conn = new ServiceConnection() { 
     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      Log.e(TAG,"onServiceDisconnected"); 
      manager = null; 
     } 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.e(TAG,"onServiceConnected"); 
      manager = ((FunfManager.LocalBinder)service).getManager(); 
      pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME); 
      JsonObject jsonobject = (new JsonParser()).parse(getString(R.string.default_pipeline)).getAsJsonObject(); 
      if (pipeline == null){ 
       Log.e(TAG,"pipeline is null"); 
       pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME); 
       manager.saveAndReload(PIPELINE_NAME, jsonobject); 
       manager.enablePipeline(PIPELINE_NAME); 
      } 
      else { 
       Log.e(TAG,"pipeline is NOT null"); 
       Log.e(TAG,"Pipeline enabled? "+String.valueOf(pipeline.isEnabled())); 
      } 
      if (pipeline != null && !pipeline.isEnabled()){ 
       Log.e(TAG,"pipeline is not null and is NOT enabled"); 
       manager.enablePipeline(PIPELINE_NAME); 
      } 

     } 
    }; 
+0

尝试启用管道后,您叫'saveAndReload'。除此之外,每次归档时都会将数据写入数据库文件。目前这是每60秒钟一次,所以我猜想在第一次保存管道后10秒内你并不需要手动存档。 – Blacklight 2014-11-05 11:55:18

+0

此外,也许您需要在之后解除并重新安装应用程序,或者在'onServiceConnected'中启用管道(如果它不为null且尚未启用):if(pipeline!= null &&!pipeline.isEnabled( ))manager.enablePipeline(PIPELINE_NAME);'。 – Blacklight 2014-11-05 12:02:54

+0

@Blacklight更新了代码,卸载并重新启动了手机,但仍然没有运气。 – Linus 2014-11-05 12:27:02

回答

1

您的管道名称不是“default_pipeline”,那只是您的资源名称。这是根据你的配置“示例”,所以试试这个:

protected static final String PIPELINE_NAME = "example"; 

除此之外你的配置看起来基本上没问题。一个小的优化仍然是可能的:

private ServiceConnection conn = new ServiceConnection() { 
    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     Log.i(TAG,"onServiceDisconnected"); 
     manager = null; 
    } 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     Log.i(TAG,"onServiceConnected"); 
     manager = ((FunfManager.LocalBinder)service).getManager(); 
     pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME); 
     if (pipeline == null){ 
      Log.i(TAG,"pipeline is null"); 
      JsonObject jsonobject = (new JsonParser()).parse(getString(R.string.default_pipeline)).getAsJsonObject(); 
      manager.saveAndReload(PIPELINE_NAME, jsonobject); 
      manager.enablePipeline(PIPELINE_NAME); 
      pipeline = (BasicPipeline) manager.getRegisteredPipeline(PIPELINE_NAME); 
     } 

     if (pipeline != null && !pipeline.isEnabled()){ 
      Log.e(TAG,"pipeline is not null and is NOT enabled"); 
      manager.enablePipeline(PIPELINE_NAME); 
     } 
    } 
}; 

您也可以尝试简化您的配置有点缩小下来:

{ 
    "name": "example", 
    "version":1, 
    "archive": { 
     "@schedule": {"interval": 60} 
    }, 
    "data": [ 
     {"@type": "edu.mit.media.funf.probe.builtin.LocationProbe", 
     "@schedule": {"interval": 5} 
     } 
    ] 
} 
+0

谢谢。我正在尝试使用0.5,但将其更改为0.4.2并且效果很好。一个简单的问题,数据库文件上传后会被删除吗? – Linus 2014-11-05 20:24:59

+0

太好了。数据库文件在上传后默认不会被删除,但它们将从存档移动到备份文件夹。 – Blacklight 2014-11-05 20:37:51

+0

如果你能看看这个http://stackoverflow.com/questions/26841717/funf-delete-archive-files-after-upload – Linus 2014-11-10 10:36:26