Skip to content

Commit

Permalink
v46 (#113)
Browse files Browse the repository at this point in the history
* refactor APIs and change URLs
* update document and js for api refactor
* change build script to ignore embed dirname
* delete unused script
* Add web update feature
* web updater on esp8266 is not supported
* Update release shellscript
* add setupmode notice
* fix backup api URL
  • Loading branch information
yakumo-saki authored Dec 31, 2022
1 parent 5fd787a commit c53421a
Show file tree
Hide file tree
Showing 69 changed files with 1,513 additions and 1,123 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ indent_style = space
indent_size = 4
indent_style = space

[*.{js}]
indent_size = 4
indent_style = tab

[*.{yml,yaml}]
indent_size = 2
indent_style = space
Expand Down
35 changes: 28 additions & 7 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "CodeQL"

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
branches: [ main ]
schedule:
- cron: '25 10 * * 6'

Expand All @@ -35,22 +35,43 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: generate embedded resources
run: ls -la

- name: generate embedded resources
run: python build_script/create_embed.py

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install PlatformIO Core
run: pip install --upgrade platformio

- name: Build PlatformIO Project
run: pio run

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# - name: Autobuild
# uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -64,4 +85,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
# embed is auto generated directory.
include/embed/**

# temporary place files when develop web ui
embed/static/**

_release
_release/**

Expand Down
49 changes: 23 additions & 26 deletions build_script/create_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def readall(path):
return data


def create_name_from_filename(filename):
def create_name_from_filename(path):
filename = os.path.basename(path)
ret = filename
ret = ret.replace(".", "_")
ret = ret.replace(" ", "_")
Expand All @@ -31,12 +32,6 @@ def create_name_from_filename(filename):
print(f"OUT_DIR = {OUT_DIR}")
print("=" * SEP_LENGTH)

# access to global build environment
#print(env.Dump())

# access to project build environment (is used source files in "src" folder)
#print(projenv.Dump())

#
# Script to build cpp file from embed directory
#
Expand All @@ -49,31 +44,33 @@ def create_name_from_filename(filename):
os.mkdir(OUT_DIR)
template = readall(TEMPLATE_FILE)

for file in pathlib.Path(EMBED_DIR).iterdir():
#print(file.name)
if file.is_dir():
print(f"ignored sub directory {file.name}")
continue
for root, dirs, files in os.walk(EMBED_DIR):

if file.name.endswith(".md") or file.name.endswith(".sh"):
print(f"ignored markdown file {file.name}")
continue
for filename in files:
print(root, filename)
file = os.path.join(root, filename)

# start generating source
output = template
data = readall(file)
if file.endswith(".md") or file.endswith(".sh"):
print(f"ignored markdown file {file.name}")
continue

output_name = create_name_from_filename(file.name)
# start generating source
output = template
data = readall(file)

# 変数名部分を置換。 変数名は全部大文字とする
output = output.replace("$$REPLACE_NAME$$", output_name.upper()).replace("$$REPLACE_CONTENT$$", data)
output_cpp_path = os.path.join(OUT_DIR, output_name + ".h")
output_name = create_name_from_filename(file)

f = open(output_cpp_path, 'w')
data = f.write(output)
f.close()
# 変数名部分を置換。 変数名は全部大文字とする
output = output.replace("$$REPLACE_NAME$$", output_name.upper()).replace("$$REPLACE_CONTENT$$", data)
output_cpp_path = os.path.join(OUT_DIR, output_name + ".h")

# todo ファイル名重複チェック。すでにあったらコケる

f = open(output_cpp_path, 'w')
data = f.write(output)
f.close()

print(f"generated {output_cpp_path}")
print(f"generated {output_cpp_path}")


print("=" * SEP_LENGTH)
Expand Down
101 changes: 54 additions & 47 deletions create_release.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash -eu

MYDIR=$(cd "$(dirname "$0")"; pwd)
RELEASE_BASE_DIR=${MYDIR}/_release/
RELEASE_DIR=${RELEASE_BASE_DIR}/EnvBoyX
RELEASE_BASE_DIR=${MYDIR}/_release
SRC_BUILD_DIR=${MYDIR}/.pio/build
TGT_BUILD_DIR=${RELEASE_DIR}/.pio/build

# SUBs

banner () {
echo ""
Expand All @@ -16,69 +16,76 @@ banner () {
copy_src_to_tgt () {
banner "Copying board build $1"

# ESP32 dev board
mkdir -p ${TGT_BUILD_DIR}/$1
cp -v ${SRC_BUILD_DIR}/$1/*.bin ${TGT_BUILD_DIR}/$1/
# $1 = boardname
cp -v ${SRC_BUILD_DIR}/$1/firmware.bin ${RELEASE_BASE_DIR}/$1_EnvBoyX_$VER$VER_ADDITIONAL.bin
}

banner "Delete source build dir and clean build"
rm -rf ${MYDIR}/.pio
# --------------------
# MAIN
# --------------------
cd ${MYDIR}
pio run

banner "Delete and create build directory"
rm -rf ${RELEASE_BASE_DIR}

mkdir -p ${RELEASE_DIR}

cp ./platformio.ini ${RELEASE_DIR}/

BOARDS=( esp32dev esp12e )

for b in ${BOARDS[@]} ; do
copy_src_to_tgt $b
done

banner "Add 'how to write' to build directory"
cp ${MYDIR}/docs/how_to_write*.md ${RELEASE_DIR}
# -------------------------------------------------------------------------
banner "Get Version"
# -------------------------------------------------------------------------
MAJOR=$(grep " ver =" ./src/global.cpp | sed -r 's/.*"(.*+)".*/\1/')
MINOR=$(grep " minorVer =" ./src/global.cpp | sed -r 's/.*"(.*+)".*/\1/')
VER=$(echo "${MAJOR}.${MINOR}")
echo "Version is ${VER}"

banner "Creating Archive"

rm -f ${MYDIR}/EnvBoyX*.tar.gz

# -------------------------------------------------------------------------
banner "Get Version additional info (debug, branch...)"
# -------------------------------------------------------------------------
# git ブランチからファイル名判定
branch=`git symbolic-ref --short HEAD`
if [ $branch != "main" ]; then
echo "git branch is not master/main!"
echo "Appending branch name"
VER_ADDITIONAL=$branch
fi

# DEBUGビルド判定
set +e
grep -i "DEBUG_BUILD.*TRUE" ${MYDIR}/src/global.cpp
IS_DEBUG=$?

if [ $IS_DEBUG -eq 0 ] ; then
grep -i "const DEBUG_MODE.*true" ${MYDIR}/embed/static/config.js
IS_DEBUG=$?
fi
set -e

if [ $IS_DEBUG -eq 0 ] ; then
echo "** DEBUG_BUILD IS SET TO TRUE"
RELEASE_FILE_PATH=${MYDIR}/EnvBoyX-DEBUG.tar.gz
elif [ $branch = "main" ]; then
RELEASE_FILE_PATH=${MYDIR}/EnvBoyX.tar.gz
else
echo "git branch is not master/main!"
echo "Appending branch name"
RELEASE_FILE_PATH=${MYDIR}/EnvBoyX-$branch.tar.gz

if [ ${#VER_ADDITIONAL} -eq 0 ] ; then
VER_ADDITIONAL=DEBUG
else
VER_ADDITIONAL=$(echo "${VER_ADDITIONAL}-DEBUG")
fi
fi

if [ ${#VER_ADDITIONAL} -gt 0 ] ; then
VER_ADDITIONAL=$(echo "-$VER_ADDITIONAL")
fi

rm -f ${MYDIR}/EnvBoyX*.tar.gz
tar -C ${RELEASE_BASE_DIR} -zcvf ${RELEASE_FILE_PATH} .
# -------------------------------------------------------------------------
banner "Delete and create build directory"
# -------------------------------------------------------------------------
rm -rf ${RELEASE_BASE_DIR}
mkdir -p ${RELEASE_BASE_DIR}

banner "Release archive created ${RELEASE_FILE_PATH}"
# -------------------------------------------------------------------------
banner "Build"
# -------------------------------------------------------------------------

# Warning
BOARDS=( esp32dev esp12e )

echo "To upload, run:"
echo "tar xvf `basename ${RELEASE_FILE_PATH}`"
echo "cd `basename ${RELEASE_FILE_PATH}`"
echo "pio run -t nobuild -t upload --disable-auto-clean"
echo "If you want to specify board, use below."
echo "pio run -e [esp32dev | esp12e] -t nobuild -t upload --disable-auto-clean"
pio run --target clean
pio run --environment esp32dev --environment esp12e

for b in ${BOARDS[@]} ; do
copy_src_to_tgt $b
done

if [ $IS_DEBUG -eq 1 ] ; then
banner "RELEASE VERSION ! Nice work !"
fi
7 changes: 0 additions & 7 deletions devel_writeback.sh

This file was deleted.

55 changes: 42 additions & 13 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ EnvBoyX has http web API.
| 2.0 | GET | / | JSON | sensor value as JSON.|
| 2.0 | GET | /ping | TEXT | returns pong. |
| 39.0 | GET | /stastics | JSON | stastics JSON |
| 3.4 | GET | /brightness | JSON | value=0-255 set display brightness (0-255) 0 means OFF (adHoc, not config) |
| 3.4 | GET | /display | JSON | set display power (1 = ON / 0 = OFF) |
| 40.0 | POST | /goto_setup | JSON | Go to setup mode at next boot |
| 41.0 | POST | /mhz19b/abc | JSON | value=1 ON value=0 OFF |
| 41.0 | POST | /mhz19b/zeroCalibration | TEXT | __DANGER__ Use if you know what you are doing |
| 42.0 | GET | /config | JSON | Get config |
| 42.0 | POST | /config | JSON | Change config |
| 42.0 | POST | /config/commit | JSON | Save config |
| 42.0 | POST | /config/revert | JSON | Revert to last saved config |
| 43.0 | GET | /config/backup | TEXT | Get current running config on curl command line |
| 44.0 | POST | /config/factory-reset | JSON | Delete ALL configs |
| 3.4 | GET | /api/v1/brightness | JSON | value=0-255 set display brightness (0-255) 0 means OFF (adHoc, not config) |
| 3.4 | GET | /api/v1/display | JSON | set display power (1 = ON / 0 = OFF) |
| 41.0 | POST | /api/v1/mhz19b/abc | JSON | value=1 ON value=0 OFF |
| 41.0 | POST | /api/v1/mhz19b/zeroCalibration | TEXT | __DANGER__ use if you know what you are doing. see below |
| 42.0 | GET | /api/v1/config | JSON | Get config |
| 42.0 | POST | /api/v1/config | JSON | Change config |
| 42.0 | POST | /api/v1/config/commit | JSON | Save config |
| 42.0 | POST | /api/v1/config/revert | JSON | Revert to last saved config |
| 43.0 | GET | /api/v1/config/backup | TEXT | Get current running config on curl command line |
| 44.0 | POST | /api/v1/config/factory-reset | JSON | Delete ALL configs |

NOTE: There is no reboot API, because of security reason.

## /config API
## /api/v1/mhz19b/zeroCalibration

This method may break your MHZ19B accuracy.
By datasheet, place mh-z19b in under 400ppm for at least 20 minutes.

## /api/v1/config API

This API changes config (same thing in setup mode)
But some paramters are not changeable.
Expand Down Expand Up @@ -79,7 +84,7 @@ example: co2alerts.warning1.low
#### cURL example

```
curl -s -X POST -d "displayFlip=yes&displayBrightness=50" "http://ebx32.local/config"
curl -s -X POST -d "displayFlip=yes&displayBrightness=50" "http://ebx32.local/api/v1/config"
```

## HTTP Web API cURL examples
Expand All @@ -89,7 +94,8 @@ These are example of calling API
### Simple get API

```
curl http://[envboy IP or mDNShostname.local]/
curl http://[envboy IP or mDNShostname.local]/api/v1/getdata
{"product":"EnvBoyX","uptime":"01:18:02","uptimeMills":4682994,"temparature":"28.60","humidity":"35.00","pressure":"976.81","luminous":"16","luminousIr":"2","co2ppm":"-999","co2ppmAccuracy":"","rssi":-12}
```

Expand All @@ -106,3 +112,26 @@ OK
$ curl -X POST http://[envboy IP or mDNShostname.local]/goto_setup
OK
```

### Appendix API urls until v45

This information is old until v45.

| since | METHOD | ENDPOINT | RETURN | description |
| ------| ------ | ------------- | -------------------------- | -------|
| 2.0 | GET | / | JSON | sensor value as JSON.|
| 2.0 | GET | /ping | TEXT | returns pong. |
| 39.0 | GET | /stastics | JSON | stastics JSON |
| 3.4 | GET | /brightness | JSON | value=0-255 set display brightness (0-255) 0 means OFF (adHoc, not config) |
| 3.4 | GET | /display | JSON | set display power (1 = ON / 0 = OFF) |
| 40.0 | POST | /goto_setup | JSON | Go to setup mode at next boot |
| 41.0 | POST | /mhz19b/abc | JSON | value=1 ON value=0 OFF |
| 41.0 | POST | /mhz19b/zeroCalibration | TEXT | __DANGER__ Use if you know what you are doing |
| 42.0 | GET | /config | JSON | Get config |
| 42.0 | POST | /config | JSON | Change config |
| 42.0 | POST | /config/commit | JSON | Save config |
| 42.0 | POST | /config/revert | JSON | Revert to last saved config |
| 43.0 | GET | /config/backup | TEXT | Get current running config on curl command line |
| 44.0 | POST | /config/factory-reset | JSON | Delete ALL configs |

NOTE: There is no reboot API, because of security reason.
Loading

0 comments on commit c53421a

Please sign in to comment.