mardi 5 avril 2016

How to append a list selectively?

I'm receiving my messages via a RFID reader. What I am trying to do is to remove duplicates and only append to a list following two conditions:

  1. If their ID (epc in this case) is unique and, (done)
  2. The datetime is after a 5 minutes interval (so I can keep track of the same tag is still being read by the RFID reader every 5 mins)

    import paho.mqtt.client as mqtt
    import json
    
    testlist = []       
    
    def on_message(client, userdata, msg):
        payloadjson = json.loads(msg.payload.decode('utf-8'))
        line = payloadjson["value"].split(',')
        epc =  line[1]
        datetime = payloadjson['datetime']
        # datetime is in this string format '2016-04-06 03:21:17'
    
        payload = {'datetime': datetime, 'epc': epc[11:35]}
    
        # this if-statement satisfy condition 1
        if payload not in testlist:
            testlist.append(payload)
            for each in teslist:
                print (each)
    
    

How can I achieve condition 2?

Aucun commentaire:

Enregistrer un commentaire