[BluetoothChat] updated BluetoothChat sources
[samples/web/BluetoothChat.git] / js / app.client.model.js
1 /*jslint plusplus: true, sloppy: true, todo: true, vars: true, browser: true, devel: true, maxerr: 999 */
2 /*global $, tizen, app */
3 /**
4  * @class Model
5  */
6 function ClientModel(parent) {
7         'use strict';
8         this.client = parent;
9         this.init();
10 }
11
12 (function () { // strict mode wrapper
13         'use strict';
14         ClientModel.prototype = {
15
16                 /**
17                  * API module initialisation
18                  */
19                 init: function ClientModel_init() {},
20
21                 searchServer: function ClientModel_searchServer() {
22                         var discoverDevicesSuccessCallback = {
23                                 onstarted: function () {
24                                         this.client.setDiscovering(true);
25                                 }.bind(this),
26                                 ondevicefound: function (device) {
27                                         this.client.addDeviceToList(device);
28                                 }.bind(this),
29                                 ondevicedisappeared: function (address) {},
30                                 onfinished: function (devices) {
31                                         this.client.setDiscovering(false);
32                                 }.bind(this)
33                         };
34
35                         this.client.adapter.discoverDevices(discoverDevicesSuccessCallback, function (e) { this.client.setDiscovering(false); });
36                 },
37
38                 stopServerSearching: function ClientModel_stopServerSearching(callback) {
39                         if (this.client.getDiscovering()) {
40                                 this.client.adapter.stopDiscovery(function () {
41                                         this.client.setDiscovering(false);
42                                         if (typeof callback === 'function') {
43                                                 callback();
44                                         }
45                                 }.bind(this), function (e) {
46                                         console.error("Error while stopDiscovery:" + e.message);
47                                 });
48                         } else if (typeof callback === 'function') {
49                                 callback();
50                         }
51                 },
52
53                 startBonding: function ClientModel_startBonding(address, callback) {
54                         this.client.adapter.createBonding(address, function (device) { callback(device); }, function (error) { console.error('bondError: ' + error.message); });
55                 },
56
57                 connectToService: function ClientModel_connectToService(device, serviceUUID, successCallback, errorCallback) {
58                         this.client.chatServerDevice = device;
59                         try {
60                                 device.connectToServiceByUUID(serviceUUID, successCallback, errorCallback);
61                         } catch (error) {
62                                 console.error('connectToServiceByUUID ERROR: ' + error.message);
63                         }
64                 },
65
66                 sendPing: function ClientModel_sendPing(name, socket) {
67                         var sendTextMsg, messageObj, messageObjToString, i, len;
68                         sendTextMsg = [];
69                         messageObj = {name: encodeURIComponent(name), text: '', ping: true, bye: false};
70                         messageObjToString = JSON.stringify(messageObj);
71                         len = messageObjToString.length;
72
73                         for (i = 0; i < len; i += 1) {
74                                 sendTextMsg[i] = messageObjToString.charCodeAt(i);
75                         }
76                         try {
77                                 if (socket !== null && socket.state === "OPEN") {
78                                         socket.writeData(sendTextMsg);
79                                 }
80                         } catch (error) {
81                                 console.error('sendPing: ' + error.message);
82                         }
83                 },
84
85                 sendMessage: function ClientModel_sendMessage(name, socket, message, callback) {
86                         var sendTextMsg = [], messageObj, messageObjToString, i, len;
87                         name = encodeURIComponent(name);
88                         message = encodeURIComponent(message);
89                         messageObj = {name: name, text: message, ping: false, bye: false};
90                         messageObjToString = JSON.stringify(messageObj);
91                         len = messageObjToString.length;
92                         for (i = 0; i < len; i += 1) {
93                                 sendTextMsg[i] = messageObjToString.charCodeAt(i);
94                         }
95                         try {
96                                 if (socket !== null && socket.state === "OPEN") {
97                                         socket.writeData(sendTextMsg);
98                                         callback(message);
99                                 }
100                         } catch (error) {
101                                 console.error('sendMessage: ' + error.message);
102                         }
103                 },
104
105                 sendBye: function ClientModel_sendBye(name, socket) {
106                         var sendTextMsg = [], messageObj, messageObjToString, i, len;
107                         name = encodeURIComponent(name);
108                         messageObj = {name: name, text: '', ping: false, bye: true};
109                         messageObjToString = JSON.stringify(messageObj);
110                         len = messageObjToString.length;
111                         for (i = 0; i < len; i += 1) {
112                                 sendTextMsg[i] = messageObjToString.charCodeAt(i);
113                         }
114                         try {
115                                 if (socket !== null && socket.state === "OPEN") {
116                                         socket.writeData(sendTextMsg);
117                                 }
118                         } catch (error) {
119                                 console.error('sendBye: ' + error.message);
120                         }
121                 },
122
123                 destroyBonding: function ClientModel_destroyBonding(device, restartCallback, showStartButtonsCallback) {
124                         if (device !== null) {
125                                 if (device.isBonded) {
126                                         this.client.adapter.destroyBonding(device.address, function () {
127                                                 device = null;
128                                                 restartCallback();
129                                         }, function (error) { console.error('ClientModel_destroyBonding: ' + error); });
130                                 } else {
131                                         device = null;
132                                         restartCallback();
133                                 }
134                         } else {
135                                 this.stopServerSearching();
136                                 showStartButtonsCallback();
137                         }
138                 }
139
140         };
141 }());