/***************************** BEGIN LICENSE BLOCK ***************************

The contents of this file are subject to the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
 
The Initial Developer is Sensia Software LLC. Portions created by the Initial
Developer are Copyright (C) 2014 the Initial Developer. All Rights Reserved.
 
******************************* END LICENSE BLOCK ***************************/

package org.sensorhub.test.impl.sensor.weatherStation;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.UUID;

import net.opengis.sensorml.v20.AbstractProcess;
import net.opengis.swe.v20.DataComponent;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sensorhub.api.common.Event;
import org.sensorhub.api.common.IEventListener;
import org.sensorhub.api.common.SensorHubException;
import org.sensorhub.api.sensor.ISensorDataInterface;
import org.sensorhub.api.sensor.SensorDataEvent;
import org.sensorhub.impl.sensor.station.metar.MetarConfig;
import org.sensorhub.impl.sensor.station.metar.MetarSensor;
import org.vast.data.TextEncodingImpl;
import org.vast.sensorML.SMLUtils;
import org.vast.swe.AsciiDataWriter;
import org.vast.swe.SWECommonUtils;


public class TestWeatherStation implements IEventListener
{
    MetarSensor driver;
    MetarConfig config;
    AsciiDataWriter writer;
    int sampleCount = 0;

        
    @Before
    public void init() throws Exception
    {
        config = new MetarConfig();
        config.id = UUID.randomUUID().toString();
        // ... 
        
        driver = new MetarSensor();
        driver.init(config);
    }
    
    
    @Test
    public void testGetOutputDesc() throws Exception
    {
        for (ISensorDataInterface di: driver.getObservationOutputs().values())
        {
            System.out.println();
            DataComponent dataMsg = di.getRecordDescription();
            new SWECommonUtils().writeComponent(System.out, dataMsg, false, true);
        }
    }
    
    
    @Test
    public void testGetSensorDesc() throws Exception
    {
        System.out.println();
        AbstractProcess smlDesc = driver.getCurrentSensorDescription();
        new SMLUtils().writeProcess(System.out, smlDesc, true);
    }
    
    
    @Test
    public void testSendMeasurements() throws Exception
    {
        System.out.println();
        ISensorDataInterface gpsOutput = driver.getObservationOutputs().get("gpsLocation");
        
        writer = new AsciiDataWriter();
        writer.setDataEncoding(new TextEncodingImpl(",", "\n"));
        writer.setDataComponents(gpsOutput.getRecordDescription());
        writer.setOutput(System.out);
        
        gpsOutput.registerListener(this);
        driver.start();
        
        synchronized (this) 
        {
            while (sampleCount < 5)
                wait();
        }
        
        System.out.println();
    }
    
    
    @Override
    public void handleEvent(Event e)
    {
        assertTrue(e instanceof SensorDataEvent);
        SensorDataEvent newDataEvent = (SensorDataEvent)e;
        
        try
        {
            //System.out.print("\nNew data received from sensor " + newDataEvent.getSensorId());
            writer.write(newDataEvent.getRecords()[0]);
            writer.flush();
            
            sampleCount++;
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
                
        synchronized (this) { this.notify(); }
    }
    
    
    @After
    public void cleanup()
    {
        try
        {
            driver.stop();
        }
        catch (SensorHubException e)
        {
            e.printStackTrace();
        }
    }
}

@@ -848,24 +848,46 @@
 OException;%0A
+import java.util.Map;%0A
 import java.
@@ -2723,32 +2723,157 @@
     
-ISensorDataInterface gps
+Map%3CString, ? extends ISensorDataInterface%3E map = driver.getObservationOutputs();%0A        System.err.println(map);%0A        ISensorDataInterface metar
 Outp
@@ -2917,14 +2917,22 @@
 et(%22
-gpsLoc
+MetarWeatherSt
 atio
@@ -3083,19 +3083,21 @@
 ponents(
-gps
+metar
 Output.g
@@ -3179,11 +3179,13 @@
     
-gps
+metar
 Outp
@@ -3239,135 +3239,8 @@
 ();%0A
-        %0A        synchronized (this) %0A        %7B%0A            while (sampleCount %3C 5)%0A                wait();%0A        %7D%0A        %0A
     

Fix bug in test code