我使用node-red通过MQTT获取数据。我在图片中有回应......
不,我想在功能的帮助下分别获取数据w.r.t Phase1,Phase2,Phase3,Phase4和Timestamp。我不知道如何这样做。 P.S我是node.red的新手。三江源
答案 0 :(得分:2)
如果你只想从每个msg中获取数值,你可以使用from collections import UserDict
# see also UserList, UserString
class Treatment(UserDict):
pass
class Disease(UserDict):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.treatments = []
class Patient(UserDict):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.diseases = []
patients = dict()
p1 = patients['0001'] = Patient(name='Eric', surname='Idle')
d1 = Disease(name='Eating too much')
t1 = Treatment(title='Radical Therapy', description='Mint pill after lunch.')
d1.treatments.append(t1)
d2 = Disease(name='Cannot Control the Legs Syndrome')
t2 = Treatment(title='Conservative Approach', description='Have a seat.')
d2.treatments.append(t2)
p1.diseases.extend([d1,d2])
for pid in patients:
print()
print('PID:{} {} {}'.format(pid, patients[pid]['name'],\
patients[pid]['surname']))
print(50*'-')
for disease in patients[pid].diseases:
print()
print('Disease: {}'.format(disease['name']))
for treatment in disease.treatments:
print('Treatment: {}'.format(treatment['title']))
print('\t\t{}'.format(treatment['description']))
节点来分割字符串,如下所示:
function
现在,您将拥有一个可以直接在仪表板元素中显示的主题和有效负载。
还有至少2个节点可以做同样的事情,而无需编写任何javascript代码。您可能希望安装并尝试var parts = msg.payload.split(":");
var value = parts.length > 1 ? parts[1].trim() : parts[0].trim();
// append the string before the ":" to the topic...
msg.topic += "/" + parts.length > 1 ? parts[0] : "Total";
// coerce the value after the ":" to be numeric...
msg.payload = +value;
return msg;
节点 - 它适用于提取字符串。也可以使用node-red-contrib-string
节点,但需要一个Jsonata表达式,这个表达式功能强大,但也比javascript代码复杂一点......