mqtt-tools / mqttwarn

A highly configurable MQTT message router, where the routing targets are notification plugins, primarily written in Python.
https://mqttwarn.readthedocs.io/
Eclipse Public License 2.0
952 stars 183 forks source link

Syntax for HTTP service #132

Closed GitBeagle closed 9 years ago

GitBeagle commented 9 years ago

Suggesting an update to the documentation for the HTTP service for dummies like me...

I'm struggling with a working syntax for the HTTP service. Here's what I currently have in mqttwarn.ini. I have tried '{id}' as well with no luck. With the curly brackets the log file shows "Parameter id cannot be formatted:". I'm sure I am missing something simple but I haven't found much in the way of examples to give me another clue.

[config:http] timeout = 5 targets = { 'sensors' : ['get', "http://192.168.1.20/SaveIfbyIdDebug.php?", {'id' : '@id', 'val' : '@val'}, None] }

[sensors/#] targets = http:sensors, file:mylog, log:info

I am publishing something like {'id' : "60", 'val' : "876"}. I have tried single quotes, no quotes, brackets, etc.

Every combination I have tried fails, usually with id and val = NOP. I have been saving data for a couple years using a URL such as "http://192.168.1.20/SaveIfbyId.php?id=60&val=813" so I know the basic http function works. I just created a copy with some extra debug logging for testing.

John

jpmens commented 9 years ago

Have you tried

{'id' : '{id}', 'val' : '{val}'}

The documentation for the http service says:

Note that transforms in parameters must be quoted strings

GitBeagle commented 9 years ago

Thank you for your response and what I'm sure will be a great tool whenever I get it working!

Yes, that was the first approach I tried. Publishing {'id' : '60', 'val' : '876'} yields the following results. I also tried publishing {'id' : 60, 'val' : 876} and {'id' : "60", 'val' : "876"}with similar results. That's when I switched to the "@" syntax. With '@id" there are no errors but '60', "60" or 60 transform to NOP. At that point I ran out of examples to copy.

2015-05-20 06:29:26,020 DEBUG [mqttwarn] Message received on sensors/Dock/Temp: {'id' : '60', 'val' : '876'} 2015-05-20 06:29:26,020 DEBUG [mqttwarn] Section [sensors/#] matches message on sensors/Dock/Temp. Processing... 2015-05-20 06:29:26,021 DEBUG [mqttwarn] Message on sensors/Dock/Temp going to http:sensors 2015-05-20 06:29:26,021 DEBUG [mqttwarn] New http:sensors' job: sensors/Dock/Temp 2015-05-20 06:29:26,022 DEBUG [mqttwarn] Message on sensors/Dock/Temp going to file:mylog 2015-05-20 06:29:26,022 DEBUG [mqttwarn] Processor is handling:http' for sensors 2015-05-20 06:29:26,022 DEBUG [mqttwarn] New file:mylog' job: sensors/Dock/Temp 2015-05-20 06:29:26,024 DEBUG [http] *** MODULE=services/http.pyc: service=http, target=sensors 2015-05-20 06:29:26,025 DEBUG [mqttwarn] Message on sensors/Dock/Temp going to log:info 2015-05-20 06:29:26,025 DEBUG [http] Parameter id cannot be formatted: 'id' 2015-05-20 06:29:26,026 DEBUG [mqttwarn] Newlog:info' job: sensors/Dock/Temp 2015-05-20 06:29:26,026 WARNING [mqttwarn] Notification of http for sensors/Dock/Temp' FAILED 2015-05-20 06:29:26,027 DEBUG [mqttwarn] Processor is handling:file' for mylog 2015-05-20 06:29:26,029 DEBUG [file] * MODULE=services/file.pyc: service=file, target=mylog 2015-05-20 06:29:26,029 DEBUG [mqttwarn] Processor is handling: `log' for info 2015-05-20 06:29:26,031 DEBUG [log] * MODULE=services/log.pyc: service=log, target=info 2015-05-20 06:29:26,031 INFO [log] {'id' : '60', 'val' : '876'}

With the following excerpt from my mqttwarn.ini configuration...

; name the service providers you will be using. launch = file, log, http

[config:file] append_newline = True targets = { 'mylog' : ['/tmp/mqtt.log'], 'f01' : ['/tmp/f.01'], 'log-me' : ['/tmp/log.me'], 'mqttwarn' : ['/tmp/mqttwarn.err'], }

[config:log]

targets = { 'info' : [ 'info' ], 'warn' : [ 'warn' ], 'crit' : [ 'crit' ], 'error' : [ 'error' ], 'full' : [ 'full' ] }

[config:http] timeout = 5 targets = { 'sensors' : ['get', "http://192.168.1.20/SaveIfbyIdDebug.php?", {'id' : '{id}', 'val' : '{val}'}, None] }

[sensors/#] targets = http:sensors, file:mylog, log:info


From: JP Mens [mailto:notifications@github.com] Sent: Wednesday, May 20, 2015 1:38 AM To: jpmens/mqttwarn Cc: GitBeagle Subject: Re: [mqttwarn] Syntax for HTTP service (#132)

Have you tried

{'id' : '{id}', 'val' : '{val}'}

The documentation for the http service says:

Note that transforms in parameters must be quoted strings

Reply to this email directly or view https://github.com/jpmens/mqttwarn/issues/132#issuecomment-103766628 it on GitHub. https://github.com/notifications/beacon/ACm8dBJVBE2GOEdPHZYzGnGrJU8Myb1Iks5 oLBU5gaJpZM4Eg5nx.gif

jpmens commented 9 years ago

OK, the good news is that this works fine. I think the only issue you're having is that you're not publishing JSON! (http://jsonlint.com is a good site for testing JSON validity). I noticed because you have single quotes around your JSON elements, and JSON wants double quotes.

To prove my point, here's the configuration I used:

mqttwarn.ini

[defaults]
hostname  = 'localhost'  ; default
port      = 1883
skipretained = True
cleansession = True
clientid = mqttwarn-2-tiggr
protocol     = 4

logformat = '%(asctime)-15s %(levelname)-5s [%(module)s] %(message)s'
logfile   = 'mqttwarn.log'

loglevel     = DEBUG
launch   = log, http

[config:http]
timeout = 60
targets = {
   'sensors'  : [ "get",  "http://localhost/~jpm/mqttwarn.php",  { 'id' : '{id}', 'val' : '{val}' },   None  ],
  }

[config:log]
targets = {
    'info'   : [ 'info' ],
  }

[sensors/#]
targets = log:info, http:sensors

mqttwarn.php

<?php
    $out = print_r($_GET, true);
    file_put_contents("/tmp/get", $out);
?>
OK

publish

mosquitto_pub -t sensors/1 -m '{ "id" : 60, "val" : 876 }'

output /tmp/get

Array
(
    [id] => 60
    [val] => 876
)
GitBeagle commented 9 years ago

Thank you! I figured it would be something simple I was doing wrong but I couldn't see what I was missing. I guess I need to read up on JSON. I have seen many references but failed to investigate enough details. Publishing in the correct format successfully stored 876 in ID 60 in my mysql database. I know I should be using post vs.. get but that's a task for another day...

I'm starting down the path of converting my JeeNode RFM12 sensor network to RFM69 and using MQTT. Your mqttwarn Is a key element in knitting things together. Thanks again.

John


From: JP Mens [mailto:notifications@github.com] Sent: Wednesday, May 20, 2015 9:59 AM To: jpmens/mqttwarn Cc: GitBeagle Subject: Re: [mqttwarn] Syntax for HTTP service (#132)

OK, the good news is that this works fine. I think the only issue you're having is that you're not publishing JSON! (http://jsonlint.com is a good site for testing JSON validity). I noticed because you have single quotes around your JSON elements, and JSON wants double quotes.

To prove my point, here's the configuration I used:

mqttwarn.ini

[defaults]

hostname = 'localhost' ; default

port = 1883

skipretained = True

cleansession = True

clientid = mqttwarn-2-tiggr

protocol = 4

logformat = '%(asctime)-15s %(levelname)-5s [%(module)s] %(message)s'

logfile = 'mqttwarn.log'

loglevel = DEBUG

launch = log, http

[config:http]

timeout = 60

targets = {

'sensors' : [ "get", "http://localhost/~jpm/mqttwarn.php", { 'id' : '{id}', 'val' : '{val}' }, None ],

}

[config:log]

targets = {

'info'   : [ 'info' ],

}

[sensors/#]

targets = log:info, http:sensors

mqttwarn.php

<?php

$out = print_r($_GET, true);

file_put_contents("/tmp/get", $out);

?>

OK

publish

mosquitto_pub -t sensors/1 -m '{ "id" : 60, "val" : 876 }'

output /tmp/get

Array

(

[id] => 60

[val] => 876

)

Reply to this email directly or view https://github.com/jpmens/mqttwarn/issues/132#issuecomment-103896656 it on GitHub. https://github.com/notifications/beacon/ACm8dNUC8xOguABVYLdo_1OPYjfNmXbiks5 oLIqJgaJpZM4Eg5nx.gif

jpmens commented 9 years ago

Glad it works. (If we ever meet, you owe me a 'beverage' for that. ;-)