Merge branch 'mqtt-refactoring' of ch1p.io:homekit into mqtt-refactoring

This commit is contained in:
Evgeny Zinoviev 2023-06-05 19:51:20 +03:00
commit b56b2125be
18 changed files with 38660 additions and 479 deletions

View File

@ -1,4 +1,4 @@
Debian packages: Debian packages:
``` ```
apt-get install git cmake build-essential python3-dev python3-wheel python3-pip python3-build python3-yaml python3-toml python3-psutil python3-aiohttp python3-requests python3-apscheduler python3-smbus apt-get install git cmake build-essential python3-dev python3-wheel python3-pip python3-build python3-yaml python3-toml python3-psutil python3-aiohttp python3-requests python3-apscheduler python3-smbus traceroute tcpdump
``` ```

View File

@ -54,6 +54,9 @@ return [
'polyfills.js' => 1, 'polyfills.js' => 1,
'modem.js' => 2, 'modem.js' => 2,
'inverter.js' => 2, 'inverter.js' => 2,
'h265webjs-dist/h265webjs-v20221106.js' => 3,
'h265webjs-dist/h265webjs-v20221106-reminified.js' => 1,
'h265webjs-dist/missile.js' => 1,
], ],
'cam_hls_access_key' => '', 'cam_hls_access_key' => '',

View File

@ -112,7 +112,7 @@ class MiscHandler extends RequestHandler
if ($include_h265) { if ($include_h265) {
$js_config['h265webjsConfig'] = $js_h265webjs_config; $js_config['h265webjsConfig'] = $js_h265webjs_config;
$this->tpl->add_static('h265webjs-dist/missile.js'); $this->tpl->add_static('h265webjs-dist/missile.js');
$this->tpl->add_static('h265webjs-dist/h265webjs-v20221106.js'); $this->tpl->add_static('h265webjs-dist/h265webjs-v20221106-reminified.js');
} }
$js_config['camsByType'] = $cams_by_type; $js_config['camsByType'] = $cams_by_type;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -4,6 +4,11 @@
<title>{{ title }}</title> <title>{{ title }}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script>
window.onerror = function(error) {
window.console && console.error(error);
}
</script>
{{ static|raw }} {{ static|raw }}
</head> </head>
<body> <body>

View File

@ -0,0 +1,21 @@
#!/bin/sh
tables="mts-azov rt-azov mts-il"
net=
case "$ACTION" in
ifup)
case "$INTERFACE" in
eth2)
net=192.168.7
;;
eth3)
net=192.168.8
;;
esac
if [ -z "$net" ]; then exit; fi
for t in $tables; do
ip r add ${net}.0/24 via ${net}.1 table $t
done
;;
esac

70
misc/openwrt/etc/rc.local Normal file
View File

@ -0,0 +1,70 @@
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
TABLES="mts-azov rt-azov mts-il"
# create ip sets
for _table in $TABLES; do
ipset create $_table hash:net
done
# add untrusted cameras set
ipset create ipcam hash:net
for addr in $(seq 21 69); do
ipset add ipcam 192.168.5.${addr}
done
sleep 0.1
# block internet access for untrusted cameras
iptables -I FORWARD 1 -m set --match-set ipcam src ! -d 192.168.5.0/24 -j REJECT
# add some default routing rules
ipset add mts-azov 192.168.5.0/24 # everybody
ipset add mts-azov 192.168.5.163 # cs1
ipset add mts-azov 192.168.5.212 # cs2
ipset add mts-azov 192.168.5.161 # cs3
ipset add rt-azov 192.168.5.133 # roof2
ipset add rt-azov 192.168.5.115 # room
ipset add rt-azov 192.168.5.170 # room
ipset add mts-il 192.168.5.120 # inv
ipset add mts-il 192.168.5.223 # inv
ipset add mts-il 192.168.5.143 # roof1
# create rules
ip rule add fwmark 100 table mts-azov
ip rule add fwmark 101 table rt-azov
ip rule add fwmark 102 table mts-il
# set default route for each custom routing table
ip route add default via 192.168.7.1 table mts-azov
ip route add default via 192.168.8.1 table rt-azov
ip route add default via 192.168.88.1 table mts-il # via mikrotik
# fix local routes
for _table in $TABLES; do
ip route add 192.168.5.0/24 via 192.168.5.1 table $_table
ip route add 192.168.6.0/24 via 192.168.88.1 table $_table
ip route add 192.168.7.0/24 via 192.168.7.1 table $_table
ip route add 192.168.8.0/24 via 192.168.8.1 table $_table
ip route add 192.168.88.0/24 via 192.168.88.1 table $_table
done
# iptables rules (see also /etc/firewall.user)
sleep 0.5
# pass already-marked packets
iptables -t mangle -A PREROUTING -m mark ! --mark 0x0 -j ACCEPT
iptables -t mangle -A PREROUTING -m set --match-set mts-azov src -j MARK --set-mark 0x64
iptables -t mangle -A OUTPUT -m set --match-set mts-azov src -j MARK --set-mark 0x64
iptables -t mangle -A PREROUTING -m set --match-set mts-il src -j MARK --set-mark 0x66
iptables -t mangle -A OUTPUT -m set --match-set mts-il src -j MARK --set-mark 0x66
iptables -t mangle -A PREROUTING -m set --match-set rt-azov src -j MARK --set-mark 0x65
iptables -t mangle -A OUTPUT -m set --match-set rt-azov src -j MARK --set-mark 0x65
exit 0

