C++: Add server side to EventAgentSample
authorjani <jani@asema.com>
Wed, 7 Aug 2019 21:10:55 +0000 (00:10 +0300)
committerjani <jani@asema.com>
Wed, 7 Aug 2019 21:10:55 +0000 (00:10 +0300)
Examples/C++/EventAgentSample/server.py [new file with mode: 0755]

diff --git a/Examples/C++/EventAgentSample/server.py b/Examples/C++/EventAgentSample/server.py
new file mode 100755 (executable)
index 0000000..a80b13c
--- /dev/null
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+
+import paho.mqtt.publish as publish
+
+from SmartAPI.factory.NotificationFactory import NotificationFactory
+from SmartAPI.factory.ResponseFactory import ResponseFactory
+from SmartAPI.common.Tools import Tools
+from SmartAPI.common.SERIALIZATION import SERIALIZATION
+from SmartAPI.common.NS import NS
+from SmartAPI.model.Status import Status
+from SmartAPI.model.Activity import Activity
+from SmartAPI.model.Entity import Entity
+
+mqtt_server = "broker.asema.com"
+
+calculation_id = 1
+
+delay_min_time_seconds = 10
+delay_max_time_seconds = 20
+
+import web
+
+class SampleServer():
+    def __init__(self):
+        pass
+
+    # Creates a dummy result in RDF format and sends it over MQTT
+    # to listeners
+    def notify_ready(self, id):
+        notification = NotificationFactory().create("http://www.calculator.com/example")
+        notification.setMessageId(id)
+        status = Status()
+        status.setPercentage(100)
+        notification.setStatus(status)
+        activity = Activity()
+        entity = Entity("http://www.calculator.com/calculatedentity")
+        activity.addEntity(entity)
+        notification.addActivity(activity)
+    
+        payload, content_type = Tools().serializeNotification(notification)
+        publish.single("sample/result/youcom/calculations", payload, hostname=mqtt_server)
+    
+    def POST(self):
+        # parse request message into Smart API Request Object     
+        req_str = rawRequest(web.ctx.env) 
+        content_type = web.ctx.env.get('Content-Type')
+        request = Tools().parseRequest(req_str, content_type)
+
+        response = ResponseFactory().create("http://talk.asema.com/demo/smart/demoresponder")
+        activity = Activity()
+        response.addActivity(activity)
+        status = Status()
+        status.setPercentage(100)
+        response.setStatus(status)
+        
+        payload, content_type = Tools().serializeResponse(response)
+        
+        self.notify_ready(1)
+        return payload
+
+
+def rawRequest(env):
+    req = env['wsgi.input'].read(int(env['CONTENT_LENGTH']))
+    return req    
+
+if __name__ == "__main__":
+    # the url for this tiny Smart API server will be: 
+    # http://0.0.0.0:8080/demo/smart/v1.0e1.0/access
+    app = web.application(('/smart/v1.0e1.0/access', 'SampleServer'), globals())
+    web.httpserver.runsimple(app.wsgifunc(), ("0.0.0.0", 3333))
+    
\ No newline at end of file