Smart Home System Programming Exercise
My name is Karaka Sri Sai Nitin & I am pursuing my Final Year in B.Tech Computer Science and Engineering(CSE) with specialization in Cyber Security. My enthusiasm for Cyber Security, Machine Learning, and Web Development has motivated me to engage in a variety of projects, continuously expanding my expertise and honing my skills in these fields.
The repository provides the solutions for both the coding exercises developed in Java.
This project involves implementing a Smart Home System that allows users to control various smart devices such as lights, thermostats, and door locks through a central hub. The system enables users to:
- Switch ON or OFF devices.
- Setting schedules for device operations.
- Automating Tasks based on Triggers (e.g., turning off lights when the thermostat reaches a certain temperature).
The solution utilizes Object-Oriented Programming (OOP) principles and incorporates the following design patterns:
- Observer Pattern: All devices are updated automatically whenever a change is made in the system.
- Factory Method Pattern: Used to create different type of smart devices (lights, thermostats, door locks).
- Proxy Pattern: Controls access to the devices, ensuring security and management through the central hub.
This ensures modularity, encapsulation, and a strong design architecture.
Here are six innovative use cases that showcase my understanding of various software design patterns, demonstrated through coding implementations:
- Scenario: When the main door is unlocked, all lights are automatically turned on. When the main door is locked, all lights are turned off.
- Pattern: Observer Pattern ensures that the status of all connected devices is updated based on changes in the system (e.g., door lock status).
- Scenario: Different temperature control strategies can be applied based on user preferences or energy-saving goals.
- Pattern: Strategy Pattern enables switching between different temperature control modes, such as Eco mode or Comfort mode, based on the user’s choice.
- Scenario: A factory method is used to create various smart home devices (lights, thermostats, door locks) based on input parameters.
- Pattern: Factory Pattern ensures that a single point of creation is maintained for all devices. If a device type is unsupported, an error is generated.
- Scenario: Used as an alternative to the Factory Pattern to create complex smart home devices, offering flexibility in their construction.
- Pattern: Builder Pattern provides an alternate way of constructing devices, especially when Factory Pattern has already been implemented.
- Scenario: The SmartHomeHub must maintain only one instance to control all connected devices.
- Pattern: Singleton Pattern ensures that only one instance of the SmartHomeHub exists throughout the system, centralizing control of the devices.
- Scenario: Legacy devices that don’t follow the modern interface of new smart home devices need to be integrated.
- Pattern: Adapter Pattern allows the legacy devices to work seamlessly within the smart home ecosystem by adapting their interface.
- Scenario: Multiple devices are grouped together (e.g., all lights in a room) so that operations can be performed on the group as a whole.
- Pattern: Composite Pattern facilitates the grouping of devices, enabling actions such as turning all lights on or off with a single operation.
- Java
- Object-Oriented Programming (OOP)
In Main.java, you will find the test code for each of these use cases, demonstrating the implementation of the various design patterns.
This console-based application allows users to manage and control their Smart Home System, enabling them to organize and automate tasks for various smart devices. Users can add, remove, view, and modify device settings, schedule device actions, and automate tasks based on specific conditions. Devices include lights, thermostats, and door locks, which can all be managed from a central hub.
This console-based application offers users several options to perform operations based on their needs. The following are the key features available in the application:
- Create a new device: Add a new smart device (light, thermostat, or door lock) to the system.
- Select a device and perform an action: Choose an existing device and perform an action like turning it on/off or locking/unlocking.
- View current devices: Display a list of all devices currently connected to the system, along with their statuses.
- Remove a device: Delete a specific device from the system.
- Set control system or custom temperature for the thermostat: Adjust the thermostat’s temperature manually or switch to automatic control which contain specific modes such as Comfort Mode and Eco Mode which will automatically set the temperature of the thermostat.
- Set a schedule: Schedule specific actions for devices (e.g., turning lights on/off at a certain time).
- Create a trigger: Automate tasks by setting conditions (e.g., turn off lights when a temperature threshold is reached).
Example input for creating a trigger:
Choose an option: 7
Enter the condition for the trigger: temperature > 70 (Enter your own condition)
Enter the action to perform when triggered: TurnOn(id) which tells us to mention the id of the device
Trigger created successfully.
- List pending schedules: View all scheduled tasks that are yet to be executed.
- List pending triggers: View all set triggers that are awaiting activation.
- Exit: Quit the application and save all current system settings.
Each class is placed in its own file, and packages are used to organize files based on their functionality.
Java naming conventions followed in the project:
- Class Names: PascalCase is used for all class names.
Example:SmartHomeHub,DeviceFactory. - Package Names: Lowercase is used for all package names.
Example:com.smarthome.device. - Methods and Variables: camelCase is used for methods and variables.
Since id and type are common for all devices, these properties have been moved to a shared abstract base class. This base class is extended by all concrete device classes (e.g., Light, Door, Thermostat). This approach avoids duplication and adheres to the DRY (Don't Repeat Yourself) principle.
Additionally, validations for common fields like id and type are implemented within the abstract class.
- Abstract Base Class: An abstract class
AbstractDevicehas been created to hold common properties likeidandtype. - Inheritance: Concrete device classes (e.g.,
Light,Door,Thermostat) extend theAbstractDeviceclass, inheriting these common properties. - Device Interface: The
Deviceinterface focuses purely on the behaviors (methods) without the need to repeat properties.
- Documentation: Added Javadoc comments to the classes and constructors to clarify their purpose and usage.
- Java
- Object Oriented Programming
- Design Patterns (Observer Pattern, Factory Method Pattern, Proxy Pattern)
- Logging Mechanism utilizes
java.util.loggingmodule for logging system activities and debugging info with Level and Logger - Exception Handling Mechanism with the help of
exceptionsmodule in which I have implemented InvalidTriggerException, UnauthorizedAccessException, UnsupportedActionException for handling any edge cases
In MainConsole.java, you will find the test code for the console-based application that provides the user with certain functionalities and it provides a user interface menu that allows users to navigate through options and performs actions based on their selection for managing your Smart Home System.
- The
src/com/smarthomecontains all the .java files where the code has been written for the project. - The
out/production/SmartHomeApplication/com/smarthomecontains the .class files of the project. - The
Main.javaandMainConsole.javaare runnable program files. Main.javais used for Exercise - 1 Design Pattern Implementation.MainConsole.javais used for Exercise - 2 Smart Home System Simulation.