// Note: Instrumentation statements are highlighted in green 

#include "devices.hpp"

///////////////////////////////////////////////////////////////////////////////
// DeviceController class
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START(DeviceController, "Device Controller", 0)
VBITSET_VARIABLE(DeviceController, type, DeviceType, "Type", 0, 0)
VVARIABLE(DeviceController, name, "Name", 0, 0)
VBITSET_VARIABLE(DeviceController, status, DeviceStatus, "Status", 0, 0)
VVARIABLE(DeviceController, xLocation, "X location", 0, 0)
VVARIABLE(DeviceController, yLocation, "Y location", 0, 0)
VISIBILITY_END

DeviceController::DeviceController(void) :
  type(UNDEFINED_DEVICE),
  status(DEVICE_OK),
  xLocation(0.00),
  yLocation(0.00)
{
}

DeviceController::DeviceController(char * _name, unsigned short _type) :
  name(_name),
  type(_type),
  status(DEVICE_OK),
  xLocation(0.00),
  yLocation(0.00)
{
}

DeviceController::~DeviceController(void)
{
}

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

void DeviceController::populateEditView(void)
{
  name = "Device Controller";
}

DeviceController * DeviceController::createCopy(void)
{
  return new DeviceController(*this);
}

DeviceController & DeviceController::operator=(const DeviceController &that)
{
  name = that.name;
  type = that.type;
  status = that.status;
  xLocation = that.xLocation;
  yLocation = that.yLocation;
  return *this;
}

ostream & DeviceController::operator<<(ostream & stream)
{
  stream << name << ends;
  stream << type << " ";
  stream << status << " ";
  stream << xLocation << " ";
  stream << yLocation << " ";
  return stream;
}

istream & DeviceController::operator>>(istream & stream)
{
  stream >> ws;
  getline(stream, name, '\0');
  stream >> type;
  stream >> status;
  stream >> xLocation;
  stream >> yLocation;
  return stream;
}

// Override VObject's getPosition to allow access to object's positional information
unsigned short DeviceController::getPosition(float x_origin, float y_origin,
                                      float & object_x, float & object_y,
                                      unsigned short coord_type)
{
  object_x = xLocation - x_origin;
  object_y =  yLocation - y_origin;
  return 1;
}

// Override VObject's setPosition to allow modification of the object's positional information
unsigned short DeviceController::setPosition(float x_origin, float y_origin,
                                      float object_x, float object_y,
                                      unsigned short coord_type)
{
  xLocation = object_x + x_origin;
  yLocation = object_y + y_origin;
  return 1;
}

///////////////////////////////////////////////////////////////////////////////
// DoorLock class
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START1(DoorLock, DeviceController, "Door lock", 0)
VVOID_FUNCTION(DoorLock, lock, "Lock Door", 0, 0)
VVOID_FUNCTION(DoorLock, unlock, "Unlock Door", 0, 0)
VVOID_FUNCTION(DoorLock, alarmOn, "Alarm On", 0, 0)
VVOID_FUNCTION(DoorLock, alarmOff, "Alarm Off", 0, 0)
VISIBILITY_END

DoorLock::DoorLock(void) :
  DeviceController("Door lock", DOOR_LOCK)
{
}

DoorLock::DoorLock(const DoorLock & that) :
  DeviceController(that)
{
}

DeviceController * DoorLock::createCopy(void)
{
  return new DoorLock(*this);
}


bool DoorLock::processCommand(unsigned long command)
{
  switch (command)
  {
    case ACTIVATE :
      status |= DEVICE_LOCKED;
      break;

    case DEACTIVATE :
      status &= ~DEVICE_LOCKED;
      break;

    case SECURE :
      status |= DEVICE_SECURED;
      break;

    case UNSECURE :
      status &= ~DEVICE_SECURED;
      break;

    default :
      return false;
  }
  return true;
}

void DoorLock::lock(void)
{
  status |= DEVICE_LOCKED;
}

void DoorLock::unlock(void)
{
  status &= ~DEVICE_LOCKED;
}

void DoorLock::alarmOn(void)
{
  status |= DEVICE_SECURED;
}

void DoorLock::alarmOff(void)
{
  status &= ~DEVICE_SECURED;
}

///////////////////////////////////////////////////////////////////////////////
// WindowLock class
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START1(WindowLock, DeviceController, "Window lock", 0)
VVOID_FUNCTION(WindowLock, lock, "Lock Window", 0, 0)
VVOID_FUNCTION(WindowLock, unlock, "Unlock Window", 0, 0)
VVOID_FUNCTION(WindowLock, alarmOn, "Alarm On", 0, 0)
VVOID_FUNCTION(WindowLock, alarmOff, "Alarm Off", 0, 0)
VISIBILITY_END
 
WindowLock::WindowLock(void) :
  DeviceController("Window lock", WINDOW_LOCK)
{
}

WindowLock::WindowLock(const WindowLock & that) :
  DeviceController(that)
{
}

DeviceController * WindowLock::createCopy(void)
{
  return new WindowLock(*this);
}

bool WindowLock::processCommand(unsigned long command)
{
  switch (command)
  {
    case ACTIVATE :
      status |= DEVICE_LOCKED;
      break;

    case DEACTIVATE :
      status &= ~DEVICE_LOCKED;
      break;

    case SECURE :
      status |= DEVICE_SECURED;
      break;

    case UNSECURE :
      status &= ~DEVICE_SECURED;
      break;

    default :
      return false;
  }
  return true;
}

void WindowLock::lock(void)
{
  status |= DEVICE_LOCKED;
}

void WindowLock::unlock(void)
{
  status &= ~DEVICE_LOCKED;
}

void WindowLock::alarmOn(void)
{
  status |= DEVICE_SECURED;
}

void WindowLock::alarmOff(void)
{
  status &= ~DEVICE_SECURED;
}

///////////////////////////////////////////////////////////////////////////////
// MotionDetector class
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START1(MotionDetector, DeviceController, "Motion Detector", 0)
VVOID_FUNCTION(MotionDetector, turnOn, "Detector On", 0, 0)
VVOID_FUNCTION(MotionDetector, turnOff, "Detector Off", 0, 0)
VISIBILITY_END

MotionDetector::MotionDetector(void) :
  DeviceController("Motion detector", MOTION_DETECTOR)
{
}

MotionDetector::MotionDetector(const MotionDetector & that) :
  DeviceController(that)
{
}

DeviceController * MotionDetector::createCopy(void)
{
  return new MotionDetector(*this);
}

bool MotionDetector::processCommand(unsigned long command)
{
  switch (command)
  {
    case TURN_ON :
      status |= DEVICE_ON;
      break;

    case TURN_OFF :
      status &= ~DEVICE_ON;
      break;

    case SECURE :
      status |= DEVICE_SECURED;
      break;

    case UNSECURE :
      status &= ~DEVICE_SECURED;
      break;

    default :
      return false;
  }
  return true;
}

void MotionDetector::turnOn(void)
{
  status |= DEVICE_ON;
}

void MotionDetector::turnOff(void)
{
  status &= ~DEVICE_ON;
}

///////////////////////////////////////////////////////////////////////////////
// SmokeDetector class
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START1(SmokeDetector, DeviceController, "Smoke Detector", 0)
VVOID_FUNCTION(SmokeDetector, turnOn, "Detector On", 0, 0)
VVOID_FUNCTION(SmokeDetector, turnOff, "Detector Off", 0, 0)
VISIBILITY_END

SmokeDetector::SmokeDetector(void) :
  DeviceController("Smoke detector", SMOKE_DETECTOR)
{
}

SmokeDetector::SmokeDetector(const SmokeDetector & that) :
  DeviceController(that)
{
}

DeviceController * SmokeDetector::createCopy(void)
{
  return new SmokeDetector(*this);
}

bool SmokeDetector::processCommand(unsigned long command)
{
  switch (command)
  {
    case TURN_ON :
      status |= DEVICE_ON;
      break;

    case TURN_OFF :
      status &= ~DEVICE_ON;
      break;

    case SECURE :
      status |= DEVICE_SECURED;
      break;

    case UNSECURE :
      status &= ~DEVICE_SECURED;
      break;

    default :
      return false;
  }
  return true;
}

void SmokeDetector::turnOn(void)
{
  status |= DEVICE_ON;
}

void SmokeDetector::turnOff(void)
{
  status &= ~DEVICE_ON;
}

///////////////////////////////////////////////////////////////////////////////
// LightController class 
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START1(LightController, DeviceController, "Light Controller", 0)
VVARIABLE(LightController, brightness, "Brightness", 0, 0)
VVARIABLE(LightController, wattage, "Wattage", 0, 0)
VVOID_FUNCTION(LightController, turnOn, "Light On", 0, 0)
VVOID_FUNCTION(LightController, turnOff, "Light Off", 0, 0)
VISIBILITY_END

LightController::LightController(void) :
  DeviceController("Light controller", LIGHT_CONTROLLER),
  wattage(150),
  brightness(100)
{
}

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

DeviceController * LightController::createCopy(void)
{
  return new LightController(*this);
}

LightController & LightController::operator=(const LightController&that)
{
  DeviceController::operator=(that);
  brightness = that.brightness;
  wattage = that.wattage;
  return *this;
}

ostream & LightController::operator<<(ostream & stream)
{
  DeviceController::operator<<(stream);
  stream << brightness << " ";
  stream << wattage << " ";
  return stream;
}

istream & LightController::operator>>(istream & stream)
{
  DeviceController::operator>>(stream);
  stream >> brightness;
  stream >> wattage;
  return stream;
}

bool LightController::processCommand(unsigned long command)
{
  switch (command)
  {
    case TURN_ON :
      status |= DEVICE_ON;
      break;

    case TURN_OFF :
      status &= ~DEVICE_ON;
      break;

    default :
      return false;
  }
  return true;
}

void LightController::turnOn(void)
{
  status |= DEVICE_ON;
}

