Skip to content

Instantly share code, notes, and snippets.

@fourlastor
Created December 18, 2021 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourlastor/596ab614dd6a2145a29c2bcdbec66200 to your computer and use it in GitHub Desktop.
Save fourlastor/596ab614dd6a2145a29c2bcdbec66200 to your computer and use it in GitHub Desktop.
Automatically trigger other Eufy stations alarms
const WebSocket = require('ws');
const base64 = require('base-64');
// SETTINGS
const HOST = "127.0.0.1";
const PORT = 3000;
const INTERVAL_IN_SECONDS = 10;
const TRIGGER_ALARM_IN_SECONDS = 10;
// DO NOT TOUCH AFTER THIS LINE
const socket = new WebSocket(`ws://${HOST}:${PORT}`);
const stations = []
const GET_ALARMS_MSG_ID = 'get-alarm-events';
const START_LISTENING_RESULT_MSG_ID = 'start-listening-result';
const INTERVAL_IN_MS = INTERVAL_IN_SECONDS * 1000;
socket.on("open", function open() {
socket.send(JSON.stringify({
messageId: "api-schema-id",
command: "set_api_schema",
schemaVersion: 7,
}));
socket.send(JSON.stringify({
messageId: START_LISTENING_RESULT_MSG_ID,
command: "start_listening",
}));
});
socket.on("message",(data) => {
const json = JSON.parse(data.toString())
switch (json.messageId) {
case START_LISTENING_RESULT_MSG_ID:
collect_station_info(json.result.state);
start_listening_to_alarms();
break;
case GET_ALARMS_MSG_ID:
check_alarms_to_trigger(json.result.events);
break;
}
})
function collect_station_info(state) {
console.log("Collecting station info")
state.stations.forEach(station => {
console.log(`Station found ${stations.serialNumber}`)
stations.push(station.serialNumber)
});
}
function start_listening_to_alarms() {
console.log("Starting to listen for alarms")
setInterval(() => {
socket.send(JSON.stringify({
messageId: GET_ALARMS_MSG_ID,
command: "driver.get_alarm_events"
}));
}, INTERVAL_IN_MS);
}
const ALARM_MSG_TYPE = 10;
const ALARM_TRIGGERED_EVENT_TYPE = 7;
function check_alarms_to_trigger(events) {
events.forEach((event) => {
const extra = JSON.parse(base64.decode(event.extra))
if (extra.msg_type === ALARM_MSG_TYPE
&& extra.event_type === ALARM_TRIGGERED_EVENT_TYPE) {
const start_time = event.start_time
const last_significant_istant = Date.now() - INTERVAL_IN_MS;
if (start_time > last_significant_istant) {
trigger_alarm(event.station_sn)
}
}
})
}
function trigger_alarm(originating_station_sn) {
console.log(`Alarm event found for station ${originating_station_sn}`)
stations
.filter((station_sn) => station_sn !== originating_station_sn)
.forEach((station_sn) => send_alarm_to_station(station_sn))
}
function send_alarm_to_station(station_sn) {
console.log(`Triggering alarm for station ${station_sn}`)
socket.send(JSON.stringify({
messageId: 'station-trigger-alarm',
command: 'station.trigger_alarm',
serialNumber: station_sn,
seconds: TRIGGER_ALARM_IN_SECONDS,
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment