<commit_before>/*
Copyright 2016-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at

    http://aws.amazon.com/apache2.0/

or in the "license" file accompanying this file. This file 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.
*/
(function() {
  'use strict';

  function LogMsg(type, content){
    this.type = type;
    this.content = content;
    this.createdTime = Date.now();
    if (this.type === 'success') {
      this.className = 'list-group-item-info';
    } else {
      this.className = 'list-group-item-danger';
    }
  }

  function LogService(){
    this.logs = [];
  }


  LogService.prototype.log = function(msg) {
    var logObj = new LogMsg('success', msg);
    this.logs.push(logObj);
  };

  LogService.prototype.logError = function(msg) {
    var logObj = new LogMsg('error', msg);
    this.logs.push(logObj);
  };

  /**
   * wrapper of received paho message
   * @class
   * @param {Paho.MQTT.Message} msg
   */
  function ReceivedMsg(msg) {
    this.msg = msg;
    this.content = msg.payloadString;
    
    var myJSON = this.content;
    var myObj = JSON.parse(myJSON);
    var myObjdesiredState = myObj.state.desired.state;
    msg.state = myObjdesiredState;
    this.destination = msg.destinationName;
    this.receivedTime = Date.now();
  }


function timeSince(date) {

  var seconds = Math.floor((new Date() - date) / 1000);  

  var interval = Math.floor(seconds / 31536000);

  if (interval >= 1) {
    return interval + " years";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval >= 1) {
    return interval + " months";
  }
  interval = Math.floor(seconds / 86400);
  if (interval >= 1) {
    return interval + " days";
  }
  interval = Math.floor(seconds / 3600);
  if (interval >= 1) {
    return interval + " hours";
  }
  interval = Math.floor(seconds / 60);
  if (interval >= 1) {
    return interval + " minutes";
  }
  return Math.floor(seconds) + " seconds";
}
 
  /** controller of the app */
  function AppController(scope){
    this.foo = {bar: false};
    this.clientId = 'someClientId';
    this.endpoint = null;
    this.accessKey = null;
    this.secretKey = null;
    this.regionName = 'ap-northeast-1';
    this.logs = new LogService();
    this.clients = new ClientControllerCache(scope, this.logs);
  }

  AppController.$inject = ['$scope'];
  
  AppController.prototype.createClient1 = function() {
    var options = {
      clientId : Math.random().toString(36).substr(2, 5),
      endpoint: 'a3ga5tu9j45csi.iot.ap-northeast-1.amazonaws.com',
      accessKey: '<KEY>',
      secretKey: '<KEY>',
      regionName: 'ap-northeast-1'
    };
    var client = this.clients.getClient(options);
    if (!client.connected) {
      client.connect(options);
    }
    }
  ;
  
  AppController.prototype.turnON = function(clientCtr){
      this.clients.turnon();
  };
  
  AppController.prototype.turnOFF = function(clientCtr){
      this.clients.turnoff();
  };
  
  AppController.prototype.removeClient = function(clientCtr) {
    this.clients.removeClient(clientCtr);
  };
  // would be better to use a seperate derective
  function ClientController(client, logs) {
    this.client = client;    
    this.topicName = '$aws/things/demo-switch/shadow/update';
    this.topicShadowGetAccepted = '$aws/things/demo-switch/shadow/get/accepted';
    this.topicShadowGet = '$aws/things/demo-switch/shadow/get';
    this.message = '{"state": { "desired":{"pin": 2,"state" : 1}  }}';
    this.msgs = [];
    this.logs = logs;
    var self = this;
    
    
    
    this.client.on('connectionLost', function(){
      self.logs.logError('Connection lost');
    });
    this.client.on('messageArrived', function(msg){
//      self.logs.log('messageArrived in ' + self.id);
//      self.msgs.push(new ReceivedMsg(msg));
    var myJSON = msg.payloadString;  
    var myObj = JSON.parse(myJSON);
    var myObjdesiredState = myObj.state.desired.motor;
    var text = "turned on ";
    if(myObj.metadata===angular.isUndefinedOrNull ){
       msg.statetimestamp = Date.now()/1000; 
    }
    else msg.statetimestamp = myObj.metadata.desired.motor.timestamp;
    var t1= timeSince(msg.statetimestamp * 1000);
    msg.refreshTimeStamp = myObj.timestamp;    
    if(myObjdesiredState===false){
        text = "turned off ";
        self.client.scope.vm.foo.bar = false;
    }else{
        self.client.scope.vm.foo.bar = true;
    }
    self.client.scope.$apply();
    self.client.scope.$apply(function(){
//    	$scope.name = "Yasmin";
//        self.client.scope.vm.foo.bar = true;
    });
    
    
    self.client.scope.$digest();
    self.logs.log('The motor was '+ text + t1 + ' ago.');
    
    });
    this.client.on('connected', function(){
      self.logs.log('connected'); 
      self.client.subscribe(self.topicShadowGetAccepted); 
      self.client.subscribe(self.topicName);
    });
    this.client.on('subscribeFailed', function(e){
      self.logs.logError('subscribeFailed ' + e);<commit_msg>Changes to the JSON objects<commit_after>/*
Copyright 2016-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at

    http://aws.amazon.com/apache2.0/

or in the "license" file accompanying this file. This file 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.
*/
(function() {
  'use strict';

  function LogMsg(type, content){
    this.type = type;
    this.content = content;
    this.createdTime = Date.now();
    if (this.type === 'success') {
      this.className = 'list-group-item-info';
    } else {
      this.className = 'list-group-item-danger';
    }
  }

  function LogService(){
    this.logs = [];
  }


  LogService.prototype.log = function(msg) {
    var logObj = new LogMsg('success', msg);
    this.logs.push(logObj);
  };

  LogService.prototype.logError = function(msg) {
    var logObj = new LogMsg('error', msg);
    this.logs.push(logObj);
  };

  /**
   * wrapper of received paho message
   * @class
   * @param {Paho.MQTT.Message} msg
   */
  function ReceivedMsg(msg) {
    this.msg = msg;
    this.content = msg.payloadString;
    
    var myJSON = this.content;
    var myObj = JSON.parse(myJSON);
    var myObjdesiredState = myObj.state.desired.state;
    msg.state = myObjdesiredState;
    this.destination = msg.destinationName;
    this.receivedTime = Date.now();
  }


function timeSince(date) {

  var seconds = Math.floor((new Date() - date) / 1000);  

  var interval = Math.floor(seconds / 31536000);

  if (interval >= 1) {
    return interval + " years";
  }
  interval = Math.floor(seconds / 2592000);
  if (interval >= 1) {
    return interval + " months";
  }
  interval = Math.floor(seconds / 86400);
  if (interval >= 1) {
    return interval + " days";
  }
  interval = Math.floor(seconds / 3600);
  if (interval >= 1) {
    return interval + " hours";
  }
  interval = Math.floor(seconds / 60);
  if (interval >= 1) {
    return interval + " minutes";
  }
  return Math.floor(seconds) + " seconds";
}
 
  /** controller of the app */
  function AppController(scope){
    this.foo = {bar: false};
    this.clientId = 'someClientId';
    this.endpoint = null;
    this.accessKey = null;
    this.secretKey = null;
    this.regionName = 'ap-northeast-1';
    this.logs = new LogService();
    this.clients = new ClientControllerCache(scope, this.logs);
  }

  AppController.$inject = ['$scope'];
  
  AppController.prototype.createClient1 = function() {
    var options = {
      clientId : Math.random().toString(36).substr(2, 5),
      endpoint: 'a3ga5tu9j45csi.iot.ap-northeast-1.amazonaws.com',
      accessKey: '<KEY>',
      secretKey: '<KEY>',
      regionName: 'ap-northeast-1'
    };
    var client = this.clients.getClient(options);
    if (!client.connected) {
      client.connect(options);
    }
    }
  ;
  
  AppController.prototype.turnON = function(clientCtr){
      this.clients.turnon();
  };
  
  AppController.prototype.turnOFF = function(clientCtr){
      this.clients.turnoff();
  };
  
  AppController.prototype.removeClient = function(clientCtr) {
    this.clients.removeClient(clientCtr);
  };
  // would be better to use a seperate derective
  function ClientController(client, logs) {
    this.client = client;    
    this.topicName = '$aws/things/demo-switch/shadow/update';
    this.topicShadowGetAccepted = '$aws/things/demo-switch/shadow/get/accepted';
    this.topicShadowGet = '$aws/things/demo-switch/shadow/get';
    this.message = '{"state": { "desired":{"motor": true}  }}';
    this.msgs = [];
    this.logs = logs;
    var self = this;
    
    
    
    this.client.on('connectionLost', function(){
      self.logs.logError('Connection lost');
    });
    this.client.on('messageArrived', function(msg){
//      self.logs.log('messageArrived in ' + self.id);
//      self.msgs.push(new ReceivedMsg(msg));
    var myJSON = msg.payloadString;  
    var myObj = JSON.parse(myJSON);
    var myObjdesiredState = myObj.state.desired.motor;
    var text = "turned on ";
    if(myObj.metadata===angular.isUndefinedOrNull ){
       msg.statetimestamp = Date.now()/1000; 
    }
    else msg.statetimestamp = myObj.metadata.desired.motor.timestamp;
    var t1= timeSince(msg.statetimestamp * 1000);
    msg.refreshTimeStamp = myObj.timestamp;    
    if(myObjdesiredState===false){
        text = "turned off ";
        self.client.scope.vm.foo.bar = false;
    }else{
        self.client.scope.vm.foo.bar = true;
    }
    self.client.scope.$apply();
//    self.client.scope.$apply(function(){
//    	$scope.name = "Yasmin";
//        self.client.scope.vm.foo.bar = true;
//    });
    
    
//    self.client.scope.$digest();
    self.logs.log('The motor was '+ text + t1 + ' ago.');
    
    });
    this.client.on('connected', function(){
      self.logs.log('connected'); 
      self.client.subscribe(self.topicShadowGetAccepted); 
      self.client.subscribe(self.topicName);
    });
    this.client.on('subscribeFailed', function(e){
      self.logs.logError('subscribeFailed ' + e);
{'fraction_non_alphanumeric': 0.14435521965044876, 'fraction_numerical': 0.013415210203117619, 'mean_word_length': 2.907715023994094, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 17, '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': '4065660', 'n_tokens_mistral': 3526, 'n_tokens_neox': 3372, 'n_words': 730}