Home Automation - Chapter 1

Modular Preparation

For awhile I wanted to have some automation on my laptop. Every now and then I’m having all kinds of issues with my Internet connection like:

  • I’m connected to the WiFi of the other room that I don’t use on this laptop. Such connection will be very poor and I’m trying to avoid it. I ended up checking all the time and connecting manually to the right network.
  • Sometimes even if I’m connected to the correct WiFi network there are problems with the connection. That’s when I start checking my DNS and ping different services.
  • The worst of all is when my router goes haywire. In such cases I have to login to its web interface and reboot it.

Those 3 pains were my initial requirements. I want to have an automated tool that can fix those problems for me. Of course those are all functional requirements. I thought about few that are non-functional as well:

  • I want to have good supportability of my code so instead of building one monolithic application I want to have modules which are getting called. I’m not going to split those to micro services but having modular application will help me on the long run.
  • I want to have single source of dependencies which I can control in automated fashion. Since I was already thinking about Ruby for this app I will have a Gemfile. For the time being my app won’t be packaged in a Ruby gem and there will be no tests.
  • Optional: I might expand the app in the future with tests and put it in gem structure.

In this first chapter I will show you my strategy for having a modular structure. It’s actually a very simple one. I have all modules in a subdirectory:

modules/wifi_check.rb
modules/internet_check.rb
modules/internet_restore.rb

I also have a main.rb in the root directory which is calling all modules.

#!/usr/bin/env ruby

# WiFi Checks
load "modules/wifi_check.rb"
load "modules/internet_check.rb"

I’m using the “load” Ruby method to handle interdependent modules. For example if the “internet_check.rb” module fails to validate the Internet connection it will call “modules/internet_restore.rb” via the load method. That’s how I’ll trigger the restore functionality which will reboot the router.

Here are the libraries that I’m using so far:

source 'https://rubygems.org'

gem 'logger'
gem 'pry'
gem 'net-ping'
gem 'watir'

I’m going to enable those libraries only on modules where I need them using the ‘require’ method. In next chapters I’ll explain the functionality in the modules.

The whole code of my Home Automation project is available on Github.

Home Automation - Chapter 1

Home Automation - Chapter 2

Home Automation - Chapter 3

Home Automation - Chapter 4

Categories:

Updated: