Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, May 5, 2014

Let Me Just Write That Into My Schedule ... Not

I knew when I was assigned the task to develop a schedule for our project it wouldn't be easy. I just didn't have any idea how not easy it would be. I'll review a few of the decisions that we made regarding how schedules will be implemented in our project.

1. Lights, Rooms, and Houses will have schedules. This is opposed to a system where a schedule would be in charge of managing all of the parts of a house. If a schedule were to be in charge of managing the house, it would simplify things because all of the schedule information would be located in one place. On the other hand, Lights, Rooms, and Houses each having their own schedule feels like a better reflection of how the world works. I like to think that I have my own personal schedule that I choose to follow in going to classes and appointments, rather than my schedule ruling over me and dictating what I must do.

2. Triggered events are represented by a start time AND an end time. This decision was primarily made based upon how we plan to implement checking whether or not the light should be on a schedule. The plan is to examine the current time and the light's schedule, and if the current time indicates that the light is scheduled to be in a certain state, then the light will be switched to that state. Then if a user changes the state of the light from its scheduled state, a variable will be set indicating that the light is intended to be in the state that it is, rather than its scheduled state. Once the end time has been reached, this "override" switch can be reset to off so that the schedule will work as planned in the future.

In an ideal world, we would like the database to push information regarding whether the lights should be on or off to the client rather than having the client constantly fetch what state the lights are in on the database. For this reason, our decisions on how to implement the schedule may change in the future. 

Tuesday, April 22, 2014

Accessing Databases via Python

In order for our Raspberry Pis to be able to turn off and on their lights according to the state on the website, some sort of interfacing with the database outside of the Django framework will be required. This can easily be accomplished using the same PyMySQL module that allowed the Django framework on Python3 to be compatible with MySQL. What follows is a basic script which we used to access our database and get information about the states of the lights. By modifying this basic script to include code that Arik has been posting about GPIO reading and writing, our application should be nearing a usable state where modifying the database equates to a change "in the real world."

import sys
import time
import pymysql

lights = {'bathroom light 1':0,'bathroom light 2':1,
        'kitchen light':2, 'bedroom light':3}
conn = pymysql.connect(
db='database',
user='user',
passwd='password',
host='123.45.67.890',
)
cur = conn.cursor()
cur.execute("SELECT * FROM lightDB WHERE user_Id = <user_id>") 
for line in cur:
    for key,val in lights.items():
        if key==line[1] and line[3]==1:
            print("turning",key,"on")
        elif key==line[1] and line[3]==0:
            print("turning",key,"off")
Each line in cur is a tuple that has various information about the lights, the 1st (line[1]) item in the tuple is the description of the light, and the 3rd (line[3]) item is the state of whether the light is off or on.

There are some obvious security concerns at this point if this database were a database maintained by the company which contained all of its users lights. For this and other reasons, we are considering restructuring our design regarding where the server for the lights is run and how the information is accessed/backed up. Look for more on this in a later post.

Thursday, March 6, 2014

Unit Test Exceptions in Python

One of the goals for the next meeting was to develop tests for logging a user in to the system. This is something that is to me personally as a software developer, so I was grateful that the Django tutorial covered how to run tests in Python/Django. I did run into some difficulty though, when it came to trying to retrieve a user that was not in the database. Trying to do this would raise an exception during execution of the program.The Django framework has a method for expected exceptions called assertRaises(), but when I tried to use this, I kept getting an exception during run-time.

I thought to myself, "What's the point of this silly method? It's supposed to detect when an exception is raised, but it doesn't catch the exception before it is thrown." Here is how I was calling the assertRaises() method.
self.assertRaises(User.DoesNotExist, User.objects.get_by_natural_key('fyodor'))
Can you guess why I was still getting an exception? I'll give you a hint, it has something to do with how function argument are passed in the stack frame. Still stumped? I'll show you the solution, and maybe that will give you an idea.
with self.assertRaises(User.DoesNotExist):
    User.objects.get_by_natural_key('fyodor') 
The exception was still being raised because when function had arguments, they are placed on the stack before the function is called. This means that User.objects.get_by_natural_key() was being called before the program called the assertRaises() function was called. The program had no idea that the exception was expected at that point, so the test kept on failing.

At least, that's how I made sense of the scenario.

Tuesday, February 18, 2014

Standing Meeting 1: Initializing...

Hot dogs.

That was what was in front of us as we had our first standing meeting for Project HAM. We had previously decided that we would have our standing meeting over dinner, which was an interesting experience to say the least. Thankfully corndogs were being served for dinner, so it made standing and eating relatively easy.

We made a list of things that we wanted to accomplish before the week's end. This is what it included:
  • Install Python3 on the server
  • Setup a MySQL database for the django framework to access
  • Create a website which our potential users could access
We set up another meeting for Wednesday evening where we will make our first steps toward these goals. Arik is the only one in our group who has experience with databases, so setting up the MySQL database will probably be the main topic of Wednesday's meeting. 

We plan on using Django with the MySQL database, which will be a new interface for all of the members of our group. Karl found this resource which might come in handy as we reach that point in our project.

Stay tuned for more info on what exactly Project HAM is and what progress we are making towards our goals.

-Team PorkPI