void LightController::turnOff(void)
{
  status &= ~DEVICE_ON;
}

///////////////////////////////////////////////////////////////////////////////
// ElectricalOutlet class
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START1(ElectricalOutlet, DeviceController, "Motion Detector", 0)
VVOID_FUNCTION(ElectricalOutlet, turnOn, "Detector On", 0, 0)
VVOID_FUNCTION(ElectricalOutlet, turnOff, "Detector Off", 0, 0)
VISIBILITY_END

ElectricalOutlet::ElectricalOutlet(void) :
  DeviceController("Electrical Outlet", ELECTRICAL_OUTLET)
{
}

ElectricalOutlet::ElectricalOutlet(const ElectricalOutlet & that) :
  DeviceController(that)
{
}

DeviceController * ElectricalOutlet::createCopy(void)
{
  return new ElectricalOutlet(*this);
}

bool ElectricalOutlet::processCommand(unsigned long command)
{
  switch (command)
  {
    case TURN_ON :
      status |= DEVICE_ON;
      break;

    case TURN_OFF :
      status &= ~DEVICE_ON;
      break;

    case SECURE :
      status |= DEVICE_SECURED;
      break;

    case UNSECURE :
      status &= ~DEVICE_SECURED;
      break;

    default :
      return false;
  }
  return true;
}

void ElectricalOutlet::turnOn(void)
{
  status |= DEVICE_ON;
}

void ElectricalOutlet::turnOff(void)
{
  status &= ~DEVICE_ON;
}

///////////////////////////////////////////////////////////////////////////////
// Thermostat class
///////////////////////////////////////////////////////////////////////////////

// Visual Member Definition 
VISIBILITY_START1(Thermostat, DeviceController, "Thermostat", 0)
VVARIABLE(Thermostat, minTemp, "Min temperature", 0, 0)
VVARIABLE(Thermostat, maxTemp, "Max temperature", 0, 0)
VVARIABLE(Thermostat, currentTemp, "Current temperature", 0, 0)
VISIBILITY_END

Thermostat::Thermostat(void) :
  DeviceController("Thermostat", THERMOSTAT),
  minTemp(78),
  maxTemp(80),
  currentTemp(78)
{
}

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

DeviceController * Thermostat::createCopy(void)
{
  return new Thermostat(*this);
}

Thermostat & Thermostat::operator=(const Thermostat &that)
{
  DeviceController::operator=(that);
  minTemp = that.minTemp;
  maxTemp = that.maxTemp;
  currentTemp = that.currentTemp;
  return *this;
}

ostream & Thermostat::operator<<(ostream & stream)
{
  DeviceController::operator<<(stream);
  stream << minTemp << " ";
  stream << maxTemp << " ";
  return stream;
}

istream & Thermostat::operator>>(istream & stream)
{
  DeviceController::operator>>(stream);
  stream >> minTemp;
  stream >> maxTemp;
  return stream;
}

bool Thermostat::processCommand(unsigned long command)
{
  switch (command)
  {
    case TURN_ON :
      status |= DEVICE_ON;
      break;

    case TURN_OFF :
      status &= ~DEVICE_ON;
      break;

    default :
      return false;
  }
  return true;
}

///////////////////////////////////////////////////////////////////////////////
// DeviceList class
///////////////////////////////////////////////////////////////////////////////

DeviceList::DeviceList(void)
{
  setDragDropFlags(VDRAGDROP_SRC_COPY | VDRAGDROP_SRC_MOVE | 
                    VDRAGDROP_DEST_COPY | VDRAGDROP_DEST_MOVE | 
                    VDRAGDROP_OWNER_CONTAINER);
}

DeviceList::DeviceList(DeviceList & device_list)
{
  *this = device_list;
}

DeviceList::~DeviceList(void)
{
  clear();
}

DeviceList & DeviceList::operator=(const DeviceList &device_list)
{
  clear();
  DeviceList::const_iterator device_iter = device_list.begin();
  while (device_iter != device_list.end()) {
    push_back((*device_iter)->createCopy());
    device_iter++;
  }
  return *this;
}

void DeviceList::clear(void)
{
  DeviceList::iterator device_iter = begin();
  while (device_iter != end()) {
    delete *device_iter;
    device_iter++;
  }
  erase(begin(), end());
}


///////////////////////////////////////////////////////////////////////////////
// DeviceTypes class
///////////////////////////////////////////////////////////////////////////////

DeviceTypes::DeviceTypes(void)
{
  setDragDropFlags(VDRAGDROP_SRC_COPY | VDRAGDROP_OWNER_CONTAINER);
  push_back(new ElectricalOutlet);
  push_back(new LightController);
  push_back(new DoorLock);
  push_back(new WindowLock);
  push_back(new Thermostat);
  push_back(new MotionDetector);
  push_back(new SmokeDetector);
}

DeviceTypes::DeviceTypes(DeviceTypes & device_types) :
  DeviceList(device_types)
{
}


Copyright 2002-2007 Outerface Technologies, Inc.