package orwell.rfid;

import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
import lejos.nxt.addon.RFIDSensor;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by parapampa on 14/06/15.
 */
public class Scanner {
    private RFIDSensor rfidSensor;
    private static String FILENAME = "rfidlog.dat";


    public Scanner(SensorPort sensorPort) {
        this.rfidSensor = new RFIDSensor(sensorPort);
    }

    private FileOutputStream createFileOutputStream() {
        FileOutputStream out = null; // declare outside the try block
        File data = new File(FILENAME);

        try {
            out = new FileOutputStream(data);
        } catch(IOException e) {
            System.err.println("Failed to create output stream");
            System.exit(1);
        }
        return out;
    }

    public void run() {
        FileOutputStream fileOutputStream = createFileOutputStream();
        DataOutputStream dataOut = new DataOutputStream(fileOutputStream);

        Sound.beep();
        String rfidValueCurrent;

        while (!Button.ESCAPE.isDown()) {
            rfidValueCurrent = Long.toString(rfidSensor.readTransponderAsLong(true));

            LCD.clear(4);
            LCD.clear(5);
            LCD.drawString(rfidValueCurrent, 0, 4, false);
            LCD.drawString(Integer.toString(rfidSensor.getStatus()), 0, 5, false);

            try {
                dataOut.writeBytes(rfidValueCurrent);
            } catch (IOException e) {
                System.err.println("Failed to write to output stream");
                Sound.buzz();
            }
        }
        try {
            dataOut.close();
        } catch (IOException e) {
            System.err.println("Failed to close output stream");
            Sound.buzz();
        }
        Sound.beepSequence();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(SensorPort.S2);
        scanner.run();

    }
}

@@ -335,16 +335,127 @@
 anner %7B%0A
+    private static final String RFID_EMPTY_VALUE = %220%22;%0A    private static final String VALUE_SEPARATOR = %22 %22;%0A
     priv
@@ -481,16 +481,16 @@
 Sensor;%0A
-
     priv
@@ -1219,16 +1219,26 @@
 und.beep
+SequenceUp
 ();%0A    
@@ -1587,24 +1587,90 @@
 5, false);%0A%0A
+            if (!rfidValueCurrent.equals(RFID_EMPTY_VALUE)) %7B%0A    
             
@@ -1667,32 +1667,36 @@
           try %7B%0A
+    
                 
@@ -1729,24 +1729,119 @@
 ueCurrent);%0A
+                    dataOut.writeBytes(VALUE_SEPARATOR);%0A                    Sound.beep();%0A    
             
@@ -1858,32 +1858,36 @@
 OException e) %7B%0A
+    
                 
@@ -1950,32 +1950,36 @@
                 
+    
 Sound.buzz();%0A  
@@ -1968,32 +1968,50 @@
   Sound.buzz();%0A
+                %7D%0A
             %7D%0A  

Add SEPARATOR between registered RFID values, add beep once a value is read, do not register 0 values