<commit_before>var app = angular.module('huewi', ['ngAnimate']);

function StateToHTMLColor(State, Model)
{
  function ToHexString(In) {
    var Result = Math.floor(In).toString(16);
    return Result.length == 1 ? "0" + Result : Result;
  }

  if (State) { // Group 0 (All available lights) doesn't have properties
    Model = Model || "LCT001";
    var RGB;
    var xy;

    if (State.colormode === 'hs') {
      RGB = huepi.HelperHueAngSatBritoRGB(State.hue * 360 / 65535, State.sat / 255, State.bri / 255);
      xy = huepi.HelperRGBtoXY(RGB.Red, RGB.Green, RGB.Blue);
      RGB = huepi.HelperXYtoRGBforModel(xy.x, xy.y, State.bri / 255, Model);
    } else if (State.colormode === 'xy') {
      RGB = huepi.HelperXYtoRGBforModel(State.xy[0], State.xy[1], State.bri / 255, Model);
    } else if (State.colormode === 'ct') {
      RGB = huepi.HelperColortemperaturetoRGB(Math.round(1000000 / State.ct));
      xy = huepi.HelperRGBtoXY(RGB.Red, RGB.Green, RGB.Blue);
      RGB = huepi.HelperXYtoRGBforModel(xy.x, xy.y, State.bri / 255, Model);
    }
    return "#" + ToHexString(RGB.Red * 255) + ToHexString(RGB.Green * 255) + ToHexString(RGB.Blue * 255);
  } else return "#ffcc88"; 
}



(function () {


angular.module('huewi').factory('hueConnector', function ($rootScope) {
  var MyHue = new huepi();
  MyHue.Username = '085efe879ee3ed83c04efc28a0da03d3';
  var HeartbeatInterval;
  var Status = '';
  var PrevStatus = '';

  if (window.isCordovaApp) {
    document.addEventListener("deviceready", onStartup, false);
    document.addEventListener("pause", onPause, false);
    document.addEventListener("resume", onResume, false);
  } else $(document).ready(onStartup);

  function onStartup() {
    //setTimeout(onResume(), 50);
    onResume();
  }

  function onResume() {
    TimeBasedGradientUpdate();
    HeartbeatInterval = setInterval(StatusHeartbeat, 2500);
    StatusHeartbeat(); // Execute Immediate Too!
  }

  function onPause() {
    clearInterval(HeartbeatInterval);
    $('#HueStatusbar').show(350);
  }

//delete localStorage.MyHueBridgeIP; // Force PortalDiscoverLocalBridges TESTCODE.
  function ConnectToHueBridge() {
    if (!localStorage.MyHueBridgeIP) { // No Cached BridgeIP?
      Status = 'Trying to Discover Bridge via Portal';
      $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
      MyHue.PortalDiscoverLocalBridges().then(function GetBridgeConfig() {
        MyHue.BridgeGetData().then(function EnsureWhitelisting() {
          if (!MyHue.BridgeUsernameWhitelisted) {
            Status = 'Please press connect button on Bridge';
            $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
            MyHue.BridgeCreateUser().then(function ReReadBridgeConfiguration() {
              Status = 'Connected';
              $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
            }, function UnableToCreateUseronBridge() {
              Status = 'Unable to Create User on Bridge';
              $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
            });
          } else {
            localStorage.MyHueBridgeIP = MyHue.BridgeIP; // Cache BridgeIP
            Status = 'Connected';
            $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
//MyHue.BridgeDeleteUser(MyHue.Username); // Force buttonpress on next Startup TESTCODE.
          }
        }, function UnableToRetreiveBridgeConfiguration() {
          Status = 'Unable to Retreive Bridge Configuration';
          $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
        });
      }, function UnableToDiscoverLocalBridgesViaPortal() {
        Status = 'Unable to find Local Bridge via Portal';
        $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
      });
    } else {
      MyHue.BridgeIP = localStorage.MyHueBridgeIP;
      MyHue.BridgeGetData().then(function CheckWhitelisting() {
        if (MyHue.BridgeUsernameWhitelisted) {
          Status = 'Connected';
          $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
        } else {
          delete localStorage.MyHueBridgeIP;
          Status = 'Not Whitelisted anymore';
          $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
        }
      }, function ErrorGettingCachedBridgeData() {
        delete localStorage.MyHueBridgeIP;
        Status = 'Not found anymore';
        $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
      });
    }
  }

  function StatusHeartbeat() {
    if (Status != 'Connected') {
      ConnectToHueBridge();
    } else {
      if (PrevStatus != Status) {
        $('#HueStatusbar').slideUp(750);
      }
      MyHue.BridgeGetData().then(function UpdateUI() {
        $rootScope.$emit('huewiUpdate'); // huewiUpdate as in new data from Bridge.
      }, function BridgeGetDataFailed() {
        setTimeout(function() {
          Status = 'Disconnected';
          onPause();
          onResume();
        }, 1000);
      });
    }
    PrevStatus = Status;
  }

return {
    MyHue: function () {
      return MyHue;
    },
    Status: function() {
      return Status;
    }
  };
});


})();



