310e05d516ccbac5746efa2bc33526f694f6cb30
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / odlChat / odlChat-module / src / main / resources / odlChat / odlChat.controller.js
1 /*
2  * Copyright (c) 2016 highstreet technologies GmbH and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 define(
10     [ 'app/odlChat/odlChat.module', 'app/odlChat/odlChat.services' ],
11     function(odlChatApp) {
12
13       var main = function($scope, $rootScope, $odlChat) {
14
15         $rootScope.section_logo = 'src/app/odlChat/odlChat.png';
16      
17         $scope.collection = [];
18         $scope.chat = {
19           nickname : 'anonymous',
20           message : "Hey, what's up?!"
21         };
22
23         var listenToNotifications = function(socketLocation) {
24           try {
25             var notificatinSocket = new WebSocket(socketLocation);
26
27             notificatinSocket.onmessage = function(event) {
28               // we process our received event here
29               $odlChat.getData(event, function(info, tweet) {
30                 $scope.collection.push(tweet);
31                 if ($scope.collection.length > 20) {
32                   $scope.collection.shift();
33                 }
34                 
35                 $scope.chat.message = info;
36               });
37             };
38             notificatinSocket.onerror = function(error) {
39               console.log("Socket error: " + JSON.stringify(error));
40             };
41             notificatinSocket.onopen = function(event) {
42               console.log("Socket connection opened.");
43             };
44             notificatinSocket.onclose = function(event) {
45               console.log("Socket connection closed.");
46             };
47             // if there is a problem on socket creation we get
48             // exception (i.e. when socket address is incorrect)
49           } catch (e) {
50             alert("Error when creating WebSocket" + e);
51           }
52         };
53
54         $scope.send = function(chat) {
55           $odlChat.send(chat, function(info) {
56             console.log(info);
57           });
58         };
59
60         var path = "/opendaylight-inventory:nodes/opendaylight-inventory:node[opendaylight-inventory:id='odlChat']";
61         $odlChat.register(path, function(socketLocation) {
62           listenToNotifications(socketLocation);
63         });
64       };
65
66       odlChatApp.register.controller('odlChatCtrl', [ '$scope', '$rootScope',
67           '$odlChat', main ]);
68
69     });