package at.jku.pervasive.ecg;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.bluetooth.BluetoothStateException;

import com.intel.bluetooth.EmulatorTestsHelper;

/**
 * Idea taken from
 * http://bluecove.org/bluecove-emu/xref-test/net/sf/bluecove/ExampleTest.html
 */
public class HeartManSimulator {

  private final Map<Long, HeartManMock> mocks;
  private final List<Thread> serverThreads;

  public HeartManSimulator() throws BluetoothStateException {
    super();

    serverThreads = new LinkedList<Thread>();
    mocks = new HashMap<Long, HeartManMock>(1);

    EmulatorTestsHelper.startInProcessServer();
    EmulatorTestsHelper.useThreadLocalEmulator();
  }

  public void sendValue(long uuid, double value) {
    HeartManMock mock = mocks.get(uuid);
    if (mock != null) {
      mock.sendValue(value);
    }
  }

  public void startDevice(long uuid) throws BluetoothStateException {
    this.startServer(uuid, new HeartManMock(uuid));
  }

  public void startServer(long uuid, HeartManMock mock)
      throws BluetoothStateException {
    mocks.put(uuid, mock);
    Thread t = EmulatorTestsHelper.runNewEmulatorStack(mock);
    serverThreads.add(t);
  }

  public void stopServer() throws InterruptedException {
    for (long uuid : mocks.keySet()) {
      mocks.get(uuid).stop();
    }
    for (Thread serverThread : serverThreads) {
      if ((serverThread != null) && (serverThread.isAlive())) {
        serverThread.interrupt();
        serverThread.join();
      }
    }
    EmulatorTestsHelper.stopInProcessServer();
  }

}

@@ -890,28 +890,30 @@
 %7D%0A%0A  public 
-void
+String
  startDevice
@@ -965,13 +965,15 @@
     
-this.
+return 
 star
@@ -1025,20 +1025,22 @@
  public 
-void
+String
  startSe
@@ -1227,16 +1227,388 @@
 .add(t);
+%0A%0A    String mockName = mock.getName();%0A    try %7B%0A      List%3CHeartManDevice%3E devices = new HeartManDiscovery()%0A          .discoverHeartManDevices();%0A      for (HeartManDevice device : devices) %7B%0A        if (mockName.equals(device.getName())) %7B%0A          return device.getDevice().getBluetoothAddress();%0A        %7D%0A      %7D%0A    %7D catch (Exception e) %7B%0A    %7D%0A%0A    return null;
 %0A  %7D%0A%0A  

return bluetooth address of mock HeartManDevice