Sunday, September 12, 2010

Project Phase 2: Google_Appengine

Project Phase 2: GOOGLE APPENGINE

    Google appengine is a cloud platform. First download google_appengine from google website. Googgle_appengine recomments Python 2.5 .So install Python 2.5. Then install libssl-dev and libsqlite3-dev. Now unzip the google_appengine folder and copy to a folder that is created for this work. We are using the web server of the appengine for getting the information received to the RFID reader, and store it in the database of the appengine.
    For this process we have to POST a request containing the data that is read from the RFID to the appengine web server and the GET request will get this request and save this data to the database. All the files needed for getting and posting must be in a located in a unique directory. This directory must contain a configuration file called 'app.yaml'. Following is the app.yaml code

app.yaml
----------------------------------
  application: <directoryname>
  version: 1
  runtime:python
  api_version: 1

  handlers:
  - url: /.*
    script: <filename>
------------------------------------

    application: <directoryname> is the directory where this configuration file and all other files needed for the appengine is kept.

    script: <filename> is the name of the file which is to be excecuted when appengine starts.


POST REQUEST

    For posting a request we have to import 2 modules to this program- httplib and urllib. httplib is for creating a connection to the localhost and urllib is for encoding the data to a url.  It is the duty of the httplib to post the request. The code for post is given below.

post.py
---------------------------------------------------------------
import httplib
from urllib import urlencode

parameters=urlencode({'post1':'hello','post2':'world'})
headers={"Content-Type":"application/x-www-form-urlencoded",
            "Accept":"text/plain"}
conn=httplib.HTTPConnection("127.0.0.1:8080")
conn.request("POST","/",parameters,headers)
response=conn.getresponse()
print response.status,response.reason
print data
conn.close()
---------------------------------------------------------------

    parameters is urlencoded encoded data. Data to be posted is first added to a dictionary. We can provide the key and using the keys we can retrive the values(data).

    headers is the part which tells the web server how the data is encoded and how to decode it.
    Response from the web server can be received using getresponse(). We can print the status and reason of the response



GET REQUEST

    GET request comes inside our main program. Name of this file must be given in the configuration file app.yaml. The database is also created inside this  program. We can use templates for seperating the html part from this program. The code is given below.

main.py
------------------------------------------------------------------
import cgi
import os
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class Storage(db.Model):
    post1=db.StringProperty()
    post2=db.StringProperty()
    time=db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
    def post(self):
        storage=Storage()
        storage.post1=self.request.get('post1')
        storage.post2=self.request.get('post2')
        storage.put()

    def get(self):
        storages_query=Storage.all()
        storages=storages_query.fetch(10)

        template_values = {
                'storages':storages
                }
        path=os.path.join(os.path.dirname(__file__),'index.html')
        self.response.out.write(template.render(path, template_values))

application = webapp.WSGIApplication([('/',MainPage)],debug = True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()
----------------------------------------------------------------------------

    We have to import all the necessary modules from webapp like db, template etc. Create a database for storing the information that received from the post method. Data can be stored to the database by using <databasename>.put() . Here templates are use for seperation the designing codes from this program. All the information that we want to print in out main page is passed to this template. Path tells the appengine where to pass the information and template.render() which has two arguments, one is the path and other is the information to be passed.Here the informations are passed to a html file called index.html .

index.html
------------------------------------------------------------------------------
<html>
    <head>
        <script type='text/javascript'>
        </script>
    </head>
    <body onload="JavaScript: timedRefresh();">
        {% for storage in storages %}
        <b>Post1:</b> {{ storage.post1|escape }}<br/>
        <b>Post2:</b> {{ storage.post2|escape }}<br/>
        {% endfor %}
        <a href="javascript:location.reload(true)">Refresh</a>
    </body>
</html>
-----------------------------------------------------------------------------

    Adding the RFID reader program with the post method mentioned above and applying the necessary changes will make our RFID reader compactible with PC.

No comments:

Post a Comment