def RecordManualWeather(mw_text):
    # print gnd_meas

    dbio.createTable("manualweather", "manualweather")

    query = "INSERT IGNORE INTO manualweather (timestamp, meas_type, site_id, observer_name, weatherdesc) VALUES " + mw_text

    dbio.commitToDb(query, 'RecordManualWeather')
def ProcessStats(line, txtdatetime):

    print 'Site status: ' + line

    try:
        msgtable = "stats"
        items = re.match(
            r'(\w{4})[-](\d{1,2}[.]\d{02}),(\d{01}),(\d{1,2})/(\d{1,2}),#(\d),(\d),(\d{1,2}),(\d)[*](\d{10})',
            line)

        site = items.group(1)
        voltage = items.group(2)
        chan = items.group(3)
        att = items.group(4)
        retVal = items.group(5)
        msgs = items.group(6)
        sim = items.group(7)
        csq = items.group(8)
        sd = items.group(9)

        #getting date and time
        msgdatetime = line[-10:]
        print 'date & time: ' + msgdatetime

        col_list = cfg.get("Misc", "AdjustColumnTimeOf").split(',')
        if site in col_list:
            msgdatetime = txtdatetime
            print "date & time adjusted " + msgdatetime
        else:
            print 'date & time no change'

    except IndexError and AttributeError:
        print '\n>> Error: Status message format is not recognized'
        print line
        return
    except:
        print '\n>>Error: Status message format unknown ' + line
        return

    dbio.createTable(str(msgtable), "stats")

    try:
        query = """INSERT INTO %s (timestamp,site,voltage,chan,att,retVal,msgs,sim,csq,sd)
        VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')""" % (
            str(msgtable), str(msgdatetime), str(site), str(voltage),
            str(chan), str(att), str(retVal), str(msgs), str(sim), str(csq),
            str(sd))

    except:
        print '>> Error writing status data to database. ' + line
        return

    dbio.commitToDb(query, 'ProcessStats')

    print 'End of Process status data'
Пример #3
0
def writeAlertToDb(alertmsg):
    dbio.createTable('smsalerts', 'smsalerts')

    today = dt.today().strftime("%Y-%m-%d %H:%M:%S")

    query = "insert into smsalerts (ts_set,alertmsg,remarks) values ('%s','%s','none')" % (
        today, alertmsg)

    print query

    dbio.commitToDb(query, 'writeAlertToDb', 'LOCAL')
def ProcessRain(line, sender):

    #msg = message
    line = re.sub("[^A-Z0-9,\/:\.\-]", "", line)

    print 'Weather data: ' + line

    if len(line.split(',')) > 9:
        line = re.sub(",(?=$)", "", line)
    line = re.sub("(?<=,)(?=(,|$))", "NULL", line)
    line = re.sub("(?<=,)NULL(?=,)", "0.0", line)
    print line

    try:

        msgtable = line.split(",")[0]
        msgdatetime = re.search("\d{02}\/\d{02}\/\d{02},\d{02}:\d{02}:\d{02}",
                                line).group(0)

        txtdatetime = dt.strptime(msgdatetime, '%m/%d/%y,%H:%M:%S')

        txtdatetime = txtdatetime.strftime('%Y-%m-%d %H:%M:00')

        # data = items.group(3)
        data = line.split(",", 3)[3]

    except IndexError and AttributeError:
        print '\n>> Error: Rain message format is not recognized'
        print line
        return
    except KeyboardInterrupt:
        print '\n>>Error: Weather message format unknown ' + line
        return

    updateSimNumTable(msgtable, sender, txtdatetime[:10])

    dbio.createTable(str(msgtable), "weather")

    try:
        query = """INSERT INTO %s (timestamp,name,temp,wspd,wdir,rain,batt,csq) VALUES ('%s','%s',%s)""" % (
            msgtable.lower(), txtdatetime, msgtable, data)

    except:
        print '>> Error writing weather data to database. ' + line
        return

    try:
        dbio.commitToDb(query, 'ProcesRain')
    except MySQLdb.ProgrammingError:
        print query[:-2]
        dbio.commitToDb(query[:-2] + ')', 'ProcessRain')

    print 'End of Process weather data'
