mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	Merge pull request #46 from hdoukas/master
code re-write, setting brightness added
This commit is contained in:
		@@ -13,6 +13,7 @@
 | 
				
			|||||||
  See the License for the specific language governing permissions and
 | 
					  See the License for the specific language governing permissions and
 | 
				
			||||||
  limitations under the License.
 | 
					  limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-->
 | 
					-->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script type="text/x-red" data-template-name="Discover">
 | 
					<script type="text/x-red" data-template-name="Discover">
 | 
				
			||||||
@@ -28,27 +29,25 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
<!-- Next, some simple help text is provided for the node.                   -->
 | 
					<!-- Next, some simple help text is provided for the node.                   -->
 | 
				
			||||||
<script type="text/x-red" data-help-name="Discover">
 | 
					<script type="text/x-red" data-help-name="Discover">
 | 
				
			||||||
    <p>This node looks for a Philips Hue Bridge in the local network.</p>
 | 
					    <p>This node looks for a Philips Hue Bridge in the local network.</p><p> The node has 2 outputs, the first one contains the IP address of the first discovered bridge and the second one the lights registered to that bridge (in JSON format).</p> <p>To use the node you need to have obtained a valid auth token (or username) from your Philips Hue Bridge. Read <a href="http://developers.meethue.com/gettingstarted.html" target="_blank">here</a> on how to do this.</p>
 | 
				
			||||||
    <p> The node has 2 outputs, the first one contains the IP address of the first discovered bridge and the second one the lights registered to that bridge (in JSON format).</p>
 | 
					 | 
				
			||||||
    <p>To use the node you need to have obtained a valid auth token (or username) from your Philips Hue Bridge. Read <a href="http://developers.meethue.com/gettingstarted.html" target="_blank">here</a> on how to do this.</p>
 | 
					 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<!-- Finally, the node type is registered along with all of its properties   -->
 | 
					<!-- Finally, the node type is registered along with all of its properties   -->
 | 
				
			||||||
<script type="text/javascript">
 | 
					<script type="text/javascript">
 | 
				
			||||||
    RED.nodes.registerType('Discover',{
 | 
					    RED.nodes.registerType('Discover',{
 | 
				
			||||||
        category: 'advanced-input',
 | 
					        category: 'advanced-input',      // the palette category
 | 
				
			||||||
        color:"#EFEFEF",
 | 
					        color:"#EFEFEF",
 | 
				
			||||||
        defaults: {
 | 
					        defaults: {             // defines the editable properties of the node
 | 
				
			||||||
            name: {value:""},
 | 
					            name: {value:""},   //  along with default values.
 | 
				
			||||||
            username: {value:"", required:true}
 | 
					            username: {value:"", required:true}
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        inputs:1,
 | 
					        inputs:1,                // set the number of inputs - only 0 or 1
 | 
				
			||||||
        outputs:2,
 | 
					        outputs:2,               // set the number of outputs - 0 to n
 | 
				
			||||||
        icon: "hue.png",
 | 
					        icon: "huediscover.png",    // set the icon (held in public/icons)
 | 
				
			||||||
        label: function() {
 | 
					        label: function() {      // sets the default label contents
 | 
				
			||||||
            return this.name||"Discover";
 | 
					            return this.name||this.topic||"Discover";
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        labelStyle: function() {
 | 
					        labelStyle: function() { // sets the class to apply to the label
 | 
				
			||||||
            return this.name?"node_label_italic":"";
 | 
					            return this.name?"node_label_italic":"";
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,6 +13,7 @@
 | 
				
			|||||||
  See the License for the specific language governing permissions and
 | 
					  See the License for the specific language governing permissions and
 | 
				
			||||||
  limitations under the License.
 | 
					  limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-->
 | 
					-->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script type="text/x-red" data-template-name="HueNode">
 | 
					<script type="text/x-red" data-template-name="HueNode">
 | 
				
			||||||
@@ -35,6 +36,11 @@
 | 
				
			|||||||
    </select>
 | 
					    </select>
 | 
				
			||||||
  </div>
 | 
					  </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  <div class="form-row">
 | 
				
			||||||
 | 
					    <label for="node-input-brightness"><i class="icon-tag"></i>Change Brightness (0->100):</label>
 | 
				
			||||||
 | 
					    <input type="text" id="node-input-brightness" placeholder="brightness">
 | 
				
			||||||
 | 
					  </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  <div class="form-row">
 | 
					  <div class="form-row">
 | 
				
			||||||
    <label for="node-input-color"><i class="icon-tag"></i>Select color:</label>
 | 
					    <label for="node-input-color"><i class="icon-tag"></i>Select color:</label>
 | 
				
			||||||
    <input type="text" id="node-input-color" placeholder="color">
 | 
					    <input type="text" id="node-input-color" placeholder="color">
 | 
				
			||||||
@@ -50,29 +56,30 @@
 | 
				
			|||||||
<script type="text/x-red" data-help-name="HueNode">
 | 
					<script type="text/x-red" data-help-name="HueNode">
 | 
				
			||||||
    <p>This node implements some basic functionality for managing a Philips Hue wireless Lamp system.</p>
 | 
					    <p>This node implements some basic functionality for managing a Philips Hue wireless Lamp system.</p>
 | 
				
			||||||
    <p>To use it you need to have obtained a valid auth token (or username) from your Philips Hue Bridge. Read <a href="http://developers.meethue.com/gettingstarted.html" target="_blank">here</a> on how to do this.</p>
 | 
					    <p>To use it you need to have obtained a valid auth token (or username) from your Philips Hue Bridge. Read <a href="http://developers.meethue.com/gettingstarted.html" target="_blank">here</a> on how to do this.</p>
 | 
				
			||||||
    <p>You can enter the ID (1, 2, ...) of a Lamp and turn it ON or OFF and also set its color. </p><p>By setting the status to AUTO, you can set the ON/OFF status as a message payload (e.g., msg.payload="ON") and the color through the message topic (e.g., msg.topic="EBF5FF") on the node input. Please note, in case you use both, the status selection overides the msg.payload!</p><p>Also, if you pass something like msg.payload="ALERT" the Lamp will flash once.</p>
 | 
					    <p>You can enter the ID (1, 2, ...) of a Lamp and turn it ON or OFF, set the color and the brightness (0->100). </p><p>By setting the status to AUTO, you can set the ON/OFF status as a message topic (e.g., msg.topic="1:ON", where 1 is the ID of the Lamp) and the color/brightness through the message payload (e.g., msg.payload="DF0101:50" will set the color to red and brightness to 50%) on the node input. Please note, in case you use both, the msg.payload overrides the node configuration through the UI!</p><p>Also, if you pass something like msg.topic="1:ALERT" the Lamp with ID 1 will flash once.</p>
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<!-- Finally, the node type is registered along with all of its properties   -->
 | 
					<!-- Finally, the node type is registered along with all of its properties   -->
 | 
				
			||||||
<script type="text/javascript">
 | 
					<script type="text/javascript">
 | 
				
			||||||
    RED.nodes.registerType('HueNode',{
 | 
					    RED.nodes.registerType('HueNode',{
 | 
				
			||||||
        category: 'advanced-input',
 | 
					        category: 'advanced-input',      // the palette category
 | 
				
			||||||
        color:"#EFEFEF",
 | 
					        color:"#EFEFEF",
 | 
				
			||||||
        defaults: {
 | 
					        defaults: {             // defines the editable properties of the node
 | 
				
			||||||
            name: {value:""},
 | 
					            name: {value:""},   //  along with default values.
 | 
				
			||||||
            username: {value:"", required:true},
 | 
					            username: {value:"", required:true},
 | 
				
			||||||
            discovery_mode: {value: "", required:false},
 | 
					            discovery_mode: {value: "", required:false},
 | 
				
			||||||
            lamp_id: {value:"", required:false},
 | 
					            lamp_id: {value:"", required:false},
 | 
				
			||||||
            color: {value:"EBF5FF"},
 | 
					            color: {value:"EBF5FF"},
 | 
				
			||||||
 | 
					            brightness: {value:"100"},
 | 
				
			||||||
            lamp_status:{}
 | 
					            lamp_status:{}
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        inputs:1,
 | 
					        inputs:1,                // set the number of inputs - only 0 or 1
 | 
				
			||||||
        outputs:1,
 | 
					        outputs:1,               // set the number of outputs - 0 to n
 | 
				
			||||||
        icon: "hue.png",
 | 
					        icon: "huemanage.png",    // set the icon (held in public/icons)
 | 
				
			||||||
        label: function() {
 | 
					        label: function() {      // sets the default label contents
 | 
				
			||||||
            return this.name||"HueNode";
 | 
					            return this.name||this.topic||"HueNode";
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        labelStyle: function() {
 | 
					        labelStyle: function() { // sets the class to apply to the label
 | 
				
			||||||
            return this.name?"node_label_italic":"";
 | 
					            return this.name?"node_label_italic":"";
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,14 +18,19 @@
 | 
				
			|||||||
 * limitations under the License.
 | 
					 * limitations under the License.
 | 
				
			||||||
 **/
 | 
					 **/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//Require node-hue-api
 | 
					//Require node-hue-api
 | 
				
			||||||
var hue = require("node-hue-api");
 | 
					var hue = require("node-hue-api");
 | 
				
			||||||
var HueApi = require("node-hue-api").HueApi;
 | 
					var HueApi = require("node-hue-api").HueApi;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Require main module
 | 
				
			||||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
 | 
					var RED = require(process.env.NODE_RED_HOME+"/red/red");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//store the IP address of the Hue Gateway
 | 
					//store the IP address of the Hue Gateway
 | 
				
			||||||
var gw_ipaddress = "";
 | 
					var gw_ipaddress = "";
 | 
				
			||||||
var username, lamp_status, lamp_id, color;
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var username, lamp_status, lamp_id, color, brightness;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function hexToRgb(hex) {
 | 
					function hexToRgb(hex) {
 | 
				
			||||||
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
 | 
					    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
 | 
				
			||||||
@@ -36,74 +41,110 @@ function hexToRgb(hex) {
 | 
				
			|||||||
    } : null;
 | 
					    } : null;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// The main node definition - most things happen in here
 | 
					// The main node definition - most things happen in here
 | 
				
			||||||
function HueNode(n) {
 | 
					function HueNode(n) {
 | 
				
			||||||
    // Create a RED node
 | 
					    // Create a RED node
 | 
				
			||||||
    RED.nodes.createNode(this,n);
 | 
					    RED.nodes.createNode(this,n);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var node = this;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //get parameters from user
 | 
					    //get parameters from user
 | 
				
			||||||
    this.username = n.username;
 | 
					    this.username = n.username;
 | 
				
			||||||
    this.lamp_status = n.lamp_status;
 | 
					    this.lamp_status = n.lamp_status;
 | 
				
			||||||
    this.lamp_id = n.lamp_id;
 | 
					    this.lamp_id = n.lamp_id;
 | 
				
			||||||
    this.color = n.color;
 | 
					    this.color = n.color;
 | 
				
			||||||
    var node = this;
 | 
					    this.brightness = n.brightness;
 | 
				
			||||||
 | 
					   
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Store local copies of the node configuration (as defined in the .html)
 | 
				
			||||||
 | 
					    this.topic = n.topic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    var msg = {};
 | 
					    var msg = {};
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    msg.topic = this.topic;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.on("input", function(msg){
 | 
					    this.on("input", function(msg){
 | 
				
			||||||
        //set the lamp status
 | 
					        var myMsg = msg;
 | 
				
			||||||
        //first locate the Hue gateway:
 | 
					            //set the lamp status
 | 
				
			||||||
        hue.locateBridges(function(err, result) {
 | 
					            //first locate the Hue gateway:
 | 
				
			||||||
 | 
					            hue.locateBridges(function(err, result) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var msg2 = {};
 | 
					                var msg2 = {};
 | 
				
			||||||
            msg2.topic = this.topic;
 | 
					                msg2.topic = this.topic;
 | 
				
			||||||
            if (err) throw err;
 | 
					                if (err) throw err;
 | 
				
			||||||
            //check for found bridges
 | 
					                //check for found bridges
 | 
				
			||||||
            if(result[0]!=null) {
 | 
					                if(result[0]!=null) {
 | 
				
			||||||
                //save the IP address of the 1st bridge found
 | 
					                    //save the IP address of the 1st bridge found
 | 
				
			||||||
                this.gw_ipaddress = result[0].ipaddress;
 | 
					                    this.gw_ipaddress = result[0].ipaddress;
 | 
				
			||||||
 | 
					 | 
				
			||||||
                //set light status
 | 
					 | 
				
			||||||
                var api = new HueApi(this.gw_ipaddress, node.username);
 | 
					 | 
				
			||||||
                var lightState = hue.lightState;
 | 
					 | 
				
			||||||
                var state = lightState.create();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                var status;
 | 
					 | 
				
			||||||
                if(msg.payload=="ALERT"){
 | 
					 | 
				
			||||||
                    status = "ALERT";
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                else if(node.lamp_status=="ON" || msg.payload=="ON") status = "ON";
 | 
					 | 
				
			||||||
                else if(node.lamp_status=="OFF" || msg.payload=="OFF") status = "OFF";
 | 
					 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if(status=="ALERT") {
 | 
					                    //set light status
 | 
				
			||||||
                    api.setLightState(node.lamp_id, state.alert()).then(displayResult).fail(displayError).done();
 | 
					                    var api = new HueApi(this.gw_ipaddress, node.username);
 | 
				
			||||||
                }
 | 
					                    var lightState = hue.lightState;
 | 
				
			||||||
                else if(status=="ON") {
 | 
					                    var state = lightState.create();
 | 
				
			||||||
                    if(node.color==null) {
 | 
					
 | 
				
			||||||
                        api.setLightState(node.lamp_id, state.on().rgb(hexToRgb(msg.topic).r,hexToRgb(msg.topic).g,hexToRgb(msg.topic).b)).then(displayResult).fail(displayError).done();
 | 
					                    var status;
 | 
				
			||||||
 | 
					                    var lamp = -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    //check for AUTO status (lamp settings set through node input)
 | 
				
			||||||
 | 
					                    if(node.lamp_status=="AUTO") {
 | 
				
			||||||
 | 
					                        var color;
 | 
				
			||||||
 | 
					                        var brightness;
 | 
				
			||||||
 | 
					                        //check for lamp ID in the topic
 | 
				
			||||||
 | 
					                        if(myMsg.topic.length>1) {
 | 
				
			||||||
 | 
					                            var tmp_status = myMsg.topic.split(":");
 | 
				
			||||||
 | 
					                            myMsg.topic = tmp_status[1];
 | 
				
			||||||
 | 
					                            lamp = tmp_status[0];
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        //check for brightness & color:
 | 
				
			||||||
 | 
					                        if(myMsg.payload.length>1) {
 | 
				
			||||||
 | 
					                            var tmp_topic = myMsg.payload.split(":");
 | 
				
			||||||
 | 
					                            color = tmp_topic[0];
 | 
				
			||||||
 | 
					                            brightness = tmp_topic[1];
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        //case of ALERT:
 | 
				
			||||||
 | 
					                        if(myMsg.topic=="ALERT"){
 | 
				
			||||||
 | 
					                            api.setLightState(lamp, state.alert()).then(displayResult).fail(displayError).done();
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        //case of ON:
 | 
				
			||||||
 | 
					                        if(myMsg.topic=="ON") {
 | 
				
			||||||
 | 
					                            api.setLightState(lamp, state.on().rgb(hexToRgb(color).r,hexToRgb(color).g,hexToRgb(color).b).brightness(brightness)).then(displayResult).fail(displayError).done();
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        else {
 | 
				
			||||||
 | 
					                            api.setLightState(lamp, state.off()).then(displayResult).fail(displayError).done();
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    else {
 | 
					                    else {
 | 
				
			||||||
                        api.setLightState(node.lamp_id, state.on().rgb(hexToRgb(node.color).r,hexToRgb(node.color).g,hexToRgb(node.color).b)).then(displayResult).fail(displayError).done();
 | 
					                        //set lamp according to node settings
 | 
				
			||||||
 | 
					                        if(node.lamp_status=="ON") 
 | 
				
			||||||
 | 
					                             api.setLightState(node.lamp_id, state.on().rgb(hexToRgb(node.color).r,hexToRgb(node.color).g,hexToRgb(node.color).b).brightness(node.brightness)).then(displayResult).fail(displayError).done();
 | 
				
			||||||
 | 
					                        else
 | 
				
			||||||
 | 
					                            api.setLightState(node.lamp_id, state.off()).then(displayResult).fail(displayError).done();
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if(lamp!=-1)
 | 
				
			||||||
 | 
					                        msg2.payload = 'Light with ID: '+lamp+ ' was set to '+myMsg.payload;
 | 
				
			||||||
 | 
					                    else
 | 
				
			||||||
 | 
					                        msg2.payload = 'Light with ID: '+node.lamp_id+ ' was set to '+node.lamp_status;
 | 
				
			||||||
 | 
					                    node.send(msg2);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                else {
 | 
					                else {
 | 
				
			||||||
                    api.setLightState(node.lamp_id, state.off()).then(displayResult).fail(displayError).done();
 | 
					                    //bridge not found:
 | 
				
			||||||
 | 
					                    var msg = {};
 | 
				
			||||||
 | 
					                    msg.payload = "Bridge not found!";
 | 
				
			||||||
 | 
					                    node.send(msg);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                msg2.payload = 'Light with ID: '+node.lamp_id+ ' was set to '+status;
 | 
					            });
 | 
				
			||||||
                node.send(msg2);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            else {
 | 
					 | 
				
			||||||
                //bridge not found:
 | 
					 | 
				
			||||||
                var msg = {};
 | 
					 | 
				
			||||||
                msg.payload = "Bridge not found!";
 | 
					 | 
				
			||||||
                node.send(msg);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.on("close", function() {
 | 
					    this.on("close", function() {
 | 
				
			||||||
        // Called when the node is shutdown - eg on redeploy.
 | 
					        // Called when the node is shutdown - eg on redeploy.
 | 
				
			||||||
        // Allows ports to be closed, connections dropped etc.
 | 
					        // Allows ports to be closed, connections dropped etc.
 | 
				
			||||||
@@ -121,6 +162,9 @@ var displayError = function(err) {
 | 
				
			|||||||
    console.error(err);
 | 
					    console.error(err);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Register the node by name. This must be called before overriding any of the
 | 
					// Register the node by name. This must be called before overriding any of the
 | 
				
			||||||
// Node functions.
 | 
					// Node functions.
 | 
				
			||||||
RED.nodes.registerType("HueNode",HueNode);
 | 
					RED.nodes.registerType("HueNode",HueNode);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user