<commit_before>/**
 *
 * This file is part of the https://github.com/BITPlan/can4eve open source project
 *
 * Copyright 2017 BITPlan GmbH https://github.com/BITPlan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 *  You may obtain a copy of the License at
 *
 *  http:www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bitplan.obdii;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;

import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.apache.commons.io.FileUtils;
import org.junit.Ignore;
import org.junit.Test;

import com.bitplan.can4eve.CANInfo;
import com.bitplan.can4eve.CANValue;
import com.bitplan.can4eve.CANValue.DoubleValue;
import com.bitplan.can4eve.Pid;
import com.bitplan.elm327.Config;
import com.bitplan.elm327.Connection;
import com.bitplan.elm327.LogImpl;
import com.bitplan.elm327.Packet;
import com.bitplan.obdii.elm327.ELM327;
import com.bitplan.obdii.elm327.ELM327SimulatorConnection;
import com.bitplan.obdii.elm327.ElmSimulator;
import com.bitplan.triplet.OBDTriplet;

/**
 * Test ELM327 communication
 * 
 * @author wf
 *
 */
public class TestELM327 extends TestOBDII {

  public static boolean debug = false;
  public static boolean simulated = true;
  // the vehicle under test

  public int SIMULATOR_TIMEOUT =50; // Simulator should be quick 2 msecs is feasible

  protected static Logger LOGGER = Logger.getLogger("com.bitplan.obdii");
  private Socket elmSocket;
  private OBDTriplet obdTriplet;
  private TripletDisplay display;

  /**
   * get the Socket for the testVehicle
   * 
   * @return
   * @throws UnknownHostException
   * @throws IOException
   */
  public Socket getTestVehicleSocket(Config config)
      throws UnknownHostException, IOException {
    Socket elmSocket = new Socket(config.getIp(), config.getPort());
    return elmSocket;
  }

  /**
   * get the simulation
   * 
   * @return the elm327 connected to the elm327 Simulator
   * @throws Exception
   */
  public ELM327 getSimulation() throws Exception {
    ELM327 elm327 = new ELM327(getVehicleGroup());
    ElmSimulator.verbose=debug;
    ElmSimulator elm327Simulator = ElmSimulator.getInstance();
    elm327Simulator.debug=debug;
    ServerSocket serverSocket = elm327Simulator.getServerSocket();
    Socket clientSocket=new Socket("localhost",serverSocket.getLocalPort());
    Connection con=elm327.getCon();
    con.connect(clientSocket);
    if (debug)
      con.setLog(new LogImpl());
    con.start();
    Thread.sleep(10);
    assertTrue(elm327.getCon().isAlive());
    ELM327SimulatorConnection elm327SimulatorConnection = elm327Simulator.getSimulatorConnection(clientSocket);
    assertNotNull(elm327SimulatorConnection);
    Connection simcon = elm327SimulatorConnection.getCon();
    assertTrue(simcon.isAlive());
    con.setTimeout(SIMULATOR_TIMEOUT);
    simcon.setTimeout(SIMULATOR_TIMEOUT);
    return elm327;
  }

  /**
   * prepare OBD Triplet
   * <commit_msg>tries fixing timing for jenkins tests<commit_after>/**
 *
 * This file is part of the https://github.com/BITPlan/can4eve open source project
 *
 * Copyright 2017 BITPlan GmbH https://github.com/BITPlan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 *  You may obtain a copy of the License at
 *
 *  http:www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bitplan.obdii;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;

import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.apache.commons.io.FileUtils;
import org.junit.Ignore;
import org.junit.Test;

import com.bitplan.can4eve.CANInfo;
import com.bitplan.can4eve.CANValue;
import com.bitplan.can4eve.CANValue.DoubleValue;
import com.bitplan.can4eve.Pid;
import com.bitplan.elm327.Config;
import com.bitplan.elm327.Connection;
import com.bitplan.elm327.LogImpl;
import com.bitplan.elm327.Packet;
import com.bitplan.obdii.elm327.ELM327;
import com.bitplan.obdii.elm327.ELM327SimulatorConnection;
import com.bitplan.obdii.elm327.ElmSimulator;
import com.bitplan.triplet.OBDTriplet;

/**
 * Test ELM327 communication
 * 
 * @author wf
 *
 */
public class TestELM327 extends TestOBDII {

  public static boolean debug = false;
  public static boolean simulated = true;
  // the vehicle under test

  public int SIMULATOR_TIMEOUT =50; // Simulator should be quick 2 msecs is feasible

  protected static Logger LOGGER = Logger.getLogger("com.bitplan.obdii");
  private Socket elmSocket;
  private OBDTriplet obdTriplet;
  private TripletDisplay display;

  /**
   * get the Socket for the testVehicle
   * 
   * @return
   * @throws UnknownHostException
   * @throws IOException
   */
  public Socket getTestVehicleSocket(Config config)
      throws UnknownHostException, IOException {
    Socket elmSocket = new Socket(config.getIp(), config.getPort());
    return elmSocket;
  }

  /**
   * get the simulation
   * 
   * @return the elm327 connected to the elm327 Simulator
   * @throws Exception
   */
  public ELM327 getSimulation() throws Exception {
    ELM327 elm327 = new ELM327(getVehicleGroup());
    ElmSimulator.verbose=debug;
    ElmSimulator elm327Simulator = ElmSimulator.getInstance();
    elm327Simulator.debug=debug;
    ServerSocket serverSocket = elm327Simulator.getServerSocket();
    Socket clientSocket=new Socket("localhost",serverSocket.getLocalPort());
    Connection con=elm327.getCon();
    con.connect(clientSocket);
    if (debug)
      con.setLog(new LogImpl());
    con.start();
    Thread.sleep(20); // 10 is not enough for jenkins on capri
    assertTrue(elm327.getCon().isAlive());
    ELM327SimulatorConnection elm327SimulatorConnection = elm327Simulator.getSimulatorConnection(clientSocket);
    assertNotNull(elm327SimulatorConnection);
    Connection simcon = elm327SimulatorConnection.getCon();
    assertTrue(simcon.isAlive());
    con.setTimeout(SIMULATOR_TIMEOUT);
    simcon.setTimeout(SIMULATOR_TIMEOUT);
    return elm327;
  }

  /**
   * prepare OBD Triplet
   * 
{'fraction_non_alphanumeric': 0.09469038384378761, 'fraction_numerical': 0.027818643841112746, 'mean_word_length': 4.330007127583749, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3829460', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2273, 'n_words': 593}