def ProcessPiezometer(line, sender):
    #msg = message
    print 'Piezometer data: ' + line
    try:
        #PUGBPZ*13173214*1511091800
        linesplit = line.split('*')
        msgname = linesplit[0].lower()
        msgname = re.sub("due", "", msgname)
        msgname = msgname + 'pz'
        print 'msg_name: ' + msgname
        data = linesplit[1]
        msgid = int(('0x' + data[:4]), 16)
        p1 = int(('0x' + data[4:6]), 16) * 100
        p2 = int(('0x' + data[6:8]), 16)
        p3 = int(('0x' + data[8:10]), 16) * .01
        piezodata = p1 + p2 + p3

        t1 = int(('0x' + data[10:12]), 16)
        t2 = int(('0x' + data[12:14]), 16) * .01
        tempdata = t1 + t2
        try:
            txtdatetime = dt.strptime(
                linesplit[2], '%y%m%d%H%M%S').strftime('%Y-%m-%d %H:%M:00')
        except ValueError:
            txtdatetime = dt.strptime(
                linesplit[2], '%y%m%d%H%M').strftime('%Y-%m-%d %H:%M:00')

    except IndexError and AttributeError:
        print '\n>> Error: Piezometer message format is not recognized'
        print line
        return
    except ValueError:
        print '>> Error: Possible conversion mismatch ' + line
        return

        # try:
    dbio.createTable(str(msgname), "piezo")
    try:
        query = """INSERT INTO %s(timestamp, name, msgid, freq, temp ) VALUES ('%s','%s', %s, %s, %s )""" % (
            msgname, txtdatetime, msgname, str(msgid), str(piezodata),
            str(tempdata))

        # print query
    except ValueError:
        print '>> Error writing query string.',
        return

    dbio.commitToDb(query, 'ProcessPiezometer')

    print 'End of Process Piezometer data'
def WriteSomsDataToDb(dlist, msgtime):
    query = """INSERT IGNORE INTO %s (timestamp,id,msgid,mval1,mval2) VALUES """ % str(
        dlist[0][0].lower())

    print "site_name", str(dlist[0][0])
    dbio.createTable(str(dlist[0][0]), "soms")
    for item in dlist:
        timetowrite = str(item[1])
        query = query + """('%s',%s,%s,%s,%s),""" % (timetowrite, str(
            item[2]), str(item[3]), str(item[4]), str(item[5]))

    query = query[:-1]
    query = query.replace("nan", "NULL")

    dbio.commitToDb(query, 'WriteSomsDataToDb')
def WriteTwoAccelDataToDb(dlist, msgtime):
    query = """INSERT IGNORE INTO %s (timestamp,id,msgid,xvalue,yvalue,zvalue,batt) VALUES """ % str(
        dlist[0][0].lower())

    dbio.createTable(dlist[0][0], "sensor v2")
    for item in dlist:
        timetowrite = str(item[1])
        query = query + """('%s',%s,%s,%s,%s,%s,%s),""" % (
            timetowrite, str(item[2]), str(item[3]), str(item[4]), str(
                item[5]), str(item[6]), str(item[7]))

    query = query[:-1]
    # print len(query)

    dbio.commitToDb(query, 'WriteTwoAccelDataToDb')
Пример #8
0
def checkAlertMessage():
    c = cfg.config()
    dbio.createTable("runtimelog", "runtime")
    server.logRuntimeStatus("alert", "checked")

    alertfile = cfg.config().fileio.allalertsfile
    f = open(alertfile, 'r')
    alertmsg = f.read()
    f.close()

    print alertmsg
    if not alertmsg:
        print '>> No alert msg read.'
        return

    # write alert message to db
    server.writeAlertToDb(alertmsg)
