'use strict';


function WebSocketController($scope) {


   $scope.servoValue1 = 1500;

   $scope.servoValue2 = 1500;


   // var host = '192.168.7.2';

   // BBB IP on the network

   var host = 'pszyjaciel.myftp.org';

   // var host = '192.168.24.99';

   var port = '8150';


   // Establish WebSocket connection with BBB.

   var ws = new WebSocket('ws://' + host + ':' + port);


   // Receive incoming messages from the WS connection and display.

   ws.onmessage = function(event) {

       $scope.$apply(function( ) {

           $scope.message = event.data;

       })

   };


   // Setup listener for slider events.

   $('.slider').slider().on('slide', function(event1) {

       // Update the UI for current slider value.

       $scope.$apply($scope.servoValue1 = event.value1);

       // Send the value over the WS connection.

       if (ws.readyState === 1)

           ws.send(event.value1);

   });


   // Setup listener for slider events.

   $('.slider').slider().on('slide', function(event2) {

       // Update the UI for current slider value.

       $scope.$apply($scope.servoValue2 = event.valu2e);

       // Send the value over the WS connection.

       if (ws.readyState === 1)

           ws.send(event.valu2e);

   });


   // Servo On button click.

   $scope.speed_Up = function( ) {

       console.log('WebSocketController: speed_Up');

       ws.send('speed_Up');

   };


   // Servo Off button click.

   $scope.speed_Down = function( ) {

       console.log('WebSocketController: speed_Down');

       ws.send('speed_Down');

   };


   // // Servo On button click.

   $scope.turn_Left = function( ) {

       console.log('WebSocketController: turn_Left');

       ws.send('turn_Left');

   };


   // Servo Off button click.

   $scope.turn_Right = function( ) {

       console.log('WebSocketController: turn_Right');

       ws.send('turn_Right');

   };


   // Servo Off button click.

   $scope.turn_On = function( ) {

       console.log('WebSocketController: turn_Right');

       ws.send('turn_On');

   };


   // Servo Off button click.

   $scope.turn_Off = function( ) {

       console.log('WebSocketController: turn_Right');

       ws.send('turn_Off');

   };

}