Files
PictureFrame/index.js

108 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-04-03 09:22:53 -06:00
const rclone = require('rclone.js')
const schedule = require('node-schedule')
2025-03-21 11:13:33 -06:00
const fs = require('fs')
const sharp = require('sharp')
const inky = require('@aeroniemi/inky')
2025-05-16 14:37:44 -06:00
const mqtt = require('mqtt')
2025-04-03 09:22:53 -06:00
2025-03-21 11:13:33 -06:00
const frame = new inky.Impression73()
2025-04-03 09:22:53 -06:00
const folder = './gallery/'
2025-05-16 14:37:44 -06:00
// Connect to MQTT
const mqttclient = mqtt.connect('mqtt://mqtt.srv.jnf.me', {
clientId: 'pictureframe_' + Math.random().toString(16).substr(2, 8),
username: 'pictureframe',
password: 'pictureframe'
})
2025-04-03 09:22:53 -06:00
var ticker = false
2025-03-21 11:13:33 -06:00
var filelist = false
2025-04-03 09:22:53 -06:00
function downloadImgs() {
console.log('Starting download from Google Photos...')
const sync = rclone.sync('gphotos:album/Family Room Photo Frame', folder)
sync.stdout.on('data', function(data) {
console.log(data.toString())
})
sync.on('exit', function() {
console.log('Synchronized from Google Photos. Loading images...')
loadImgs()
})
}
2025-03-21 11:13:33 -06:00
function loadImgs() {
fs.readdir(folder, (err, files) => {
if (err || files.length == 0) {
console.log('No images found. Exiting.')
process.exit(1)
} else {
console.log(files.length + ' images found. Starting album display...')
filelist = files
displayAlbum()
}
})
}
function displayAlbum() {
if (filelist.length == 0) loadImgs()
else {
imgref = Math.floor(Math.random() * filelist.length)
imgfile = filelist.splice(imgref, 1)[0]
console.log('Randomly displaying image ' + (imgref + 1) + ' of ' + (filelist.length + 1) + ' remaining: ' + imgfile)
displayImage(imgfile)
2025-04-03 09:22:53 -06:00
ticker = setTimeout(displayAlbum, 1800000)
2025-03-21 11:13:33 -06:00
}
}
function displayImage(filename) {
sharp(folder + filename)
.resize(800,480)
.toFile('output.png', (err, info) => {
frame.display_png('output.png')
frame.show()
})
}
2025-05-16 14:37:44 -06:00
function displayWifi() {
console.log('Displaying wifi connection information for 10 minutes...')
2025-05-16 14:37:44 -06:00
clearTimeout(ticker)
frame.display_png('img/wifi.png')
frame.show()
ticker = setTimeout(displayAlbum, 600000)
}
function skipToNext() {
clearTimeout(ticker)
displayAlbum()
}
2025-05-16 14:37:44 -06:00
// Subscribe to our MQTT topic...
mqttclient.on('connect', () => {
mqttclient.subscribe('pictureframe/command')
})
// Listen for MQTT command messages
mqttclient.on('message', (topic, message) => {
message = message.toString()
console.log('MQTT command received: ' + message)
if (message == 'skip') skipToNext()
2025-05-16 14:37:44 -06:00
if (message == 'wifi') displayWifi()
if (message == 'reset') {
clearTimeout(ticker)
downloadImgs()
}
})
2025-04-03 09:22:53 -06:00
// We start by downloading images. Other functions will be called if this is successful...
downloadImgs()
// Define a cron-job to restart everything at 1:30am (including a refresh of the album)....
schedule.scheduleJob('0 30 1 * * *', function() {
clearTimeout(ticker)
console.log('Resetting...')
downloadImgs()
})