puh current config pre departure
This commit is contained in:
130
templates/app-service.py.j2
Normal file
130
templates/app-service.py.j2
Normal file
@ -0,0 +1,130 @@
|
||||
import subprocess
|
||||
import requests
|
||||
from lxml import html
|
||||
from flask import Flask, request, jsonify
|
||||
import json
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
def start_service():
|
||||
command = "systemctl start timelapse.service"
|
||||
try:
|
||||
# Run the command using subprocess.run()
|
||||
process = subprocess.Popen(command, shell=True)
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
return {"Error": e.strip('\n')}
|
||||
|
||||
command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2- | cut -b 2-"
|
||||
try:
|
||||
# Run the command using subprocess.run()
|
||||
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
||||
return {"Message": result.stdout.strip('\n')}
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
return {"Error": e.strip('\n')}
|
||||
|
||||
def stop_service():
|
||||
command = "systemctl stop timelapse.service"
|
||||
try:
|
||||
# Run the command using subprocess.run()
|
||||
process = subprocess.Popen(command, shell=True)
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
return {"Error": e.strip('\n')}
|
||||
|
||||
command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2- | cut -b 2-"
|
||||
try:
|
||||
# Run the command using subprocess.run()
|
||||
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
||||
return {"Message": result.stdout.strip('\n')}
|
||||
except subprocess.CalledProcessError as e:
|
||||
return {"Error": e.strip('\n')}
|
||||
|
||||
def service_status():
|
||||
command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2- | cut -b 2-"
|
||||
try:
|
||||
# Run the command using subprocess.run()
|
||||
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
||||
command = "systemctl status timelapse.service | grep Active | cut -d ':' -f 2 | cut -d ' ' -f 2"
|
||||
status = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
|
||||
return {"Message": result.stdout.strip('\n'), "Status": status.stdout.strip('\n')}
|
||||
except subprocess.CalledProcessError as e:
|
||||
return {"Error": e.strip('\n')}
|
||||
|
||||
def read_gps_data_file():
|
||||
# Define the path to your file. This is a relative path example. Adjust it as needed.
|
||||
file_path = '{{ gps_service_directory }}/gps_data'
|
||||
|
||||
if os.path.exists(file_path):
|
||||
with open(file_path, 'r') as file:
|
||||
lines = file.readlines()
|
||||
|
||||
# Assuming the data is in a specific format, you can parse it accordingly
|
||||
lat = lines[0].split(':')[1]
|
||||
lon = lines[1].split(':')[1]
|
||||
speed = lines[2].split(':')[1]
|
||||
|
||||
try:
|
||||
lat = float(lat.strip('\n'))
|
||||
lon = float(lon.strip('\n'))
|
||||
speed = float(speed.strip('\n')) * 0.62
|
||||
except:
|
||||
lat = 0
|
||||
lon = 0
|
||||
speed = 0
|
||||
|
||||
# Round latitude and longitude to 5 decimal places at most
|
||||
lat_rounded = round(lat, 3)
|
||||
lon_rounded = round(lon, 3)
|
||||
speed_rounded = round(speed, 1)
|
||||
data = {
|
||||
"lat": lat_rounded,
|
||||
"lon": lon_rounded,
|
||||
"speed": speed_rounded
|
||||
}
|
||||
else:
|
||||
data = {
|
||||
"lat": 37.548,
|
||||
"lon": -121.961,
|
||||
"speed": 0
|
||||
}
|
||||
|
||||
return jsonify(data)
|
||||
|
||||
@app.route('/return_gps', methods=['GET'])
|
||||
def return_gps():
|
||||
return read_gps_data_file()
|
||||
|
||||
@app.route('/start', methods=['GET'])
|
||||
def start():
|
||||
try:
|
||||
return jsonify(start_service())
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
return jsonify({'error': e}), 400
|
||||
|
||||
@app.route('/stop', methods=['GET'])
|
||||
def stop():
|
||||
try:
|
||||
return jsonify(stop_service())
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
return jsonify({'error': e}), 400
|
||||
|
||||
@app.route('/status', methods=['GET'])
|
||||
def status():
|
||||
try:
|
||||
return jsonify(service_status())
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
return jsonify({'error': e}), 400
|
||||
|
||||
@app.route('/test', methods=['GET'])
|
||||
def test():
|
||||
return jsonify({'message': 'Hello there'})
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
|
||||
@ -5,5 +5,6 @@ rm $RUN_FILE
|
||||
rm $WORKING_DIR/*temp*.jpg
|
||||
|
||||
# create timelapse
|
||||
/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" -vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4 &
|
||||
echo "Creating Timelapse"
|
||||
/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" -vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
date_default_timezone_set('America/Los_Angeles');
|
||||
# set default values
|
||||
$status = "Unchecked";
|
||||
$message = "No attempt made yet.";
|
||||
@ -8,7 +9,7 @@ $button_recent = false;
|
||||
$button_action = "";
|
||||
$button_result = "";
|
||||
$http_host = $_SERVER['HTTP_HOST'];
|
||||
|
||||
$run_file = "{{ gps_service_directory }}/gps_data";
|
||||
$debug_string = "";
|
||||
|
||||
if (isset($_GET['action'])) {
|
||||
@ -38,8 +39,11 @@ function runAPI($submitted_status) {
|
||||
// API URL
|
||||
$debug_string = $debug_string."After switch, apiUrl is ".$apiUrl."<br>";
|
||||
// Use file_get_contents or cURL to fetch the API data
|
||||
$response = file_get_contents($apiUrl);
|
||||
|
||||
try {
|
||||
$response = file_get_contents($apiUrl);
|
||||
} catch (Exception $e) {
|
||||
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
|
||||
}
|
||||
if ($response === FALSE) {
|
||||
$debug_string = $debug_string."Response ERROR!!<br>";
|
||||
return "error"; // API error
|
||||
@ -52,47 +56,44 @@ function runAPI($submitted_status) {
|
||||
return isset($data['Status']) ? $data['Status'] : 'unknown';
|
||||
}
|
||||
|
||||
// | awk '{printf(\"%.5f\n\", $1)}'
|
||||
// | awk '{printf(\"%.5f\n\", $1)}'
|
||||
|
||||
|
||||
function getGPS(){
|
||||
if (!file_exists("{{ gps_service_directory }}/gps_data")) {
|
||||
return "No GPS data available.";
|
||||
}
|
||||
// check the API
|
||||
$gps_data = file_get_contents("http://172.17.0.1:5000/return_gps");
|
||||
try {
|
||||
$LAT = shell_exec("cat {{ gps_service_directory }}/gps_data | grep lat | cut -d ':' -f 2 | awk '{printf(\"%.5f\n\", $1)}')");
|
||||
$LON = shell_exec("cat {{ gps_service_directory }}/gps_data | grep lon | cut -d ':' -f 2 | awk '{printf(\"%.5f\n\", $1)}')");
|
||||
$SPEED = floatval(trim(shell_exec("cat {{ gps_service_directory }}/gps_data | grep speed | cut -d ':' -f 2)"))) * 0.62;
|
||||
} catch (Exception $e) {
|
||||
return "Failed to parse gps_data: ".$e;
|
||||
}
|
||||
$gps_data = json_decode($gps_data, true);
|
||||
|
||||
if (is_null($LAT)) {
|
||||
return "No GPS data available.";
|
||||
} catch (Exception $e){
|
||||
$gps_data = '{c}';
|
||||
}
|
||||
|
||||
//set the vars
|
||||
$LAT = $gps_data['lat'];
|
||||
$LON = $gps_data['lon'];
|
||||
$SPEED = $gps_data['speed'];
|
||||
|
||||
if (is_null($LAT) || $LAT == 0) {
|
||||
return "No GPS data available - null LAT";
|
||||
}
|
||||
if (is_null($LON)) {
|
||||
return "No GPS data available.";
|
||||
if (is_null($LON) || $LON == 0) {
|
||||
return "No GPS data available - null LON";
|
||||
}
|
||||
|
||||
return $LAT.", ".$LON.", ".$SPEED."mph";
|
||||
}
|
||||
|
||||
|
||||
// Check if the button was clicked via AJAX request
|
||||
// if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// $button_recent = true;
|
||||
// $status_at_submit = $_POST['status'];
|
||||
// echo "Set when clicked, second in code - ".$status_at_submit."<br>";
|
||||
// // Get the API result
|
||||
// $apiResult = runAPI($status_at_submit);
|
||||
// // Return the result as a JSON response
|
||||
// echo json_encode(['status' => $apiResult]);
|
||||
// exit;
|
||||
// }
|
||||
|
||||
// URL of the external API
|
||||
$apiUrl = "http://172.17.0.1:5000/status";
|
||||
|
||||
// Use file_get_contents to fetch data from the API
|
||||
$response = file_get_contents($apiUrl);
|
||||
try {
|
||||
$response = file_get_contents($apiUrl);
|
||||
} catch (Exception $e) {
|
||||
$response = '{ "Message": "unknowable", "Status": "unknowable" }';
|
||||
}
|
||||
|
||||
// Check if the request was successful
|
||||
if ($response === FALSE) {
|
||||
@ -132,6 +133,10 @@ switch ($status) {
|
||||
$button_text = "Service Stopping...";
|
||||
$button_active = false;
|
||||
break;
|
||||
case "unknowable":
|
||||
$button_text = "Service Manager Down...";
|
||||
$button_active = false;
|
||||
break;
|
||||
default:
|
||||
$button_text = "Error, No Status";
|
||||
$button_active = false;
|
||||
|
||||
10
templates/purge_thumbnails.sh.j2
Normal file
10
templates/purge_thumbnails.sh.j2
Normal file
@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
TARGET_DIR="{{ working_folder }}/small_thumbs"
|
||||
cd "$TARGET_DIR" || exit
|
||||
# Find all files, sort them by modification time (oldest first), and delete all but the 50 most recent
|
||||
ls -t | tail -n +51 | while read -r file; do
|
||||
if [ -f "$file" ]; then
|
||||
# echo "Deleting file: $file"
|
||||
rm "$file"
|
||||
fi
|
||||
done
|
||||
14
templates/smb.conf.j2
Normal file
14
templates/smb.conf.j2
Normal file
@ -0,0 +1,14 @@
|
||||
# timelapse.conf
|
||||
# timelapse share folder
|
||||
|
||||
|
||||
[timelapse]
|
||||
path = {{ working_folder }}
|
||||
writable = yes
|
||||
read only = no
|
||||
only guest = yes
|
||||
public = yes
|
||||
guest ok = yes
|
||||
guest only = yes
|
||||
guest account = nobody
|
||||
browsable = yes
|
||||
@ -3,7 +3,7 @@ Description=Timelapse service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/opt/carputer/timelapse/timelapse_service.sh
|
||||
ExecStart={{ working_folder }}/timelapse_service.sh
|
||||
Restart=always
|
||||
User=root
|
||||
Group=root
|
||||
|
||||
@ -1,54 +1,60 @@
|
||||
#!/bin/bash
|
||||
trap "source {{ working_folder }}/create_timelapse.sh" SIGINT SIGTERM
|
||||
|
||||
echo "Start"
|
||||
# initialize all the variables
|
||||
# basic things
|
||||
BEGIN=$(date +%Y%m%d-%H%M%S)
|
||||
echo $BEGIN
|
||||
WORKING_DIR="{{ working_folder }}/storage/$BEGIN"
|
||||
# be greedy about API calls
|
||||
WHERES_GALI=$(curl -s http://10.18.1.1:8184/wheres_gali?api_key={{ tesla_api_key }})
|
||||
# parse the one API call
|
||||
CITY=$(echo $WHERES_GALI | jq .city)
|
||||
STATE=$(echo $WHERES_GALI | jq .state)
|
||||
ZIPCODE=$(echo $WHERES_GALI | jq .postcode)
|
||||
DISPLAY_NAME=$(echo $WHERES_GALI | jq .display_name)
|
||||
mkdir -p $WORKING_DIR
|
||||
|
||||
if ( nc -w 3 -z 10.18.1.1 8184 2>&1 >/dev/null ); then
|
||||
# be greedy about API calls
|
||||
WHERES_GALI=$(curl -s http://10.18.1.1:8184/wheres_gali?api_key={{ tesla_api_key }})
|
||||
# parse the one API call
|
||||
CITY=$(echo $WHERES_GALI | jq .city)
|
||||
STATE=$(echo $WHERES_GALI | jq .state)
|
||||
ZIPCODE=$(echo $WHERES_GALI | jq .postcode)
|
||||
DISPLAY_NAME=$(echo $WHERES_GALI | jq .display_name)
|
||||
# Generate Status Report
|
||||
echo Timelapse Initiated at $BEGIN >> $WORKING_DIR/info.txt
|
||||
echo Shuttlecraft Galileo located at $CITY, $STATE $ZIPCODE >> $WORKING_DIR/info.txt
|
||||
echo $DISPLAY_NAME >> $WORKING_DIR/info.txt
|
||||
else
|
||||
echo "No Gali API Data Available"
|
||||
fi
|
||||
|
||||
RUN_FILE={{ working_folder }}/run
|
||||
|
||||
# Generate Status Report
|
||||
mkdir -p $WORKING_DIR
|
||||
echo Timelapse Initiated at $BEGIN >> $WORKING_DIR/info.txt
|
||||
echo Shuttlecraft Galileo located at $CITY, $STATE $ZIPCODE >> $WORKING_DIR/info.txt
|
||||
echo $DISPLAY_NAME >> $WORKING_DIR/info.txt
|
||||
|
||||
# timelapse creation helpers
|
||||
echo "To create timelapse, here's the ffpmeg script" >> $WORKING_DIR/info.txt
|
||||
echo "/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" \\" >> $WORKING_DIR/info.txt
|
||||
echo "-vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4" >> $WORKING_DIR/info.txt
|
||||
#echo "To create timelapse, here's the ffpmeg script" >> $WORKING_DIR/info.txt
|
||||
#echo "/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" \\" >> $WORKING_DIR/info.txt
|
||||
#echo "-vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4" >> $WORKING_DIR/info.txt
|
||||
# there should be a script that can do them all
|
||||
echo "/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" \\" >> {{ working_folder }}/generate_timelapses.sh
|
||||
echo "-vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4" >> {{ working_folder }}/generate_timelapses.sh
|
||||
#echo "/bin/ffmpeg -r 30 -pattern_type glob -i "$WORKING_DIR/*.jpg" \\" >> {{ working_folder }}/generate_timelapses.sh
|
||||
#echo "-vf "scale=1920x1080" -vcodec libx264 /$WORKING_DIR/00-timelapse.mp4" >> {{ working_folder }}/generate_timelapses.sh
|
||||
|
||||
# capture time
|
||||
touch $RUN_FILE
|
||||
echo $RUN_FILE
|
||||
i=1
|
||||
|
||||
|
||||
while [ -e $RUN_FILE ]
|
||||
do
|
||||
FILENAME=$(printf "img-%05d" "$i")
|
||||
# old API based latlon
|
||||
#LATLON=$(curl -s http://10.18.1.1:8184/where_is?api_key={{ tesla_api_key }} )
|
||||
#LON=$(echo $LATLON | jq .lon)
|
||||
#LAT=$(echo $LATLON | jq .lat)
|
||||
LAT=$(cat {{ gps_service_directory }}/gps_data | grep lat | cut -d ":" -f 2 | awk '{printf("%.5f\n", $1)}')
|
||||
LON=$(cat {{ gps_service_directory }}/gps_data | grep lon | cut -d ":" -f 2 | awk '{printf("%.5f\n", $1)}')
|
||||
SPEED_KPH=$(cat {{ gps_service_directory }}/gps_data | grep speed | cut -d ":" -f 2)
|
||||
if [ $SPEED_KPH -lt 1 ]; then
|
||||
if [ $SPEED_KPH > 1 ]; then
|
||||
SPEED_MPH=$(echo "scale=3; $SPEEK_KPH * 0.62" | bc)
|
||||
else
|
||||
SPEED_MPH=0
|
||||
fi
|
||||
COORDINATES="$LAT, $LON, $SPEED_MPH mph"
|
||||
curl http://127.0.0.1:7123/snapshot --output $WORKING_DIR/$FILENAME.jpg
|
||||
|
||||
echo $FILENAME
|
||||
echo $COORDINATES
|
||||
# This variable is for the timestamp
|
||||
TIME=$(date +%c)
|
||||
|
||||
@ -67,17 +73,14 @@ do
|
||||
rm $WORKING_DIR/$FILENAME-temp1.jpg $WORKING_DIR/$FILENAME-temp2.jpg
|
||||
|
||||
# This is for the tiny pic
|
||||
# I'll make one every 5 seconds
|
||||
# I have 1340x580. i think i will truncate the top
|
||||
# Calculate the height to trim based on the aspect ratios of the input and output images
|
||||
if (( $i % 5 == 0 )); then
|
||||
NOW=$(date +%Y%m%d%H%M%S)
|
||||
convert $WORKING_DIR/$FILENAME.jpg -gravity North -crop 1920x830+0+260 +repage -resize 1340x580 {{ working_folder }}/small_thumbs/$NOW.jpg
|
||||
fi
|
||||
NOW=$(date +%Y%m%d%H%M%S)
|
||||
convert $WORKING_DIR/$FILENAME.jpg -gravity North -crop 1920x830+0+260 +repage -resize 1340x580 {{ working_folder }}/small_thumbs/$NOW.jpg
|
||||
|
||||
sleep 1
|
||||
|
||||
((i++))
|
||||
|
||||
done
|
||||
|
||||
# end the loop
|
||||
|
||||
Reference in New Issue
Block a user