Tuesday, June 30, 2015

Garden Robot Update 6

Quick update.  I know these aren't as frequent as they should be, but hopefully I can cover most of the important things I have done since the last one.

First of all, after a few days, I finally figured out a nagging issue with the existing NXTBotGuard streaming solution.  It turned out that my browser was caching the image, causing it to only update on refresh.  I forced this out by adding this line to the NanoHTTP header:

res.addHeader( "Cache-Control", "no-store");

With this out of the way, the streaming solution worked fine, but I didn't (and still don't) understand it.  Apparently the developer " modified the NanoHTTPD  server to serve files directly out of the compressed assets folder that comes with your app package (apk)."  I don't get how he is doing that, so I took the simpler solution of just reading the index.html file via AssetManager:

try {
    // Open file from Asset Manager
    InputStream inputStream = getApplicationContext().getAssets().open("index.html");    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));    String line = "";    while ((line = reader.readLine()) != null) {
        answer += line;    }
    reader.close();
} catch(IOException ioe) {
    Log.w("Httpd", ioe.toString());}


return new NanoHTTPD.Response(answer);

Which works perfectly for html but doesn't seem to work for Javascript, as I discovered tonight, as my index.html file will call the javascript functions as expected when I access it locally, but not when I have it read using the above method.  Grr.  (For future personal reference, this index.html is in the "Code Snippets" folder)

So in general, I think that a quick and dirty method to get all this done would be to just host an ever-changing image from the camera using a simple html tag, and have that consistently update to simulate a video.  This should work using the above method.  Although that (should?) work, the dynamic canvas property of html5 has intrigued me so much that I am dying to use it, as well as learn more about html and javascript (which I would use to update the canvas element, and maybe some other things).

So...now, I am going to look online for any more solutions that I can understand about reading directly from the Asset Manager, or just work on my dirty method in pure html and see if it works.