我通过RFID阅读器收到我的消息。我想要做的是删除重复项,并仅在以下两个条件后附加到列表:
datetime
)是唯一的,(已完成) 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)
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()
是间隔5分钟后(因此我可以跟踪RFID阅读器每隔5分钟读取相同的标签)
{'datetime': 2016-04-06 03:21:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:21:18', 'epc': 00000002} # from Tag B
...
...
# 5 minutes later
{'datetime': 2016-04-06 03:21:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:21:18', 'epc': 00000002} # from Tag B
{'datetime': 2016-04-06 03:26:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:26:18', 'epc': 00000002} # from Tag B
...
...
# Another 5 minutes later
{'datetime': 2016-04-06 03:21:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:21:18', 'epc': 00000002} # from Tag B
{'datetime': 2016-04-06 03:26:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:26:18', 'epc': 00000002} # from Tag B
{'datetime': 2016-04-06 03:31:17', 'epc': 00000001} # from Tag A
{'datetime': 2016-04-06 03:31:18', 'epc': 00000002} # from Tag B
...
...
我如何达到条件2?
更新
我为我正在努力实现的目标不明确而道歉
我想要的输出看起来像这样:
type Mod[A <: Nat, B <: Nat] = A#FoldR[_0, Nat, ModFold[B]]
type ModFold[By <: Nat] = Fold[Nat, Nat] {
type Wrap[Acc <: Nat] = By#Compare[Acc]#eq
type Apply[N <: Nat, Acc <: Nat] = Wrap[Succ[Acc]]#IF[_0, Succ[Acc], Nat]
}
toBoolean[ Eq[ Mod[_4, _6], _3] ]
Error:(110, 18) could not find implicit value for parameter b: BoolRep[Eq[Mod[_4,_6],_3]]
toBoolean[ Eq[ Mod[_4, _6], _3] ]
^
答案 0 :(得分:2)
这可能是一种更简单的方法:
do.call(rbind
答案 1 :(得分:1)
我不得不怀疑d_C/d_bias_L
值是否可以来回交替。也就是说,epc值'A'是否可能出现,然后是epc值'B',然后epc值'A'再次出现? (或者可能是A,B,C等?)
如果假设只有一个标签,那么只需查看最近的条目:
epc
现在,您可以将当前last_tag = testlist[-1]
last_dt = last_tag['datetime']
与之前的值进行比较,看它是否适合您的窗口。
请注意,将日期时间代码放入has中对于现有代码实际上不起作用,因为日期时间不断变化,因此datetime
将始终为真,除非您获得两次RFID读取完全相同的第二个。
答案 2 :(得分:1)
你的问题有点不清楚。假设你想要更新测试列表,如果它是一个新的epc,或者它已经是&gt;自上次更新到epc以来的5分钟。 dict()
在这种情况下效果很好。使用标准库中的datetime
模块计算日期或时间的差异。
import paho.mqtt.client as mqtt
import json
import datetime as dt
TIMELIMIT = dt.timedelta(minutes=5)
testlist = {} ## <<-- changed this to a dict()
def on_message(client, userdata, msg):
payloadjson = json.loads(msg.payload.decode('utf-8'))
line = payloadjson["value"].split(',')
epc = line[1]
# this converts the time stamp string to something python can
# use in date / time calculations
when = dt.datetime.strptime(payloadjson['datetime'], '%Y-%m-%d %H:%M:%S')
if epc not in testlist or when - testlist[epc] > TIMELIMIT:
testlist[epc] = when
for epc, when in teslist.items():
print (epc, when)
答案 3 :(得分:0)
有几点可以解决您的问题:
epc
字段,而不是整个payload
对象,因为payload
对象包含datetime
。您可以使用set
存储所有可见的ID。datetime
和timedelta
个对象来比较时间# -*- coding: utf-8 -*-
"""
06 Apr 2016
To answer http://stackoverflow.com/questions/36440618/
"""
# Import statements
# import paho.mqtt.client as mqtt
import json
import datetime as dt
testlist = []
seen_ids = set() # The set of seen IDs
five_minutes = dt.timedelta(minutes=5) # The five minutes differences
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'
# Convert the string into datetime object
datetime = dt.datetime.strptime(datetime, '%Y-%m-%d %H:%M:%S')
payload = {'datetime': datetime, 'epc': epc[11:35]}
# this if-statement satisfy condition 1
if payload['epc'] not in seen_ids: # Need to use another set
should_add = True # The flag whether to add
if len(testlist) > 0:
last_payload = testlist[-1]
diff = payload['datetime'] - last_payload['datetime']
if diff < five_minutes: # If the newer one is less than five minutes, do not add
should_add = False
if should_add:
seen_ids.add(payload['epc']) # Mark as seen
testlist.append(payload)
print ('Content of testlist now:')
for each in testlist:
print (each)
class Message(object):
def __init__(self, payload):
self.payload = payload
def main():
test_cases = []
for i in range(10):
payload = '{{"value":",{}","datetime":"2016-04-06 03:{:02d}:17"}}'.format('0'*34+str(i), i)
test_case = Message(payload)
test_cases.append(test_case)
for test_case in test_cases:
on_message(None, None, test_case)
if __name__ == '__main__':
main()
将打印:
Content of testlist now: {'epc': u'000000000000000000000000', 'datetime': datetime.datetime(2016, 4, 6, 3, 0, 17)} Content of testlist now: {'epc': u'000000000000000000000000', 'datetime': datetime.datetime(2016, 4, 6, 3, 0, 17)} {'epc': u'000000000000000000000005', 'datetime': datetime.datetime(2016, 4, 6, 3, 5, 17)}