Part 2 for SwatchDog Automation, now that we have Swatchdog installed and looking for certain expressions real time in the log we can do many things, we can log stuff, execute commands, send email and LOTS more.
For my use case I want to turn an outlet off and send an email notification so lets look and see how we would do that.
@Ranjib as usual has done a great job on the coding side and has provided a restful api integration, basically everything that you can do via the web interface can be done through the api, so lets dive into that. I don't think many realize the power they have if they need it to do something outside the UI and it may be over many head but if your comfortable on cli interface it allows you to leverage your reef-pi in many ways.
Here is the first site that will get you going -
https://reef-pi.github.io/additional-documentation/open-api/ I actually do not know if this is the latest api documentation but its a good place to start. Here is the full api documentation for reef-pi -
https://reef-pi.github.io/api.html
As the first site describes, using the api requires authentication like you logging into the web interface and that requires a session cookie/token. Once you have that session token you need to guard it and ensure you keep it somewhere that only your userid and root has access to since with that token anyone can also do stuff to your reef-pi instance, lol.
Lets brake down getting that session cookie:
Code:
curl -X POST -c cookie.txt -d '{"user":"reef-pi", "password":"reef-pi"}' http://reef-pi.local/auth/signin
I think most is self explanatory, you need to provide your username and password and the other is you need to provide the destination IP or hostname (reef-pi.local). Now that command will work if you are not using TLS (SSL) for your instance, if you are using TLS then that command will fail due to the self signed cert, to bypass the cert here is the command:
Code:
curl -X POST -c cookie.txt -d '{"user":"reef-pi", "password":"reef-pi"}' -k https://reef-pi.local/auth/signin
Once you have completed that command that should download/create your session cookie in the directory that the command was issued from, again note the permissions on the file and make sure world cannot access that file. something like this etc
That will remove the world permissions, yeah it's being paranoid but still should do it.
Now that you have the session cookie the world is your oyster via the reef-pi api, the second example shows how to get the output of all the equipment defined:
Code:
curl -b cookie.txt http://reef-pi.local/api/equipment
Here is the output of that command for me:
Looks messy but if you look closely you will see the details and the individual sections for each piece of equipment that you have defined, for instance for me I name my outlets (I am using a dj power strip so I name them dj1, dj2...plus a name. So here are the details for one of my outlets:
Code:
{"id":"10","name":"dj1","outlet":"10","on":false,"stay_off_on_boot":true}
That controls my dj outlet 1, its reef-pi id is 10 and currently its off (on:false) and its configured to stay off on boot. pretty cool stuff. Now that you know what id you want to do stuff you can go to that second api documentation and do anything that you want -
https://reef-pi.github.io/api.html#operation/equipmentControl for me I want to control a piece of equipment so to turn it off/on I can issue this command:
Code:
curl -b /home/bishop/reefpi-api/cookie.txt -d '{"on":false}' -k https://atollpi/api/equipment/15/control
Keep in mind when testing to use an outlet that your not currently not open, I put in a night light that i could turn on/off so I could see results. This is basically different from the initial command, you provide the path to the cookie but to control things you need to pass more statements with the -d option. With that option you can control or cahnge any of the parameters for that piece of the equipment, again all of the options are detailed in the api documentation. For my example that would turn the outlet off, if I issued this command it would turn it on:
Code:
curl -b /home/bishop/reefpi-api/cookie.txt -d '{"on":true}' -k https://atollpi/api/equipment/17/control
That pretty much is the primer for the api, its really powerful and allows you to do anything from the cli that you could do with the web interface, part three will be putting it together with swatchdog and allowing you to control equipment etc that way...
