Skip to content

Instantly share code, notes, and snippets.

@mpolinowski
Created January 4, 2024 17:39
Show Gist options
  • Save mpolinowski/d7c1c6e28e8760410bd461f0eb1f447b to your computer and use it in GitHub Desktop.
Save mpolinowski/d7c1c6e28e8760410bd461f0eb1f447b to your computer and use it in GitHub Desktop.
INSTAR WQHD 2k+ Camera CGI Commands / REST API - Get camera state using javascript:
const getRequest = {
cgiGetCommand: 'getmqttattr',
cameraIp: '192.168.2.125',
cameraPort: '80',
authToken: '7PZv8N63c7wYYRxBw39pnRYNRnbdaBTTaK'
}
const fetchDataWithAuthHeader = async (getRequest) => {
try {
const customHeaders = new Headers()
customHeaders.append('Authorization', 'Bearer ' + getRequest.authToken)
const response = await fetch(
'http://' + getRequest.cameraIp + ':' + getRequest.cameraPort + '/' + 'param.cgi?cmd=' + getRequest.cgiGetCommand,
{
method: 'GET',
headers: customHeaders
}
)
if (!response.ok) {
throw new Error(`ERROR :: Request failed with status ${response.status}`)
}
const textResponse = await response.text()
console.log(textResponse)
const cleanedTextResponse = textResponse
.replace('cmd="getmqttattr";', '{"')
.replace('response="200";', '}')
.replace(/=/g, '":')
.replace(/";/g, '","')
.replace(/\s/g, '')
.replace(/","}/g, '"}');
const jsonData = JSON.parse(cleanedTextResponse)
console.log(`Enable Broker: ${jsonData.mq_enable},\nWebsocket Support: ${jsonData.mq_broker_ws},\nWebsocket Port: ${jsonData.mq_broker_ws_port},\nWebsocket Port SSL: ${jsonData.mq_broker_ws_portssl},\nTLS Version: ${jsonData.mq_broker_min_tls},\nExternal Broker IP: ${jsonData.mq_host},\nBroker Port: ${jsonData.mq_port},\nBroker Port SSL: ${jsonData.mq_portssl},\nEnable Encryption: ${jsonData.mq_ssl},\nEnable Authentication: ${jsonData.mq_auth},\nUsername: ${jsonData.mq_user},\nTLS Certificate Verification: ${jsonData.mq_insecure},\nMQTT Prefix: ${jsonData.mq_prefix},\nMQTT LWT: ${jsonData.mq_lwt},\nMQTT LWT on: ${jsonData.mq_lwmon},\nMQTT LWT off: ${jsonData.mq_lwmoff},\nMQTT Client ID: ${jsonData.mq_clientid},\nMQTT QoS Level: ${jsonData.mq_qos}`)
}
catch (error) {
console.log('ERROR :: ', error.message)
}
}
fetchDataWithAuthHeader(getRequest)
@mpolinowski
Copy link
Author

Get Auth Token

/param.cgi?cmd=gettoken

cmd="gettoken";
token="7PZv8N63c7wYYRxBw39pnRYNRnbdaBTTaK";
validity="1706973422";
response="200";

Run Script

node getMqttAttr.js 

cmd="getmqttattr";
mq_enable="1";
mq_broker="0";
mq_broker_ws="1";
mq_broker_ws_port="1885";
mq_broker_ws_portssl="8885";
mq_broker_min_tls="tlsv1.2";
mq_host="192.168.2.19";
mq_port="1883";
mq_portssl="8883";
mq_ssl="0";
mq_auth="1";
mq_user="admin";
mq_insecure="1";
mq_prefix="cameras";
mq_lwt="/connection";
mq_lwmon="online";
mq_lwmoff="offline";
mq_clientid="125";
mq_qos="2";
response="200";

Enable Broker:1,
Websocket Support: 1,
Websocket Port: 1885,
Websocket Port SSL: 8885,
TLS Version: tlsv1.2,
External Broker IP: 192.168.2.19,
Broker Port: 1883,
Broker Port SSL: 8883,
Enable Encryption: 0,
Enable Authentication: 1,
Username: admin,
TLS Certificate Verification: 1,
MQTT Prefix: cameras,
MQTT LWT: /connection,
MQTT LWT on: online,
MQTT LWT off: offline,
MQTT Client ID: 125,
MQTT QoS Level: 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment