Script to change led_brightness_schedule in synoinfo.conf

Currently reading
Script to change led_brightness_schedule in synoinfo.conf

4
2
NAS
DS218+, DS211
Operating system
  1. Windows
Mobile operating system
  1. iOS
I am working on a script that changes the led_brightness_schedule of my NAS, based on the sunrise and sunset times at my location, in the synoinfo.conf file, see code below (feel free to use it)

Code:
#!/bin/bash
#Sunrise and Sunset times by https://sunrise-sunset.org

strindex() { 
  x="${1%%$2*}"
  [[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}

#Check DSM version
DSMVERSION=`cat "/etc/VERSION"`
PRODUCTVERSION=`grep 'productversion="6.2.3"' <<< "$DSMVERSION"`
if [ -z "${PRODUCTVERSION}" ]; then
    >&2 echo 'INVALID DSM VERSION'
    exit 1
fi

cp "/etc/synoinfo.conf" "/volume1/Miura/Scripts/synoinfo.conf.daily.bak"

URL='https://api.sunrise-sunset.org/json?lat=52.3745403&lng=4.89797550561798'
JSON=$(curl -s ${URL})
echo "$JSON" > "/volume1/Miura/Scripts/sunrise-sunset_org.txt"

STATUSOK=`grep '"status":"OK"' <<< "$JSON"`
if [ -z "${STATUSOK}" ]; then
    >&2 echo 'API STATUS NOT OK'
    exit 1
fi

STARTSUNRISE=$(strindex "$JSON" "sunrise")
STARTSUNRISE=$((STARTSUNRISE+10))
SUNRISE=${JSON:STARTSUNRISE:10}

STARTSUNSET=$(strindex "$JSON" "sunset")
STARTSUNSET=$((STARTSUNSET+9))
SUNSET=${JSON:STARTSUNSET:10}

SUNRISE=$(date --date="$SUNRISE" +%H)
SUNSET=$(date --date="$SUNSET" +%H)

MORNING=$(($SUNRISE+1))
EVENING=$((24-$SUNSET-1))
ON=$((24-$MORNING-$EVENING))

for (( i=1; i<=$MORNING; i++ )); do
    CONF1=$CONF1"1"
done

for (( i=1; i<=$ON; i++ )); do
    CONF1=$CONF1"0"
done

for (( i=1; i<=$EVENING; i++ )); do
    CONF1=$CONF1"1"
done

for (( i=1; i<=7; i++ )); do
    CONF=$CONF$CONF1
done

# Check created string length (7 x 24)
if [ ${#CONF} != 168 ]; then
    >&2 echo 'CONFIG LENGTH FAIL'
    exit 1
fi

CONF='led_brightness_schedule="'$CONF'"'

#Currently only testing on a copy of synoinfo.conf
cp "/etc/synoinfo.conf" "/volume1/Miura/Scripts/synoinfo.conf"
sed -i "/led_brightness_schedule/c\\$CONF" '/volume1/Miura/Scripts/synoinfo.conf'

Currently it only makes a copy of the conf file in which the old schedule is replaced with the new sunrise/sunset times. I included some checks (hardcoded DSM version check, api status OK, length of the new schedule should be 168 characters long) to makes sure that the new config that I create is valid.

My question is whether or not I can run such a script safely without corrupting my NAS/Config. Are there things I am overlooking, before taking such a script in production? Of course I am testing it now, to make sure I don't invalidate my config, with the current script.
 

Rusty

Moderator
NAS Support
2,895
879
www.blackvoid.club
NAS
DS412+, DS718+, DS918+, 2x RS3614RPxs+ with expansions
Router
  1. RT1900ac
  2. RT2600ac
  3. MR2200ac
Operating system
  1. macOS
Mobile operating system
  1. iOS
Nice one. Can't answer the question. Would be best not to run this on your one and only machine, but I guess not much can go wrong (or can it)?
 
4
2
NAS
DS218+, DS211
Operating system
  1. Windows
Mobile operating system
  1. iOS
As you maybe have noticed I do make a backup of the conf file before making the change. Besides that I make a weekly backup as well (better safe than sorry?). All those files are included in a daily backup to a secondary NAS with retention. So I am quite sure to be able to retrieve a valid version of the conf file in case of ....
 
4
2
NAS
DS218+, DS211
Operating system
  1. Windows
Mobile operating system
  1. iOS
Look how nice Synology has been to us:
they created a service to get/set the led_brightness config. It's called 'syno_led_brightness'. Who would have thought ..
 

fredbert

Moderator
NAS Support
Subscriber
1,863
759
NAS
DS1520+, DS218+, DS215j
Router
  1. RT2600ac
  2. MR2200ac
Operating system
  1. macOS
Mobile operating system
  1. iOS
So that means your script v2 is a little shorter?
 
4
2
NAS
DS218+, DS211
Operating system
  1. Windows
Mobile operating system
  1. iOS
Definitely! I removed the DSM version check, as well as the copies of synoinfo.conf. My current version looks like:

Bash:
#!/bin/bash
#Sunrise and Sunset times by https://sunrise-sunset.org

LATITUDE='52.3745403'
LONGITUDE='4.89797550561798'

#Function to find the index/location of string 2 in string 1
strindex() {
  x="${1%%$2*}"
  [[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}

URL="https://api.sunrise-sunset.org/json?lat=$LATITUDE&lng=$LONGITUDE"
JSON=$(curl -s ${URL})

STATUSOK=`grep "\"status\":\"OK\"" <<< "$JSON"`
if [ -z "${STATUSOK}" ]; then
    >&2 echo 'API STATUS NOT OK'
    exit 1
fi
echo "$JSON"

STARTSUNRISE=$(strindex "$JSON" "sunrise")
STARTSUNRISE=$((STARTSUNRISE+10))
SUNRISE=${JSON:STARTSUNRISE:10}

STARTSUNSET=$(strindex "$JSON" "sunset")
STARTSUNSET=$((STARTSUNSET+9))
SUNSET=${JSON:STARTSUNSET:10}

SUNRISE=$(date --date="$SUNRISE" +%H)
SUNSET=$(date --date="$SUNSET" +%H)

MORNING=$(($SUNRISE+1))
EVENING=$((24-$SUNSET-1))
ON=$((24-$MORNING-$EVENING))

for (( i=1; i<=$MORNING; i++ )); do
    CONF1=$CONF1"1"
done

for (( i=1; i<=$ON; i++ )); do
    CONF1=$CONF1"0"
done

for (( i=1; i<=$EVENING; i++ )); do
    CONF1=$CONF1"1"
done

for (( i=1; i<=7; i++ )); do
    CONF=$CONF$CONF1
done

# Check created string length (7 x 24)
if [ ${#CONF} != 168 ]; then
    >&2 echo 'CONFIG LENGTH FAIL'
    exit 1
fi

syno_led_brightness --set_config 1 "$CONF"
-- post merged: --

The 'only' thing I am not sure about is 'syno_led_brightness --update [--force]'. The help states:
Update brightness according to configuration if "--force" isn't appened, beightness will not be updated if current schedule is same as last time. It is used to avoid to interrupt hibernation.
However, using the syno_led_brightness --set_config command already changes the brightness of the led's. So I am not sure whether I need this or not
 

fredbert

Moderator
NAS Support
Subscriber
1,863
759
NAS
DS1520+, DS218+, DS215j
Router
  1. RT2600ac
  2. MR2200ac
Operating system
  1. macOS
Mobile operating system
  1. iOS
I find that the lowest brightness level is bright enough for me so my schedule doesn't change: 0700 LEDs on; 2200 LEDs off. The NAS are in the study/office so LEDs are not visible until I go in there.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Welcome to SynoForum.com!

SynoForum.com is an unofficial Synology forum for NAS owners and enthusiasts.

Registration is free, easy and fast!

Similar threads

Similar threads

Top