Sunday, October 12, 2014

Using an 'rc' File in My System

First though, I've been away from my house control system for a while because of other projects, and I decided to do some major updates to portions of it.  Over the months, I've learned a lot and some of that new knowledge needs to be put into controlling the house.  So, yes, I'm posting more often.

Often times, I want to post a new update to one of the processes, but I have keys and other stuff in the modules that have to be removed before I can put it on the web.  I'm also going to move the code into GitHub at some point so folk don't have to copy and paste things for their own use.  They can just grab what they want from there.  That means I have to get the keys out of the code.

Unix has long had a customary way of doing things like this, an rc file.  The 'rc' stands for run command and has a long history, look it up.  There's a lot of files out that go into the user's home directory and have a name like .bashrc, .cshrc, etc.  These are configuration commands and aliases.  This is just the tool I need to use.  I can put an rc file in the home directory and all the keys, database names, etc can go in there to be read at runtime by the applications.  Cool, that way I can make something public without having to worry about somebody updating my data feeds by mistake.

But, I hate parsing data out of a file.  I was looking at parsers and ways of storing data when I got an email about decoding a JSON file.  While I was answering the mail I realized how easy it is in python to parse through JSON, so you guessed it, my rc file is JSON.  That means that it's a tiny bit harder to create and incredibly easy to use in a program.  Here's my .houserc file (with the secrets removed):

{
"database":"/home/pi/database/database",

"xbeeport": "/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A901QLZ1-if00-port0",
"zigbeeport":"/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A901QQ2F-if00-port0",

"oldxively":{
"key":"secretkeythingieforlegacyxivelygoeshere",
"feed":"1234"},

"xively":{
"key":"secretkeythingieforthenewinterfacehere",
"feed":"1234567890"},

"emoncms":{
"key":"theyuseashorterkey"},

"grovestreams":{
"org":"grovestream-has-even-stranger-one",
"apiKey":"with-a-strange-key-as-well"},

"thingspeak":{
"key":"ASIMPLEKEYEASYTOUSE"},

"oreo":"double stuff"
}

It's just a simple JSON string that can be easily taken apart.  By using meaningful (to me anyway) names, it's pretty easy to read and edit as well.  To get the data back out in a python program, all you have to do is:

def getHouseValues():
 json_data=open("/home/pi/.houserc").read()
 return json.loads(json_data)
hv = getHouseValues()
FEED_ID = hv["oldxively"]["feed"]
API_KEY = hv["oldxively"]["key"]
# the database where I'm storing stuff
cookie = hv["oreo"]

And, since I don't want to type the directory name over and over again, I put the getHouseValues() in a python file and imported it.

No parsing, no silly obscure things to learn, just things I already had to work with doing this project.  Admittedly, it was a pain going to each module and modifying it to use the rc file, but it was worth it.  Notice I included the weird USB ports I use for the two XBees I have attached?  I can do that with other things that may get changed from time to time.  Just add them to the JSON string and I'll be able to get them back at run-time without changing code in any module.

Heck, it's not even hard to read.

6 comments:

  1. Ahhhh.....sometimes simpler is better. Gotta love JSON.

    ReplyDelete
    Replies
    1. It's hard to believe that I had so much trouble with JSON when I first ran into it. I think it was the jargon involved in the various descriptions. When I finally got a handle on dictionaries and lists in python, the mystery sort of disappeared.

      Then, when I reformatted the various JSON strings I ran into such that they could actually be read, it actually became pretty simple.

      Delete
    2. When I saw the title rc files.....I misread it to be rc flies. I thought to myself Great, Dave's into rc control and we are going to see his latest Dave's Drone project......Where's the aerial pictures of the ranch???

      Delete
    3. I would love to play with a drone, and they're getting cheap enough to play with. I have so many projects going that I won't get to that for a long while. Maybe they'll be even cheaper then.

      Meanwhile, just use google earth.

      Delete
  2. My entire home automation routine operates off of the JSON data that is readily available from the Razberry Z-Wave daughterboard. Extremely easy and powerful. Love the idea of storing data for the program in a JSON file...

    ReplyDelete
    Replies
    1. What's also cool that I haven't done yet is that the file can be rewritten. I could decide I wanted to change something like my super-secret-never-to-be-spoken-of password and just put it in the file. Then when I wanted to change it, I could do it through a web page by reading the file, changing one item, the writing the file back.

      Sure, there should be critical sections around that, but if that's the only way to change it, I could do that. Heck, we could put schedules in there like the time to turn on the light by my bed. We could have sprinkler timers controlled by it.

      There's literally no end to the possibilities.

      Delete