Extract values from a json object using a function node - mqtt

I have a problem and knowing how to ask the correct question is key! I was looking around and found someone asking the same question in a different way, unfortunately the answer was not complete. So I am going to ask it here using my flows and situation.
I am trying to extract values from an json object using a function node, tried many ways and nothing seems to work for me. I am trying to extract 7 values to create 7 different messages with 7 different Topics so they can be published via 7 different MQTT nodes.
This is the complete message object:
`
2022-11-11, 1:59:04 p.m.node: 3ec81adb55be266d
v3/rak-wis-blocks#ttn/devices/rak4631-sensor1/up : msg : Object
object
topic: "v3/rak-wis-blocks#ttn/devices/rak4631-sensor1/up"
payload: object
Vbat_Per: 110
Vbat: 4269
Fixed: 1
TemperatureAM: 11.85
HumidityAM: 99.99
TempInside: 12.42
Pressure: 995.44
qos: 0
retain: false
_msgid: "85957ff31d82e49b"
`
I found an example that used a Function node, with the below code, and it spits out 7 separate messages with the variables I want. I just don't know how to take each new message and modify its properties with new and different Topic values.
Example: topic: sensor/Vbat_per, topic: sensor/Vbat, topic: sensor/Fixed, etc.
`
var keys = Object.keys(msg.payload);
var msgs = keys.map(function(key) {
return { topic: key, payload: msg.payload[key] };
});
return [msgs];
`
This is the output from the Function node with the above code:
`
2022-11-11, 3:09:04 p.m.node: 9bc14535de62758e
Vbat_Per : msg.payload : number
75
2022-11-11, 3:09:04 p.m.node: 9bc14535de62758e
Vbat : msg.payload : number
4034
2022-11-11, 3:09:04 p.m.node: 9bc14535de62758e
Fixed : msg.payload : number
1
2022-11-11, 3:09:04 p.m.node: 9bc14535de62758e
TemperatureAM : msg.payload : number
12.32
2022-11-11, 3:09:04 p.m.node: 9bc14535de62758e
HumidityAM : msg.payload : number
99.99
2022-11-11, 3:09:04 p.m.node: 9bc14535de62758e
TempInside : msg.payload : number
12.49
2022-11-11, 3:09:04 p.m.node: 9bc14535de62758e
Pressure : msg.payload : number
994.3
`
To me it looks like there are 7 new messages with only a payload and no Topic. Am I understanding this correctly?

Related

Parsing text file - Lua

I have a text file contains:
<Response>
<IP>17.178.96.59</IP>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode></RegionCode>
<RegionName></RegionName>
<City>Chicago</City>
<ZipCode></ZipCode>
<TimeZone>America/Chicago</TimeZone>
<Latitude>37.751</Latitude>
<Longitude>-97.822</Longitude>
<MetroCode>0</MetroCode>
</Response>
How to remove each and so I get only:
17.178.96.59,
US,
United States,
nil,
nil,
Chicago,
nil,
America/Chicago,
37.751,
-97.822,
0
Using the Lua script.
You don't show what you already tried, but there is a recently discussed SO question on how to iterate over XML nodes that should work well for your purposes. Just replace fixed element names with something like [%w_]+ and collect parsed values into a table and you'll get the result you are looking for.
This is what I am trying to do.
int = getInternet()
url3 = 'https://freegeoip.app/xml/17.178.96.59'
result = int.getURL(url3)
result = result:gsub("><",">nil<"):gsub("<.->",""):gsub("%c%s+",","):sub(2)..'nil'
print(result)
-- = 17.178.96.59,US,United States,nil,nil,nil,nil,America/Chicago,37.751,-97.822,0,mil
geoResult = {}
for word in string.gmatch(result, '([^,]+)') do
--print(word)
table.insert(geoResult, word)
--for i,v in ipairs(result) do print(i,v) end
end
print('IP : '..geoResult[1])
print('Country ID : '..geoResult[2])
print('Country Name : '..geoResult[3])
print('Region Code : '..geoResult[4])
print('Region Name : '..geoResult[5])
print('City : '..geoResult[8])
print('ZIP Code : '..geoResult[7])
print('Time Zone : '..geoResult[8])
print('Latitude : '..geoResult[9])
print('Longitude : '..geoResult[10])
print('Metro Code : '..geoResult[11])
-- result:
17.178.96.59,US,United States,nil,nil,nil,nil,America/Chicago,37.751,-97.822,0,nil
IP : 17.178.96.59
Country ID : US
Country Name : United States
Region Code : nil
Region Name : nil
City : America/Chicago
ZIP Code : nil
Time Zone : America/Chicago
Latitude : 37.751
Longitude : -97.822
Metro Code : 0

