Python: added sample client / server code
authorjani <jani@asema.com>
Sat, 3 Aug 2019 18:08:30 +0000 (21:08 +0300)
committerjani <jani@asema.com>
Sat, 3 Aug 2019 18:08:30 +0000 (21:08 +0300)
.gitignore
Examples/Python/SampleClientServer/sampleServer.py [new file with mode: 0644]
Examples/Python/SampleClientServer/smartapisample.py [new file with mode: 0644]
codestats.sh [new file with mode: 0755]

index ed6f4b21f8e6dd06e664797cc59b4315e303965b..e6b1645bbbcf4208ea22452cf429ff442bb99150 100644 (file)
@@ -2,7 +2,6 @@
 .pydevproject
 *.war
 *.jar
-!javalib/*.jar
 *.dll
 *.log
 *.old
diff --git a/Examples/Python/SampleClientServer/sampleServer.py b/Examples/Python/SampleClientServer/sampleServer.py
new file mode 100644 (file)
index 0000000..c6d91aa
--- /dev/null
@@ -0,0 +1,69 @@
+#!/usr/bin/python
+
+import web
+
+from SmartAPI.common.Tools import Tools
+from SmartAPI.common.SERIALIZATION import SERIALIZATION
+from SmartAPI.common.RESOURCE import RESOURCE
+from SmartAPI.common.NS import NS
+from SmartAPI.model.PhysicalEntity import PhysicalEntity
+from SmartAPI.model.ValueObject import ValueObject
+from SmartAPI.model.Activity import Activity
+from SmartAPI.model.Velocity import Velocity
+from SmartAPI.model.Direction import Direction
+from SmartAPI.factory.ResponseFactory import ResponseFactory
+
+urls = (
+    '/demo/smart/v1.0e1.0/access', 'DummyTestingHandler'
+)
+
+class DummyTestingHandler:
+    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)
+        # do something with this Request object if you wish.
+        print type(request)
+        
+        response = ResponseFactory().create("http://talk.asema.com/demo/smart/demoresponder")
+        activity = Activity()
+        e1 = PhysicalEntity("http://talk.asema.com/demo/smart/objects/Cdemoindentity1")
+        e2 = PhysicalEntity("http://talk.asema.com/demo/smart/objects/Cdemoindentity2")
+        
+        e1.addType(NS.SMARTAPI + "Skooter")
+        e2.addType(NS.SMARTAPI + "Skooter")
+        
+        v1 = Velocity()
+        v2 = Velocity()
+        d1 = Direction()
+        d2 = Direction()
+        
+        v1.setGroundSpeed(ValueObject(unit = NS.UNIT + "KilometerPerHour", value = 54))
+        v2.setGroundSpeed(ValueObject(unit = NS.UNIT + "KilometerPerHour", value = 41))
+        d1.setBearing(ValueObject(unit = NS.UNIT + "DegreeAngle", value = 183))
+        d2.setBearing(ValueObject(unit = NS.UNIT + "DegreeAngle", value = 164))
+        
+        e1.setVelocity(v1)
+        e2.setVelocity(v2)
+        e1.setDirection(d1)
+        e2.setDirection(d2)
+        
+        activity.addEntity(e1)
+        activity.addEntity(e2)
+        response.addActivity(activity)
+               
+        payload, content_type = Tools().serializeResponse(response)
+        web.header('Content-Type', content_type)
+        
+        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(urls, globals())
+    app.run()
\ No newline at end of file
diff --git a/Examples/Python/SampleClientServer/smartapisample.py b/Examples/Python/SampleClientServer/smartapisample.py
new file mode 100644 (file)
index 0000000..1a29e4d
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+
+from SmartAPI.common.HttpClient import HttpClient
+from SmartAPI.model.ValueObject import ValueObject
+from SmartAPI.factory.Factory import Factory
+from SmartAPI.model.Entity import Entity
+from SmartAPI.common.Tools import Tools
+from SmartAPI.common.NS import NS
+
+import traceback
+
+myIdentity = "http://smart-api.io/smart/examples/Cskooterfetchersample"
+
+#serverUri = "http://talk.smart-api.io/demo/smart/v1.0e1.0/access"
+serverUri = 'http://0.0.0.0:8080/demo/smart/v1.0e1.0/access'
+
+httpClient = HttpClient()
+
+def main():
+    try:
+        e = Entity()
+        e.addType(NS.SMARTAPI + "Skooter")
+        request = Factory.createReadRequest(myIdentity, e)
+        
+        payload, content_type = Tools.serializeRequest(request)
+        print 'sending...'
+        print payload
+        
+        response_body, response_headers = httpClient.sendPost(serverUri, payload, content_type = content_type)
+        print 'receiving...'
+        print response_body
+                 
+        resp = Tools().parseResponse(response_body, content_type)
+        # Or
+#         resp = httpClient.sendPostWithRequest(serverUri, request)
+        
+        for skooter in resp.firstActivity().getEntities():
+            print "Found a skooter", skooter.getIdentifierUri()
+            print "Current velocity:", skooter.getVelocity().getGroundSpeed().getValue().asInt(), skooter.getVelocity().getGroundSpeed().getUnit()
+            print "Current bearing:", skooter.getDirection().getBearing().getValue().asInt(), skooter.getDirection().getBearing().getUnit()
+            print
+    except:
+        print 'Failed to fetch entity information.'
+        traceback.print_exc()
+
+if __name__ == '__main__':
+    main()
\ No newline at end of file
diff --git a/codestats.sh b/codestats.sh
new file mode 100755 (executable)
index 0000000..2cca668
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+cs=`( find ./ -name '*.cs' -print0 | xargs -0 cat ) | wc -l`
+java=`( find ./ -name '*.java' -print0 | xargs -0 cat ) | wc -l`
+py=`( find ./ -name '*.py' -print0 | xargs -0 cat ) | wc -l`
+php=`( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l`
+rb=`( find ./ -name '*.rb' -print0 | xargs -0 cat ) | wc -l`
+go=`( find ./ -name '*.go' -print0 | xargs -0 cat ) | wc -l`
+cpp=`( find ./ -regex '\(.*cpp\|.*h\)' -print0 | xargs -0 cat ) | wc -l`
+
+echo var lc_cs=$cs;
+echo var lc_java=$java;
+echo var lc_py=$py;
+echo var lc_rb=$rb;
+echo var lc_php=$php;
+echo var lc_cpp=$cpp;
+echo var lc_go=$go;