// Note: Instrumentation statements are highlighted in green 

#include "home controller.hpp"

///////////////////////////////////////////////////////////////////////////////
// HomeController
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START(HomeController, "Home Security System", 0)
VVCONTAINER(HomeController, deviceList, "Control Devices", 0, 0)
VVITERATOR(HomeController, currentDevice, deviceList, DeviceList, "Current Device", 0, VNULL_CONTROL)
VVCONTAINER(HomeController, deviceTypes, "Device Types", 0, 0)
VBITSET_VARIABLE(HomeController, status, SystemStatus, "Status", 0, 0)
VVOID_FUNCTION(HomeController, lockWindows, "Lock windows", 0, 0)
VVOID_FUNCTION(HomeController, unlockWindows, "Unlock windows", 0, 0)
VVOID_FUNCTION(HomeController, lockDoors, "Lock doors", 0, 0)
VVOID_FUNCTION(HomeController, unlockDoors, "Unlock doors", 0, 0)
VVOID_FUNCTION(HomeController, turnOnLights, "Turn on lights", 0, 0)
VVOID_FUNCTION(HomeController, turnOffLights, "Turn off lights", 0, 0)
VVOID_FUNCTION(HomeController, enableAlarms, "Enable alarms", 0, 0)
VVOID_FUNCTION(HomeController, disableAlarms, "Disable alarms", 0, 0)
VVOID_FUNCTION(HomeController, enableOutlets, "Enable outlets", 0, 0)
VVOID_FUNCTION(HomeController, disableOutlets, "Disable outlets", 0, 0)
VVOID_FUNCTION(HomeController, selectAll, "Select all", 0, 0)
VVOID_FUNCTION(HomeController, clearSelection, "Clear selection", 0, 0)
VVOID_FUNCTION(HomeController, enableSelectedItems, "Enable selection", 0, 0)
VVOID_FUNCTION(HomeController, disableSelectedItems, "Disable selection", 0, 0)
VVOID_FUNCTION(HomeController, deleteSelectedItems, "Delete selection", 0, 0)
VVARIABLE(HomeController, trashCan, "Trash can", 0, 0);
VISIBILITY_END

HomeController::HomeController(void)
{
  currentDevice = deviceList.begin();
  status = SYSTEM_OK;
}

HomeController::HomeController(const HomeController & that)
{
  *this = that;
}

HomeController::~HomeController(void)
{
}

void HomeController::populateEditView(void)
{
  DeviceController * device_controller = new LightController;
  device_controller->xLocation = 2.0;
  device_controller->yLocation = 1.0;
  deviceList.push_back(device_controller);
  device_controller = new Thermostat;
  device_controller->xLocation = 3;
  device_controller->yLocation = 4;
  deviceList.push_back(device_controller);
  device_controller = new DoorLock;
  device_controller->xLocation = 7.0;
  device_controller->yLocation = 5.0;
  deviceList.push_back(device_controller);
  currentDevice = deviceList.begin();
  status = SYSTEM_OK;
}

HomeController & HomeController::operator=(const HomeController &that)
{
  deviceList = that.deviceList;
  currentDevice = deviceList.begin();
  status = that.status;
  return *this;
}

void HomeController::processCommand(unsigned short device_mask, unsigned long command)
{
  DeviceList::iterator device_iter = deviceList.begin();
  while (device_iter != deviceList.end()) {
    if ((*device_iter)->type & device_mask)
      (*device_iter)->processCommand(command);
    device_iter++;
  }
}

void HomeController::lockDoors(void)
{
  processCommand(DOOR_LOCK, ACTIVATE);
}

void HomeController::unlockDoors(void)
{
  processCommand(DOOR_LOCK, DEACTIVATE);
}

void HomeController::lockWindows(void)
{
  processCommand(WINDOW_LOCK, ACTIVATE);
}

void HomeController::unlockWindows(void)
{
  processCommand(WINDOW_LOCK, DEACTIVATE);
}

