Exemple #1
0
    def POST(self):
        auth = web.ctx.env.get('HTTP_AUTHORIZATION')
        authreq = False
        allowed = [(u.username, u.password) for u in Authuser.select()]
        if auth is None:
            authreq = True
        else:
            auth = re.sub('^Basic', '', auth)
            username, password = base64.decodestring(auth).split(':')
            password = md5(password).hexdigest()
            auth = None
            if (username, password) in allowed:
                x = web.input()
                of = open('/tmp/svrdata.pkl', 'wb')
                of.write(x['svr_data'])
                of.close()
                to_load = {}
                with open('/tmp/svrdata.pkl', 'rb') as f:
                    to_load = cPickle.load(f)
                for d in to_load.get('pos_to_put', []):
                    try:
                        agent = Agent.select(Agent.q.AgentName == d['name'])[0]
                    except IndexError:
                        try:
                            agent = Agent(**d['data'])
                            # karena 'id'nya tidak standar,
                            # SQLObject bilang Not Found
                        except SQLObjectNotFound:
                            agent = Agent.select(
                                Agent.q.AgentName == d['name'])[0]
                    # Periksa apakah table pemuat Logs untuk
                    # pos ini telah tersedia
                    try:
                        rs = conn.queryAll("SELECT SamplingDate FROM %s \
                                           LIMIT 0, 1" % (agent.table_name))
                    except:
                        rs = conn.queryAll("CREATE TABLE %s \
                                           LIKE tpl_agent" % agent.table_name)
                    for l in d['logs']:
                        sql = "SELECT COUNT(*) FROM %s \
                            WHERE SamplingDate='%s' AND \
                            SamplingTime='%s'" % (agent.table_name,
                                                  l['SamplingDate'],
                                                  l['SamplingTime'])
                        rs = conn.queryAll(sql)
                        if rs[0][0] == 0:

                            sql = "INSERT INTO %s (RID, ReceivedDate, \
                                ReceivedTime, DataType, StatusPort, \
                                SamplingDate, SamplingTime, Temperature, \
                                Humidity, Rain, Rain1, Rain2, Rain3, \
                                Rain4, WLevel, Wlevel1, WLevel2, \
                                WLevel3, WLevel4, up_since, sq) VALUES (%s, '%s', \
                                '%s', %s, '%s', '%s', '%s', %s, %s, \
                                %s, %s, %s, %s, %s, %s, %s, %s, %s, \
                                %s, '%s', %s)" % (
                                agent.table_name, l['RID'], l['ReceivedDate'],
                                l['ReceivedTime'], l['DataType'],
                                l['StatusPort'], l['SamplingDate'],
                                l['SamplingTime'], l['Temperature'],
                                l['Humidity'], l['Rain'], l['Rain1'],
                                l['Rain2'], l['Rain3'], l['Rain4'],
                                l['WLevel'], l['WLevel1'], l['WLevel2'],
                                l['WLevel3'], l['WLevel4'], l['up_since'],
                                l['sq'])
                            rs = conn.query(sql)
                            l = None

                    try:
                        new_pos_data = d['data']
                        for k in new_pos_data.keys():
                            setattr(agent, k, new_pos_data[k])
                    except:
                        pass
                # POS to Del
                for d in to_load.get('pos_to_del', []):
                    try:
                        agent = Agent.select(
                            Agent.q.AgentName == d['AgentName'])[0]
                        agent.destroySelf()
                    except:
                        pass
                return "Ok"
            else:
                authreq = True
        if authreq:
            web.header('WWW-Authenticate', 'Basic realm="incoming"')
            web.ctx.status = '401 unauthorized'
            return """<html>
Exemple #2
0
    def import_input(self):
        print('import_input running')
        to_load = {}
        with open('/tmp/svrdata.pkl', 'rb') as f:
            to_load = pickle.load(f)
        for d in to_load.get('pos_to_put', []):
            try:
                agent = Agent.select(Agent.q.AgentName == d['name'])[0]
            except IndexError:
                try:
                    agent = Agent(**d['data'])
                    # karena 'id'nya tidak standar,
                    # SQLObject bilang Not Found
                except SQLObjectNotFound:
                    agent = Agent.select(
                        Agent.q.AgentName == d['name'])[0]
            # Periksa apakah table pemuat Logs untuk
            # pos ini telah tersedia
            print('incoming:', agent.table_name)
            try:
                rs = conn.queryAll("SELECT SamplingDate FROM %s \
                                   LIMIT 0, 1" % (agent.table_name))
            except:
                try:
                    rs = conn.queryAll("CREATE TABLE %s \
                                   LIKE tpl_agent" % agent.table_name)
                except:
                    pass
            for l in d['logs']:
                sql = "SELECT COUNT(*) FROM %s \
                    WHERE SamplingDate='%s' AND \
                    SamplingTime='%s'" % (agent.table_name,
                                          l['SamplingDate'],
                                          l['SamplingTime'])
                rs = conn.queryAll(sql)
                if rs[0][0] == 0:

                    sql = "INSERT INTO %s (RID, ReceivedDate, \
                        ReceivedTime, DataType, StatusPort, \
                        SamplingDate, SamplingTime, Temperature, \
                        Humidity, Rain, Rain1, Rain2, Rain3, \
                        Rain4, WLevel, Wlevel1, WLevel2, \
                        WLevel3, WLevel4, up_since, sq) VALUES (%s, '%s', \
                        '%s', %s, '%s', '%s', '%s', %s, %s, \
                        %s, %s, %s, %s, %s, %s, %s, %s, %s, \
                        %s, '%s', %s)" % (agent.table_name, l['RID'],
                                l['ReceivedDate'],
                                l['ReceivedTime'],
                                l['DataType'],
                                l['StatusPort'],
                                l['SamplingDate'],
                                l['SamplingTime'],
                                l['Temperature'],
                                l['Humidity'],
                                l['Rain'],
                                l['Rain1'],
                                l['Rain2'],
                                l['Rain3'],
                                l['Rain4'],
                                l['WLevel'],
                                l['WLevel1'],
                                l['WLevel2'],
                                l['WLevel3'],
                                l['WLevel4'],
                                l['up_since'],
                                l['sq']) 
                    rs = conn.query(sql)
                    l = None

            try:
                new_pos_data = d['data']
                for k in new_pos_data.keys():
                    setattr(agent, k, new_pos_data[k])
            except:
                pass
        # POS to Del
        for d in to_load.get('pos_to_del', []):
            try:
                agent = Agent.select(
                    Agent.q.AgentName == d['AgentName'])[0]
                agent.destroySelf()
            except:
                pass
        return "Ok"