コード例 #1
0
ファイル: o2s.py プロジェクト: padelt/pista
def on_start(mosq, userdata, msg):
    if (skip_retained and msg.retain == 1) or len(msg.payload) == 0:
        return

    log.debug("_start: {0} {1}".format(msg.topic, msg.payload))

    save_rawdata(msg.topic, msg.payload)
    watcher(mosq, msg.topic, msg.payload)

    imei = version = ""
    try:
        imei, version, tstamp = msg.payload.split(' ')
    except:
        log.error("Cannot split() on ../start")
        return

    startup_dt = None
    try:
        startup_dt = dateutil.parser.parse(tstamp)
    except:
        startup_dt = datetime.datetime.now()

    # Inventory must have base topic in it so that we can later associate TID
    # with the IMEI

    basetopic, suffix = tsplit(msg.topic)

    odo = 0
    try:
        inv = Inventory.get(Inventory.imei == imei)
        odo = int(inv.odo)

        try:
            inv.topic = basetopic
            inv.version = version
            inv.startup = startup_dt
            try:
                if basetopic in devices:
                    inv.tid = devices[basetopic]['tid']
            except:
                pass
            inv.tstamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
            inv.save()
        except Exception, e:
            raise
            log.error("DB error on UPDATE Inventory: {0}".format(str(e)))
    except Inventory.DoesNotExist:
        try:
            inv = Inventory(topic=basetopic, imei=imei, version=version, startup=startup_dt)
            if basetopic in devices:
                inv.tid = devices[basetopic]['tid']
            inv.save()
        except Exception, e:
            log.error("DB error on SAVE Inventory: {0}".format(str(e)))
コード例 #2
0
def on_voltage(mosq, userdata, msg):
    if (skip_retained and msg.retain == 1) or len(msg.payload) == 0:
        return

    if not storage:
        return

    save_rawdata(msg.topic, msg.payload)
    watcher(mosq, msg.topic, msg.payload)

    basetopic, suffix = tsplit(msg.topic)
    key = basetopic.split('/')[-1]  # IMEI or TID

    vext = vbatt = None
    if suffix.endswith('voltage/ext'):
        try:
            vext = float(msg.payload)
            if basetopic in devices:
                devices[basetopic].update(dict(vext=vext))
        except:
            return

    if suffix.endswith('voltage/batt'):
        try:
            vbatt = float(msg.payload)
            if basetopic in devices:
                devices[basetopic].update(dict(vbatt=vbatt))
        except:
            return

    try:
        inv = Inventory.select().where((Inventory.imei == key)
                                       | (Inventory.tid == key)).get()
        try:
            if vext is not None:
                inv.vext = vext
            if vbatt is not None:
                inv.vbatt = vbatt
            inv.tstamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
            inv.save()
        except Exception, e:
            raise
            log.error("DB error on UPDATE Inventory in voltage: {0}".format(
                str(e)))
    except Inventory.DoesNotExist:
        pass
    except Exception, e:
        raise
        log.error("DB error on GET Inventory: {0}".format(str(e)))
        return
コード例 #3
0
ファイル: o2s.py プロジェクト: padelt/pista
def on_voltage(mosq, userdata, msg):
    if (skip_retained and msg.retain == 1) or len(msg.payload) == 0:
        return

    if not storage:
        return

    save_rawdata(msg.topic, msg.payload)
    watcher(mosq, msg.topic, msg.payload)

    basetopic, suffix = tsplit(msg.topic)
    key = basetopic.split('/')[-1]      # IMEI or TID

    vext = vbatt = None
    if suffix.endswith('voltage/ext'):
        try:
            vext = float(msg.payload)
            if basetopic in devices:
                devices[basetopic].update(dict(vext=vext))
        except:
            return

    if suffix.endswith('voltage/batt'):
        try:
            vbatt = float(msg.payload)
            if basetopic in devices:
                devices[basetopic].update(dict(vbatt=vbatt))
        except:
            return

    try:
        inv = Inventory.select().where(
                (Inventory.imei == key) | (Inventory.tid == key)
           ).get()
        try:
            if vext is not None:
                inv.vext = vext
            if vbatt is not None:
                inv.vbatt = vbatt
            inv.tstamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
            inv.save()
        except Exception, e:
            raise
            log.error("DB error on UPDATE Inventory in voltage: {0}".format(str(e)))
    except Inventory.DoesNotExist:
            pass
    except Exception, e:
        raise
        log.error("DB error on GET Inventory: {0}".format(str(e)))
        return
