mirror of
https://github.com/10h30/sinric-n8n-bridge.git
synced 2026-05-12 15:21:23 +09:00
Refactor index.js for improved readability and consistency
This commit is contained in:
@@ -1,59 +1,44 @@
|
|||||||
const axios = require("axios");
|
const axios = require('axios');
|
||||||
const { SinricPro, SinricProSwitch } = require("sinricpro");
|
const { SinricPro, SinricProSwitch } = require('sinricpro');
|
||||||
|
|
||||||
const APP_KEY = process.env.APP_KEY;
|
const APP_KEY = process.env.APP_KEY;
|
||||||
const APP_SECRET = process.env.APP_SECRET;
|
const APP_SECRET = process.env.APP_SECRET;
|
||||||
const DEVICE_ID = process.env.DEVICE_ID;
|
const DEVICE_ID = process.env.DEVICE_ID;
|
||||||
|
|
||||||
const N8N_WEBHOOK_URL = process.env.N8N_WEBHOOK_URL;
|
const N8N_WEBHOOK_URL = process.env.N8N_WEBHOOK_URL;
|
||||||
const N8N_TOKEN = process.env.N8N_TOKEN;
|
const N8N_TOKEN = process.env.N8N_TOKEN;
|
||||||
|
|
||||||
async function callN8n(action) {
|
async function callN8n(action) {
|
||||||
try {
|
try {
|
||||||
const res = await axios.post(
|
const res = await axios.post(
|
||||||
N8N_WEBHOOK_URL,
|
N8N_WEBHOOK_URL,
|
||||||
{ action },
|
{ action },
|
||||||
{
|
{ headers: { 'X-Token': N8N_TOKEN, 'Content-Type': 'application/json' } },
|
||||||
headers: {
|
);
|
||||||
"X-Token": N8N_TOKEN,
|
console.log(`[n8n] ${action} -> ${res.status}`);
|
||||||
"Content-Type": "application/json"
|
} catch (err) {
|
||||||
}
|
console.error('[n8n error]', err.message);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
console.log(`[n8n] ${action} -> ${res.status}`);
|
|
||||||
} catch (err) {
|
|
||||||
console.error("[n8n error]", err.message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const device = new SinricProSwitch(DEVICE_ID);
|
const device = new SinricProSwitch(DEVICE_ID);
|
||||||
|
|
||||||
device.onPowerState(async (state) => {
|
// Use onPowerState, not device.on("setPowerState")
|
||||||
const action = state ? "on" : "off";
|
device.onPowerState(async (deviceId, state) => {
|
||||||
|
const action = state ? 'on' : 'off';
|
||||||
console.log(`[sinric] ${action.toUpperCase()}`);
|
console.log(`[sinric] ${action.toUpperCase()}`);
|
||||||
|
await callN8n(action);
|
||||||
await callN8n(action);
|
return true; // must return true to confirm to Sinric
|
||||||
|
|
||||||
return true;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const sinric = new SinricPro();
|
const sinric = new SinricPro();
|
||||||
|
|
||||||
sinric.add(device);
|
sinric.add(device);
|
||||||
|
|
||||||
sinric.onConnected(() => {
|
sinric.onConnected(() => console.log('[bridge] Connected'));
|
||||||
console.log("[bridge] Connected");
|
sinric.onDisconnected(() => console.log('[bridge] Disconnected'));
|
||||||
});
|
|
||||||
|
|
||||||
sinric.onDisconnected(() => {
|
|
||||||
console.log("[bridge] Disconnected");
|
|
||||||
});
|
|
||||||
|
|
||||||
sinric.begin({
|
sinric.begin({
|
||||||
appKey: APP_KEY,
|
appKey: APP_KEY,
|
||||||
appSecret: APP_SECRET,
|
appSecret: APP_SECRET,
|
||||||
server: "ws.sinric.pro",
|
server: 'ws.sinric.pro',
|
||||||
port: 443
|
port: 443,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user