View File

@ -0,0 +1,7 @@
#!/bin/bash
for f in $(ls /etc/ipcam_capture.conf.d/ | xargs); do
camera="${f/.conf/}"
echo "restarting $camera"
systemctl restart ipcam_capture@${camera}
done

View File

@ -0,0 +1,8 @@
#!/bin/bash
cd /etc/ipcam_rtsp2hls.conf.d/
for f in *-low.conf; do
f=${f/-low.conf/}
echo "restarting $f"
systemctl restart ipcam_rtsp2hls@${f}
systemctl restart ipcam_rtsp2hls@${f}-low
done

View File

@ -0,0 +1,38 @@
#!/bin/bash
set -x
set -e
get_default_iface() {
ip -4 r show default | awk '{print $5}'
}
declare -A UPSTREAMS=(
[mtsil]=102
[mtsazov]=100
[rtazov]=101
)
for name in "${!UPSTREAMS[@]}"; do
mark=${UPSTREAMS[$name]}
veth_addr=10.${mark}.1.1
vpeer_addr=10.${mark}.1.2
veth_if=veth${name}
vpeer_if=vpeer${name}
ip netns add $name
ip link add $veth_if type veth peer name $vpeer_if
ip link set $vpeer_if netns $name
ip addr add $veth_addr/24 dev $veth_if
ip link set $veth_if up
ip netns exec $name ip addr add $vpeer_addr/24 dev $vpeer_if
ip netns exec $name ip link set $vpeer_if up
ip netns exec $name ip link set lo up
ip netns exec $name ip route add default via $veth_addr
iptables -t mangle -A PREROUTING -s $vpeer_addr/24 -j MARK --set-mark $mark
iptables -t nat -A POSTROUTING -s $vpeer_addr/24 -o "$(get_default_iface)" -j MASQUERADE
done
sysctl net.ipv4.ip_forward=1

View File