コード例 #4
0
def on_start(mosq, userdata, msg):
    if (skip_retained and msg.retain == 1) or len(msg.payload) == 0:
        return

    log.debug("_start: {0} {1}".format(msg.topic, msg.payload))

    save_rawdata(msg.topic, msg.payload)
    watcher(mosq, msg.topic, msg.payload)

    imei = version = ""
    try:
        imei, version, tstamp = msg.payload.split(' ')
    except:
        log.error("Cannot split() on ../start")
        return

    startup_dt = None
    try:
        startup_dt = dateutil.parser.parse(tstamp)
    except:
        startup_dt = datetime.datetime.now()

    # Inventory must have base topic in it so that we can later associate TID
    # with the IMEI

    basetopic, suffix = tsplit(msg.topic)

    odo = 0
    try:
        inv = Inventory.get(Inventory.imei == imei)
        odo = int(inv.odo)

        try:
            inv.topic = basetopic
            inv.version = version
            inv.startup = startup_dt
            try:
                if basetopic in devices:
                    inv.tid = devices[basetopic]['tid']
            except:
                pass
            inv.tstamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
            inv.save()
        except Exception, e:
            raise
            log.error("DB error on UPDATE Inventory: {0}".format(str(e)))
    except Inventory.DoesNotExist:
        try:
            inv = Inventory(topic=basetopic,
                            imei=imei,
                            version=version,
                            startup=startup_dt)
            if basetopic in devices:
                inv.tid = devices[basetopic]['tid']
            inv.save()
        except Exception, e:
            log.error("DB error on SAVE Inventory: {0}".format(str(e)))
コード例 #5
0
ファイル: o2s.py プロジェクト: padelt/pista
def t_imei(rest, val):
    ''' val is a TID. Return IMEI from Inventory '''

    tid = val
    resp = 'unknown TID=%s' % tid

    try:
        inv = Inventory.get(Inventory.tid == tid)
        resp = "TID={0} IMEI={1} topic={2}".format(inv.tid, inv.imei, inv.topic)
    except Inventory.DoesNotExist:
        log.warning("IMEI for TID={0} requested but not found".format(tid))
        pass
    except Exception, e:
        log.error("DB error on GET Inventory: {0}".format(str(e)))
        pass
コード例 #6
0
def t_imei(rest, val):
    ''' val is a TID. Return IMEI from Inventory '''

    tid = val
    resp = 'unknown TID=%s' % tid

    try:
        inv = Inventory.get(Inventory.tid == tid)
        resp = "TID={0} IMEI={1} topic={2}".format(inv.tid, inv.imei,
                                                   inv.topic)
    except Inventory.DoesNotExist:
        log.warning("IMEI for TID={0} requested but not found".format(tid))
        pass
    except Exception, e:
        log.error("DB error on GET Inventory: {0}".format(str(e)))
        pass
コード例 #7
0
def flotbatt(voltage):
    ''' Find all user's devices and add vbatt levels into list
        for HW page '''

    current_user = request.auth[0]
    usertids = getusertids(current_user)

    battlevels = []

    try:
        query = (Inventory.select(Inventory).where(
            (Inventory.tid << usertids)))
        for q in query.naive():
            try:
                battlevels.append([q.tid, float(q.vbatt)])
            except:
                pass
    except Exception, e:
        log.warn("User {0} failed Inventory query on flotbatt: {1}".format(
            current_user, str(e)))
        pass
コード例 #8
0
def page_hw():

    current_user = request.auth[0]
    usertids = getusertids(current_user)

    device_list = []

    # Find all devices in Inventory and extract their info into a list
    # of objects. Ensure values of hash keys are None if unset or
    # the template will bail out.

    try:
        query = (Inventory.select(Inventory).where(
            (Inventory.tid << usertids)))
        query = query.order_by(Inventory.tid.asc())
        for q in query.naive():
            try:
                device_list.append({
                    'tid': q.tid,
                    'imei': q.imei,
                    'version': q.version,
                    'tstamp': q.startup,
                    'topic': q.topic,
                })
            except:
                pass
    except:
        pass  # device_list will be empty => no data, which is OK here

    device_list.sort(key=lambda x: x['tid'], reverse=False)
    params = {
        'devices': device_list,
        'pistapages': cf.g('pista', 'pages'),
    }

    return template('hw', params)
コード例 #9
0
ファイル: pista.py プロジェクト: dhozac/pista
    if not superuser:
        query = (Acl.select(Acl). where(
                    (Acl.username == username)
                ))
        sublist = [ q.topic for q in query.naive() ]
    else:
        sublist.append('#')

    # Find distinct topic, tid combinations in Inventory table and
    # let Paho check if subscription matches

    topiclist = []

    query = (Inventory.select(Inventory.tid, Inventory.topic)
                    .distinct()
                    .order_by(Inventory.tid)
                    )
    for q in query:
        for sub in sublist:
            if paho.topic_matches_sub(sub, q.topic):
                topiclist.append( dict(tid=q.tid, topic=q.topic) )

    return topiclist


#-----------------

@app.hook('after_request')
def enable_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'
コード例 #10
0
        return []
    except Exception, e:
        raise

    if not superuser:
        query = (Acl.select(Acl).where((Acl.username == username)))
        sublist = [q.topic for q in query.naive()]
    else:
        sublist.append('#')

    # Find distinct topic, tid combinations in Inventory table and
    # let Paho check if subscription matches

    topiclist = []

    query = (Inventory.select(
        Inventory.tid, Inventory.topic).distinct().order_by(Inventory.tid))
    for q in query:
        for sub in sublist:
            if paho.topic_matches_sub(sub, q.topic):
                topiclist.append(dict(tid=q.tid, topic=q.topic))

    return topiclist


#-----------------


@app.hook('after_request')
def enable_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'