Last active
March 9, 2024 04:54
-
-
Save oianmol/54cd14861e2448f2f5b40e91e108eba1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const axios = require('axios'); | |
const yargs = require('yargs/yargs'); | |
const {hideBin} = require('yargs/helpers'); | |
class AxisControls { | |
constructor(cameraIP, username, password) { | |
// Replace these with your actual camera details | |
this.baseUrl = `http://${username}:${password}@${cameraIP}/axis-cgi/com/ptz.cgi`; | |
} | |
// to move the camera | |
// direction can be 'up', 'down', 'left', 'right' | |
// speed is an integer typically between 1 and 100 | |
moveCamera(direction, speed) { | |
const command = `move=${direction}&speed=${speed}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Move command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending move command', error); | |
}); | |
} | |
// to set the camera's zoom level | |
// zoomLevel is an integer typically between 1 and 9999 | |
setZoom(zoomLevel) { | |
const command = `rzoom=${zoomLevel}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Zoom command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending zoom command', error); | |
}); | |
} | |
// Moves the camera to a preset position | |
goToPreset(presetName) { | |
const command = `gotoserverpresetname=${presetName}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Go to preset command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending go to preset command', error); | |
}); | |
} | |
// Sets the camera to its home position | |
goHome() { | |
const command = 'move=home'; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Go home command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending go home command', error); | |
}); | |
} | |
// Focus controls: "near" or "far" | |
adjustFocus(focusDirection) { | |
const command = `focus=${focusDirection}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Focus command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending focus command', error); | |
}); | |
} | |
// Adjust the iris: "open" or "close" | |
adjustIris(irisAction) { | |
const command = `iris=${irisAction}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Iris command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending iris command', error); | |
}); | |
} | |
// Adjusts the brightness: value range typically between 1 and 9999 | |
adjustBrightness(brightnessLevel) { | |
const command = `brightness=${brightnessLevel}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Brightness command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending brightness command', error); | |
}); | |
} | |
// Autopan controls: start and stop | |
controlAutopan(action) { | |
const command = `autopan=${action}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Autopan command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending autopan command', error); | |
}); | |
} | |
// Start continuous move (pan or tilt) | |
// direction can be 'up', 'down', 'left', 'right' | |
// speed is typically between 1 and 100 | |
startContinuousMove(direction, speed) { | |
const command = `continuouspantiltmove=${direction},${speed}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Continuous move command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending continuous move command', error); | |
}); | |
} | |
// Move to an absolute position | |
// pan, tilt, and zoom are numerical values specifying the desired position | |
moveToAbsolutePosition(pan, tilt, zoom) { | |
const command = `absoluteptzmove=${pan},${tilt},${zoom}`; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Absolute position command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending absolute position command', error); | |
}); | |
} | |
// Stop all movements | |
stopAllMovements() { | |
const command = 'stop=1'; | |
axios.get(`${this.baseUrl}?${command}`) | |
.then(response => { | |
console.log('Stop all movements command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending stop all movements command', error); | |
}); | |
} | |
} | |
const argv = yargs(hideBin(process.argv)) | |
.usage('Usage: $0 <command> [options]') | |
.option('ip', { | |
describe: 'Camera IP address', | |
demandOption: true, | |
type: 'string' | |
}) | |
.option('username', { | |
describe: 'Camera username', | |
demandOption: true, | |
type: 'string' | |
}) | |
.option('password', { | |
describe: 'Camera password', | |
demandOption: true, | |
type: 'string' | |
}) | |
.command('move', 'Move the camera', { | |
direction: { | |
describe: 'Direction to move the camera (up, down, left, right)', | |
demandOption: true, | |
type: 'string' | |
}, | |
speed: { | |
describe: 'Speed of the movement (1-100)', | |
demandOption: true, | |
type: 'number' | |
} | |
}) | |
.command('zoom', 'Set the camera zoom level', { | |
level: { | |
describe: 'Zoom level (1-9999)', | |
demandOption: true, | |
type: 'number' | |
} | |
}) | |
.command('preset', 'Move the camera to a preset position', { | |
name: { | |
describe: 'Preset name', | |
demandOption: true, | |
type: 'string' | |
} | |
}) | |
.command('home', 'Move the camera to its home position') | |
.command('focus', 'Adjust the camera focus', { | |
direction: { | |
describe: 'Focus direction (near, far)', | |
demandOption: true, | |
type: 'string' | |
} | |
}) | |
.command('iris', 'Adjust the camera iris', { | |
action: { | |
describe: 'Iris action (open, close)', | |
demandOption: true, | |
type: 'string' | |
} | |
}) | |
.command('brightness', 'Adjust the camera brightness', { | |
level: { | |
describe: 'Brightness level (1-9999)', | |
demandOption: true, | |
type: 'number' | |
} | |
}) | |
.help('h') | |
.alias('h', 'help') | |
.argv; | |
const camera = new AxisControls(argv.ip, argv.username, argv.password); | |
// ./axisControl.js --ip your_camera_ip --username your_username --password your_password zoom --level 5000 | |
if (argv._.includes('move')) { | |
camera.moveCamera(argv.direction, argv.speed); | |
} else if (argv._.includes('zoom')) { | |
camera.setZoom(argv.level); | |
} else if (argv._.includes('preset')) { | |
camera.goToPreset(argv.name); | |
} else if (argv._.includes('home')) { | |
camera.goHome(); | |
} else if (argv._.includes('focus')) { | |
camera.adjustFocus(argv.direction); | |
} else if (argv._.includes('iris')) { | |
camera.adjustIris(argv.action); | |
} else if (argv._.includes('brightness')) { | |
camera.adjustBrightness(argv.level); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const axios = require('axios'); | |
const yargs = require('yargs/yargs'); | |
const { hideBin } = require('yargs/helpers'); | |
class VivotekControls { | |
constructor(cameraIP, username, password) { | |
this.cameraIP = cameraIP; | |
this.username = username; | |
this.password = password; | |
this.baseUrl = `http://${username}:${password}@${cameraIP}/cgi-bin`; | |
} | |
moveCamera(direction, step) { | |
const command = `/camctrl/camctrl.cgi?move=${direction}&step=${step}`; | |
axios.get(`${this.baseUrl}${command}`) | |
.then(response => { | |
console.log('Move command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending move command', error); | |
}); | |
} | |
setZoom(zoomLevel) { | |
const command = `/camctrl/camctrl.cgi?zoom=${zoomLevel}`; | |
axios.get(`${this.baseUrl}${command}`) | |
.then(response => { | |
console.log('Zoom command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending zoom command', error); | |
}); | |
} | |
goToPreset(presetNumber) { | |
const command = `/camctrl/camctrl.cgi?moveto=${presetNumber}`; | |
axios.get(`${this.baseUrl}${command}`) | |
.then(response => { | |
console.log('Preset command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending preset command', error); | |
}); | |
} | |
setPreset(presetNumber) { | |
const command = `/camctrl/camctrl.cgi?setpos=${presetNumber}`; | |
axios.get(`${this.baseUrl}${command}`) | |
.then(response => { | |
console.log('Set preset command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error setting preset command', error); | |
}); | |
} | |
startPatrol(patrolSequence) { | |
const command = `/patrol/patrol.cgi?patrol=${patrolSequence}`; | |
axios.get(`${this.baseUrl}${command}`) | |
.then(response => { | |
console.log('Patrol command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending patrol command', error); | |
}); | |
} | |
stopPatrol() { | |
const command = `/patrol/patrol.cgi?patrol=stop`; | |
axios.get(`${this.baseUrl}${command}`) | |
.then(response => { | |
console.log('Stop patrol command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error stopping patrol command', error); | |
}); | |
} | |
rebootCamera() { | |
const command = `/admin/reboot.cgi`; | |
axios.get(`${this.baseUrl}${command}`) | |
.then(response => { | |
console.log('Reboot command sent successfully', response.data); | |
}) | |
.catch(error => { | |
console.error('Error sending reboot command', error); | |
}); | |
} | |
// Add more methods according to Vivotek API documentation | |
} | |
const argv = yargs(hideBin(process.argv)) | |
.usage('Usage: $0 <command> [options]') | |
.option('ip', { | |
describe: 'Camera IP address', | |
demandOption: true, | |
type: 'string' | |
}) | |
.option('username', { | |
describe: 'Camera username', | |
demandOption: true, | |
type: 'string' | |
}) | |
.option('password', { | |
describe: 'Camera password', | |
demandOption: true, | |
type: 'string' | |
}) | |
// Define options for your additional commands here | |
.help('h') | |
.alias('h', 'help') | |
.argv; | |
const camera = new VivotekControls(argv.ip, argv.username, argv.password); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment