How a Fi module works

Fi is a platform. It could be compared to Android, in the sense that it lets you, as a user, install, manage and run apps flawlessly. What Fi offers for developers (Read: anyone with the ability to read Python) is a simple and easy to understand modular platform. What this means is that you could write a script that makes your microwave warm the milk for your morning coffee, silence its very useful and repetitive beeper, and tell you exactly how many hours of school you’ve missed by troubleshooting a script that warms the milk for your coffee at six o’clock in the morning.

In this post I’ll be explaining how Fi works, and how to write a script for Fi in Python.

The Internet Protocol

First of all, you should understand how Fi can control your microwave. Trust me, it’s important.

Fi is installed on a chip that’s connected to the router with a USB cable supplying the power, and with a LAN cable supplying the internet. The LAN cable is the bit we’re interested in, because once Fi gets assigned an IP address, it can brute force its way into any device in the house.

To understand this, you’ll need to understand how an IP address works:

An IP address looks like this: 192.168.1.1. Every series of three digits ranges from 000 to 255. Every device in your house has an IP address that starts with the same nine digits (192.168.001.XXX). If we list all the possible relevant IP addresses in this house, we’ll get a list of 255 addresses:

192.168.001.001

192.168.001.002

192.168.001.003

192.168.001.00…

Once we have that, we can ask every IP address if it’s occupied, or in other words if it belongs to a device in your house. If it does, it is relevant, and Fi will pick it up, try to talk to it, and if the device with this IP address answers, Fi will offer access to this device to any script that requests it.

So now that Fi gave us access to our microwave, not only can we assume we have access to our toaster, we can write a script to control our microwave. This is where Fi comes in handy.

A module of Fi

Instead of writing a piece of software to hack into every single device in a house, talking to that device, making sense of it, and then trying to tell that device what to do, you could just tell Fi what you want of a device, and Fi will do the rest.

You can write scripts, or modules, for Fi in Python or JavaScript. Here I’ll be describing what a Python module consists of:

from py4j.java_gateway import JavaGateway

# Configuration
config = dict(
name='Template.py', # This MUST be equal to the file name.
title='Sample module', # This is what the user will see.
version='1.0.0', # First release.
publisher='Rockbutton', # That's me!
update_freq=60.0  # Sixty seconds.
)

#
# This code is called before Start(), set up any global variables here.
#
gateway = JavaGateway() # This is a bit complicated, assume it's Fi's dad and carry on.
server = gateway.entry_point # This is Fi, ask her anything!

#
# This function gets called when the module loads for the first time.
#
def Start():
print("Module started!")
server.ModuleUpdated(config.get('name'), config.get('title'), "This is a sample module.", 0)

#
# This function gets called every [update_freq] seconds.
#
def Update():
if server.IsModuleRunning(config.get('name')) == False:
return # If the user wants to pause this module, we'll respect him and do nothing until he resumes the module.
server.ModuleUpdated(config.get('name'), config.get('title'), "Testing progress updates...", 50) # Tell Fi we're 50% done with something.

#
# This function is called when the user closes the module.
#
def Closed():
server.ModuleUpdated(config.get('name'), config.get('title'), "Canceled", 0) # Any last words?
print("I'm dead, Jim!") # Chop!

The way Fi will treat this module is as follows:

If this module is placed in the folder /Modules and is called Template,py (Read the code for explanation), Fi will first execute Start(), and then every sixty seconds (Again, read the code) Fi will execute Update(). Now, if the user wants to pause the module for any reason what so ever, we can detect that and either respect the user by doing nothing, or carry on with the execution of the script like nothing happened.

Eventually, when Fi feels like our module has over stayed its welcome, Closed() will be called, and out module will never be heard of again until the user starts it up again, manually.

How Fi, the server, works

In my next blog post, I’ll be walking you through the code base of Fi, describing the most important lines of code that make the modules do what they do.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s