data from bus to publishing function

IngoognI <[email protected]> Tue, 10 Jan 2017 13:14:35 -0800 (PST)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
A bunch of sensors is posting their data. I want to republish their data in 
a graph on a site using SSE. Simple versions of the latter work. Pushing 
data into the bus is also fine.

My problem is how to get the data into the publishing function? I can nest 
the print_cpu in the pubcpu and have the data printed, but not pushed to a 
site.

Codes below.

Ingo


---%<---server.py
import cherrypy
class Root():
    @cherrypy.expose
    @cherrypy.tools.json_in()
    def sensor(self, **kwargs):
        """push data POSTed by sensor to the bus"""
        input_json = cherrypy.request.json
        cpu = input_json['cpu']
        cherrypy.engine.publish("cpu", cpu)
    
    @cherrypy.expose
    def pubcpu(self):
        """publish data from sensor channel to page (SSE)"""
        pass
        
    def print_cpu(cpu):
        print("\n", cpu, "\n")
    cherrypy.engine.subscribe("cpu", print_cpu)

if __name__ == '__main__':
    cherrypy.quickstart(Root())
---

A more extende version of pubcpu for SSE would look like:

    @cherrypy.expose
    def pubcpu(self):
        cherrypy.response.headers["Content-Type"] = "text/event-stream"
        def pub(cpu):
            print("\n", cpu ,"\n")
            yield "event: time\n" + "data: " + str(cpu)+ "\n\n"
        return pub(cpu) #<-------??????????? put what in place of cpu
        cherrypy.engine.subscribe("cpu", pub)
    pubcpu._cp_config = {'response.stream': True}

---%<---sensor.py
import psutil, requests
headers = {"Content-Type": "application/json"}
url = 'http://localhost:8080/sensor/'
while True:
    payload={'cpu': psutil.cpu_percent(2)}
    r = requests.post(url, headers=headers, json=payload)
---

-- 
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.