package com.ociweb.device.testApp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ociweb.iot.camera.RaspiCam;
import com.ociweb.iot.maker.*;

/**
 * Simple Pi image capture test.
 *
 * @author Brandon Sanders [brandon@alicorn.io]
 */
public class PiCamTest implements FogApp {

    private final List<File> images = new ArrayList<>();
    private byte[] frameBytes = null;
    private int frameBytesHead = 0;
    private long start = -1;

    public static void main( String[] args) {
        FogRuntime.run(new PiCamTest());
    }

    @Override
    public void declareConnections(Hardware hardware) {
        hardware.setImageTriggerRate(1);
    }

    @Override
    public void declareBehavior(FogRuntime runtime) {
        runtime.addImageListener(new ImageListener() {

            File workingFile = null;

            @Override
            public void onFrameStart(int width, int height, long timestamp, int frameBytesCount) {

                // Prepare file.
                workingFile = new File("image-" + timestamp + ".raw");
                start = System.currentTimeMillis();

                // Prepare byte array.
                if (frameBytes == null || frameBytes.length != frameBytesCount) {
                    frameBytes = new byte[frameBytesCount];
                    System.out.printf("Created new frame buffer for frames of size %dW x %dH with %d bytes.\n", width, height, frameBytesCount);
                }

                System.out.printf("Started new frame (%d) @ %d.\n", timestamp, System.currentTimeMillis());

                frameBytesHead = 0;
            }

            @Override
            public void onFrameRow(byte[] frameRowBytes) {

                // Copy bytes.
                System.arraycopy(frameRowBytes, 0, frameBytes, frameBytesHead, frameRowBytes.length);
                frameBytesHead += frameRowBytes.length;

                // Flush to disk if we have a full frame.
                if (frameBytesHead >= frameBytes.length) {

                    // Clean existing files if there are too many.
                    if (images.size() > 50) {
                        File f = images.get(0);
                        images.remove(0);
                        f.delete();
                    }

                    // Write file.
//                    try {
//                        workingFile.createNewFile();
//                        FileOutputStream fos = new FileOutputStream(workingFile);
//                        fos.write(frameBytes);
//                        fos.flush();
//                        fos.close();
//                        images.add(workingFile);
                        System.out.printf("Captured image to disk (%s) @ %d (took %d milliseconds).\n", workingFile.getName(), System.currentTimeMillis(), System.currentTimeMillis() - start);
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
                }
            }
        });
    }
}

@@ -1060,16 +1060,18 @@
 e file.%0A
+//
         
@@ -1133,16 +1133,16 @@
 .raw%22);%0A
-
         
@@ -1535,117 +1535,8 @@
  %7D%0A%0A
-                System.out.printf(%22Started new frame (%25d) @ %25d.%5Cn%22, timestamp, System.currentTimeMillis());%0A%0A
     
@@ -2039,16 +2039,18 @@
 o many.%0A
+//
         
@@ -2083,24 +2083,26 @@
 e() %3E 50) %7B%0A
+//
             
@@ -2133,24 +2133,26 @@
 ges.get(0);%0A
+//
             
@@ -2181,16 +2181,18 @@
 ove(0);%0A
+//
         
@@ -2215,24 +2215,26 @@
 f.delete();%0A
+//
             
@@ -2693,13 +2693,8 @@
 disk
- (%25s)
  @ %25
@@ -2726,31 +2726,8 @@
 %5Cn%22,
- workingFile.getName(),
  Sys

Disable file generation for a bit.