Coder Social home page Coder Social logo

Comments (2)

ttu avatar ttu commented on May 18, 2024

Hi. Can you send me your server file (ruuvi.py).

File "ruuvi.py", line 50, in update_data
allData[key][0] = value
TypeError: tuple indices must be integers or slices, not str

It seems that it uses tuple instead of dictionary for some reason. Couldn't find same line from the default example implementation (https://github.com/ttu/ruuvitag-sensor/blob/master/examples/http_server.py).

from ruuvitag-sensor.

richardklingler avatar richardklingler commented on May 18, 2024

Huomenta (o;

Well it is actually this file just with modified MAC addresses:

"""
Simple http server, that returns data in json.
Executes get data for sensors in the background.
Endpoints:
    http://0.0.0.0:5000/data
    http://0.0.0.0:5000/data/<mac>
Requires:
    Flask - pip install flask
"""

from datetime import datetime
import json
from multiprocessing import Manager
from concurrent.futures import ProcessPoolExecutor
from flask import Flask, abort
from ruuvitag_sensor.ruuvi import RuuviTagSensor

app = Flask(__name__)

m = Manager()
q = m.Queue()

allData = {}

tags = {
    'EF:DE:E1:49:3C:9B': 'office',
    'E9:39:41:56:D6:2D': 'livingroom'
}


def run_get_data_background(macs, queue):
    """
    Background process from RuuviTag Sensors
    """
    def callback(data):
        data[1]['time'] = str(datetime.now())
        queue.put(data)

    RuuviTagSensor.get_datas(callback, macs)


def update_data():
    """
    Update data sent by background process to global allData
    """
    global allData
    while not q.empty():
        allData = q.get()
    for key, value in tags.items():
        if key in allData:
            allData[key]['name'] = value


@app.route('/data')
def get_all_data():
    update_data()
    return json.dumps(allData)


@app.route('/data/<mac>')
def get_data(mac):
    update_data()
    if mac not in allData:
        abort(404)
    return json.dumps(allData[mac])


if __name__ == '__main__':
    # Start background process
    executor = ProcessPoolExecutor(1)
    executor.submit(run_get_data_background, list(tags.keys()), q)

    # Strt Flask application
    app.run(host='0.0.0.0', port=5000)

Just redownloaded it and changed the MAC addresses....still the same error:

from ruuvitag-sensor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.