def ProcessCoordinatorMsg(coordsms, num):

    print ">> Coordinator message received"
    print coordsms

    dbio.createTable("coordrssi", "coordrssi")

    coordsms = re.sub("(?<=,)(?=(,|$))", "NULL", coordsms)

    try:
        datafield = coordsms.split('*')[1]
        timefield = coordsms.split('*')[2]
        timestamp = dt.strptime(timefield,
                                "%y%m%d%H%M%S").strftime("%Y-%m-%d %H:%M:%S")

        smstype = datafield.split(',')[0]
        # process rssi parameters
        if smstype == "RSSI":
            site_name = datafield.split(',')[1]
            rssi_string = datafield.split(',', 2)[2]
            print rssi_string
            # format is
            # <router name>,<rssi value>,...
            query = "INSERT IGNORE INTO coordrssi (timestamp, site_name, router_name, rssi_val) VALUES ("
            tuples = re.findall("[A-Z]+,\d+", rssi_string)
            for item in tuples:
                query += "'" + timestamp + "',"
                query += "'" + site_name + "',"
                query += "'" + item.split(',')[0] + "',"
                query += item.split(',')[1] + "),("

            query = query[:-2]

            print query

            dbio.commitToDb(query, 'ProcessCoordinatorMsg')

            return True
        else:
            print ">> Processing coordinator weather"
    except IndexError:
        print "IndexError: list index out of range"
        return False
    except:
        print ">> Unknown Error", coordsms
        return False
Пример #10
0
def main():
    try:
        if sys.argv[1] == 'test': writetodb = False
        else: writetodb = True
    except:
        writetodb = True

    c = cfg.config()
    dbio.createTable("runtimelog", "runtime")
    server.logRuntimeStatus("alert", "checked")

    print '>> Checking for alert sms'
    alertmsg = server.CheckAlertMessages()

    print alertmsg
    if alertmsg:
        # server.WriteOutboxMessageToDb(alertmsg,c.smsalert.smartnum)
        # server.WriteOutboxMessageToDb(alertmsg,c.smsalert.globenum)
        query = """select nickname, numbers from dewslcontacts where grouptags like '%alert%'"""
        contacts = dbio.querydatabase(query, 'checkalert')
        # print contacts

        query = "INSERT INTO smsoutbox (timestamp_written,recepients,sms_msg,send_status) VALUES "

        tsw = dt.today().strftime("%Y-%m-%d %H:%M:%S")
        for item in contacts:
            message = 'SENSOR ALERT:\n%s' % (alertmsg)
            message = message.replace("ALERT", "AL3RT")
            query += "('%s','%s','%s','UNSENT')," % (tsw, item[1], message)
        query = query[:-1]

        if writetodb: dbio.commitToDb(query, 'checkalertmsg', 'GSM')
        else: print query
        print 'done'
    else:
        print '>> No alert msg read.'
