예제 #1
0
def get_sensor_data_csv():
    try:
        id = request.args.get('id')
        chan = request.args.get('chan')
        limit = request.args.get('limit')
        file = request.args.get('file')
        data = db.get_sensor_data(id, chan, limit)
        # create a dynamic csv or file here using `StringIO`
        # (instead of writing to the file system)

        Utils.log(data)
        strIO = io.BytesIO()

        strdata = db.extract_csv_multichan(data)
        strIO.write(strdata.encode("utf-8"))

        # strIO.write(data)
        strIO.seek(0)

        if file:
            return send_file(strIO,
                             mimetype='text/csv',
                             attachment_filename='downloadFile.csv',
                             as_attachment=True)
        else:
            # assume bytes_io is a `BytesIO` object
            byte_str = strIO.read()

            # Convert to a "unicode" object
            # Or use the encoding you expect
            text_obj = byte_str.decode('UTF-8')

            # return json.dumps({
            #     "status": True,
            #     "data": text_obj
            # })
            return text_obj
    except:
        logg.log(Utils.format_exception(""))
        return json.dumps({
            "status": False
        })
예제 #2
0
    def create_message_model(self, data):
        msg = MQTTMessage()
        msg.data = data
        msg.ts = datetime.now()
        return msg

    def run(self):
        t0 = time.time()
        while True:
            time.sleep(Constants.LOOP_DELAY)
            t1 = time.time()
            try:
                if (t1 - t0) >= self.default_log_rate or self.logstart:
                    self.logstart = False
                    t0 = t1
                    self.logg.log("Requesting ext api")
                    self.request_data()
                    # self.log_sensor_data()

            except:
                Utils.print_exception(self.__class__.__name__)


if __name__ == '__main__':
    Constants.load()
    Utils.log("config loaded")
    db = Database.instance()
    test = ExtApi.instance()
    print("requesting data")
    test.connect()
    test.request_data()
예제 #3
0
        # # as_attachment = True
        # return send_file(strIO,
        #                  mimetype='image/jpg',
        #                  attachment_filename='logo.png',
        #                  )
    except:
        logg.log(Utils.format_exception(""))
        return json.dumps({
            "status": False
        })


if __name__ == '__main__':

    Constants.load()
    Utils.log("config loaded")

    logg = Logg.instance()
    logg.start()

    mqtt_manager = MQTTManager()

    mqtt_manager.create_client()
    mqtt_manager.start()

    if Constants.conf["ENV"]["ENABLE_DB"]:
        Utils.log("enable db")
        db = Database.instance()
        # db.connect()
        mqtt_manager.load_sensors()
        db.run_process()