@ -30,6 +30,7 @@ SensorData Si7021::read() {
Wire.requestFrom(dev_addr, 2); Wire.requestFrom(dev_addr, 2);
if (Wire.available() < 2) { if (Wire.available() < 2) {
PRINTLN("Si7021: 0xf3: could not read 2 bytes"); PRINTLN("Si7021: 0xf3: could not read 2 bytes");
error = 1;
} }
uint16_t temp_raw = Wire.read() << 8 | Wire.read(); uint16_t temp_raw = Wire.read() << 8 | Wire.read();
double temperature = ((175.72 * temp_raw) / 65536.0) - 46.85; double temperature = ((175.72 * temp_raw) / 65536.0) - 46.85;
@ -38,6 +39,7 @@ SensorData Si7021::read() {
Wire.requestFrom(dev_addr, 2); Wire.requestFrom(dev_addr, 2);
if (Wire.available() < 2) { if (Wire.available() < 2) {
PRINTLN("Si7021: 0xf5: could not read 2 bytes"); PRINTLN("Si7021: 0xf5: could not read 2 bytes");
error = 1;
} }
uint16_t hum_raw = Wire.read() << 8 | Wire.read(); uint16_t hum_raw = Wire.read() << 8 | Wire.read();
double humidity = ((125.0 * hum_raw) / 65536.0) - 6.0; double humidity = ((125.0 * hum_raw) / 65536.0) - 6.0;
@ -84,4 +86,4 @@ end:
return sd; return sd;
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "homekit_temphum", "name": "homekit_temphum",
"version": "1.0.2", "version": "1.0.3",
"build": { "build": {
"flags": "-I../../include" "flags": "-I../../include"
} }

View File

@ -4,10 +4,12 @@ After=network-online.target
[Service] [Service]
Restart=always Restart=always
RestartSec=3
User=user User=user
Group=user Group=user
EnvironmentFile=/etc/ipcam_capture.conf.d/%i.conf EnvironmentFile=/etc/ipcam_capture.conf.d/%i.conf
ExecStart=/home/user/homekit/tools/ipcam_capture.sh --outdir $OUTDIR --creds $CREDS --ip $IP --port $PORT $ARGS ExecStart=/home/user/homekit/tools/ipcam_capture.sh --outdir $OUTDIR --creds $CREDS --ip $IP --port $PORT $ARGS
Restart=always
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -4,6 +4,7 @@ After=network-online.target
[Service] [Service]
Restart=always Restart=always
RestartSec=3
User=user User=user
Group=user Group=user
EnvironmentFile=/etc/ipcam_rtsp2hls.conf.d/%i.conf EnvironmentFile=/etc/ipcam_rtsp2hls.conf.d/%i.conf

View File

@ -5,7 +5,8 @@ After=network-online.target
[Service] [Service]
User=user User=user
Group=user Group=user
Restart=on-failure Restart=always
RestartSec=10
ExecStart=/home/user/homekit/src/ipcam_server.py ExecStart=/home/user/homekit/src/ipcam_server.py
WorkingDirectory=/home/user WorkingDirectory=/home/user

View File

@ -8,14 +8,15 @@ DEBUG=0
CHANNEL=1 CHANNEL=1
FORCE_UDP=0 FORCE_UDP=0
FORCE_TCP=0 FORCE_TCP=0
EXTENSION="mp4"
die() { die() {
echo >&2 "error: $@" echo >&2 "error: $@"
exit 1 exit 1
} }
usage() { usage() {
cat <<EOF cat <<EOF
usage: $PROGNAME [OPTIONS] COMMAND usage: $PROGNAME [OPTIONS] COMMAND
Options: Options:
@ -29,54 +30,62 @@ Options:
--channel 1|2 --channel 1|2
EOF EOF
exit exit
} }
validate_channel() { validate_channel() {
local c="$1" local c="$1"
case "$c" in case "$c" in
1|2) 1 | 2)
: :
;; ;;
*) *)
die "Invalid channel" die "Invalid channel"
;; ;;
esac esac
} }
[ -z "$1" ] && usage [ -z "$1" ] && usage
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
--ip|--port|--creds|--outdir) --ip | --port | --creds | --outdir)
_var=${1:2} _var=${1:2}
_var=${_var^^} _var=${_var^^}
printf -v "$_var" '%s' "$2" printf -v "$_var" '%s' "$2"
shift shift
;; ;;
--debug) --debug)
DEBUG=1 DEBUG=1
;; ;;
--force-tcp) --force-tcp)
FORCE_TCP=1 FORCE_TCP=1
;; ;;
--force-udp) --force-udp)
FORCE_UDP=1 FORCE_UDP=1
;; ;;
--channel) --channel)
CHANNEL="$2" CHANNEL="$2"
shift shift
;; ;;
*) --mov)
die "Unrecognized argument: $1" EXTENSION="mov"
;; ;;
esac
shift --mpv)
EXTENSION="mpv"
;;
*)
die "Unrecognized argument: $1"
;;
esac
shift
done done
[ -z "$OUTDIR" ] && die "You must specify output directory (--outdir)." [ -z "$OUTDIR" ] && die "You must specify output directory (--outdir)."
@ -86,25 +95,25 @@ done
validate_channel "$CHANNEL" validate_channel "$CHANNEL"
if [ ! -d "${OUTDIR}" ]; then if [ ! -d "${OUTDIR}" ]; then
mkdir "${OUTDIR}" || die "Failed to create ${OUTDIR}/${NAME}!" mkdir "${OUTDIR}" || die "Failed to create ${OUTDIR}/${NAME}!"
echo "Created $OUTDIR." echo "Created $OUTDIR."
fi fi
args= args=
if [ "$DEBUG" = "1" ]; then if [ "$DEBUG" = "1" ]; then
args="$args -v info" args="$args -v info"
else else
args="$args -nostats -loglevel warning" args="$args -nostats -loglevel warning"
fi fi
if [ "$FORCE_TCP" = "1" ]; then if [ "$FORCE_TCP" = "1" ]; then
args="$args -rtsp_transport tcp" args="$args -rtsp_transport tcp"
elif [ "$FORCE_UDP" = "1" ]; then elif [ "$FORCE_UDP" = "1" ]; then
args="$args -rtsp_transport udp" args="$args -rtsp_transport udp"
fi fi
[ ! -z "$CREDS" ] && CREDS="${CREDS}@" [ ! -z "$CREDS" ] && CREDS="${CREDS}@"
ffmpeg $args -i rtsp://${CREDS}${IP}:${PORT}/Streaming/Channels/${CHANNEL} \ ffmpeg $args -i rtsp://${CREDS}${IP}:${PORT}/Streaming/Channels/${CHANNEL} \
-c copy -f segment -strftime 1 -segment_time 00:10:00 -segment_atclocktime 1 \ -c copy -f segment -strftime 1 -segment_time 00:10:00 -segment_atclocktime 1 \
"$OUTDIR/record_%Y-%m-%d-%H.%M.%S.mp4" "$OUTDIR/record_%Y-%m-%d-%H.%M.%S.${EXTENSION}"