void HomeController::turnOnLights(void)
{
  processCommand(LIGHT_CONTROLLER, TURN_ON);
}

void HomeController::turnOffLights(void)
{
  processCommand(LIGHT_CONTROLLER, TURN_OFF);
}

void HomeController::enableAlarms(void)
{
  processCommand(ALL_DEVICES, SECURE);
}

void HomeController::disableAlarms(void)
{
  processCommand(ALL_DEVICES, UNSECURE);
}

void HomeController::enableOutlets(void)
{
  processCommand(ELECTRICAL_OUTLET, TURN_ON);
}

void HomeController::disableOutlets(void)
{
  processCommand(ELECTRICAL_OUTLET, TURN_OFF);
}

void HomeController::selectAll(void)
{
  DeviceList::iterator device_iter = deviceList.begin();
  while (device_iter != deviceList.end()) {
     selectContainerItem(&deviceList, (*device_iter), true);
    device_iter++;
  }
}

void HomeController::clearSelection(void)
{
  DeviceList::iterator device_iter = deviceList.begin();
  while (device_iter != deviceList.end()) {
     selectContainerItem(&deviceList, (*device_iter), false);
    device_iter++;
  }
}

void HomeController::enableSelectedItems(void)
{
  VSelectionList * selection = getSelectionList(&deviceList);
  VSelectionList::iterator sel_iter = selection->begin();
  while (sel_iter != selection->end()) {
    dynamic_cast<DeviceController *>(*sel_iter)->processCommand(TURN_ON);
    sel_iter++;
  }
  delete selection;
}

void HomeController::disableSelectedItems(void)
{
  VSelectionList * selection = getSelectionList(&deviceList);
  VSelectionList::iterator sel_iter = selection->begin();
  while (sel_iter != selection->end()) {
    dynamic_cast<DeviceController *>(*sel_iter)->processCommand(TURN_OFF);
    sel_iter++;
  }
  delete selection;
}

void HomeController::deleteSelectedItems(void)
{
  DeviceList::iterator device_iter;
  VSelectionList * selection = getSelectionList(&deviceList);
  VSelectionList::iterator sel_iter = selection->begin();
  while (sel_iter != selection->end()) {
    device_iter = std::find(deviceList.begin(), deviceList.end(), dynamic_cast<DeviceController *>(*sel_iter));
    if (device_iter != deviceList.end()) {
      delete *device_iter;
      deviceList.erase(device_iter);
    }
    sel_iter++;
  }
  delete selection;
  currentDevice = deviceList.begin();
}

ostream & HomeController::operator<<(ostream & stream)
{
  DeviceList::iterator device_iter = deviceList.begin();
  stream << (int) deviceList.size() << " ";
  while (device_iter != deviceList.end()) {
    stream << (*device_iter)->type << " ";
    (*device_iter)->operator<<(stream);
    device_iter++;
  }
  stream << status << " ";
  return stream;
}

istream & HomeController::operator>>(istream & stream)
{
  DeviceController * device_entry;
  unsigned short type;
  int size;
  stream >> size;
  while (size--) {
    stream >> type;
    if (type == DOOR_LOCK)
      device_entry = new DoorLock;
    else if (type == WINDOW_LOCK)
      device_entry = new WindowLock;
    else if (type == LIGHT_CONTROLLER)
      device_entry = new LightController;
    else if (type == MOTION_DETECTOR)
      device_entry = new MotionDetector;
    else if (type == SMOKE_DETECTOR)
      device_entry = new SmokeDetector;
    else if (type == THERMOSTAT)
      device_entry = new Thermostat;
    else if (type == ELECTRICAL_OUTLET)
      device_entry = new ElectricalOutlet;
    device_entry->operator>>(stream);
    deviceList.push_back(device_entry);
  }
  stream >> status;
  currentDevice = deviceList.begin();
  return stream;
}




Copyright 2002-2007 Outerface Technologies, Inc.