/**
 * Copyright 2013 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

var RED = require(process.env.NODE_RED_HOME+"/red/red");
var SunCalc = require('suncalc');

function SunNode(n) {
    RED.nodes.createNode(this,n);
    this.lat = n.lat;
    this.lon = n.lon;
    this.start = n.start;
    this.end = n.end;

    var node = this;
    var oldval = null;

    this.tick = setInterval(function() {
        var now = new Date();
        var hour = now.getUTCHours();
        var mins = now.getUTCMinutes();
        var times = SunCalc.getTimes(now, node.lat, node.lon);
        var hour1 = times[node.start].getUTCHours();
        var mins1 = times[node.start].getUTCMinutes();
        var hour2 = times[node.end].getUTCHours();
        var mins2 = times[node.end].getUTCMinutes();
        var e1 = (hour*60+mins) - (hour1*60+mins1);
        var e2 = (hour*60+mins) - (hour2*60+mins2);
        var moon = SunCalc.getMoonIllumination(now).fraction;
        msg = { payload:0, topic:"sun", moon:moon };
        if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; }
        if (oldval == null) { oldval = msg.payload; }
        if (msg.payload != oldval) {
            oldval = msg.payload;
            msg2 = msg;
            node.send( [msg,msg2] );
        }
        else { node.send(msg); }
    }, 60000);

    this.on("close", function() {
        clearInterval(this.tick);
    });
}
RED.nodes.registerType("sunrise",SunNode);

@@ -589,65 +589,45 @@
 */%0A%0A
-var RED = require(process.env.NODE_RED_HOME+%22/red/red%22);%0A
+module.exports = function(RED) %7B%0A    
 var 
@@ -657,16 +657,20 @@
 alc');%0A%0A
+    
 function
@@ -687,16 +687,20 @@
 ) %7B%0A    
+ 
+   
 RED.node
@@ -721,24 +721,28 @@
 his,n);%0A    
+   
+ 
 this.lat = n
@@ -747,24 +747,28 @@
  n.lat;%0A    
+  
+  
 this.lon = n
@@ -773,24 +773,28 @@
  n.lon;%0A    
+ 
+   
 this.start =
@@ -803,24 +803,28 @@
 .start;%0A    
+   
+ 
 this.end = n
@@ -830,16 +830,20 @@
 n.end;%0A%0A
+    
     var 
@@ -855,24 +855,28 @@
 = this;%0A    
+ 
+   
 var oldval =
@@ -883,24 +883,28 @@
  null;%0A%0A    
+   
+ 
 this.tick = 
@@ -932,24 +932,28 @@
 ) %7B%0A        
+    
 var now = ne
@@ -962,32 +962,36 @@
 Date();%0A        
+ 
+   
 var hour = now.g
@@ -1004,32 +1004,36 @@
 ours();%0A        
+   
+ 
 var mins = now.g
@@ -1040,32 +1040,36 @@
 etUTCMinutes();%0A
+    
         var time
@@ -1115,32 +1115,36 @@
 e.lon);%0A        
+ 
+   
 var hour1 = time
@@ -1172,32 +1172,36 @@
 ours();%0A        
+   
+ 
 var mins1 = time
@@ -1231,32 +1231,36 @@
 utes();%0A        
+    
 var hour2 = time
@@ -1286,32 +1286,36 @@
 ours();%0A        
+ 
+   
 var mins2 = time
@@ -1347,24 +1347,28 @@
 ();%0A        
+   
+ 
 var e1 = (ho
@@ -1399,16 +1399,20 @@
 mins1);%0A
+    
         
@@ -1459,24 +1459,28 @@
 2);%0A        
+ 
+   
 var moon = S
@@ -1529,16 +1529,20 @@
         
+   
+ 
 msg = %7B 
@@ -1578,16 +1578,20 @@
 moon %7D;%0A
+    
         
@@ -1640,24 +1640,28 @@
 ; %7D%0A        
+ 
+   
 if (oldval =
@@ -1690,24 +1690,203 @@
 .payload; %7D%0A
+            if (msg.payload == 1) %7B node.status(%7Bfill:%22yellow%22,shape:%22dot%22,text:%22day%22%7D,true); %7D%0A            else %7B node.status(%7Bfill:%22blue%22,shape:%22dot%22,text:%22night%22%7D,true); %7D%0A    
         if (
@@ -1906,24 +1906,28 @@
 = oldval) %7B%0A
+    
             
@@ -1960,16 +1960,20 @@
         
+    
 msg2 = m
@@ -1988,16 +1988,20 @@
         
+  
+  
 node.sen
@@ -2017,16 +2017,20 @@
 sg2%5D );%0A
+    
         
@@ -2031,32 +2031,36 @@
       %7D%0A        
+    
 else %7B node.send
@@ -2072,16 +2072,20 @@
 ; %7D%0A    
+  
+  
 %7D, 60000
@@ -2084,24 +2084,28 @@
 %7D, 60000);%0A%0A
+    
     this.on(
@@ -2134,16 +2134,20 @@
         
+    
 clearInt
@@ -2172,14 +2172,26 @@
     
+  
+  
 %7D);%0A
-%7D%0A
+    %7D%0A    
 RED.
@@ -2229,8 +2229,10 @@
 nNode);%0A
+%7D%0A

add some status to suncalc (to see how it looks)