Kodi can play video playlist files which have a .strm file extension, so you can create a one-liner .strm file somewhere in your video library and add the rtsp url/path info for the camera you want to show briefly. For Hikvision, for example, the file can contain:
Code: Select all
rtsp://username:[email protected]:554/Streaming/Channels/101
I have Kodi installed on a dedicated machine (running LibreELEC) which the script quickly pings to decide whether the doorbell-interruption makes sense or not. If you don't want that then strip the pinging code from the script below. If you do want to ping so as to exit if the machine is down, then you will need to install a required npm package via
Code: Select all
npm install ping --save
Here's the script:
Code: Select all
/* eslint-disable func-names */
/* eslint-disable no-console */
// (c) philchillbill, 2019. No rights reserved
// Change the following SEVEN parameters to match your setup
const kodiIP = '192.168.1.5'; // IP address of kodi server on LAN
const kodiPort = '8080'; // port for HTTP control of Kodi
const authneeded = false; // set to true if your Kodi instance has password protection for HTTP access
const username = "kodiusername"; // only if the above is true
const password = "kodipassword"; // only if the above is true
const IPcamStreamFile = "smb://192.168.1.3//video/IPcams/driveway.strm"; // path to .strm file, here on different server
const showfor = 8; // number of seconds to show camera before reverting
//--------------------------------------------------------------------------------------------
// no more changes needed hereafter
const client = require('http');
const ping = require('ping');
if (authneeded) {
global.auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
global.reqheaders = { 'Authorization': auth, "Content-Type": "application/json" }
} else {
global.reqheaders = { "Content-Type": "application/json" }
}
const cfg = {
timeout: 1
};
ping.sys.probe(kodiIP, function(isAlive) {
if (!isAlive) {
console.log('Player ping failed, exiting');
process.exit()
}
}, cfg);
const apiKODI = function(query) {
return new Promise((resolve, reject) => {
var jbody = query
console.log('\n' + query)
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
const options = {
host: kodiIP,
port: kodiPort,
method: 'POST',
json: true,
path: '/jsonrpc?request=',
headers: reqheaders,
body: jbody
}
var req = client.request(options, (res) => {
var rbody = []
var response
res.on('data', (chunk) => {
rbody += chunk
});
res.on('end', () => {
try {
var result = JSON.parse(rbody).result;
response = result
global.responsive = true
} catch (e) {
console.log('apiKODI response body: ', rbody)
console.log('apiKODI error message: ', e)
response = {}
global.responsive = false
}
resolve(response)
})
});
req.on('error', (e) => {
console.error(e);
resolve(false) // e.message
});
req.write(jbody);
req.end()
})
};
const showcamera = async() => {
response = await apiKODI('{"jsonrpc":"2.0","method":"Player.GetProperties","params":{"playerid":1,"properties":["percentage"]},"id":"1"}')
global.percentage = response.percentage
if (responsive) { console.log(response) }
response = await apiKODI(`{"jsonrpc":"2.0","id":"1","method":"Playlist.Add","params":{"playlistid":1, "item":{"file":${IPcamStreamFile}}}}`)
if (responsive) { console.log(response) }
response = await apiKODI(`{"jsonrpc":"2.0","method":"Player.GoTo","params":{"playerid":1,"to": "next"},"id":1}`)
if (responsive) { console.log(response) }
response = await apiKODI(`{"jsonrpc": "2.0", "method": "Playlist.GetItems", "params": { "playlistid": 1}, "id": 1}`)
if (responsive) { console.log(response) }
global.tempitem = response.items.length - 1
}
const resume = async() => {
response = await apiKODI(`{"jsonrpc":"2.0","method":"Player.GoTo","params":{"playerid":1,"to": "previous"},"id":1}`)
if (responsive) { console.log(response) }
response = await apiKODI(`{"jsonrpc":"2.0","id":1, "method":"Playlist.Remove","params":{"playlistid":1,"position": ${tempitem}}}`)
if (responsive) { console.log(response) }
response = await apiKODI(`{"jsonrpc": "2.0", "method": "Playlist.GetItems", "params": { "playlistid": 1}, "id": 1}`)
if (responsive) { console.log(response) }
return setTimeout(goto, 2000);
}
const goto = async() => {
response = await apiKODI(`{"jsonrpc":"2.0","id": 4,"method":"Player.Seek","params":{"playerid":1 ,"value":{"percentage": ${percentage}} } }`)
if (responsive) { console.log(response) }
}
showcamera()
setTimeout(resume, Number(showfor * 1000));
Code: Select all
IPcamStreamFile = "smb://192.168.1.3//video/IPcams/driveway.strm"
To trigger this script when the Domoticz-aware doorbell is pressed, add a script:// to the on-action for the switch in question.
Enjoy!