package es.unican.docencia.y2012.ed.practica10;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* Clase que sirve como punto de entrada a la aplicaci�n
* y que contiene metodos de prueba
* @author Pablo Sanchez (p.sanchez@unican.es)
* http://personales.unican.es/sanchezbp
*/
public class Runner {

public enum HouseObject {FLOOR, ROOM, HEATER, THERMOMETER}

/**
* Punto de entrada a la aplicacion
* @param args Argumentos de la l�nea de comandos. Simplemente se ignoran
*/
public static void main(String[] args) {

Map<HouseObject,Map<String,Object>> houseObjects = new HashMap<HouseObject,Map<String,Object>>();
houseObjects.put(HouseObject.FLOOR, new HashMap<String,Object>());
houseObjects.put(HouseObject.ROOM, new HashMap<String,Object>());
houseObjects.put(HouseObject.HEATER, new HashMap<String,Object>());
houseObjects.put(HouseObject.THERMOMETER, new HashMap<String,Object>());

feedHouseObjects(houseObjects);

// Estructura de la casa. La guardamos para trabajar con ella
Gateway gtw = houseStructureTest(houseObjects);

// Comprobamos que no se admitan duplicados
testDuplicates(gtw);

// Comprobamos que no las habitaciones se han anhadido correctamente
testFloorsAdded(gtw);

testRoomsAdded(gtw);
Map<String,Heater> heaters = addHeatersTest(gtw, houseObjects);
testHeatersDuplicated(gtw);
heaters = testRemoveHeater(gtw, houseObjects);

Map<String,Thermometer> thermos = addThermosTest(gtw,houseObjects);
testThermosDuplicated(gtw);
thermos = testRemoveThermos(gtw, houseObjects);

// testActivateHeatersPerFloor(gtw,heaters);
// testDeactivateHeatersPerFloor(gtw,heaters);
// testActivateHeatersPerRoom(gtw,heaters);
// testDeactivateHeatersPerRoom(gtw,heaters);
// testActivateSingleHeater(gtw,heaters);
// testDeactivateSingleHeater(gtw,heaters);

// testSetTemperaturePerFloor(gtw,heaters);
// testSetTemperaturePerRoom(gtw,heaters);
// testSetTemperatureSingleHeater(gtw,heaters);

testTempChangesHeaterInactive(gtw, heaters, thermos);
testActivatingHeater(gtw, heaters, thermos, 25.0,25.0);
testActivatingHeater(gtw, heaters, thermos, 12.0,25.0);
testActivatingHeater(gtw, heaters, thermos, 25.0,12.0);
testChangeActiveInactive(gtw, heaters, thermos);

} // main

protected static void feedHouseObjects(Map<HouseObject,Map<String,Object>> ho) {

Floor ground = new Floor("Ground");
ho.get(HouseObject.FLOOR).put(ground.getId(), ground);
Floor first = new Floor("First");
ho.get(HouseObject.FLOOR).put(first.getId(), first);

Room kitchen = new Room("Kitchen",ground);
Room living = new Room("Living",ground);
Room groundBathroom = new Room("GroundBathroom",ground);
Room mainBedroom = new Room("MainBedroom",first);
Room firstBedroom = new Room("FirstBedroom",first);
Room secondBedroom = new Room("SecondBedroom",first);

ho.get(HouseObject.ROOM).put(kitchen.getId(), kitchen);
ho.get(HouseObject.ROOM).put(living.getId(), living);
ho.get(HouseObject.ROOM).put(groundBathroom.getId(), groundBathroom);
ho.get(HouseObject.ROOM).put(mainBedroom.getId(), mainBedroom);
ho.get(HouseObject.ROOM).put(firstBedroom.getId(), firstBedroom);
ho.get(HouseObject.ROOM).put(secondBedroom.getId(), secondBedroom);

} // feedHouseObject

protected static Gateway houseStructureTest(Map<HouseObject,Map<String,Object>> ho) {

Gateway gtw = new Gateway();

System.out.println(" Creating the structure of the house ");
System.out.println("=======================================");

testAddFloor(gtw, (Floor) ho.get(HouseObject.FLOOR).get("Ground"));
testAddFloor(gtw, (Floor) ho.get(HouseObject.FLOOR).get("First"));

System.out.println("");

testAddRoom(gtw, (Room) ho.get(HouseObject.ROOM).get("Kitchen"));
testAddRoom(gtw, (Room) ho.get(HouseObject.ROOM).get("Living"));
testAddRoom(gtw, (Room) ho.get(HouseObject.ROOM).get("GroundBathroom"));
testAddRoom(gtw, (Room) ho.get(HouseObject.ROOM).get("MainBedroom"));
testAddRoom(gtw, (Room) ho.get(HouseObject.ROOM).get("FirstBedroom"));
testAddRoom(gtw, (Room) ho.get(HouseObject.ROOM).get("SecondBedroom"));

System.out.println("=======================================");
System.out.println(" Structure sucessfully created");
System.out.println("");

return gtw;

} // houseStructureTest

protected static void testAddRoom(Gateway gtw, Room room) {
if (gtw.addRoom(room)) {
System.out.println(room.getId() + " room added");
} else {
System.out.println("Failure adding " + room.getId() + " room");
} // if
} // testAddRoom

protected static void testAddFloor(Gateway gtw, Floor floor) {
if (gtw.addFloor(floor)) {
System.out.println(floor.getId() + " floor added");
} else {
System.out.println("Failure adding " + floor.getId() + " floor");
} // if
} // houseStructureTest

protected static void testDuplicates(Gateway gtw) {

Floor first = new Floor("First");
Room mr = new Room("MainBedroom",first);
Room kitchen = new Room("Kitchen",first);

System.out.println("");
System.out.println("Adding some duplicates");
System.out.println("");

testAddFloor(gtw,first);
testAddRoom(gtw,kitchen);
testAddRoom(gtw,mr);
System.out.println("");

} // testDuplicates

protected static void testFloorsAdded(Gateway gtw) {
Collection<Floor> floors = gtw.getFloors();

System.out.println("");
System.out.println("Floors in the house ");
System.out.println("=======================");

for (Floor f : floors) {
System.out.println("Floor " + f.getId() + " included in the house");
} // for

System.out.println("");

} // testFloorsAdded

protected static void testRoomsAdded(Gateway gtw) {
Collection<Floor> floors = gtw.getFloors();

System.out.println("");
System.out.println("Rooms in the house ");
System.out.println("=======================");
System.out.println("");

for (Floor f : floors) {
System.out.println("Floor " + f.getId());
System.out.println("-------------------------------");
Collection<Room> rooms = f.getRooms();
for (Room r : rooms) {
System.out.println("Room " + r.getId());
} // for
System.out.println("");
} // for

System.out.println("");

} // testFloorsAdded

protected static Map<String,Heater> addHeatersTest(Gateway gtw,
Map<HouseObject,Map<String,Object>> ho) {

Map<String,Heater> result = new HashMap<String,Heater>();

Heater h1 = new Heater("FirstLiving", (Room) ho.get(HouseObject.ROOM).get("Living"));
Heater h2 = new Heater("SecondLiving",(Room) ho.get(HouseObject.ROOM).get("Living"));
Heater h3 = new Heater("Kitchen",(Room) ho.get(HouseObject.ROOM).get("Kitchen"));

System.out.println("Adding some heaters");
System.out.println("=======================");
System.out.println("");

if (testAddHeater(gtw,h1)) {
result.put(h1.getId(), h1);
} // if
if (testAddHeater(gtw,h2)) {
result.put(h2.getId(), h2);
} // if
if (testAddHeater(gtw,h3)) {
result.put(h3.getId(), h3);
} // if

System.out.println("");

return result;

} // addHeaters

protected static boolean testAddHeater(Gateway gtw, Heater h) {

boolean result = true;

if (gtw.addHeater(h)) {
System.out.println(h.getId() + " heater added");
result = true;
} else {
System.out.println("Failure when adding the " + h.getId() + " heater");
result = false;
} // if

return result;
} // houseStructureTest

protected static void testHeatersDuplicated(Gateway gtw) {

Floor ground = new Floor("Ground");
Room living = new Room("Living",ground);

Heater h1 = new Heater("FirstLiving",living);
Heater h2 = new Heater("SecondLiving",living);

System.out.println("");
System.out.println("Adding some duplicates");
System.out.println("");

testAddHeater(gtw,h1);
testAddHeater(gtw,h2);
System.out.println("");

} // testHeatersDuplicated

protected static Map<String,Heater> testRemoveHeater(Gateway gtw,
Map<HouseObject,Map<String,Object>> ho) {

Map<String,Heater> result = null;

System.out.println("");
System.out.println("Removing some heaters");
System.out.println("");

gtw.removeHeater("UnexistingHeater");
gtw.removeHeater("FirstLiving");
gtw.removeHeater("SecondLiving");
gtw.removeHeater("Kitchen");

System.out.println("Heaters should have been removed, so we can add them again");
System.out.println("");

result = addHeatersTest(gtw,ho);

System.out.println("");

return result;

} // testRemoveHeater

protected static void testActivateHeatersPerFloor(Gateway gtw, Map<String,Heater> heaters) {
printHeatersStatus(heaters);
System.out.println("");
System.out.println("Activating all heaters in the ground floor");
gtw.activateHeatersPerFloor("Ground");
System.out.println("Heaters sucessfully activated");
System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
} // testActivateHeatersPerFloor

protected static void testDeactivateHeatersPerFloor(Gateway gtw, Map<String,Heater> heaters) {
System.out.println("Deactivating all heaters in the ground floor");
gtw.deactivateHeatersPerFloor("Ground");
System.out.println("Heaters sucessfully deactivated");
System.out.println("");
printHeatersStatus(heaters);
} // testDeactivateHeatersPerFloor

protected static void testActivateHeatersPerRoom(Gateway gtw, Map<String,Heater> heaters) {
printHeatersStatus(heaters);
System.out.println("");
System.out.println("Activating all heaters in the living room");
gtw.activateHeatersPerRoom("Living");
System.out.println("Heaters sucessfully activated");
System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
} // testActivateHeatersPerFloor

protected static void testDeactivateHeatersPerRoom(Gateway gtw, Map<String,Heater> heaters) {
System.out.println("Deactivating all heaters in the living room");
gtw.deactivateHeatersPerRoom("Living");
System.out.println("Heaters sucessfully deactivated");
System.out.println("");
printHeatersStatus(heaters);
} // testDeactivateHeatersPerFloor

protected static void testActivateSingleHeater(Gateway gtw, Map<String,Heater> heaters) {
printHeatersStatus(heaters);
System.out.println("");
System.out.println("Activating the kitchen heater");
gtw.activate("Kitchen");
System.out.println("Heater sucessfully activated");
System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
} // testActivateHeatersPerFloor

protected static void testDeactivateSingleHeater(Gateway gtw, Map<String,Heater> heaters) {
System.out.println("Deactivating the kitchen heater");
gtw.deactivate("Kitchen");
System.out.println("Heater sucessfully deactivated");
System.out.println("");
printHeatersStatus(heaters);
} // testDeactivateHeatersPerFloor

protected static void testSetTemperaturePerFloor(Gateway gtw, Map<String,Heater> heaters) {
printHeatersStatus(heaters);
System.out.println("");
System.out.println("Setting all heaters in the first floor to 10 celsius");
gtw.setTemperaturePerFloor("Ground",10.0);
System.out.println("Heaters sucessfully set");
System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
} // testActivateHeatersPerFloor

protected static void testSetTemperaturePerRoom(Gateway gtw, Map<String,Heater> heaters) {
printHeatersStatus(heaters);
System.out.println("");
System.out.println("Setting all heaters in the living to 10 celsius");
gtw.setTemperaturePerRoom("Living",10.0);
System.out.println("Heaters sucessfully set");
System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
} // testActivateHeatersPerFloor

protected static void testSetTemperatureSingleHeater(Gateway gtw, Map<String,Heater> heaters) {
printHeatersStatus(heaters);
System.out.println("");
System.out.println("Setting the kitchen heater to 10 celsius");
gtw.setTemperature("Kitchen",10.0);
System.out.println("Heater sucessfully set");
System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
} // testActivateHeatersPerFloor



protected static Map<String,Thermometer> addThermosTest(Gateway gtw,
Map<HouseObject,Map<String,Object>> ho) {

Map<String,Thermometer> map4Thermos = new HashMap<String,Thermometer>();

Room living = (Room) ho.get(HouseObject.ROOM).get("Living");
Room kitchen = (Room) ho.get(HouseObject.ROOM).get("Kitchen");

Thermometer t1 = new Thermometer("FirstLiving",living);
Thermometer t2 = new Thermometer("SecondLiving",living);
Thermometer t3 = new Thermometer("Kitchen",kitchen);

System.out.println("Adding some thermomemters");
System.out.println("=========================");
System.out.println("");

if (testAddThermometer(gtw,t1)) {
map4Thermos.put(t1.getId(), t1);
} // if
if (testAddThermometer(gtw,t2)) {
map4Thermos.put(t2.getId(), t2);
} // if
if (testAddThermometer(gtw,t3)) {
map4Thermos.put(t3.getId(), t3);
} // if

System.out.println("");

return map4Thermos;

} // addThermosTest

protected static boolean testAddThermometer(Gateway gtw, Thermometer t) {

boolean result = false;

if (gtw.addThermometer(t)) {
System.out.println(t.getId() + " thermometer added");
result = true;
} else {
System.out.println("Failure when adding the " + t.getId() + " thermometer");
result = false;
} // if

return result;
} // testAddThermometer

protected static Map<String,Thermometer> testRemoveThermos(Gateway gtw, Map<HouseObject,Map<String,Object>> ho) {

System.out.println("");
System.out.println("Removing some thermos");
System.out.println("");

gtw.removeThermometer("UnexistingThermo");
gtw.removeThermometer("FirstLiving");
gtw.removeThermometer("SecondLiving");
gtw.removeThermometer("Kitchen");

System.out.println("Thermos should have been removed, so we can add them again");
System.out.println("");

Map<String,Thermometer> result = addThermosTest(gtw,ho);

System.out.println("");

return result;

} // testRemoveHeater

protected static void testThermosDuplicated(Gateway gtw) {

Floor ground = new Floor("Ground");
Room living = new Room("Living",ground);

Thermometer t1 = new Thermometer("FirstLiving",living);
Thermometer t2 = new Thermometer("SecondLiving",living);

System.out.println("");
System.out.println("Adding some duplicates");
System.out.println("");

testAddThermometer(gtw,t1);
testAddThermometer(gtw,t2);
System.out.println("");

} // testHeatersDuplicated

protected static void printHeatersStatus(Map<String,Heater> heaters) {

Collection<Heater> heatersList = heaters.values();

for (Heater h : heatersList) {
System.out.println(h);
} // for
} // printHeatersStatus

protected static void printThermosStatus(Map<String,Thermometer> thermos) {

Collection<Thermometer> thermoList = thermos.values();

for (Thermometer t : thermoList) {
System.out.println(t);
} // for
} // printHeatersStatus

protected static void allHeatersInactive(Gateway gtw) {
for(Floor f : gtw.getFloors()) {
gtw.deactivateHeatersPerFloor(f.getId());
} // for
} // allHeatersInactive

protected static void testTempChangesHeaterInactive(Gateway gtw,
Map<String,Heater> heaters,
Map<String,Thermometer> thermos) {


printHeatersStatus(heaters);
System.out.println("");
printThermosStatus(thermos);
System.out.println("");

Thermometer living = thermos.get("Kitchen");
living.setTemperature(12.0);

System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
printThermosStatus(thermos);


} // testTempChangesHeaterInactive

protected static void testActivatingHeater(Gateway gtw, Map<String,Heater> heaters,
Map<String,Thermometer> thermos,
double thermoTemp, double heaterTemp) {

printHeatersStatus(heaters);
System.out.println("");
printThermosStatus(thermos);
System.out.println("");

Heater h = heaters.get("Kitchen");
h.deactive();

Thermometer living = thermos.get("Kitchen");
living.setTemperature(thermoTemp);
System.out.println("Cambiada la temperatura del termometro de la cocina a " + thermoTemp);

h.setTemperature(heaterTemp);
gtw.activate(h.getId());
System.out.println("Heater de la cocina activado " + heaterTemp);

System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
printThermosStatus(thermos);

} // testTempChangesHeaterInactive

protected static void testChangeActiveInactive(Gateway gtw, Map<String,Heater> heaters,
Map<String,Thermometer> thermos) {

printThermosStatus(thermos);
System.out.println("");

allHeatersInactive(gtw);

System.out.println("Ponemos los heaters del sal�n a 22 grados.");
gtw.setTemperaturePerRoom("Living", 22.0);
System.out.println("Activamos los heaters del salon.");
gtw.activateHeatersPerRoom("Living");

System.out.println("");
printHeatersStatus(heaters);
System.out.println("");

Thermometer living = thermos.get("FirstLiving");
System.out.println("Cambiamos la temperatura del termometro del salon a 18");
living.setTemperature(18.0);

System.out.println("");
printHeatersStatus(heaters);
System.out.println("");

System.out.println("Desactivamos los heaters del salon");
gtw.deactivateHeatersPerRoom("Living");
System.out.println("");

System.out.println("Cambiamos la temperatura del termometro del salon a 30");
living.setTemperature(30.0);

System.out.println("");
printHeatersStatus(heaters);
System.out.println("");
printThermosStatus(thermos);

} // testChangeInactive


} // Runner

Última modificación: jueves, 25 de mayo de 2017, 19:43