initial commit, carputer ansible role
This commit is contained in:
78
files/app.py
Normal file
78
files/app.py
Normal file
@ -0,0 +1,78 @@
|
||||
# app.py
|
||||
from flask import Flask, request, jsonify
|
||||
import subprocess
|
||||
import requests
|
||||
import psycopg2
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime, timedelta
|
||||
import io
|
||||
import json
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# API endpoint for charge
|
||||
@app.route('/test', methods=['GET'])
|
||||
def test():
|
||||
return jsonify({"message": "Hello there"}), 200
|
||||
|
||||
# API endpoint for service status
|
||||
@app.route('/get-info', methods=['GET'])
|
||||
def get_info():
|
||||
result = service_status()
|
||||
print(result)
|
||||
return jsonify(result)
|
||||
|
||||
# # Stop Service
|
||||
# @app.route('/stop', methods=['GET'])
|
||||
# def stop_service():
|
||||
# result = stop_timelapse()
|
||||
# print(result)
|
||||
# return jsonify(result)
|
||||
#
|
||||
# # Start Service
|
||||
# @app.route('/start', methods=['GET'])
|
||||
# def start_service():
|
||||
# result = start_timelapse()
|
||||
# print(result)
|
||||
# return jsonify(result)
|
||||
|
||||
# get service status
|
||||
|
||||
def service_status():
|
||||
service_name = "timelapse.service"
|
||||
try:
|
||||
# Check the status of the service using systemctl
|
||||
result = subprocess.run(['systemctl', 'is-active', service_name], check=True, capture_output=True, text=True)
|
||||
|
||||
if result.returncode == 0:
|
||||
active_status = "running" if result.stdout.strip() == b'active\n' else "not running"
|
||||
|
||||
# Get the last message from the system log for this service
|
||||
logs_result = subprocess.run(['journalctl', '-u', service_name, '-xe'], check=True, capture_output=True, text=True)
|
||||
last_message = logs_result.stdout
|
||||
|
||||
response = {
|
||||
"service_status": active_status,
|
||||
"service_message": last_message.strip() if last_message else "No message available"
|
||||
}
|
||||
|
||||
return json.dumps(response)
|
||||
else:
|
||||
raise Exception("Service is not running")
|
||||
except subprocess.CalledProcessError as e:
|
||||
response = {
|
||||
"service_status": "error",
|
||||
"service_message": str(e)
|
||||
}
|
||||
return json.dumps(response)
|
||||
except Exception as e:
|
||||
response = {
|
||||
"service_status": "error",
|
||||
"service_message": str(e)
|
||||
}
|
||||
return json.dumps(response)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
12
files/create_timelapse.sh
Normal file
12
files/create_timelapse.sh
Normal file
@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
# create timelapse
|
||||
|
||||
# Check if the timestamp is provided
|
||||
#if [ -z "$1" ]; then
|
||||
# echo "Usage: ./test.sh --timestamp=\"2025-07-25 12:00:00\""
|
||||
# exit 1
|
||||
#fi
|
||||
|
||||
# Extract the timestamp from the argument
|
||||
#TIMESTAMP=$(echo $1 | awk -F'=' '{print $2}')4
|
||||
|
||||
75
files/notes.txt
Normal file
75
files/notes.txt
Normal file
@ -0,0 +1,75 @@
|
||||
Notes on system itself
|
||||
The service contol will have the following components:
|
||||
PHP site that will make the API calls
|
||||
I don't have time to learn something new
|
||||
It will be similar to the photo refresh site
|
||||
Flow will be get status API, draw box depending on result, if pressed execute appropriate API, return
|
||||
The API calls are to a another service running a Python script
|
||||
I think I will have the following text in Green (Red):
|
||||
- THE SERVICE IS (NOT) RUNNING
|
||||
The button will be Red (Green):
|
||||
- Stop (Start)
|
||||
I'll rip off gali for the style
|
||||
|
||||
Notes on the python service script:
|
||||
There will be the cheap API, with a start, stop, and status
|
||||
Start will initialize another service
|
||||
There will be a variable to store the time of the last start/stop API call and limit calls for 5 seconds after this time
|
||||
No such limit will exist on the status API
|
||||
|
||||
Some Timelapse and snapshot Notes
|
||||
I think I will have two services, and the snapshot recording service will be a subservice of the timelapse service
|
||||
The timelapse service will start the snapshot
|
||||
|
||||
=== Old Timelapse Code ===
|
||||
|
||||
## Script to create timelapse
|
||||
|
||||
# Set TODAY variable
|
||||
TODAY=$(date +%Y-%m-%d)
|
||||
|
||||
# Create folder for today's photos
|
||||
mkdir /var/opt/mattstream/$TODAY
|
||||
|
||||
# How long between photos
|
||||
PERIOD=1
|
||||
|
||||
# How long to record for
|
||||
LENGTH=900
|
||||
|
||||
# Calculate number of photos to take
|
||||
ITERATIONS=$((LENGTH / PERIOD))
|
||||
|
||||
# Begin capture loop
|
||||
i=1
|
||||
while [ $i -le $ITERATIONS ]
|
||||
do
|
||||
|
||||
# This variable is for the filename
|
||||
NOW=$(date +%Y%m%d-%H%M%S)
|
||||
# This variable is for the timestamp
|
||||
TIME=$(date +%Y%m%d-%H%M)
|
||||
# Download current capture
|
||||
curl http://10.55.10.1:6288/snapshot --output /var/opt/mattstream/$TODAY/$NOW.jpg
|
||||
# This should add the current time to the image and save it
|
||||
convert /var/opt/mattstream/$TODAY/$NOW.jpg -gravity NorthWest -pointsize 22 \
|
||||
-fill yellow -annotate +30+30 $TIME /var/opt/mattstream/$TODAY/$NOW-ts.jpg
|
||||
# Delete original
|
||||
rm /var/opt/mattstream/$TODAY/$NOW.jpg
|
||||
# And now we wait
|
||||
sleep $PERIOD
|
||||
# Increment the counter and start over duh
|
||||
((i++))
|
||||
# For more obvious commentary, the loop is now over
|
||||
done
|
||||
|
||||
# Generate the timelapse now
|
||||
ffmpeg -r 24 -pattern_type glob -i "/var/opt/mattstream/$TODAY/*.jpg" \
|
||||
-vf "scale=1280x720" -vcodec libx264 /var/opt/mattstream/$TODAY/$TODAY.mp4
|
||||
|
||||
# If we want to start deleting original images, uncomment this
|
||||
#rm /var/opt/mattstream/$TODAY/*.jpg
|
||||
|
||||
# Slap a copy of this somewhere convenient for me to grab it
|
||||
cp /var/opt/mattstream/$TODAY/$TODAY.mp4 /media/pleiades/matt-lapse/$TODAY.mp4
|
||||
|
||||
13
files/timelapse.service
Normal file
13
files/timelapse.service
Normal file
@ -0,0 +1,13 @@
|
||||
[Unit]
|
||||
Description=Timelapse service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/opt/carputer/timelapse/timelapse_service.sh
|
||||
|
||||
Restart=always
|
||||
User=timelapse
|
||||
Group=timelapse
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Reference in New Issue
Block a user