mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	Allow hue lamp to specify IP manually...
for those cases where discovery doesn't...
This commit is contained in:
		@@ -17,6 +17,7 @@
 | 
			
		||||
-->
 | 
			
		||||
 | 
			
		||||
<script type="text/x-red" data-template-name="HueNode">
 | 
			
		||||
 | 
			
		||||
  <div class="form-row">
 | 
			
		||||
     <label for="node-input-topic"><i class="fa fa-tasks"></i>Hue App Username:</label>
 | 
			
		||||
     <input type="text" id="node-input-username" placeholder="username">
 | 
			
		||||
@@ -28,7 +29,12 @@
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
  <div class="form-row">
 | 
			
		||||
    <label for="node-input-name"><i class="fa fa-tag"></i>Lamp Status:</label>
 | 
			
		||||
     <label for="node-input-ip_address"><i class="fa fa-tag"></i>Hue Bridge IP Address:</label>
 | 
			
		||||
     <input type="text" id="node-input-ip_address" placeholder="IP Address (optional)">
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
  <div class="form-row">
 | 
			
		||||
    <label for="node-input-lamp_status"><i class="fa fa-tag"></i>Lamp Status:</label>
 | 
			
		||||
    <select id="node-input-lamp_status" placeholder="lamp_status">
 | 
			
		||||
    <option value="AUTO">AUTO</option>
 | 
			
		||||
    <option value="ON">ON</option>
 | 
			
		||||
@@ -65,6 +71,8 @@
 | 
			
		||||
    </ul>
 | 
			
		||||
 | 
			
		||||
    <p>Please note, by setting the status to AUTO on the node configuration, the rest of the node parameters are ignored, you need to set all parameters through the message input.</p>
 | 
			
		||||
    <p>By default, the node will search for Philips Hue Bridges. However, by specifying an IP address, you can find Hue systems when this may not be possible due to network configuration.
 | 
			
		||||
    </p>
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<!-- Finally, the node type is registered along with all of its properties   -->
 | 
			
		||||
@@ -77,6 +85,7 @@
 | 
			
		||||
            username: {value:"", required:true},
 | 
			
		||||
            discovery_mode: {value: "", required:false},
 | 
			
		||||
            lamp_id: {value:"", required:false},
 | 
			
		||||
            ip_address: {value:"", required:false},
 | 
			
		||||
            color: {value:"EBF5FF"},
 | 
			
		||||
            brightness: {value:"100"},
 | 
			
		||||
            lamp_status:{}
 | 
			
		||||
 
 | 
			
		||||