(function () {

  
angular.module('huewi').controller('HueStatusController', function($rootScope, $scope, hueConnector) {
  $scope.MyHue = hueConnector.MyHue(); // For conveinient usage of MyHue in HTML within this controllers $scope
window.MyHue = hueConnector.MyHue(); // For Debugging TESTCODE
  $rootScope.$on('huewiUpdate', function(event, data) {
    $scope.Status = hueConnector.Status();
    $scope.$apply(); 
  });

});


})();



(function () {


angular.module('huewi').controller('MenuController', function($rootScope, $scope, hueConnector) {
  $scope.MenuItem = 'Connecting';
  
  $scope.SetMenuItem = function(NewItem, NewIndex) {

    $scope.MenuItem = NewItem;
    if ($scope.MenuItem === '') // No Overlay selected
      $('body').css('overflow', 'initial'); // Enable scrolling of the <Body>
    else $('body').css('overflow', 'hidden'); // Disable scrolling of the <Body>

    if ($scope.MenuItem === 'Group')
      hueConnector.MyHue().GroupAlertSelect(NewIndex);
    if ($scope.MenuItem === 'Light')
      hueConnector.MyHue().LightAlertSelect(NewIndex);
    
    $scope.$broadcast('MenuUpdate', NewIndex);
  };
});


})();



(function () {

  
angular.module('huewi').controller('TabController', function($scope) {
  $scope.Tab = 1;

  $scope.TabIsSet = function(CheckTab) {
    return $scope.Tab === CheckTab;
  };
  $scope.SetTab = function(SetTab) {
    $scope.Tab = SetTab;
  };
  $scope.GetTab = function() {
    return $scope.Tab;
  };
});


})();



(function () {

  
angular.module('huewi').directive("huewiGroups", function() {
  return {
    restrict: 'E',
    templateUrl: "huewi-groups.html"
  };
});

angular.module('huewi').controller('GroupsController', function($rootScope, $scope, hueConnector) {
  $scope.Groups = [{'name': 'All available lights', HTMLColor: "#ffcc88"}, {'name': 'Group1'}, {'name': 'Group2'}, {'name': 'Group3'}];

  $rootScope.$on('huewiUpdate', function(event, data) {
    if (hueConnector.MyHue().Groups.length) {
      $scope.Groups = _.toArray(hueConnector.MyHue().Groups);
      $scope.Groups.unshift({'name': 'All available lights'});
      _.each($scope.Groups, function(Group) {
        Group.HTMLColor = StateToHTMLColor(Group.action);
      });
      $scope.$apply();
    }
  });
});


})();



(function () {

  
angular.module('huewi').directive("huewiLights", function() {
  return {
    restrict: 'E',
    templateUrl: "huewi-lights.html"
  };
});

angular.module('huewi').controller('LightsController', function($rootScope, $scope, hueConnector) {
  $scope.Lights = [{'name': 'Light1'}, {'name': 'Light2'}, {'name': 'Light3'}];

  $rootScope.$on('huewiUpdate', function(event, data) {
    if (hueConnector.MyHue().Lights.length) {
      $scope.Lights = _.toArray(hueConnector.MyHue().Lights);
      _.each($scope.Lights, function(Light) {
        Light.HTMLColor = StateToHTMLColor(Light.state, Light.modelid);
      });
      $scope.$apply();
    }
  });
});


})();
<commit_msg>Fixed Update only if Lights & Groups received<commit_after>var app = angular.module('huewi', ['ngAnimate']);

