Ejemplo n.º 1
0
 def _serverParseJSON_FETCH(self, j_body):
     k_limit = self._getQueryAcceptableSize(j_body)
     k_skip = self._getQueryCursor(j_body.get("cursor"))
     #
     # copy the query for the response.
     #
     keys = self._getKey_JSONList(j_body.get("key"))
     if keys == None:
         self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_BAD_REQUEST, self.emsg)
         return self.doc
     j_plist = []
     j_rquery = j_body
     total = 0
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         #
         # get points.
         #
         for k in keys:
             try:
                 cursor = m.iterPoint(k, k_limit, k_skip)
             except fiapMongo.fiapMongoException as et:
                 self.emsg = "%s (%s)" % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
                 self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
                 return self.doc
             #
             # do fetch data.
             #
             j_value = []
             for i in cursor:
                 if isinstance(i["time"], datetime) == False:
                     print "ERROR: invalid time data in the database. %s (type=%s). ignored." % (
                         i["time"],
                         str(type(i["time"])),
                     )
                     continue
                 fixed_dt = datetime_naive_to_aware(i["time"], self.tzname)
                 fixed_dt = fixed_dt.astimezone(dateutil.tz.gettz(self.tzname))
                 j_value.append({"time": fixed_dt.strftime(_FIAPY_DATETIME_FORMAT), "value": i["value"]})
                 k["result"] += 1
                 total += 1
             if len(j_value) == 0:
                 j_value.append({"time": "1970-01-01T00:00:00Z", "value": 0})
             j_plist.append({k["pid"]: j_value})
             if k["next"] != 0:
                 k["query"]["cursor"] = k["next"]
         if self.debug > 0:
             for k in keys:
                 print "DEBUG: search key =", k
     except fiapMongo.fiapMongoException as et:
         self.emsg = "An error occured when it accesses to the DB. (%s)" % et
         self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
         return self.doc
     #
     if total == 0:
         self.emsg = "There is no matched point for the query."
         self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_NOT_FOUND, self.emsg)
         return self.doc
     self._setJSONResponse(_FIAP_METHOD_QUERYRS, 200, "OK", {"query": j_rquery, "point": j_plist})
     return self.doc