@@ -41,6 +41,69 @@ function hexToRgb(hex) {
 | 
			
		||||
    } : null;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function setLights(node, myMsg) {
 | 
			
		||||
 | 
			
		||||
    var msg2 = {};
 | 
			
		||||
    msg2.topic = this.topic;
 | 
			
		||||
 | 
			
		||||
    //set light status
 | 
			
		||||
    var api = new HueApi(node.gw_ipaddress, node.username);
 | 
			
		||||
    var lightState = hue.lightState;
 | 
			
		||||
    var state = lightState.create();
 | 
			
		||||
 | 
			
		||||
    var status;
 | 
			
		||||
    var lamp = -1;
 | 
			
		||||
 | 
			
		||||
    //check for AUTO status (lamp settings set through node input)
 | 
			
		||||
    if(node.lamp_status=="AUTO") {
 | 
			
		||||
        var color;
 | 
			
		||||
        var brightness;
 | 
			
		||||
 | 
			
		||||
        //get lamp id from msg.lamp:
 | 
			
		||||
        lamp = myMsg.lamp;
 | 
			
		||||
 | 
			
		||||
        //get brightness:
 | 
			
		||||
        brightness = myMsg.brightness;
 | 
			
		||||
 | 
			
		||||
        //get colour either from msg.color or msg.topic
 | 
			
		||||
        if(myMsg.color!=null && myMsg.color.length>0) {
 | 
			
		||||
            color = myMsg.color;
 | 
			
		||||
        }
 | 
			
		||||
        else if(myMsg.topic!=null && myMsg.topic.length>0) {
 | 
			
		||||
            color = myMsg.topic;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        //check the payload for on/off/alert:
 | 
			
		||||
        //case of ALERT:
 | 
			
		||||
        if(myMsg.payload=="ALERT" || myMsg.payload=="alert"){
 | 
			
		||||
            api.setLightState(lamp, state.alert()).then(displayResult).fail(displayError).done();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //case of ON:
 | 
			
		||||
        if(myMsg.payload=="ON" || myMsg.payload=="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 {
 | 
			
		||||
        //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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// The main node definition - most things happen in here
 | 
			
		||||
function HueNode(n) {
 | 
			
		||||
@@ -53,89 +116,34 @@ function HueNode(n) {
 | 
			
		||||
    this.username = n.username;
 | 
			
		||||
    this.lamp_status = n.lamp_status;
 | 
			
		||||
    this.lamp_id = n.lamp_id;
 | 
			
		||||
    this.gw_ipaddress = n.ip_address;
 | 
			
		||||
    this.color = n.color;
 | 
			
		||||
    this.brightness = n.brightness;
 | 
			
		||||
   
 | 
			
		||||
 | 
			
		||||
    // Store local copies of the node configuration (as defined in the .html)
 | 
			
		||||
    this.topic = n.topic;
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    var msg = {};
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    msg.topic = this.topic;
 | 
			
		||||
 | 
			
		||||
    this.on("input", function(msg){
 | 
			
		||||
        var myMsg = msg;
 | 
			
		||||
            //set the lamp status
 | 
			
		||||
        //set the lamp status
 | 
			
		||||
        if (this.gw_ipaddress) {
 | 
			
		||||
            setLights(node, myMsg);
 | 
			
		||||
        } else {
 | 
			
		||||
            //first locate the Hue gateway:
 | 
			
		||||
            hue.locateBridges(function(err, result) {
 | 
			
		||||
 | 
			
		||||
                var msg2 = {};
 | 
			
		||||
                msg2.topic = this.topic;
 | 
			
		||||
 | 
			
		||||
                if (err) throw err;
 | 
			
		||||
                //check for found bridges
 | 
			
		||||
                if(result[0]!=null) {
 | 
			
		||||
                    //save the IP address of the 1st bridge found
 | 
			
		||||
                    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;
 | 
			
		||||
                    var lamp = -1;
 | 
			
		||||
 | 
			
		||||
                    //check for AUTO status (lamp settings set through node input)
 | 
			
		||||
                    if(node.lamp_status=="AUTO") {
 | 
			
		||||
                        var color;
 | 
			
		||||
                        var brightness;
 | 
			
		||||
                        
 | 
			
		||||
                        //get lamp id from msg.lamp:
 | 
			
		||||
                        lamp = myMsg.lamp;
 | 
			
		||||
 | 
			
		||||
                        //get brightness:
 | 
			
		||||
                        brightness = myMsg.brightness;
 | 
			
		||||
 | 
			
		||||
                        //get colour either from msg.color or msg.topic
 | 
			
		||||
                        if(myMsg.color!=null && myMsg.color.length>0) {
 | 
			
		||||
                            color = myMsg.color;
 | 
			
		||||
                        } 
 | 
			
		||||
                        else if(myMsg.topic!=null && myMsg.topic.length>0) {
 | 
			
		||||
                            color = myMsg.topic;
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                        //check the payload for on/off/alert:
 | 
			
		||||
                        //case of ALERT:
 | 
			
		||||
                        if(myMsg.payload=="ALERT" || myMsg.payload=="alert"){
 | 
			
		||||
                            api.setLightState(lamp, state.alert()).then(displayResult).fail(displayError).done();
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        //case of ON:
 | 
			
		||||
                        if(myMsg.payload=="ON" || myMsg.payload=="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 {
 | 
			
		||||
                        //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);
 | 
			
		||||
                    setLights(node, myMsg);
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    //bridge not found:
 | 
			
		||||
@@ -145,6 +153,7 @@ function HueNode(n) {
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user