Пример #11
0
def RunSenslopeServer(network):
    minute_of_last_alert = dt.now().minute
    timetosend = 0
    lastAlertMsgSent = ''
    logruntimeflag = True
    global checkIfActive
    checkIfActive = True

    try:
        gsm = gsmio.gsmInit(network)
    except serial.SerialException:
        print '**NO COM PORT FOUND**'
        serverstate = 'serial'
        gsm.close()
        logRuntimeStatus(network, "com port error")
        raise ValueError(">> Error: no com port found")

    dbio.createTable("runtimelog", "runtime", cfg.config().mode.logtoinstance)
    logRuntimeStatus(network, "startup")

    dbio.createTable('smsinbox', 'smsinbox', cfg.config().mode.logtoinstance)
    dbio.createTable('smsoutbox', 'smsoutbox', cfg.config().mode.logtoinstance)

    sensor_numbers_str = str(getSensorNumbers())

    print '**' + network + ' GSM server active**'
    print time.asctime()
    while True:
        m = gsmio.countmsg()
        if m > 0:
            allmsgs = gsmio.getAllSms(network)

            try:
                WriteRawSmsToDb(allmsgs, sensor_numbers_str)
            except MySQLdb.ProgrammingError:
                print ">> Error: May be an empty line.. skipping message storing"

            deleteMessagesfromGSM()

            print dt.today().strftime("\n" + network +
                                      " Server active as of %A, %B %d, %Y, %X")
            logRuntimeStatus(network, "alive")

            trySendingMessages(network)

        elif m == 0:
            trySendingMessages(network)

            gsmio.gsmflush()
            today = dt.today()
            if (today.minute % 10 == 0):
                if checkIfActive:
                    print today.strftime(
                        "\nServer active as of %A, %B %d, %Y, %X")
                checkIfActive = False
            else:
                checkIfActive = True

        elif m == -1:
            print 'GSM MODULE MAYBE INACTIVE'
            serverstate = 'inactive'
            logRuntimeStatus(network, "gsm inactive")
            gsmio.resetGsm()

        elif m == -2:
            print '>> Error in parsing mesages: No data returned by GSM'
            gsmio.resetGsm()
        else:
            print '>> Error in parsing mesages: Error unknown'
            gsmio.resetGsm()
def ProcessARQWeather(line, sender):

    #msg = message

    print 'ARQ Weather data: ' + line

    line = re.sub("(?<=\+) (?=\+)", "NULL", line)

    try:
        # ARQ+1+3+4.143+4.128+0.0632+5.072+0.060+0000+13+28.1+75.0+55+150727/160058
        #table name
        linesplit = line.split('+')

        msgname = checkNameOfNumber(sender).lower()
        if msgname:
            print ">> Number registered as", msgname
            msgname_contact = msgname
            msgname = msgname + 'w'

        # else:
        # print ">> New number", sender
        # msgname = ''

        r15m = int(linesplit[1]) * 0.5
        r24h = int(linesplit[2]) * 0.5
        batv1 = linesplit[3]
        batv2 = linesplit[4]
        current = linesplit[5]
        boostv1 = linesplit[6]
        boostv2 = linesplit[7]
        charge = linesplit[8]
        csq = linesplit[9]
        if csq == '':
            csq = '0'
        temp = linesplit[10]
        hum = linesplit[11]
        flashp = linesplit[12]
        txtdatetime = dt.strptime(
            linesplit[13], '%y%m%d/%H%M%S').strftime('%Y-%m-%d %H:%M:00')

        updateSimNumTable(msgname_contact, sender, txtdatetime[:8])

        # print str(r15m),str(r24h),batv1, batv2, current, boostv1, boostv2, charge, csq, temp, hum, flashp,txtdatetime

    except IndexError and AttributeError:
        print '\n>> Error: Rain message format is not recognized'
        print line
        return
    except ValueError:
        print '>> Error: Possible conversion mismatch ' + line
        return

    if msgname:
        dbio.createTable(str(msgname), "arqweather")
    else:
        print ">> Error: Number does not have station name yet"
        return

    try:
        query = """INSERT INTO %s (timestamp,name,r15m, r24h, batv1, batv2, cur, boostv1, boostv2, charge, csq, temp, hum, flashp) 
        VALUES ('%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" % (
            msgname, txtdatetime, msgname, r15m, r24h, batv1, batv2, current,
            boostv1, boostv2, charge, csq, temp, hum, flashp)
        # print query
    except ValueError:
        print '>> Error writing query string.',
        return

    dbio.commitToDb(query, 'ProcessARQWeather')

    print 'End of Process ARQ weather data'
def ProcessEarthquake(msg):
    line = msg.data.upper()
    print "Processing earthquake data"
    print line

    dbio.createTable('earthquake', 'earthquake')

    #find date
    if re.search("\d{1,2}\w+201[6789]", line):
        datestr_init = re.search("\d{1,2}\w+201[6789]", msg.data).group(0)
        pattern = ["%d%B%Y", "%d%b%Y"]
        datestr = None
        for p in pattern:
            try:
                datestr = dt.strptime(datestr_init, p).strftime("%Y-%m-%d")
                break
            except:
                print ">> Error in datetime conversion", datestr, "for pattern", p
        if datestr == None:
            return False
    else:
        print ">> No date string recognized"
        return False

    #find time
    if re.search("\d{1,2}[:\.]\d{1,2} *[AP]M", line):
        timestr = re.search("\d{1,2}[:\.]\d{1,2} *[AP]M", line).group(0)
        timestr = timestr.replace(" ", "").replace(".", ":")
        try:
            timestr = dt.strptime(timestr, "%I:%M%p").strftime("%H:%M:00")
        except:
            print ">> Error in datetime conversion", timestr
            return False
    else:
        print ">> No time string recognized"
        return False

    datetimestr = datestr + ' ' + timestr

    #find magnitude
    if re.search("((?<=M[SBLVOW]\=)|(?<=M\=)|(?<=MLV\=))\d+\.\d+(?= )", line):
        magstr = re.search(
            "((?<=M[SBLVOW]\=)|(?<=M\=)|(?<=MLV\=))\d+\.\d+(?= )",
            line).group(0)
    else:
        print ">> No magnitude string recognized"
        magstr = 'NULL'

    #find depth
    if re.search("(?<=D\=)\d+(?=K*M)", line):
        depthstr = re.search("(?<=D\=)\d+(?=K*M)", line).group(0)
    else:
        print ">> No depth string recognized"
        depthstr = 'NULL'

    #find latitude
    if re.search("\d+[\.\:]\d+(?=N)", line):
        latstr = re.search("\d+[\.\:]\d+(?=N)", line).group(0)
    else:
        print ">> No latitude string recognized"
        latstr = 'NULL'

    #find longitude
    if re.search("\d+[\.\:]\d+(?=E)", line):
        longstr = re.search("\d+[\.\:]\d+(?=E)", line).group(0)
    else:
        print ">> No longitude string recognized"
        longstr = 'NULL'

    #find epicenter distance
    if re.search("(?<=OR )\d+(?=KM)", line):
        diststr = re.search("(?<=OR )\d+(?=KM)", line).group(0)
    else:
        print ">> No distance string recognized"
        diststr = 'NULL'

    # find heading
    if re.search("[NS]\d+[EW]", line):
        headstr = re.search("[NS]\d+[EW]", line).group(0)
    else:
        print ">> No heading string recognized"
        headstr = 'NULL'

    # find Municipality
    if re.search("(?<=OF )[A-Z ]+(?= \()", line):
        munistr = re.search("(?<=OF )[A-Z ]+(?= \()", line).group(0)
    else:
        print ">> No municipality string recognized"
        munistr = 'NULL'

    # find province
    if re.search("(?<=\()[A-Z ]+(?=\))", line):
        provistr = re.search("(?<=\()[A-Z ]+(?=\))", line).group(0)
    else:
        print ">> No province string recognized"
        provistr = 'NULL'

    # find issuer
    if re.search("(?<=\<)[A-Z]+(?=\>)", line):
        issuerstr = re.search("(?<=\<)[A-Z]+(?=\>)", line).group(0)
    else:
        print ">> No issuer string recognized"
        issuerstr = 'NULL'

    query = "INSERT INTO senslopedb.earthquake (timestamp, mag, depth, lat, longi, dist, heading, municipality, province, issuer) VALUES ('%s',%s,%s,%s,%s,%s,'%s','%s','%s','%s') ON DUPLICATE KEY UPDATE mag=mag, depth=depth, lat=lat, longi=longi, dist=dist, heading=heading, municipality=municipality, province=province, issuer=issuer;" % (
        datetimestr, magstr, depthstr, latstr, longstr, diststr, headstr,
        munistr, provistr, issuerstr)

    print query

    query.replace("'NULL'", "NULL")

    dbio.commitToDb(query, 'earthquake')

    # subprocess.Popen(["python",cfg.config().fileio.eqprocfile])
    exec_line = "~/anaconda2/bin/python %s > ~/scriptlogs/earthquakescript.txt 2>&1" % (
        cfg.config().fileio.eqprocfile)
    p = subprocess.Popen(exec_line,
                         stdout=subprocess.PIPE,
                         shell=True,
                         stderr=subprocess.STDOUT)

    return True
def ProcessColumn(line, txtdatetime, sender):
    msgtable = line[0:4]
    print 'SITE: ' + msgtable
    ##msgdata = line[5:len(line)-11] #data is 6th char, last 10 char are date
    msgdata = (line.split('*'))[1]
    print 'raw data: ' + msgdata
    #getting date and time
    #msgdatetime = line[-10:]
    try:
        msgdatetime = (line.split('*'))[2][:10]
        print 'date & time: ' + msgdatetime
    except:
        print '>> Date and time defaults to SMS not sensor data'
        msgdatetime = txtdatetime

    # col_list = cfg.get("Misc","AdjustColumnTimeOf").split(',')
    # if msgtable == 'PUGB':
    #     msgdatetime = txtdatetime
    #     print "date & time adjusted " + msgdatetime
    # else:
    msgdatetime = dt.strptime(msgdatetime,
                              '%y%m%d%H%M').strftime('%Y-%m-%d %H:%M:00')
    # print 'date & time no change'

    dlen = len(msgdata)  #checks if data length is divisible by 15
    #print 'data length: %d' %dlen
    nodenum = dlen / 15
    #print 'number of nodes: %d' %nodenum
    if dlen == 0:
        print 'Error: There is NO data!'
        return
    elif ((dlen % 15) == 0):
        #print 'Data has correct length!'
        valid = dlen
    else:
        print 'Warning: Excess data will be ignored!'
        valid = nodenum * 15

    updateSimNumTable(msgtable, sender, msgdatetime[:10])

    query = query = """INSERT IGNORE INTO %s (timestamp,id,xvalue,yvalue,zvalue,mvalue) VALUES """ % (
        str(msgtable.lower()))

    try:
        i = 0
        while i < valid:
            #NODE ID
            #parsed msg.data - NODE ID:
            node_id = int('0x' + msgdata[i:i + 2], 16)
            i = i + 2

            #X VALUE
            #parsed msg.data - TEMPX VALUE:
            tempx = int('0x' + msgdata[i:i + 3], 16)
            i = i + 3

            #Y VALUE
            #parsed msg.data - TEMPY VALUE:
            tempy = int('0x' + msgdata[i:i + 3], 16)
            i = i + 3

            #Z VALUE
            #parsed msg.data - ZVALUE:
            tempz = int('0x' + msgdata[i:i + 3], 16)
            i = i + 3

            #M VALUE
            #parsed msg.data - TEMPF VALUE:
            tempf = int('0x' + msgdata[i:i + 4], 16)
            i = i + 4

            valueX = tempx
            if valueX > 1024:
                valueX = tempx - 4096

            valueY = tempy
            if valueY > 1024:
                valueY = tempy - 4096

            valueZ = tempz
            if valueZ > 1024:
                valueZ = tempz - 4096

            valueF = tempf  #is this the M VALUE?

            query = query + """('%s',%s,%s,%s,%s,%s),""" % (
                str(msgdatetime), str(node_id), str(valueX), str(valueY),
                str(valueZ), str(valueF))

            print "%s\t%s\t%s\t%s\t%s" % (str(node_id), str(valueX),
                                          str(valueY), str(valueZ),
                                          str(valueF))

        query = query[:-1]

        # print query

        if i != 0:
            dbio.createTable(str(msgtable), "sensor v1")
            dbio.commitToDb(query, 'ProcessColumn')

        SpawnAlertGen(msgtable)

    except KeyboardInterrupt:
        print '\n>>Error: Unknown'
        raise KeyboardInterrupt
        return
    except ValueError:
        print '\n>>Error: Unknown'
        return