Source

MyIOTest / myiotest / views.py

The default branch has multiple heads

from myiotest.models import DBSession
from myiotest.models import MyModel
from pyramid.view import view_config
from pyramid.response import Response
from pyramid.settings import get_settings

import eventlet
from eventlet import wsgi
from pyramid.response import Response
from pyramid.view import view_config
from stargate import WebSocketAwareResource, WebSocketView, is_websocket


class JobRoot(object):
    """A container for jobs

    Gets or creates Job objects for certain ids
    """

    def __init__(self):
        self._jobs = {}

    def __getitem__(self, item):
        """
        """
        item=int(item)
        job = None
        if item not in self._jobs :
            job = self.create_job(item)
        else :
            job = self._jobs[item]
        print "la liste des jobs existants", self._jobs.keys()
        return job

    def create_job(self, id):
        job = Job(id, self)
        self._jobs[id] = job
        return job

myJobRoot = JobRoot()

class Job(WebSocketAwareResource):
    """This is a permanent object.

    It's responsible for maintaning a list of connected clients (websockets)
    and updating them when its state changes
    """

    def __init__(self, id, parent):
        self.__name__ = id
        self.__parent__ = parent
        self.state = "OFF"

    def control(self, state):
        """This function updates the state

        Its called by the control function (in response to a post)
        It triggers the sending of self.state to all connected clients. If you
        connect multiple browsers (or tabs) they will all be updated
        """
        self.state = state
        self.send(state)

# @view_config(route_name='jobview', context=Job)
@view_config(route_name='jobview', context=Job)
class JobView(WebSocketView):
    """The view connects pyramid with the resource

    In this simple example it simply adds the websocket to the Job's listeners.
    It then goes to sleep *blocking the thread it's in* this is where eventlet
    comes in. In real life you'd do things like listening for updates and
    handling messages coming in on the websocket in the while block.
    """

    def handler(self, websocket):
        job = self.request.context
        job.add_listener(websocket)
        while True:
            print "j'attends"
            # time.sleep(10)
            eventlet.sleep(20)

@view_config(route_name='control', context=Job, renderer='json', xhr=True)
def control(job, request):
    """Post to this view to set the state

    this will trigger Job to report the state to connected clients
    """
    state = request.POST.get("state")
    print "l'etat demande c'est : ", state
    if state:
        job.control(state)
    return dict(id=job.__name__, state=job.state)

class Root(dict):
    """The root of url traversal"""

    def __init__(self):
        pass

@view_config(route_name='home', context=Root, renderer='/index.mako')
def home(context, request):
    """Serves up home_html, setting up a simple js demo"""
    settings = get_settings()
    print settings
    port = request.server_port
    host = request.server_name
    return {u'host':host,u'port':port}