How to disable Bitcoind ZeroMQ notification publish block or transaction in mempool

TL;DR
zmq send the block data in mempool not confirmed in blockchain,how to filter these unconfirmed data.
the problem
In 2019 our production use blocknotify to fire event of new block,but this year(2020) I'm required to changed to using zeromq.So I change my code fired by zeromq's publish zmqpubhashblock , it's easy.But I found zmq send the block data in mempool not confirmed in blockchain, and not found anyother config can disable this behavior.
current node(a testnet btc node for development)
Bitcoin Core Daemon version v0.18.0.0
disabled wallet feature of bitcoind
add -txindex in config
my code (a zmq sub demo in laravel command)
$context = new \ZMQContext();
$subscriber = new \ZMQSocket($context, \ZMQ::SOCKET_SUB);
$subscriber->connect("tcp://192.168.1.136:28332"); //btc testnet
$subscriber->setSockOpt(\ZMQ::SOCKOPT_SUBSCRIBE, "hashblock");
// $subscriber->setSockOpt(\ZMQ::SOCKOPT_SUBSCRIBE, "hashtx");
$subscriber->setSockOpt(\ZMQ::SOCKOPT_SUBSCRIBE, "rawblock");
// $subscriber->setSockOpt(\ZMQ::SOCKOPT_SUBSCRIBE, "rawtx");
$this->info("sub btc");
while (true) {
$multiRec = $subscriber->recvMulti();
// if ($multiRec != false) {
// var_dump($multiRec);
$this->info(date('Y-m-d H:i:s') . ' rec:');
$topic = $multiRec[0];
$body = \bin2hex($multiRec[1]);
$sequence = \bin2hex($multiRec[2]);
$this->info("topic: " . $topic . " , sequence : " . $sequence);
$this->info("body " . " , sequence : " . $sequence);
$this->info($body);
$this->info('----------------------');
}
It output like this
sub btc
2020-01-09 07:55:10 rec:
topic: hashblock , sequence : 4c2b0000
body , sequence : 4c2b0000
00000000000119f7061e4de7bc09f7526ad6d03057da7eadb2a8c68260765b20
----------------------
2020-01-09 07:55:10 rec:
topic: rawblock , sequence : 4c2b0000
body , sequence : 4c2b0000
0000ff3fef3ca23d30df507cfa0bbbccb27cbed53e714cf4da599c917fc200000000000035f889f1bb70bac042d7f63dc0056279d16564ef2b5a9ef6ef6e02a122ae3cea67dc165eb08f091b056252a702020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff2703b038190458dc165e726567696f6e312f46756e3230313931322f010000012700000000000000ffffffff02c40c5402000000001976a9149dbb856bf9bfe4cebc7cc3aec5434c14e540ba7d88ac0000000000000000266a24aa21a9ed0b17f5f2ecfe5fff1fde0d8e23223f2aa9fc2fad5b04c0430bb682c69f4f59dd012000000000000000000000000000000000000000000000000000000000000000000000000001000000000101d266d0eba0eb71e7f4c163caa5ce49d9f9d167cec65f7a1299cda5e6a10b3b2b0000000000ffffffff02e8030000000000001976a914c48ce75ce550d6d13548fe683106facbc0aeee3c88acb0290c0000000000160014f52b799d88c35973313bc2f5110a53f74e4dde810247304402203a001e87cbda9f36570963ff8dbb3f9b086008f34c15c360bcea2417aea6c575022071bf52e8394b54bab9d5a4a87e386c995d3651f9d09dc958e38d5fffeae1910701210339363cbe9f2a914801db907302a6658c28c145b247cb7c0133f70f5db0cdd0aa00000000
----------------------
2020-01-09 07:55:12 rec:
topic: hashblock , sequence : 4d2b0000
body , sequence : 4d2b0000
0000000000017d572741e884f5b6d20a86351dc52f432072eba0f86ac5026cbd
----------------------
2020-01-09 07:55:12 rec:
topic: rawblock , sequence : 4d2b0000
body , sequence : 4d2b0000
00000020205b766082c6a8b2ad7eda5730d0d66a52f709bce74d1e06f719010000000000eb6c4b961cfb08a367efdc0770df9377b5c1ffc7358ecf25870648e99e44d51c5edc165eb08f091bbd17f63801010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4c03b13819045edc165e08fabe6d6d00000000000000000000000000000000000000000000000000000000000000000100000000000000180000708b1800000d2f6e6f64655374726174756d2f00000000020000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9e40b5402000000001976a914bd3400d71504033fb1e7c947f2e0f55775899f2288ac0120000000000000000000000000000000000000000000000000000000000000000000000000
----------------------
2020-01-09 07:55:12 rec:
topic: hashblock , sequence : 4e2b0000
body , sequence : 4e2b0000
000000000000df593a7a72c65d1ddcb8d7aef78bac448b288b798de256d04db3
----------------------
2020-01-09 07:55:12 rec:
topic: rawblock , sequence : 4e2b0000
body , sequence : 4e2b0000
00000020bd6c02c56af8a0eb7220432fc51d35860ad2b6f584e84127577d010000000000f70bd9ad38658f46e958c11bc3f98e3bc7b5082c72beee27df6672750a2c936c60dc165eb08f091b1b18f5f401010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4c03b238190460dc165e08fabe6d6d0000000000000000000000000000000000000000000000000000000000000000010000000000000018000070b40200000d2f6e6f64655374726174756d2f00000000020000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9e40b5402000000001976a914bd3400d71504033fb1e7c947f2e0f55775899f2288ac0120000000000000000000000000000000000000000000000000000000000000000000000000
----------------------
2020-01-09 07:55:13 rec:
topic: hashblock , sequence : 4f2b0000
body , sequence : 4f2b0000
00000000000082663372af34f08cb4463bba26bc50849d1f0cf559a6beb52c30
----------------------
2020-01-09 07:55:13 rec:
topic: rawblock , sequence : 4f2b0000
body , sequence : 4f2b0000
00000020b34dd056e28d798b288b44ac8bf7aed7b8dc1d5dc6727a3a59df0000000000003ed7970bbe29d8f94424d80bfd597d11ccb0501a9ca9744bae7ce7e9e68f26a460dc165eb08f091b54bb9f6601010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4c03b338190460dc165e08fabe6d6d0000000000000000000000000000000000000000000000000000000000000000010000000000000018000070c90400000d2f6e6f64655374726174756d2f00000000020000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9e40b5402000000001976a914bd3400d71504033fb1e7c947f2e0f55775899f2288ac0120000000000000000000000000000000000000000000000000000000000000000000000000
----------------------
I decode the rawBlockdata and get the txid a4268fe6e9e77cae4b74a99c1a50b0cc117d59fd0bd82444f9d829be0b97d73e, but it is zero confirmations (current 08:12:18 UTC Thursday, January 9, 2020).
check other testnet explorer solved my problem, the explorer I used is 200 behind current
By checking other testnet explorer, I find https://tbtc.bitaps.com/ is 200 block behind current (testnet January 9, 2020).
And the data is right now.(January 10, 2020).
It's not bitcoind's problem or my code's problem just check other testnet explorer.

