Esempio n. 1
0
    def GET(self, did=None):
        '''@params:
            did: (str)device id
            sampling: (datetime'''
        conn = pg.connect(dbname="bsolo3", user="******", password="******")
        cursor = conn.cursor()

        web.header('Content-Type', 'text/json')
        web.header('Access-Control-Allow-Origin', '*')
        if did:
            sql = "SELECT content FROM raw WHERE content->>'device' LIKE %s ORDER BY id DESC LIMIT 35"
            cursor.execute(sql, ('%/' + did + '/%', ))
            '''
            regx = re.compile('.*'+did+'/', re.IGNORECASE)
            rst = [r for r in db.sensors.find({"device": regx},
                                                {"_id": 0}).sort(
                                                    "_id", -1).limit(25)]
            '''
            rst = [r[0] for r in cursor.fetchall()]
            if not rst:
                return web.notfound()
            if web.input().get('sampling', None):
                #try:
                sampling = to_date(web.input().get('sampling'))
                _dari = time.mktime(sampling.timetuple())
                _hingga = _dari + 86400
                # satu hari = 86400 ms
                '''
                    rst = [r for r in db.sensors.find(
                        {"$and": [{"device": regx},
                                  {"sampling": {"$gte": _dari}},
                                  {"sampling": {"$lt": _hingga}}]}, {_id: 0})]
                '''
                sql = "SELECT content FROM raw WHERE content->>'device' LIKE %s AND (content->>'sampling')::int >= %s AND (content->>'sampling')::int <= %s"
                cursor.execute(sql, ('%/' + did + '/%', _dari, _hingga))
                rst = [r[0] for r in cursor.fetchall()]
                #except Exception as e:
                #    print e
            out = {}
            if web.input().get('raw'):
                out['periodic'] = rst
            else:
                out['periodic'] = [map_periodic(r) for r in rst]
            out["bsolo_logger"] = BSOLO_LOGGER.get(did)
        else:
            out = []
            sql = "SELECT DISTINCT(content->>'device') FROM raw"
            cursor.execute(sql)
            out = [r[0] for r in cursor.fetchall()]
        cursor.close()
        conn.close()

        return json.dumps(out)
Esempio n. 2
0
def map_periodic(src):
    try:
        loc = BSOLO_LOGGER.get(src.get('device').split('/')[1])
    except KeyError:
        loc = src.get('device').split('/')[1]
    ret = {"up_since": ts(src.get('up_since') or 0),
            "sampling": ts(src.get('sampling') or 0),
            "time_set_at": ts(src.get("time_set_at") or 0),
            "tick": src.get("tick"),
            "distance": src.get("distance"),
            "wl_scale": src.get("wl_scale"),
            "wlevel": src.get("wlevel", None),
            "temperature": src.get("temperature"),
            "humidity": src.get("humidity"),
            "signal_quality": src.get("signal_quality"),
            "battery": src.get("battery", None),
            "pressure": src.get("pressure", None),
            "altitude": src.get("altitude", None),
            "location": loc}
    return ret
Esempio n. 3
0
def map_periodic(src):
    try:
        loc = BSOLO_LOGGER.get(src.get('device').split('/')[1])
    except KeyError:
        loc = src.get('device').split('/')[1]
    data_keys = "tick;distance;wl_scale;wlevel;temperature_ambien_scale;humidity_ambien_scale;wind_speed_scale;wind_dir_scale;sun_radiation_scale".split(
        ';')
    ret = {
        "up_since": ts(src.get('up_since') or 0),
        "sampling": ts(src.get('sampling') or 0),
        "time_set_at": ts(src.get("time_set_at") or 0),
        "temperature": src.get("temperature"),
        "humidity": src.get("humidity"),
        "signal_quality": src.get("signal_quality"),
        "battery": src.get("battery", None),
        "pressure": src.get("pressure", None),
        "altitude": src.get("altitude", None),
        "location": loc
    }
    for i in data_keys:
        if i in src:
            ret.update({i: src.get(i)})
    return ret
