I am using a RFID reader to receive messages. I'm trying to avoid duplicated message by appending them to emptylist = []
and don't further append them if they exist in the list. Below are my code:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("/whatver/#")
def on_message(client, userdata, msg): # when PUBLISH msg is rcvd frm the server
payloadjson = json.loads(msg.payload.decode('utf-8'))
line = payloadjson["value"].split(',')
epc = line[1]
payload = {'a': epc[11:35], 'b':payloadjson["devicename"], 'c':payloadjson["sensorname"]}
emptylist = []
emptylist.append(payload)
if payload not in emptylist:
emptylist.append(payload)
print (emptylist)
test = mqtt.Client(protocol = mqtt.MQTTv31)
test.connect(host=_host, port=1883, keepalive=60, bind_address="")
test.on_connect = on_connect
test.on_message = on_message
test.loop_forever()
However I am getting output as below, which shows that payload
is appended to the emptylist
, but is stored into multiple separated lists. And it keeps printing the same output if the RFID reader is still reading the same tag. I would like to remove the duplicated messages and retain only one even if the RFID reader reads the same tag.
[{'b': 'READERBASIN.3', 'c': 'ANTENNA.2', 'a': '000000000000130000624462'}] # from Tag A
[{'b': 'READERBASIN.3', 'c': 'ANTENNA.2', 'a': 'abcxxx000000130000627000'}] # from Tag B
[{'b': 'READERBASIN.3', 'c': 'ANTENNA.2', 'a': '000000000000130000624462'}] # from Tag A
[{'b': 'READERBASIN.3', 'c': 'ANTENNA.2', 'a': '000000000000130000624462'}] # from Tag A
.
.
.
How can I fix this?