Programmatically changing light configuration with reef-pi

themericks

Community Member
View Badges
Joined
Sep 14, 2020
Messages
89
Reaction score
54
Location
Bay Area
Rating - 0%
0   0   0
Hi all,

I'm trying to modify my light configuration using the reef-pi HTTP API. I am able to authenticate and read data as shown here. The trouble starts when I try to use the command
Code:
curl -X POST -b cookie.txt -d '{"id": "11", "channels":{"0":{"profile":{"config":{"start":"13:00:00"}}}}}' -D headers.txt http://reefpi.local/api/lights

to modify the light start time. The command executes with no apparent error, but when I check the reef-pi GUI the light start time has not changed. Additionally, when I check the headers.txt file it says HTTP/1.1 405 Method Not Allowed. My HTTP knowledge is rather limited, but I've gathered this means that the server accepts the POST method but it is not supported by the api/lights resource.

Am I doing something wrong or is there a bug?
 

SuncrestReef

That Apex guy
View Badges
Joined
Jan 18, 2018
Messages
4,214
Reaction score
9,227
Location
Oregon
Rating - 0%
0   0   0
Edit: Sorry for my previous reply, I misread the subject, but couldn't delete my incorrect reply.
 

Ranjib

7500 Club Member
View Badges
Joined
Apr 16, 2016
Messages
9,843
Reaction score
17,058
Location
Pleasant Hill, Concord
Rating - 0%
0   0   0
Hi there
Sorry for responding late, that hash tag didn’t notify me :-/ , you have to use @ .
Coming back to the original question , the curl command should not fail since the http request is successful. The 405 error code on the other hand indicates reef-pi is rejecting the request. This is because you are not specifying the light of in url, Try addding /11 at the end of the url .
That is .. curl http://reef-pi.local/api/lights/11

Rest looks good. You want to specify the entire config, not just start time, else other details will be reset.
 
OP
OP
themericks

themericks

Community Member
View Badges
Joined
Sep 14, 2020
Messages
89
Reaction score
54
Location
Bay Area
Rating - 0%
0   0   0
Thanks, that worked! So every time I want to change one light parameter, I have to specify all light parameters? I was hoping there was a more concise way to just update specific parameters, but if not I'll work with it. Thanks again!
 
OP
OP
themericks

themericks

Community Member
View Badges
Joined
Sep 14, 2020
Messages
89
Reaction score
54
Location
Bay Area
Rating - 0%
0   0   0
Another issue came up, and since it is related to the same topic I decided to post here and not open a new thread.

I switched over to using the python requests library instead of command line curl (since the rest of my project will be written in python). I've got the following code working, which gets the light configuration, changes the first value of the interval curve for channel 1 to 100, and then posts the new light configuration. This returns HTTP status code 200 and I can confirm that the value changes in the reef-pi GUI.

Code:
import requests, cookielib, json

cookies = cookielib.MozillaCookieJar("/home/pi/cookie.txt")
cookies.load()

r = requests.get('http://reefpi.local/api/lights', cookies=cookies)

light_params = r.json()

id = light_params[0]['id']

light_params[0]['channels']['1']['profile']['config']['values'][0] = 100

r = requests.post('http://reefpi.local/api/lights/{0}'.format(id), json=light_params[0], cookies=cookies)

print(r.status_code)

However, when I try to change the light start time instead, it does not work. The following code returns HTTP status code 500 and the light start time does not change in the reef-pi GUI.

Code:
import requests, cookielib, json

cookies = cookielib.MozillaCookieJar("/home/pi/cookie.txt")
cookies.load()

r = requests.get('http://reefpi.local/api/lights', cookies=cookies)

light_params = r.json()

id = light_params[0]['id']

light_params[0]['channels']['1']['profile']['config']['start'] = u'12:00:00'

r = requests.post('http://reefpi.local/api/lights/{0}'.format(id), json=light_params[0], cookies=cookies)

print(r.status_code)

I've also tried using just '12:00:00' and r'12:00:00' for the start time value, but I always get return status 500 and no change. Any idea what the issue could be?
 
OP
OP
themericks

themericks

Community Member
View Badges
Joined
Sep 14, 2020
Messages
89
Reaction score
54
Location
Bay Area
Rating - 0%
0   0   0
For those interested, I figured it out. When changing the start time/end time, you must also change the interval parameter. This sets the time spacing of the points in the light profile. The post will fail if the interval value is not compatible with the start time, end time and number of control points. I had to calculate the new interval myself, as shown in the code below. This code returns HTTP status code 200 and I can verify that the start time and interval change appropriately in the reef-pi GUI.

Code:
import requests, cookielib, json, datetime

cookies = cookielib.MozillaCookieJar("/home/pi/cookie.txt")
cookies.load()

r = requests.get('http://reefpi.local/api/lights', cookies=cookies)

light_params = r.json()

id = light_params[0]['id']

end_time_old = datetime.datetime.strptime(light_params[0]['channels']['1']['profile']['config']['end'], '%H:%M:%S')
start_time_new = datetime.datetime(1900, 1, 1, 12, 0, 0)
num_points = len(light_params[0]['channels']['1']['profile']['config']['values'])
delta_time_new = (end_time_old - start_time_new).total_seconds()
interval_new = delta_time_new / (num_points - 1)

light_params[0]['channels']['1']['profile']['config']['start'] = start_time_new.strftime('%H:%M:%S')
light_params[0]['channels']['1']['profile']['config']['interval'] = interval_new

r = requests.post('http://reefpi.local/api/lights/{0}'.format(id), json=light_params[0], cookies=cookies)

print(r.status_code, ' : ', r.reason)
 

Tentacled trailblazer in your tank: Have you ever kept a large starfish?

  • I currently have a starfish in my tank.

    Votes: 27 30.3%
  • Not currently, but I have kept a starfish in the past.

    Votes: 21 23.6%
  • I have never kept a starfish, but I hope to in the future.

    Votes: 22 24.7%
  • I have no plans to keep a starfish.

    Votes: 19 21.3%
  • Other.

    Votes: 0 0.0%
Back
Top