Esempio n. 4
0
    def GET(self, did=None):
        '''@params:
            did: (str)device id
            sampling: (datetime'''
        conn = pg.connect(dbname="bsolo3", user="******", password="******")
        cursor = conn.cursor()

        if did:
            sql = "SELECT content FROM raw WHERE content->>'device' LIKE %s ORDER BY id DESC LIMIT 35"
            cursor.execute(sql, ('%/' + did + '/%', ))
            '''
            regx = re.compile('.*'+did+'/', re.IGNORECASE)
            rst = [r for r in db.sensors.find({"device": regx},
                                                {"_id": 0}).sort(
                                                    "_id", -1).limit(25)]
            '''
            rst = [r[0] for r in cursor.fetchall()]
            if not rst:
                return web.notfound()
            if web.input().get('sampling', None):
                #try:
                sampling = to_date(web.input().get('sampling'))
                _dari = time.mktime(sampling.timetuple())
                _hingga = _dari + 86400
                # satu hari = 86400 ms
                '''
                    rst = [r for r in db.sensors.find(
                        {"$and": [{"device": regx},
                                  {"sampling": {"$gte": _dari}},
                                  {"sampling": {"$lt": _hingga}}]}, {_id: 0})]
                '''
                sql = "SELECT content FROM raw WHERE content->>'device' LIKE %s AND (content->>'sampling')::int >= %s AND (content->>'sampling')::int <= %s"
                cursor.execute(sql, ('%/' + did + '/%', _dari, _hingga))
                rst = [r[0] for r in cursor.fetchall()]
                if not rst:
                    return "Tidak Ada Data Pada Tanggal " + web.input().get(
                        'sampling')
                rst.reverse()
                #except Exception as e:
                #    print e
            out = {}
            if web.input().get('raw'):
                out['periodic'] = rst
            else:
                out['periodic'] = [map_periodic(r) for r in rst]
            out["bsolo_logger"] = BSOLO_LOGGER.get(did)
        else:
            out = []
            sql = "SELECT DISTINCT(content->>'device') FROM raw"
            cursor.execute(sql)
            out = [r[0] for r in cursor.fetchall()]
        cursor.close()
        conn.close()

        #testing from asnan (data untuk kategori grafik) tinggal return untuk melihat
        data = []
        kategori = []
        battery = []
        signal_quality = []
        tick = []
        are_tick = False
        distance = []
        are_distance = False
        wl_scale = []
        are_wl_scale = False

        sun_radiation_scale = []
        wind_dir_scale = []
        temperature_ambien_scale = []
        humidity_ambien_scale = []
        wind_speed_scale = []
        are_klimatologi = False

        r = out["periodic"]
        r.reverse()

        for j in r:
            if "distance" in j:
                are_distance = True
                distance.append(j.get("distance"))
            if "wl_scale" in j:
                are_wl_scale = True
                wl_scale.append(j.get("wl_scale"))
            if "tick" in j:
                if "sun_radiation_scale" and "wind_dir_scale" and "temperature_ambien_scale" and "humidity_ambien_scale" and "wind_speed_scale" in j:
                    are_klimatologi = True
                    sun_radiation_scale.append(j.get("sun_radiation_scale"))
                    wind_dir_scale.append(j.get("wind_dir_scale"))
                    temperature_ambien_scale.append(
                        j.get("temperature_ambien_scale"))
                    humidity_ambien_scale.append(
                        j.get("humidity_ambien_scale"))
                    wind_speed_scale.append(j.get("wind_speed_scale"))
                    tick.append(j.get("tick"))
                else:
                    are_tick = True
                    tick.append(j.get("tick"))

            kategori.append(j.get("sampling"))
            battery.append(j.get("battery"))
            signal_quality.append(j.get("signal_quality"))
        #end
        data.append({'name': 'signal_quality', 'data': signal_quality})
        data.append({'name': 'battery', 'data': battery})
        if are_distance == True:
            data.append({'name': 'distance', 'data': distance})
            jenis_prima = "SONAR"
        if are_wl_scale == True:
            data.append({'name': 'wl_scale', 'data': wl_scale})
            jenis_prima = "PRESSURE"
        if are_tick == True:
            data.append({'name': 'tick', 'data': tick})
            jenis_prima = "ARR"
        if are_klimatologi == True:
            data.append({'name': 'tick', 'data': tick})
            data.append({
                'name': 'sun_radiation_scale',
                'data': sun_radiation_scale
            })
            data.append({'name': 'wind_dir_scale', 'data': wind_dir_scale})
            data.append({
                'name': 'temperature_ambien_scale',
                'data': temperature_ambien_scale
            })
            data.append({
                'name': 'humidity_ambien_scale',
                'data': humidity_ambien_scale
            })
            data.append({'name': 'wind_speed_scale', 'data': wind_speed_scale})
            jenis_prima = "KLIMATOLOGI"

        conn = Agent._connection
        sql = "SELECT cname from agent where prima_id = %s" % ('"' + did + '"')
        result = conn.queryAll(sql)
        if result:
            pname = result[0][0]
        else:
            pname = "--"
        #print result

        return render.sensor.sensor_graph({
            'data': str(data),
            'kategori': str(kategori),
            'did': did,
            'jenis_prima': jenis_prima,
            'pname': pname
        })