Ejemplo n.º 2
0
 def _serverParseJSON_DataRQ(self, j_root, j_pchunk):
     if self._fixTimeInPchunk(j_pchunk) != True:
         return self.doc
     #
     # prepare an interface for MongoDB
     #
     total = 0
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         try:
             total = m.insertPointChunk(j_pchunk)
         except fiapMongo.fiapMongoException as et:
             self.emsg = "%s (%s)" % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
             self._setJSONResponse(_FIAP_METHOD_DATARS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
             return self.doc
     except fiapMongo.fiapMongoException as et:
         self.emsg = "%s (%s)" % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
         self._setJSONResponse(_FIAP_METHOD_DATARS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
         return self.doc
     #
     # response to the data request in JSON
     #
     if total == 0:
         self.emsg = "There is no point saved."
         self._setJSONResponse(_FIAP_METHOD_DATARS, _HTTP_CODE_NOT_FOUND, self.emsg)
         return self.doc
     self.emsg = ""
     self._setJSONResponse(_FIAP_METHOD_DATARS, 200, "OK")
     return self.doc
Ejemplo n.º 3
0
 def _serverParseJSON_DataRQ(self, j_root, j_pchunk):
     if self._fixTimeInPchunk(j_pchunk) != True:
         return self.doc
     #
     # prepare an interface for MongoDB 
     #
     total = 0
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         try:
             total = m.insertPointChunk(j_pchunk)
         except fiapMongo.fiapMongoException as et:
             self.emsg = '%s (%s)' % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
             self._setJSONResponse(_FIAP_METHOD_DATARS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
             return self.doc
     except fiapMongo.fiapMongoException as et:
         self.emsg = '%s (%s)' % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
         self._setJSONResponse(_FIAP_METHOD_DATARS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
         return self.doc
     #
     # response to the data request in JSON
     #
     if total == 0:
         self.emsg = 'There is no point saved.'
         self._setJSONResponse(_FIAP_METHOD_DATARS, _HTTP_CODE_NOT_FOUND, self.emsg)
         return self.doc
     self.emsg = ''
     self._setJSONResponse(_FIAP_METHOD_DATARS, 200, 'OK')
     return self.doc
Ejemplo n.º 4
0
 def _serverParseJSON_FETCH(self, j_body):
     k_limit = self._getQueryAcceptableSize(j_body)
     k_skip = self._getQueryCursor(j_body.get('cursor'))
     #
     # copy the query for the response.
     #
     keys = self._getKey_JSONList(j_body.get('key'))
     if keys == None:
         self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_BAD_REQUEST, self.emsg)
         return self.doc
     j_plist = []
     j_rquery = j_body
     total = 0
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         #
         # get points.
         #
         for k in keys:
             try:
                 cursor = m.iterPoint(k, k_limit, k_skip)
             except fiapMongo.fiapMongoException as et:
                 self.emsg = '%s (%s)' % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
                 self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
                 return self.doc
             #
             # do fetch data.
             #
             j_value = []
             for i in cursor:
                 if isinstance(i['time'], datetime) == False:
                     print 'ERROR: invalid time data in the database. %s (type=%s). ignored.' % (i['time'], str(type(i['time'])))
                     continue
                 fixed_dt = datetime_naive_to_aware(i['time'], self.jc.tzname)
                 fixed_dt = fixed_dt.astimezone(dateutil.tz.gettz(self.jc.tzname))
                 j_value.append({
                     'time' : fixed_dt.strftime(_FIAPY_DATETIME_FORMAT),
                     'value' : i['value'] })
                 k['result'] += 1
                 total += 1
             if len(j_value) == 0:
                 j_value.append({ 'time' : '1970-01-01T00:00:00Z', 'value' : 0 })
             j_plist.append({ k['pid'] : j_value })
             if k['next'] != 0:
                 k['query']['cursor'] = k['next']
         if self.debug > 0:
             for k in keys:
                 print 'DEBUG: search key =', k
     except fiapMongo.fiapMongoException as et:
         self.emsg = 'An error occured when it accesses to the DB. (%s)' % et
         self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_INTERNAL_ERROR, self.emsg)
         return self.doc
     #
     if total == 0:
         self.emsg = 'There is no matched point for the query.'
         self._setJSONResponse(_FIAP_METHOD_QUERYRS, _HTTP_CODE_NOT_FOUND, self.emsg)
         return self.doc
     self._setJSONResponse(_FIAP_METHOD_QUERYRS, 200, 'OK', { 'query' : j_rquery, 'point' : j_plist })
     return self.doc
Ejemplo n.º 5
0
 def _serverParseXML_FETCH(self, e_root, e_query):
     k_limit = self._getQueryAcceptableSize(e_query)
     k_skip = self._getQueryCursor(e_query.get('cursor'))
     iter_keys = e_query.iterfind('./fiap:key', namespaces=_NSMAP)
     keys = self._getKey_XMLList(iter_keys)
     if keys == None:
         return None
     #
     # create a header for the response message.
     #
     e_newroot, e_header, e_body = self.getNewXMLdoc(_FIAP_METHOD_QUERYRS)
     e_rquery = copy.deepcopy(e_query)
     for e in e_rquery.findall('./*'):
         e_rquery.remove(e)
     e_header.append(e_rquery)
     #
     # fetch
     #
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         #
         # get points.
         #
         total = 0
         for k in keys:
             try:
                 cursor = m.iterPoint(k, k_limit, k_skip)
             except fiapMongo.fiapMongoException as et:
                 self.emsg = '%s (%s)' % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
                 self._getErrorObject(e_header, 'error', self.emsg)
             else:
                 for i in cursor:
                     if isinstance(i['time'], datetime) == False:
                         print 'ERROR: invalid time data in the database. %s (type=%s). ignored.' % (i['time'], str(type(i['time'])))
                         continue
                     fixed_dt = datetime_naive_to_aware(i['time'], self.jc.tzname)
                     e_point = ElementTree.SubElement(e_body, '{%s}point' % NS_FIAP, {'id': k['pid']})
                     e_value = ElementTree.SubElement(e_point, '{%s}value' % NS_FIAP, {'time' : fixed_dt.strftime('%Y-%m-%dT%H:%M:%S%z') } )
                     e_value.text = i['value']
                     k['result'] += 1
                     if k['next'] != 0:
                         k['query'].set('cursor', str(k['next']))
                     e_rquery.append(k['query'])
                 total += k['result']
         if self.debug > 0:
             for k in keys:
                 print 'DEBUG: search key =', k
     except fiapMongo.fiapMongoException as et:
         self.emsg = 'An error occured when it accesses to the DB. (%s)' % et
         self._getErrorObject(e_header, 'error', self.emsg)
     #
     if total == 0:
         self.emsg = 'There is no matched point for the query.'
         self._getErrorObject(e_header, 'error', self.emsg)
     else:
         ElementTree.SubElement(e_header, '{%s}OK' % NS_FIAP)
     self.doc = self.getXMLdoc(e_newroot)
     return self.doc
Ejemplo n.º 6
0
 def _serverParseXML_TRAP(self, e_root, e_query):
     f_remove = False
     req = {}
     req["limit"] = self._getQueryAcceptableSize(e_query)
     ttl = self._getQueryTTL(e_query.get("ttl"))
     if ttl == 0:
         req["tte"] = 0
     else:
         dt = datetime.now(dateutil.tz.gettz(self.tzname)) + timedelta(seconds=ttl)
         req["tte"] = dt.astimezone(dateutil.tz.tzutc())
     iter_keys = e_query.iterfind("./fiap:key", namespaces=_NSMAP)
     req["qk"] = self._getKey_XMLList(iter_keys)
     if req["qk"] == None:
         return None
     #
     # register it to trapy.py
     #
     req["cd"] = e_query.get("callbackData")
     if req["cd"] == None:
         self.emsg = "callbackData is not specified."
         return None
     req["cc"] = e_query.get("callbackControl")
     if req["cc"] == None:
         self.emsg = "callbackControl is not specified."
         return None
     e_query = e_root.find(".//{%s}query" % NS_FIAP)
     req["h"] = ElementTree.tostring(e_query.getroot(), encoding="utf-8")
     req["rip"] = self.requester_address
     if req["rip"] == None:
         self.emsg = "internall error happens.  requester's addrss must be specified"
         return None
     req["rsan"] = self.requester_san
     #
     # register this trap and create a response message.
     #
     e_newroot, e_header, e_body = self.getNewXMLdoc(_FIAP_METHOD_QUERYRS, e_root)
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         try:
             m.saveTrap(req)
         except:
             self.emsg = "%s (%s)" % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
             self._getErrorObject(e_header, "error", self.emsg)
     except fiapMongo.fiapMongoException as et:
         self.emsg = "An error occured when it accesses to the DB. (%s)" % et
         self._getErrorObject(e_header, "error", self.emsg)
     else:
         ElementTree.SubElement(e_header, "{%s}OK" % NS_FIAP)
     self.doc = self.getXMLdoc(e_newroot)
     return self.doc
Ejemplo n.º 7
0
 def _serverParseXML_TRAP(self, e_root, e_query):
     f_remove = False
     req = {}
     req['limit'] = self._getQueryAcceptableSize(e_query)
     ttl = self._getQueryTTL(e_query.get('ttl'))
     if ttl == 0:
         req['tte'] = 0
     else:
         dt = datetime.now(dateutil.tz.gettz(self.jc.tzname)) + timedelta(seconds=ttl)
         req['tte'] = dt.astimezone(dateutil.tz.tzutc())
     iter_keys = e_query.iterfind('./fiap:key', namespaces=_NSMAP)
     req['qk'] = self._getKey_XMLList(iter_keys)
     if req['qk'] == None:
         return None
     #
     # register it to trapy.py
     #
     req['cd'] = e_query.get('callbackData')
     if req['cd'] == None:
         self.emsg = 'callbackData is not specified.'
         return None
     req['cc'] = e_query.get('callbackControl')
     if req['cc'] == None:
         self.emsg = 'callbackControl is not specified.'
         return None
     e_query = e_root.find('.//{%s}query' % NS_FIAP)
     req['h'] = ElementTree.tostring(e_query.getroot(), encoding='utf-8')
     req['rip'] = self.requester_address
     if req['rip'] == None:
         self.emsg = "internall error happens.  requester's addrss must be specified"
         return None
     req['rsan'] = self.requester_san
     #
     # register this trap and create a response message.
     #
     e_newroot, e_header, e_body = self.getNewXMLdoc(_FIAP_METHOD_QUERYRS, e_root)
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         try:
             m.saveTrap(req)
         except:
             self.emsg = '%s (%s)' % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
             self._getErrorObject(e_header, 'error', self.emsg)
     except fiapMongo.fiapMongoException as et:
         self.emsg = 'An error occured when it accesses to the DB. (%s)' % et
         self._getErrorObject(e_header, 'error', self.emsg)
     else:
         ElementTree.SubElement(e_header, '{%s}OK' % NS_FIAP)
     self.doc = self.getXMLdoc(e_newroot)
     return self.doc
Ejemplo n.º 8
0
 def _serverParseXML_DataRQ(self, e_root, e_header, e_body):
     #
     # response to data request in XML
     #
     e_root, e_header, e_body = self.getNewXMLdoc(_FIAP_METHOD_DATARS)
     #
     # prepare an interface for MongoDB
     #
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         #
         # parse the point objects and save data to the DB
         #
         pset_all = []
         for e in e_root.findall(".//fiap:point", namespaces=_NSMAP):
             pset = self._getPointList(e)
             if len(pset) == 0:
                 self.msg = "There is no point data in a Point Object. Just skip it. (%s)" % e
                 # XXX skip it to process all data as much as possible.
                 continue
             try:
                 m.insertPointList(pset)
             except fiapMongo.fiapMongoException as et:
                 self.emsg = "%s (%s)" % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
                 self._getErrorObject(e_header, "error", self.emsg)
                 break
             pset_all.extend(pset)
     except fiapMongo.fiapMongoException as et:
         self.emsg = "An error occured when it accesses to the DB. (%s)" % et
         self._getErrorObject(e_header, "error", self.emsg)
     #
     if len(pset_all) == 0:
         self.emsg = "There is no Point in the object."
         self._getErrorObject(e_header, "error", self.emsg)
     else:
         self.emsg = ""
         ElementTree.SubElement(e_header, "{%s}OK" % NS_FIAP)
     self.doc = self.getXMLdoc(e_root)
     return self.doc
Ejemplo n.º 9
0
 def _serverParseXML_DataRQ(self, e_root, e_header, e_body):
     #
     # prepare an interface for MongoDB 
     #
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         #
         # parse the point objects and save data to the DB
         #
         pset_all = []
         for e in e_root.findall('.//fiap:point', namespaces=_NSMAP):
             pset = self._getPointList(e)
             if len(pset) == 0:
                 self.msg = 'There is no point data in a Point Object. Just skip it. (%s)' % e
                 # XXX skip it to process all data as much as possible.
                 continue
             try:
                 m.insertPointList(pset)
             except fiapMongo.fiapMongoException as et:
                 self.emsg = '%s (%s)' % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
                 self._getErrorObject(e_header, 'error', self.emsg)
                 break
             pset_all.extend(pset)
     except fiapMongo.fiapMongoException as et:
         self.emsg = 'An error occured when it accesses to the DB. (%s)' % et
         self._getErrorObject(e_header, 'error', self.emsg)
     #
     # response to data request in XML
     #
     e_root, e_header, e_body = self.getNewXMLdoc(_FIAP_METHOD_DATARS)
     #
     if len(pset_all) == 0:
         self.emsg = 'There is no Point in the object.'
         self._getErrorObject(e_header, 'error', self.emsg)
     else:
         self.emsg = ''
         ElementTree.SubElement(e_header, '{%s}OK' % NS_FIAP)
     self.doc = self.getXMLdoc(e_root)
     return self.doc
Ejemplo n.º 10
0
 def _serverParseXML_FETCH(self, e_root, e_query):
     k_limit = self._getQueryAcceptableSize(e_query)
     k_skip = self._getQueryCursor(e_query.get("cursor"))
     iter_keys = e_query.iterfind("./fiap:key", namespaces=_NSMAP)
     keys = self._getKey_XMLList(iter_keys)
     if keys == None:
         return None
     #
     # create a header for the response message.
     #
     e_newroot, e_header, e_body = self.getNewXMLdoc(_FIAP_METHOD_QUERYRS)
     e_rquery = copy.deepcopy(e_query)
     for e in e_rquery.findall("./*"):
         e_rquery.remove(e)
     e_header.append(e_rquery)
     #
     # fetch
     #
     try:
         m = fiapMongo.fiapMongo(**_FIAPY_MONGODB)
         #
         # get points.
         #
         total = 0
         for k in keys:
             try:
                 cursor = m.iterPoint(k, k_limit, k_skip)
             except fiapMongo.fiapMongoException as et:
                 self.emsg = "%s (%s)" % (_FIAPY_EMSG_INTERNAL_ACCESS_DATA, et)
                 self._getErrorObject(e_header, "error", self.emsg)
             else:
                 for i in cursor:
                     if isinstance(i["time"], datetime) == False:
                         print "ERROR: invalid time data in the database. %s (type=%s). ignored." % (
                             i["time"],
                             str(type(i["time"])),
                         )
                         continue
                     fixed_dt = datetime_naive_to_aware(i["time"], self.tzname)
                     e_point = ElementTree.SubElement(e_body, "{%s}point" % NS_FIAP, {"id": k["pid"]})
                     e_value = ElementTree.SubElement(
                         e_point, "{%s}value" % NS_FIAP, {"time": fixed_dt.strftime("%Y-%m-%dT%H:%M:%S%z")}
                     )
                     e_value.text = i["value"]
                     k["result"] += 1
                     if k["next"] != 0:
                         k["query"].set("cursor", str(k["next"]))
                     e_rquery.append(k["query"])
                 total += k["result"]
         if self.debug > 0:
             for k in keys:
                 print "DEBUG: search key =", k
     except fiapMongo.fiapMongoException as et:
         self.emsg = "An error occured when it accesses to the DB. (%s)" % et
         self._getErrorObject(e_header, "error", self.emsg)
     #
     if total == 0:
         self.emsg = "There is no matched point for the query."
         self._getErrorObject(e_header, "error", self.emsg)
     else:
         ElementTree.SubElement(e_header, "{%s}OK" % NS_FIAP)
     self.doc = self.getXMLdoc(e_newroot)
     return self.doc
Ejemplo n.º 11
0
    print 'total =', key['total']
    print 'rest  =', key['rest']
    print 'next  =', key['next']
    for i in cursor:
        print '%s %s' % (i['time'], i['value'])
    cursor.close()
    print

#
# main
#

#
# limit, skip
#
m = fiapMongo.fiapMongo(port=27036)

key = {}
key['pid'] = 'http://example.org/fiapy/test/p01'
key['next'] = 0
key['rest'] = 0
k_limit = 2
while True:
    select_print(key, k_limit, key['next'])
    if key['rest'] == 0:
        break

#
# max and min
#
keys = [