示例#1
0
def handler(msg):
    print "Received message"
    flights = JsonArray()
    for flight in msg.body:
        the_flight = JsonObject()
        #print str(flight)
        #the_flight.putString("name", flight.getString("callsign"))
        #the_flight.putString("planeType", flight.getString("equipment") )
        the_flight.putString("speed",
                             flight.get("properties").get("direction"))
        the_flight.putString("alt", flight.get("properties").get("route"))
        position_array = flight.get("geometry").get("coordinates")
        #print position_array

        #There can sometimes be two positions readings but I am not sure what they do so I am just going to take the first
        #position =  position_array[0]

        the_flight.putNumber("lat", position_array[1])
        the_flight.putNumber("lon", position_array[0])

        #build the object to persist to mongo
        forMongo = JsonObject()
        forMongo.putString("action", "save")
        forMongo.putString("collection", "buses")
        forMongo.putObject("document", the_flight)
        #persist it
        EventBus.publish('vertx.mongopersistor', forMongo)

        #add now to the array
        flights.addObject(the_flight)

    #Sent the array on the EventBus - this is the one the page should subscribe to
    EventBus.publish('flights.updated', flights)
    print("published the flights")
示例#2
0
def handler(msg):
    print "Received message"
    flights = JsonArray()
    for flight in msg.body:
        the_flight = JsonObject()
        #print str(flight)
        #the_flight.putString("name", flight.getString("callsign"))
        #the_flight.putString("planeType", flight.getString("equipment") )
        the_flight.putString("speed", flight.get("properties").get("direction"))
        the_flight.putString("alt",  flight.get("properties").get("route"))
        position_array =  flight.get("geometry").get("coordinates")
        #print position_array

        #There can sometimes be two positions readings but I am not sure what they do so I am just going to take the first
        #position =  position_array[0]

        the_flight.putNumber("lat", position_array[1])
        the_flight.putNumber("lon", position_array[0])
        
        #build the object to persist to mongo
        forMongo = JsonObject()
        forMongo.putString("action", "save")
        forMongo.putString("collection", "buses")
        forMongo.putObject("document", the_flight)
        #persist it
        EventBus.publish('vertx.mongopersistor', forMongo)

        #add now to the array
        flights.addObject(the_flight)

    #Sent the array on the EventBus - this is the one the page should subscribe to
    EventBus.publish('flights.updated', flights)
    print("published the flights")
def handler(msg):
    print 'Received message'
    flights = JsonArray()
    for flight in msg.body:
        the_flight = JsonObject()
        the_flight.putString("name", flight.getString("callsign"))
        the_flight.putString("planeType", flight.getString("equipment") )
        position_array =  flight.getArray("positions").toArray()

        #There can sometimes be two positions readings but I am not sure what they do so I am just going to take the first
        position =  position_array[0]

        the_flight.putNumber("lat", position.get("lat"))
        the_flight.putNumber("lon", position.get("lon"))
        the_flight.putNumber("speed", position.get("speedMph"))
        the_flight.putNumber("alt",  position.get("altitudeFt"))


        flights.addObject(the_flight)

    #Sent the array on the EventBus - this is the one the page should subscribe to
    EventBus.publish('flights.updated', flights)
    print("published the flights")
    def _sendResults(self, message, rows, pages, total_rows, session):
        count = 0


        #Set a timeout, if the user doesn't reply within 10 secs, close the cursor

        first_result = None
        collection = self.getMandatoryString("collection", message)

        results = JsonArray()
        for obj in rows.all():
            json = self.to_json(obj,collection)
            if first_result is None:
                first_result = json
            results.addObject(json)

            count += 1

        reply = JsonObject().putArray("rows", results).putNumber("pages", pages).putNumber("total_rows", total_rows)
        if not first_result is None:
            reply.putObject("result", first_result)

        session.close()
        self.send_ok(message, reply)
    def to_json(self, obj,collection):
        try:
            json = JsonObject()
            for name, type in vars(getattr(self, obj.__class__.__name__)).iteritems():
                if str(type.__class__) == "<class 'sqlalchemy.orm.attributes.InstrumentedAttribute'>":
                    column_value = getattr(obj, name)

                    if column_value:
                        column_name = name

                        class__ = column_value.__class__

                        table = getattr(self, collection)

                        column_type =str(table.__table__.columns[name].type)

                        print ("and type is... "+column_type)

                        if isinstance(class__, DeclarativeMeta):
                            json.putObject(column_name, self.to_json(getattr(obj, name),column_type))

                        elif column_type == "BOOLEAN":
                            json.putBoolean(column_name, getattr(obj, column_name))

                        elif column_type in ["<class 'sqlalchemy.orm.collections.InstrumentedList'>"]:
                            print("A LIST")
                            array = JsonArray()
                            for instance in column_value:
                                print(str(instance))
                                array.addObject(self.to_json(instance,column_type))
                            json.putArray(column_name, array)
                            print("A LIST COMPLETE")

                        elif column_type == "DATETIME":
                            attr = getattr(obj, column_name).strftime("%Y-%m-%d %H:%M:%S")
                            json.putString(column_name,attr)

                        elif column_type == "TIME":
                            attr = getattr(obj, column_name).strftime("%H:%M:%S")
                            json.putString(column_name,attr)

                        elif column_type == "DATE":
                            attr = getattr(obj, column_name).strftime("%Y-%m-%d")
                            json.putString(column_name,attr)

                        elif column_type in ["NUMERIC","FLOAT"]:
                            attr = getattr(obj, column_name)

                            column = table.__table__.columns[name]
                            if not hasattr(column,"info"):
                                precision = 2
                            elif not column.info.has_key("precision"):
                                precision = 2
                            else:
                                precision = column.info["precision"]

                            attr=BigDecimal(str(attr)).setScale(precision, 5)  #BigDecimal.ROUND_HALF_DOWN=5
                            json.putNumber(column_name, attr)

                        elif column_type in ["SMALLINT","BIGINT","INTEGER","NUMERIC"]:
                            attr = getattr(obj, column_name)
                            json.putNumber(column_name, attr)

                        else:
                            json.putString(column_name, getattr(obj, column_name))

            print ("JSON:" + str(json))
            return json
        except Exception:
            exc = str(sys.exc_info()[0])+"\n"+str(sys.exc_info()[1])+"\n"+traceback.format_exc(sys.exc_info()[2])
            print exc
            return None