All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.epfl.gsn.wrappers.WanWrapper Maven / Gradle / Ivy

/**
* Global Sensor Networks (GSN) Source Code
* Copyright (c) 2006-2016, Ecole Polytechnique Federale de Lausanne (EPFL)
* 
* This file is part of GSN.
* 
* GSN is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* GSN is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with GSN.  If not, see .
* 
* File: src/ch/epfl/gsn/wrappers/WanWrapper.java
*
* @author Ali Salehi
* @author Mehdi Riahi
*
*/

package ch.epfl.gsn.wrappers;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import au.com.bytecode.opencsv.CSVReader;
import ch.epfl.gsn.beans.DataField;
import ch.epfl.gsn.beans.StreamElement;
import ch.epfl.gsn.wrappers.AbstractWrapper;

public class WanWrapper extends AbstractWrapper {
  // The first line describes the data logger, had to check for each file it reads.
  // The 2nd, 3rd and 4th lines are going to have data structure information for rest of the output
  // Time stamp is always the first column in the output.
  private static final String DateFormat = "yyyy-MM-dd HH:mm:ss";
  
  private static final String QUOTE = "\"";
  private static final String SAMPLING = "sampling";
  private int sampling = -1; //in milliseconds.
  private int SAMPLING_DEFAULT = 10*60*1000; // 10 mins
  
  private static final String FILE = "file";
  private String filename  =null; //in milliseconds.
  
  private final transient Logger   logger             = LoggerFactory.getLogger( WanWrapper.class );
  private DataField[] structure;
  private int threadCounter=0;
  private SimpleDateFormat dateTimeFormat ;
  
  public static final String NOT_A_NUMBER = "not_a_number";
  private List not_a_number_constants = new ArrayList() ;
  
  public boolean initialize() {
    dateTimeFormat = new SimpleDateFormat( DateFormat );
    sampling = getActiveAddressBean( ).getPredicateValueAsInt(SAMPLING, SAMPLING_DEFAULT);
    filename = getActiveAddressBean().getPredicateValue(FILE);
    String not_a_number_constant_val = getActiveAddressBean().getPredicateValue(NOT_A_NUMBER);
    
    if (not_a_number_constant_val != null && not_a_number_constant_val.trim().length()>0) {
      StringTokenizer st = new StringTokenizer(not_a_number_constant_val,",");
      while (st.hasMoreTokens()) 
        not_a_number_constants.add(st.nextToken().trim());
    }
    
    if (filename ==null||filename.length()==0 ) {
      logger.error("The wrapper failed, the "+FILE+" parameter is missing.");
      return false;
    }
    try {
      structure = headerToStructure(getHeader(filename));
    } catch (IOException e) {
      logger.error("The system expects a valid content [at least headers] inside "+filename);
      logger.error(e.getMessage(),e);
      return false;
    }
    return true;
  }
  
  public  String[][] getHeader(String filename) throws IOException {
    CSVReader reader = new CSVReader(new FileReader(filename));
    String[] data =  reader.readNext();
    if (data == null)
      return null;
    String[][] headers = new String[4][data.length];
    headers[0]=data;
    headers[1]= reader.readNext();
    headers[2]= reader.readNext();
    headers[3]= reader.readNext();
    if (headers[0]==null||headers[1]==null ||headers[2]==null||headers[3]==null) {
      logger.debug("Header read incompletely.");
      logger.debug(""+(headers[0]==null));
      logger.debug(""+(headers[1]==null));
      logger.debug(""+(headers[2]==null));
      logger.debug(""+(headers[3]==null));
      return null;
    }
    reader.close();
    return headers;
  }
  
  public static DataField[]  headerToStructure(String[][] header) {
    if (header == null)
      return null;
    DataField[] output = new DataField[header[1].length-1];
    for (int i=1;i2)
        description.append(" - ").append(header[3][i]);
      output[i-1]= new DataField(convertHeaderName(header[1][i]),"double", description.toString());
    }
    return output;
  }
  
  public static String convertHeaderName(String name) {
    return name.replace("(", "").replace(")", "");
  }
  public  StreamElement rowToSE(DataField[] structure, String[] data) {
    Date date;
    StreamElement se = null;
    try {
      date = dateTimeFormat.parse(data[0]);
      se = new StreamElement(structure,removeTimestampFromRow(structure,data),date.getTime());
    } catch (ParseException e) {
      logger.error("invalide date format! "+data[0]);
      logger.error(e.getMessage(),e);
    }finally {
      return se;
    }
  }
  
  public Double[] removeTimestampFromRow(DataField[] structure, String [] data) {
    Double[] toReturn = new Double[structure.length];
    next_val:for (int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy