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.
 
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 ....
 
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 ..
 
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
 

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.

Similar threads

Hello, I’m looking for someone able to write script for Syno DSM having good knowledge of DSM systems...
Replies
0
Views
821
Jeyare, You bring up several good points, plus you introduce BTRFS in to the mix. I will tell you honestly...
Replies
10
Views
4,944
Saves the results of the successful processing of shared folder synchronization to a DB file through the...
Replies
0
Views
2,457
A simple trick to adjust the scheduled run time of a script is to use the unix sleep command as the top of...
Replies
0
Views
3,155
Thanks ! I overlooked the Triggered Task option
Replies
6
Views
5,511

Welcome to SynoForum.com!

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

Registration is free, easy and fast!

Back
Top