Python: add asDateTime to Variant
authorjani <jani@asema.com>
Thu, 8 Aug 2019 08:05:44 +0000 (11:05 +0300)
committerjani <jani@asema.com>
Thu, 8 Aug 2019 08:05:44 +0000 (11:05 +0300)
Common/Python/SmartAPI/rdf/Variant.py
Examples/Python/AdaptDataService/AdaptDataService.py

index 34ff8d66a16616d39396f3c5be2d9f26fe125589..0f5b7e52704c07904ec6995243db2b00c2cf6429 100644 (file)
@@ -29,14 +29,6 @@ class Variant(object):
                else:
                        return self.asTerm()
 
-       def asTerm(self):
-               if self.isUri():
-                       return self.variant
-               elif self.isNull():
-                       return None
-               else:
-                       return Literal(self.variant)
-
        @classmethod
        def parse(cls, element, custom_classes = None):
                # get predicate and object
@@ -186,7 +178,15 @@ class Variant(object):
        def getAsString(self):
                return str(self.variant)
 
-       # Most method below have no significance in Python as the language
+       def asTerm(self):
+               if self.isUri():
+                       return self.variant
+               elif self.isNull():
+                       return None
+               else:
+                       return Literal(self.variant)
+               
+       # Most methods below have no significance in Python as the language
        # is not strongly typed. They are however offered as convenience
        # to make implementions between different languages as similar
        # as possible
@@ -226,6 +226,9 @@ class Variant(object):
 
        def asBoolean(self):
                return self.variant
+       
+       def asDateTime(self):
+               return self.variant
                
        def asDate(self):
                try:
index dc27c58ca57974fbd7b14dd260cdbcda28466e72..9aa16d4971f0a25581ea0f4f02002cc7c96159ef 100755 (executable)
@@ -5,6 +5,7 @@ from io import BytesIO
 from threading import Thread
 from httpclient import HttpClient
 import time
+import datetime
 import traceback
 import random
 
@@ -235,8 +236,6 @@ class SampleDataService(BaseHTTPRequestHandler):
                                        self.handleSubscription(a)
                                        
                                elif a.method.asString() == RESOURCE.READ:
-                                       print "Reading data"
-                                       
                                        # Handle timeseries request
                                        if a.hasTemporalContext():
                                                response_bytes.write(self.handleTemporalContext(a, request))
@@ -254,18 +253,16 @@ class SampleDataService(BaseHTTPRequestHandler):
                        iface = activity.getInterfaces()[0]
                        print "Will send data to", iface.getHost().asString()
                        n = Notifier(iface)
-                       n.run()
+                       #n.run()
                
                else:
                        print "Error: cannot subscribe without inteface data"
        
        def handleTemporalContext(self, activity, request):
-               print("Timeseries request: ")
-               print("start")
-               print(activity.temporalContext.start)
-               print("end")
-               print(activity.temporalContext.end)
-               
+               start = activity.temporalContext.start.asDateTime()
+               end =  activity.temporalContext.end.asDateTime()
+               print "Timeseries request from ", start, "to", end
+
                # For the client to recognize from which activity this result is, the response
                # activity will carry the same identifier that was in the request
                response = ResponseFactory.create(myIdentity, request)
@@ -275,29 +272,20 @@ class SampleDataService(BaseHTTPRequestHandler):
                The way results are obtained is up to the server. This could
                for instance be a result from a database query,
                """
-               results = [
-                       {'time': '2019-06-10T15:18:54.842', 'power': 25},
-                       {'time': '2019-06-01T02:11:54.842', 'power': 21},
-                       {'time': '2019-05-25T11:11:54.842', 'power': 32}]
-               
                time_series = TimeSeries()
-               
-               # Add data to response timeseries
-               for r in results:
+               while start < end:
                        value_object = ValueObject()
-                       value_object.setInstant(r["time"])
-                       value_object.setValue(r["power"])
+                       value_object.setInstant(start)
+                       value_object.setValue(random.randint(100, 400))
                        
                        time_series.addListItem(value_object)
+                       start = start + datetime.timedelta(hours=1)
                        
-                       # Add timeseries to respose activity
-                       response_activity.addTimeSerie(time_series)
-                       
-                       response.addActivity(response_activity)
-                       
-                       print("Reply:")
-                       print(Tools.serializeResponse(response)[0])
-                       return Tools.serializeResponse(response)[0]
+               # Add timeseries to respose activity
+               response_activity.addTimeSerie(time_series)
+               response.addActivity(response_activity)
+               
+               return Tools.serializeResponse(response)[0]
 
 
 def main():