Skip to content

Commit

Permalink
update os script added
Browse files Browse the repository at this point in the history
  • Loading branch information
bertouttier committed Sep 18, 2020
1 parent 17d8dfe commit a8b60a5
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 49 deletions.
110 changes: 110 additions & 0 deletions board/mendeleev/rootfs_overlay/etc/mendeleev/update.lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
FW_LATESTURL=https://api.github.com/repos/area3001/ArduinoMendeleev/releases/latest
OS_LATESTURL=https://api.github.com/repos/area3001/mendeleevos/releases/latest

blinkled() {
cd /sys/class/leds/$1
echo timer > trigger
echo $2 > delay_on
echo $2 > delay_off
}

# Set LED brightness value
setled() {
cd /sys/class/leds/$1
echo none > trigger
echo $2 > brightness
}

# download latest firmware
download_latest()
{
url=$1
dir=$2

mkdir -p ${dir}

if curl -sL --show-error --fail -o ${dir}/latest.json ${url}; then
# success
TAG=`jq -r .tag_name ${dir}/latest.json`

# downoad the release if we don't have it yet
if [ ! -d ${dir}/${TAG} ]; then
logger -s -t "OTA" -p 5 "Downloading tag ${TAG}..."
mkdir -p ${dir}/${TAG}
cd ${dir}/${TAG}
for ASSETURL in $(jq -r '.assets[]|select((.name|startswith("release.bin")) or (.name=="zImage")).browser_download_url' ${dir}/latest.json); do
if curl -sLO --show-error --fail ${ASSETURL}; then
logger -s -t "OTA" -p 5 "downloaded ${ASSETURL}"
else
logger -s -t "OTA" -p 3 "Failed to download ${ASSETURL}!"
blinkled ext_led 100
return 1
fi
done
else
logger -s -t "OTA" -p 4 "Tag ${TAG} already downloaded"
fi
ln -snf ${TAG} ${dir}/latest
else
logger -s -t "OTA" -p 3 "Failed to get json!"
blinkled ext_led 100
return 1
fi
return 0
}

# update kernel
update_os()
{
dir=$1

logger -s -t "OTA" -p 5 "starting OS update..."
if ! download_latest ${OS_LATESTURL} ${dir}; then
logger -s -t "OTA" -p 3 "Failed downloading latest files"
return 1
fi

LATEST_VERSION=$(readlink ${OS_DIR}/latest)
. /etc/os-release

if [ "${LATEST_VERSION}" = "${MENDELEEV_VERSION}" ]; then
logger -s -t "OTA" -p 4 "Version ${LATEST_VERSION} is already installed"
return 1
fi

# apply the release
logger -s -t "OTA" -p 5 "Updating to ${LATEST_VERSION}..."
cd ${OS_DIR}/latest
mount -o remount,rw /boot
cp ${OS_DIR}/latest/zImage /boot/zImage
logger -s -t "OTA" -p 5 "rebooting..."
sync
reboot
exit 0
}

# update arduino
update_fw()
{
dir=$1
host=$2

logger -s -t "OTA" -p 5 "starting arduino update..."
if ! download_latest ${FW_LATESTURL} ${dir}; then
logger -s -t "OTA" -p 3 "Failed downloading latest files"
return 1
fi

# apply the firmware
cd ${FW_DIR}/latest
if sha1sum -sc release.bin.sha1; then
logger -s -t "OTA" -p 5 "Updating arduino to ${TAG}..."
cat ${FW_DIR}/latest/release.bin | mosquitto_pub -h ${HOST} -t mendeleev/255/ota -s
setled ext_led 0
else
logger -s -t "OTA" -p 3 "Sha1 sum is not correct!"
blinkled ext_led 100
return 1
fi
return 0
}
58 changes: 9 additions & 49 deletions board/mendeleev/rootfs_overlay/usr/bin/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,24 @@
# Configuration
BUTTON_KEYCODE="LEFT"
HOST=localhost
UPDATES_DIR=/updates

LATESTURL=https://api.github.com/repos/area3001/ArduinoMendeleev/releases/latest
. /etc/mendeleev/update.lib

# get settings from file in boot partition
if [ -e /boot/settings ]; then
echo "Overriding defaults from /boot/settings"
. /boot/settings
fi

blinkled() {
cd /sys/class/leds/$1
echo timer > trigger
echo $2 > delay_on
echo $2 > delay_off
}

# Set LED brightness value
setled() {
cd /sys/class/leds/$1
echo none > trigger
echo $2 > brightness
}

update() {
echo "starting update..." >> /updates/log
if curl -L --show-error --fail -o /updates/latest.json ${LATESTURL}; then
# success
TAG=`jq -r .tag_name /updates/latest.json`
echo "starting update to version ${TAG}..." >> /updates/log

if [ ! -d /updates/${TAG} ]; then
mkdir -p /updates/${TAG}
cd /updates/${TAG}
for ASSETURL in $(jq -r '.assets[].browser_download_url' /updates/latest.json); do
if curl -sLO --show-error --fail ${ASSETURL}; then
echo "downloaded ${ASSETURL}..." >> /updates/log
else
echo "Failed to download ${ASSETURL}!" >> /updates/log
blinkled ext_led 100
continue
fi
done
fi
FW_DIR=${UPDATES_DIR}/arduino
OS_DIR=${UPDATES_DIR}/os

cd /updates/${TAG}
if sha1sum -sc release.bin.sha1; then
echo "Updating to ${TAG}..." >> /updates/log
cat /updates/${TAG}/release.bin | mosquitto_pub -h ${HOST} -t mendeleev/255/ota -s
setled ext_led 0
else
echo "Sha1 sum is not correct!" >> /updates/log
blinkled ext_led 100
fi
else
echo "Failed to get json!" >> /updates/log
blinkled ext_led 100
fi
update()
{
logger -s -t "OTA" -p 5 "starting update..."
update_os ${OS_DIR}
update_fw ${FW_DIR} ${HOST}
}

# turn update led off
Expand All @@ -76,7 +37,6 @@ else
KEY_${BUTTON_KEYCODE}) # button
if [ $VALUE == 1 ]; then # is pressed
blinkled ext_led 500
echo -en "\n\n" >> /updates/log
else # is released
update
fi;
Expand Down
1 change: 1 addition & 0 deletions package/pymqtt2mendeleev/S90mqtt2mendeleev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ case "$1" in
;;
restart|reload)
$0 stop
sleep 1
$0 start
;;
*)
Expand Down
2 changes: 2 additions & 0 deletions package/pymqtt2mendeleev/mqtt2mendeleev.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import asyncio
import sys
import os
import argparse

from mendeleev import MendeleevProtocol
Expand All @@ -14,6 +15,7 @@

def update():
logger.info("update")
os.system("/bin/sh /usr/bin/update.sh 1")

def sensor_test():
logger.info("sensor test")
Expand Down

0 comments on commit a8b60a5

Please sign in to comment.