示例#1
0
def get_eq_by_tag(db, tag_name):
    db = machbase()
    if db.open('127.0.0.1','SYS','MANAGER',5656) is 0 :
        return db.result()
    query = "select equipment_id from tag_equipment where tag_name = '"+tag_name + "'"
    if db.execute(query) is 0:
       return db.result()
    result = db.result()
    for item in re.findall('{[^}]+}',result):
        res = json.loads(item)
        result = res.get("equipment_id")
    return result
示例#2
0
def sample():
    sPort = int(os.environ['MACHBASE_PORT_NO'])
    sTablename = 'tag'
    sPath = PROGRAM_ROOT + 'sample_data.csv'

    sTags = [
        'TEMPERATURE', 'RAINFALL', 'WINDSPEED', 'PRESSURE_hPa', 'PRESSURE_SEA',
        'HUMIDITY'
    ]

    sDb = machbase()
    if sDb.open('127.0.0.1', 'SYS', 'MANAGER', sPort) is 0:
        return sDb.result()

    sDb.columns(sTablename)
    sColumns = sDb.result()

    if sDb.close() is 0:
        return sDb.result()


# append start
    sTypes = []
    for sItem in re.findall('{[^}]+}', sColumns):
        sTypes.append(json.loads(sItem).get('type'))

    sStart = time.time()

    sDb = machbase()
    if sDb.open('127.0.0.1', 'SYS', 'MANAGER', sPort) is 0:
        return sDb.result()

    if sDb.appendOpen(sTablename, sTypes) is 0:
        return sDb.result()
    print "append open"

    print "append data...",
    sys.stdout.flush()

    try:
        sCount = 0
        with open(sPath) as sInputFile:
            sLines = csv.reader(sInputFile)
            for sLine in sLines:
                #print sLine
                sValues = []
                for sIdx in range(len(sTags)):
                    sTemp = []
                    sTemp.append(sTags[sIdx])
                    sTemp.append(sLine[1] + ':00')
                    sTemp.append(
                        float(0 if sLine[sIdx + 2] == '' else sLine[sIdx + 2]))
                    sValues.append(sTemp)
                    sCount += 1
                if sDb.appendData(sTablename, sTypes, sValues) is 0:
                    return sDb.result()
    except:
        e = sys.exc_info()[0]
        print e
        return False

    if sDb.appendClose() is 0:
        return sDb.result()

    if sDb.close() is 0:
        return sDb.result()
    sEnd = time.time()
    print "append Close (" + str(sCount) + " records)"
    print 'elapsed time : ' + str(sEnd - sStart) + " sec\n"
    # append end

    return "successfully executed."
示例#3
0
       return db.result()
    result = db.result()
    for item in re.findall('{[^}]+}',result):
         print item
    return result

def query_eq_lot(db, eq, lot_id):
    querytagname = "select tag_name from tag_equipment where equipment_id = '"+eq+"'"
    tfrom, to, lot_no = get_time_lotno_by_eq_lotid(db, eq, lot_id)
    if db.execute(querytagname) is 0:
       return db.result()
    resloop = db.result()
    for item in re.findall('{[^}]+}',resloop):
        res = json.loads(item)
        tagname = res.get("tag_name")
        result = get_tagdata_by_tagtime_lotno(db, tagname, tfrom, to, lot_no)
    return result


if __name__=="__main__":
    db = machbase()
    if db.open('127.0.0.1','SYS','MANAGER',5656) is 0 :
        print("eror connection")
    print get_eq_by_tag(db, "EQ0^TAG1")
    print get_time_lotno_by_eq_lotid(db, get_eq_by_tag(db, "EQ0^TAG1"), "LOT101")
    tfrom, to, lot_no =  get_time_lotno_by_eq_lotid(db, get_eq_by_tag(db, "EQ0^TAG1"), "LOT101")
    print get_tagdata_by_tagtime_lotno(db, "EQ0^TAG1", tfrom, to, lot_no)
    print(query_eq_lot(db, "EQ0", "LOT202"))
    if db.close() is 0 :
       print("disconnect error")