Sha256: 2069564b5d63487ce16b1d6f9c4aa762a8b9d14090c8e5bd312a54031cad5867

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

var DeviceEventsCtrl = function DeviceEventsCtrl($scope, $filter) {
  $scope.listen = function() {
    if ($scope.eventName === "") { return; }

    if ($scope.device.events == null) { $scope.device.events = []; }
    if ($scope.device.listeners == null) { $scope.device.listeners = {}; }

    var robot = $scope.robot.name,
        device = $scope.device.name,
        event = $scope.eventName;

    var uri = "/api/robots/" + robot + "/devices/" + device + "/events/" + event;
    var $device = $scope.device;
    var source = new EventSource(uri);

    source.addEventListener('message', function(message) {
      $scope.$apply(function() {
        if ($device.events.length > 4) { $device.events.pop(); }
        $device.events.unshift({ name: event, data: JSON.parse(message.data) });
      });
    }, false);

    $scope.device.listeners[event] = source;
    $scope.eventName = "";
  };

  $scope.remove = function(name) {
    $scope.device.listeners[name].close();
    delete $scope.device.listeners[name];
  };
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
robeaux-0.3.0 js/controllers/device_events_ctrl.js
robeaux-0.2.0 js/controllers/device_events_ctrl.js