/**************************************************************************************************
 * Copyright (C) 2010 Sense Observation Systems, Rotterdam, the Netherlands. All rights reserved. *
 *************************************************************************************************/
package nl.sense_os.service.ambience;

import java.util.ArrayList;
import java.util.List;

import nl.sense_os.service.R;
import nl.sense_os.service.constants.SenseDataTypes;
import nl.sense_os.service.constants.SensorData.DataPoint;
import nl.sense_os.service.constants.SensorData.SensorNames;
import nl.sense_os.service.provider.SNTP;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.util.Log;

public class PressureSensor implements SensorEventListener {

    private static final String TAG = "Sense Pressure Sensor";

    private long sampleDelay = 0; // in milliseconds
    private long[] lastSampleTimes = new long[50];
    private Context context;
    private List<Sensor> sensors;
    private SensorManager smgr;
    private Handler PressureHandler = new Handler();
    private Runnable PressureThread = null;
    private boolean PressureSensingActive = false;

    public PressureSensor(Context context) {
        this.context = context;
        smgr = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        sensors = new ArrayList<Sensor>();
        if (null != smgr.getDefaultSensor(Sensor.TYPE_PRESSURE)) {
            sensors.add(smgr.getDefaultSensor(Sensor.TYPE_PRESSURE));
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Log.d(TAG, "Accuracy changed...");
        // Log.d(TAG, "Sensor: " + sensor.getName() + "(" + sensor.getType() + "), accuracy: " +
        // accuracy);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        if (System.currentTimeMillis() > lastSampleTimes[sensor.getType()] + sampleDelay) {
            lastSampleTimes[sensor.getType()] = System.currentTimeMillis();

            String sensorName = "";
            if (sensor.getType() == Sensor.TYPE_PRESSURE) {
                sensorName = SensorNames.PRESSURE;
            }

            String jsonString = "{";
            int x = 0;
            for (float value : event.values) {
                if (x == 0) {
                    if (sensor.getType() == Sensor.TYPE_PRESSURE)
                        jsonString += "\"millibar\":" + value;
                }
                x++;
            }
            jsonString += "}";

            // send msg to MsgHandler
            Intent i = new Intent(context.getString(R.string.action_sense_new_data));
            i.putExtra(DataPoint.DATA_TYPE, SenseDataTypes.JSON);
            i.putExtra(DataPoint.VALUE, jsonString);
            i.putExtra(DataPoint.SENSOR_NAME, sensorName);
            i.putExtra(DataPoint.SENSOR_DESCRIPTION, sensor.getName());
            i.putExtra(DataPoint.TIMESTAMP, SNTP.getInstance().getTime());
            context.startService(i);
        }
        if (sampleDelay > 500 && PressureSensingActive) {
            // unregister the listener and start again in sampleDelay seconds
            stopPressureSensing();
            PressureHandler.postDelayed(PressureThread = new Runnable() {

                @Override
                public void run() {
                    startPressureSensing(sampleDelay);
                }
            }, sampleDelay);
        }
    }

    public void setSampleDelay(long _sampleDelay) {
        sampleDelay = _sampleDelay;
    }

    public void startPressureSensing(long _sampleDelay) {
        PressureSensingActive = true;
        setSampleDelay(_sampleDelay);
        for (Sensor sensor : sensors) {
            if (sensor.getType() == Sensor.TYPE_PRESSURE) {
                // Log.d(TAG, "registering for sensor " + sensor.getName());
                smgr.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
            }
        }
    }

    public void stopPressureSensing() {
        try {
            PressureSensingActive = false;
            smgr.unregisterListener(this);

            if (PressureThread != null)
                PressureHandler.removeCallbacks(PressureThread);
            PressureThread = null;

        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

    }

    public long getSampleDelay() {
        return sampleDelay;
    }
}
@@ -2450,11 +2450,12 @@
 g = 
-%22%7B%22
+null
 ;%0A  
@@ -2469,169 +2469,239 @@
    i
-nt x = 0;%0A            for (float value : event.values) %7B%0A                if (x == 0) %7B%0A                    if (sensor.getType() == Sensor.TYPE_PRESSURE)%0A        
+f (sensor.getType() == Sensor.TYPE_PRESSURE) %7B%0A                jsonString = %22%7B%22;%0A                // value is millibar, convert to Pascal%0A                float millibar = event.values%5B0%5D;%0A                float pascal = value * 100;%0A
     
@@ -2733,28 +2733,27 @@
  %22%5C%22
-millibar
+Pascal
 %5C%22:%22 + 
-value
+pascal
 ;%0A  
@@ -2757,34 +2757,51 @@
 %0A               
+ 
- %7D
+jsonString += %22%7D%22;
 %0A               
@@ -2801,16 +2801,16 @@
     
-    x++;
+%7D else %7B
 %0A   
@@ -2822,40 +2822,85 @@
     
-%7D%0A            jsonString += %22%7D%22;
+    // not the right sensor%0A                return;%0A            %7D            
 %0A%0A  

Use Pascal as unit for pressure sensor