function StateToHTMLColor(State, Model)
{
  function ToHexString(In) {
    var Result = Math.floor(In).toString(16);
    return Result.length == 1 ? "0" + Result : Result;
  }

  if (State) { // Group 0 (All available lights) doesn't have properties
    Model = Model || "LCT001";
    var RGB;
    var xy;

    if (State.colormode === 'hs') {
      RGB = huepi.HelperHueAngSatBritoRGB(State.hue * 360 / 65535, State.sat / 255, State.bri / 255);
      xy = huepi.HelperRGBtoXY(RGB.Red, RGB.Green, RGB.Blue);
      RGB = huepi.HelperXYtoRGBforModel(xy.x, xy.y, State.bri / 255, Model);
    } else if (State.colormode === 'xy') {
      RGB = huepi.HelperXYtoRGBforModel(State.xy[0], State.xy[1], State.bri / 255, Model);
    } else if (State.colormode === 'ct') {
      RGB = huepi.HelperColortemperaturetoRGB(Math.round(1000000 / State.ct));
      xy = huepi.HelperRGBtoXY(RGB.Red, RGB.Green, RGB.Blue);
      RGB = huepi.HelperXYtoRGBforModel(xy.x, xy.y, State.bri / 255, Model);
    }
    return "#" + ToHexString(RGB.Red * 255) + ToHexString(RGB.Green * 255) + ToHexString(RGB.Blue * 255);
  } else return "#ffcc88"; 
}



(function () {


angular.module('huewi').factory('hueConnector', function ($rootScope) {
  var MyHue = new huepi();
  MyHue.Username = '<PASSWORD>';
  var HeartbeatInterval;
  var Status = '';
  var PrevStatus = '';

  if (window.isCordovaApp) {
    document.addEventListener("deviceready", onStartup, false);
    document.addEventListener("pause", onPause, false);
    document.addEventListener("resume", onResume, false);
  } else $(document).ready(onStartup);

  function onStartup() {
    //setTimeout(onResume(), 50);
    onResume();
  }

  function onResume() {
    TimeBasedGradientUpdate();
    HeartbeatInterval = setInterval(StatusHeartbeat, 2500);
    StatusHeartbeat(); // Execute Immediate Too!
  }

  function onPause() {
    clearInterval(HeartbeatInterval);
    $('#HueStatusbar').show(350);
  }

//delete localStorage.MyHueBridgeIP; // Force PortalDiscoverLocalBridges TESTCODE.
  function ConnectToHueBridge() {
    if (!localStorage.MyHueBridgeIP) { // No Cached BridgeIP?
      Status = 'Trying to Discover Bridge via Portal';
      $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
      MyHue.PortalDiscoverLocalBridges().then(function GetBridgeConfig() {
        MyHue.BridgeGetData().then(function EnsureWhitelisting() {
          if (!MyHue.BridgeUsernameWhitelisted) {
            Status = 'Please press connect button on Bridge';
            $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
            MyHue.BridgeCreateUser().then(function ReReadBridgeConfiguration() {
              Status = 'Connected';
              $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
            }, function UnableToCreateUseronBridge() {
              Status = 'Unable to Create User on Bridge';
              $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
            });
          } else {
            localStorage.MyHueBridgeIP = MyHue.BridgeIP; // Cache BridgeIP
            Status = 'Connected';
            $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
//MyHue.BridgeDeleteUser(MyHue.Username); // Force buttonpress on next Startup TESTCODE.
          }
        }, function UnableToRetreiveBridgeConfiguration() {
          Status = 'Unable to Retreive Bridge Configuration';
          $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
        });
      }, function UnableToDiscoverLocalBridgesViaPortal() {
        Status = 'Unable to find Local Bridge via Portal';
        $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
      });
    } else {
      MyHue.BridgeIP = localStorage.MyHueBridgeIP;
      MyHue.BridgeGetData().then(function CheckWhitelisting() {
        if (MyHue.BridgeUsernameWhitelisted) {
          Status = 'Connected';
          $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
        } else {
          delete localStorage.MyHueBridgeIP;
          Status = 'Not Whitelisted anymore';
          $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
        }
      }, function ErrorGettingCachedBridgeData() {
        delete localStorage.MyHueBridgeIP;
        Status = 'Not found anymore';
        $rootScope.$emit('huewiUpdate'); // huewiUpdate as Status Update
      });
    }
  }

  function StatusHeartbeat() {
    if (Status != 'Connected') {
      ConnectToHueBridge();
    } else {
      if (PrevStatus != Status) {
        $('#HueStatusbar').slideUp(750);
      }
      MyHue.BridgeGetData().then(function UpdateUI() {
        $rootScope.$emit('huewiUpdate'); // huewiUpdate as in new data from Bridge.
      }, function BridgeGetDataFailed() {
        setTimeout(function() {
          Status = 'Disconnected';
          onPause();
          onResume();
        }, 1000);
      });
    }
    PrevStatus = Status;
  }

return {
    MyHue: function () {
      return MyHue;
    },
    Status: function() {
      return Status;
    }
  };
});


})();