What are the GLTF animations sampler input/output values?

I am reading the specification, but I can not understand the properties of the sampler.
This is the animation that I have
"animations" : [
{
"channels" : [
{
"sampler" : 0,
"target" : {
"node" : 0,
"path" : "translation"
}
}
],
"name" : "00001_2780.datAction",
"samplers" : [
{
"input" : 9,
"interpolation" : "CUBICSPLINE",
"output" : 10
}
]
},
{
"channels" : [
{
"sampler" : 0,
"target" : {
"node" : 1,
"path" : "translation"
}
}
],
"name" : "00002_2780.datAction",
"samplers" : [
{
"input" : 9,
"interpolation" : "CUBICSPLINE",
"output" : 11
}
]
}
],
What I can not understand is what are the values 9 and 10 for the first sample and 9 and 11 for the second
All that we have in the specification is
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#animations
Each of the animation's samplers defines the input/output pair: a set of floating point scalar values representing linear time in seconds; and a set of vectors or scalars representing animated property.
And this makes it more unclear to me.
Is there a more detailed explanation about what input/output values are and what they represent. What will happen for example if I change the input from 9 to 99 or to 9.9 or to 0.9 or to 0. How will this change the animation?
Thanks
The numbers 9 and 10 here are glTF accessor index ID values. If you decode accessor index 9, you'll find the list of times for each of the keyframes of the animation. If you decode accessor 10, normally you would expect to find the list of values for the keyframes. But since this is CUBICSPLINE, accessor 10 will contain the in-tangent, value, and out-tangent for each keyframe.
One way to investigate glTF files like this is to use the glTF Tools extension for VSCode. You can right-click the input or output value and choose Go To Definition to get to the accessor in question, and choose Go To Definition again to decode it. (Disclaimer, I'm a contributor to glTF Tools).

Derivative node in kapacitor batch

I am using derivative node to calculate bandwidth utilization of network devices, below is the script.
I am using where clause because i wanted alert for specific interface for specific Ip.
// database
var database = 'router'
// measurement from where data is coming
var measurement = 'cisco_router'
// RP from where data is coming
var RP = 'autogen'
// which influx cluster to use
var clus = 'network'
// durations
var period = 7m
var every = 10s
// alerts
var crit = 320
var alertName = 'cisco_router_bandwidth_alert'
var triggerType = 'threshold'
batch
|query(''' SELECT (mean("bandwidth_in") * 8) as "value" FROM "router"."autogen"."cisco_router" where host = '10.1.11.1' and ( interface_name = 'GigabitEthernet0/0/0' or interface_name = 'GigabitEthernet0/0/1') ''')
.cluster('network')
.period(7m)
.every(6m)
.groupBy(*)
|derivative('value')
.unit(1s)
.nonNegative()
.as('value')
|alert()
.crit(lambda: "value" > crit)
.stateChangesOnly()
.message(' {{.Level}} for {{ index .Tags "device_name" }} on Port {{ index .Tags "name" }} {{ .Time.Local.Format "2006.01.02 - 15:04:05" }} ')
.details('''
<pre>
------------------------------------------------------------------
CLIENT NAME : XXXXXXXX
ENVIRONMENT : Prod
DEVICE TYPE : Router
CATEGORY : {{ index .Tags "type" }}
IP ADDRESS : {{ index .Tags "host" }}
DATE : {{ .Time.Local.Format "2006.01.02 - 15:04:05" }}
INTERFACE NAME : {{ index .Tags "name" }}
VALUE : {{ index .Fields "value" }}
SEVERITY : {{.Level}}
------------------------------------------------------------------
</pre>
''')
.log('/tmp/chronograf/cisco_router_interface_alert.log')
.levelTag('level')
.idTag('id')
.messageField('message')
.email()
.to('XXXXXXX')
|influxDBOut()
.database('chronograf')
.retentionPolicy(RP)
.measurement('alerts')
.tag('alertName', alertName)
But it is not showing anything when i do kapacitor watch and not showing any errors in logs.
derivative() and some other nodes like stateDuration() kind of resets their state on each new batch query, in opposite to stream mode, where their state is kept whole time.
Actually, it is because in batch mode this nodes designed to track changes only inside the current batch of points.
Since your query returns single point - there is no result from derivative().
Try move derivative to the query. And use |httpOut() node to track results on each step - really helpful to understand kapacitor logic.
here is some example:
dbrp "telegraf"."autogen"
var q= batch
|query('SELECT derivative(mean("bytes_recv"), 1s) AS "bytes_recv_1s" FROM "telegraf"."autogen"."net" WHERE time < now() AND "interface"=\'eth0\' GROUP BY time(10m) fill(none)')
.period(10m)
.every(30s).align()
.groupBy(time(10m))
.fill('none')
|last('bytes_recv_1s').as('value')
|httpOut('query')
Note, there is a bugs associated with query parsing, that requires specify GROUP BY in both query and tick
https://github.com/influxdata/kapacitor/issues/971
https://github.com/influxdata/kapacitor/issues/622

Splitting a string using a delimiter in Groovy and avoiding IndexOutOfBoundsException

I want to split an input parameter inputDetails to unit level. I'm using tokenize for doing this. Here is my code:
Groovy Code:
def inputDetails = "1234-a0-12;1111-b0-34";
def cDesc = inputDetails.tokenize(";");
for (int i=0; i<cDesc.size(); ++i)
{
def cVer = cDesc.get(i);
def cNum = cVer.tokenize("-");
def a = cNum.get(0);
def b = cNum.get(1);
def c = cNum.get(2);
println (" DEBUG : Input details are, ${a} : ${b} : ${c} \n");
}
Output:
DEBUG : Input details are, 1234 : a0 : 12
DEBUG : Input details are, 1111 : b0 : 34
This output is correct and expected. But if I change the first line of Groovy code to following:
def inputDetails = "1234-a0-12;1111-b0";
I get following error message:
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java_util_List$get$6.call(Unknown Source)
at Script1.run(Script1.groovy:9)
How can I fix it to prevent getting IndexOutOfBoundsException while supporting both, 1234-a0-12;1111-b0-34 and 1234-a0-12;1111-b0 inputs?
You can use Groovy's multiple assignment feature to safely grab 3 values from the second tokenization. Consider following example:
def inputDetails = "1234-a0-12;1111-b0-34"
def cDesc = inputDetails.tokenize(";")
cDesc.each { part ->
def (p1, p2, p3) = part.tokenize('-')
println "DEBUG: Input details are, ${p1} : ${p2} : ${p3}"
}
Output:
DEBUG: Input details are, 1234 : a0 : 12
DEBUG: Input details are, 1111 : b0 : 34
The good thing is that this approach prevents IndexOutOfBoundsException or NullPointerException. If we change the first line to
def inputDetails = "1234-a0-12;1111-b0"
the result is:
DEBUG: Input details are, 1234 : a0 : 12
DEBUG: Input details are, 1111 : b0 : null
You can split the string into a 2D list by further splitting on '-':
def inputDetails = "1234-a0-12;1111-b0-34"
def elements = inputDetails.split(';').collect{it.split('-')}
elements is of type List<List<String>>. When printed, it yields:
[[1234, a0, 12], [1111, b0, 34]]
With this, you can afford more flexibility instead of hard-coding array indexes.
And with "1234-a0-12;1111-b0", it's split into [[1234, a0, 12], [1111, b0]]

Resources