Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var HueApi = require("node-hue-api").HueApi;
- // Replace IP and username!!! IP=Bridge IP/Username=aus dem hue controller kopieren!!!
- var host = "192.168.10.23",
- username = "ytTzQ4FfD2QNqXRW5jpgnHMA-GcLshxXmr-oiSuT",
- api = new HueApi(host, username);
- var groups_ = [],
- lights_ = [],
- objects_ = [];
- // Log JSON results
- var displayResults = function(result) {
- console.log('Reponse: '+JSON.stringify(result, null, 2));
- };
- // Parse Light Group 0 (All Lights)
- var parseGroup0 = function(result) {
- if (!result.lights){return} // Empty group
- var id = result.id,
- lights = result.lights,
- name = "All Lights";
- console.debug('group: '+name+', lights: '+lights);
- groups_[lights] = name;
- };
- // Parse Light Groups
- var parseGroups = function(result) {
- for (var i = 0; i < result.length; i++) {
- if (!result[i].lights){continue} // Empty group
- var id = result[i].id,
- lights = result[i].lights,
- name = result[i].name;
- console.debug('group: '+name+', lights: '+lights);
- groups_[lights] = name;
- }
- };
- // Parse Lights
- var parseLights = function(result) {
- for (var i = 0; i < result.length; i++) {
- var id = result[i].id,
- name = result[i].name;
- console.debug('light: '+name+', id: '+id);
- lights_[id] = name;
- }
- };
- // Create States in ioBroker
- var createStates = function(result) {
- // Resync button
- createState('PhilipsHue.Scenes.Resync', false, {role: "button", name: 'Resync Philips Hue Groups, Lights and Scenes'});
- for (var i = 0; i < result.length; i++) {
- if (!result[i].appdata.data){continue} // skip internal szenes
- var id = result[i].id,
- lights = result[i].lights,
- name = result[i].name.replace(/"/g,''),
- pathname = name.replace(/ /g,'_');
- // Get light names
- var light_names = [];
- for (var j = 0; j < lights.length; j++) {
- var light_name = lights_[lights[j]];
- light_names.push(light_name);
- }
- // Room, group or lights linked with scene
- var group = 'Group: '+groups_[lights] || 'Lights: '+light_names.join(", ");
- // Create States and skip duplicates
- if (!objects_[lights+pathname]){
- console.debug('scene: '+name+', '+group);
- createState('PhilipsHue.Scenes.'+pathname+'.'+id, false, {role: "button", name: 'Scene: '+name+' ('+group+')'});
- objects_[lights+pathname] = true;
- }
- }
- };
- // Delete States
- function deleteStates(){
- console.log('Deleting current objects for scenes...');
- objects_ = [];
- $('javascript.0.PhilipsHue.Scenes.*').each(function (id) {
- deleteState(id);
- });
- }
- // Fetch data from Hue API
- function init(){
- api.getGroup(0, function(err, group0) {
- if (err) throw err;
- console.log('Processing group 0...');
- //displayResults(group0);
- parseGroup0(group0);
- });
- api.groups(function(err, groups) {
- if (err) throw err;
- console.log('Processing ' + groups.length + ' groups...');
- //displayResults(groups);
- parseGroups(groups);
- });
- api.lights(function(err, lights) {
- if (err) throw err;
- console.log('Processing ' + lights.lights.length + ' lights...');
- //displayResults(lights);
- parseLights(lights.lights);
- });
- api.scenes(function(err, scenes) {
- if (err) throw err;
- console.log('Processing ' + scenes.length + ' scenes...');
- //displayResults(scenes);
- createStates(scenes);
- });
- }
- // Init on start
- init();
- // Activate scene
- on({id: /^javascript\.0\.PhilipsHue.Scenes\./, val: true}, function (obj) {
- if (obj.id == 'javascript.0.PhilipsHue.Scenes.Resync'){return}
- sceneId = obj.id.split('.').pop();
- console.log('Activating '+obj.name);
- api.activateScene(sceneId, function(err, result) {
- if (err) throw err;
- displayResults(result);
- });
- setState(obj.id, false);
- });
- // Resync
- on({id: 'javascript.0.PhilipsHue.Scenes.Resync', val: true}, function (obj) {
- console.log('Resync triggered...');
- groups_ = [];
- lights_ = [];
- deleteStates();
- init();
- });
- schedule("0 3 * * *", function () {
- console.log('Resync triggered...');
- groups_ = [];
- lights_ = [];
- deleteStates();
- init();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement