Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

SinricPro (NodeJs-SDK)

Installation

npm install sinricpro

Dependencies

Basic example

const {
  SinricPro,
  SinricProActions,
  raiseEvent,
  eventNames,
  SinricProUdp
} = require("../index"); // Use require('sinricpro'); if you are using NPM

const appKey = ""; // d89f1***-****-****-****-************
const secretKey = ""; // f44d1d31-1c19-****-****-9bc96c34b5bb-d19f42dd-****-****-****-************
const device1 = ""; // 5d7e7d96069e275ea9******
const device2 = ""; // 5d80ac5713fa175e99******
const deviceId = [device1, device2];

function setPowerState(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

const callbacks = {
  setPowerState
};

const sinricpro = new SinricPro(appKey, deviceId, secretKey, true);

SinricProActions(sinricpro, callbacks);

setInterval(() => {
  raiseEvent(sinricpro, eventNames.powerState, device1, { state: "On" });
}, 2000);

// https://github.com/sinricpro/nodejs-sdk/blob/master/examples/simple-example/simple-example.js

Advanced example

Add a credential file into your root folder (credential.js)

const credential = {
  appkey: "",
  secretKey: ""
};

const deviceIds = {
  deviceId1: "",
  deviceId2: "",
  deviceId3: ""
};

// No need to edit the code below

let deviceIdT = [];

for (let key in deviceIds) {
  if (deviceIds.hasOwnProperty(key)) {
    deviceIdT.push(deviceIds[key]);
  }
}

module.exports = {
  appKey: credential.appkey,
  secretKey: credential.secretKey,
  deviceId: deviceIdT
};

Example (app.js)

const { appKey, deviceId, secretKey } = require("../credentials");
const {
  SinricPro,
  SinricProActions,
  raiseEvent,
  eventNames,
  SinricProUdp
} = require("../index");

function setPowerState(deviceid, data) {
  console.log(deviceid, data);
  return true;
}
function setPowerLevel(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

function adjustPowerLevel(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

function setColor(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

function setRangeValue(deviceid, data) {
  console.log(deviceid, data);
  return true;
}
function setLockState(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

function setBrightness(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

function setVolume(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

function adjustVolume(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

function setMute(deviceid, data) {
  console.log(deviceid, data);
  return true;
}

const callbacks = {
  setPowerState,
  setPowerLevel,
  adjustPowerLevel,
  setColor,
  setRangeValue,
  setLockState,
  setBrightness,
  setVolume,
  adjustVolume,
  setMute
};

const sinricpro = new SinricPro(appKey, deviceId, secretKey, true);
SinricProActions(sinricpro, callbacks);

setInterval(() => {
  // raiseEvent(sinricpro, eventNames.powerState, 'deviceId', { state: 'On' });
  // raiseEvent(sinricpro, eventNames.setBrightness, 'deviceId', { brightness: 44 });
  // raiseEvent(sinricpro, eventNames.powerLevel, 'deviceId', { powerLevel: 44 });
  // raiseEvent(sinricpro, eventNames.color, 'DeviceId', { color: { b: 0, g: 0, r: 0 } });
  // raiseEvent(sinricpro, eventNames.colorTemperature, 'Deviceid', { colorTemperature: 8 });
  // raiseEvent(sinricpro, eventNames.doorBell, 'DeviceId', { state: 'pressed' });
  // raiseEvent(sinricpro, eventNames.thermostatMode, 'DeviceId', { thermostatMode: 'AUTO' });
  // raiseEvent(sinricpro, eventNames.rangvalue, 'DeviceId', { rangvalue: 3 });
  // raiseEvent(sinricpro, eventNames.motion, 'DeviceId', { state: 'detected' });
  // raiseEvent(sinricpro, eventNames.contact, 'DeviceId', { state: 'closed' });
  // raiseEvent(sinricpro, eventNames.setVolume, 'DeviceId', { volume: 29 });
  // raiseEvent(sinricpro, eventNames.selectInput, 'DeviceId', { input: 'HDMI' });
  // raiseEvent(sinricpro, eventNames.media, 'DeviceId', { control: 'FastForward' });
  // raiseEvent(sinricpro, eventNames.channel, 'DeviceId', { channel: { name: 'HBO' } });
  // raiseEvent(sinricpro, eventNames.mode, 'DeviceId', { mode: 'MOVIE' });
  // raiseEvent(sinricpro, eventNames.lock, 'DeviceId', { state: 'LOCKED' });
  // raiseEvent(sinricpro, eventNames.mute, 'DeviceId', { mute: true });
}, 2000);