(function () {

  
angular.module('huewi').controller('HueStatusController', function($rootScope, $scope, hueConnector) {
  $scope.MyHue = hueConnector.MyHue(); // For conveinient usage of MyHue in HTML within this controllers $scope
window.MyHue = hueConnector.MyHue(); // For Debugging TESTCODE
  $rootScope.$on('huewiUpdate', function(event, data) {
    $scope.Status = hueConnector.Status();
    $scope.$apply(); 
  });

});


})();



(function () {


angular.module('huewi').controller('MenuController', function($rootScope, $scope, hueConnector) {
  $scope.MenuItem = 'Connecting';
  
  $scope.SetMenuItem = function(NewItem, NewIndex) {

    $scope.MenuItem = NewItem;
    if ($scope.MenuItem === '') // No Overlay selected
      $('body').css('overflow', 'initial'); // Enable scrolling of the <Body>
    else $('body').css('overflow', 'hidden'); // Disable scrolling of the <Body>

    if ($scope.MenuItem === 'Group')
      hueConnector.MyHue().GroupAlertSelect(NewIndex);
    if ($scope.MenuItem === 'Light')
      hueConnector.MyHue().LightAlertSelect(NewIndex);
    
    $scope.$broadcast('MenuUpdate', NewIndex);
  };
});


})();



(function () {

  
angular.module('huewi').controller('TabController', function($scope) {
  $scope.Tab = 1;

  $scope.TabIsSet = function(CheckTab) {
    return $scope.Tab === CheckTab;
  };
  $scope.SetTab = function(SetTab) {
    $scope.Tab = SetTab;
  };
  $scope.GetTab = function() {
    return $scope.Tab;
  };
});


})();



(function () {

  
angular.module('huewi').directive("huewiGroups", function() {
  return {
    restrict: 'E',
    templateUrl: "huewi-groups.html"
  };
});

angular.module('huewi').controller('GroupsController', function($rootScope, $scope, hueConnector) {
  $scope.Groups = [{'name': 'All available lights', HTMLColor: "#ffcc88"}, {'name': 'Group1'}, {'name': 'Group2'}, {'name': 'Group3'}];

  $rootScope.$on('huewiUpdate', function(event, data) {
    if (hueConnector.MyHue().GroupIds.length >0) {
      $scope.Groups = _.toArray(hueConnector.MyHue().Groups);
      $scope.Groups.unshift({'name': 'All available lights'});
      _.each($scope.Groups, function(Group) {
        Group.HTMLColor = StateToHTMLColor(Group.action);
      });
      $scope.$apply();
    }
  });
 });


})();



(function () {

  
angular.module('huewi').directive("huewiLights", function() {
  return {
    restrict: 'E',
    templateUrl: "huewi-lights.html"
  };
});

angular.module('huewi').controller('LightsController', function($rootScope, $scope, hueConnector) {
  $scope.Lights = [{'name': 'Light1'}, {'name': 'Light2'}, {'name': 'Light3'}];

  $rootScope.$on('huewiUpdate', function(event, data) {
    if (hueConnector.MyHue().LightIds.length >0) {
      $scope.Lights = _.toArray(hueConnector.MyHue().Lights);
      _.each($scope.Lights, function(Light) {
        Light.HTMLColor = StateToHTMLColor(Light.state, Light.modelid);
      });
      $scope.$apply();
    }
  });
});


})();

{'fraction_non_alphanumeric': 0.15746723278567473, 'fraction_numerical': 0.01052242938896068, 'mean_word_length': 3.1682482687868685, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1006229', 'n_tokens_mistral': 5411, 'n_tokens_neox': 5091, 'n_words': 996}