Ejemplo n.º 1
0
Archivo: main.py Proyecto: Fosvang/Lora
def admin():
    cursor = g.dbhandle.cursor()
    if request.method == "GET":
        devices  = db.get_all_devices(cursor)
        profiles = db.get_all_profiles(cursor)
        return render_template('admin.html', devices=devices, profiles=profiles)
    elif request.method == "POST":
        deveui = request.form.get('deveui')
        profile = request.form.get('profile')
        if db.add_device(cursor, deveui, profile):
            g.dbhandle.commit()
        else:
            flash('This Device already exists!')
        return redirect(url_for('admin'))
Ejemplo n.º 2
0
def generate_rack_table():
    racks = db.get_all_racks()
    racks.sort(key=lambda rack: rack.order)
    names_to_position = {rack.name: i for i, rack in enumerate(racks)}
    max_height = max(rack.height for rack in racks)
    devices_in_rack = [[] for rack in racks]
    for device in db.get_all_devices():
        devices_in_rack[names_to_position[device.rack]].append(device)
    racks_out = []
    columns = []
    for rack, devices in zip(racks, devices_in_rack):
        column = []
        next_slot = 1
        devices.sort(key=lambda device: device.rack_first_slot)
        for device in devices:
            if device.rack_first_slot < 1 or device.rack_last_slot < device.rack_first_slot or device.rack_last_slot > rack.height:
                raise Exception("device range error")
            if next_slot > device.rack_first_slot:
                backwards = next_slot - 1
                cidx = len(column) - 1
                while backwards >= device.rack_first_slot:
                    # TODO: handle this better (edge cases exist involving partial overlaps)
                    column[cidx].merge(device.name, device.id)
                    cidx -= 1
                    backwards -= column[cidx].span
            while next_slot < device.rack_first_slot:
                column.append(Cell("-- empty --", rack.name, next_slot))
                next_slot += 1
            slots = device.rack_last_slot - next_slot + 1
            if slots > 0:
                column.append(
                    Cell(device.name, rack.name, next_slot, device.id, slots))
                next_slot += slots
        while next_slot <= rack.height:
            column.append(Cell("-- empty --", rack.name, next_slot))
            next_slot += 1
        while next_slot <= max_height:
            column.append(None)
            next_slot += 1
        columns.append(column[::-1])
        racks_out.append(rack)
    return racks_out, spannify(columns, max_height)
Ejemplo n.º 3
0
    def check_phones(self):
        if self.stopped:
            # stops recursively generating threads.
            return

        while not self.stopped:
            print("[CHECKING PHONES]")

            # ==== Check devices' num failed counts ====
            for device in db.get_all_devices():

                # ==== If the device has failed too many times, decommission it ==== #
                if device.num_failed_acks + device.num_failed_jobs > MAX_FAILS and not device.decommissioned:
                    device.stop_charging()
                    device.decommission()
                    print(f"[DEVICE {device.id}] DECOMMISSIONED.")

            # Start another thread in a min
            # threading.Timer(db.HEARTBEAT_ACTIVE_RANGE_MINUTES * 60, self.check_phones).start()
            time.sleep(db.HEARTBEAT_ACTIVE_RANGE_MINUTES * 60)
async def device_notifier():
    generator = db.get_all_devices()
    print("woop")
    for device in generator:
        try:
            async with aiohttp.ClientSession() as session:
                async with session.get(device.url + "/info") as r:
                    try:
                        text = await r.text()
                        plant = db.get_plants_of_device(device.id)["data"]
                        print("plant")
                        print(plant[0])

                        print("na dole to tekst")
                        print(text)
                        json_text = json.loads(text)
                        counter = 0
                        for i in json_text:
                            for x in i:
                                print(i)
                                db.add_measure(device_id=device.id,
                                               plant_id=plant[counter]["id"],
                                               value_of_measure=i[x],
                                               sensor_name=x)
                            counter = counter + 1

                        print(json_text)
                    except JSONDecodeError:
                        print(r)
                    except IndexError:
                        print("nope")
        except Exception:
            print("somethin wrong with")
            print(device.url)

    await asyncio.sleep(2)
Ejemplo n.º 5
0
def devices_list():
    return jsonify(
        success=True,
        devices=[device.to_json() for device in db.get_all_devices()])