17
17
*
18
18
* @author [email protected] (Yu Wang)
19
19
*/
20
- goog . provide ( 'cwc.mode.makeblock.mbot .Connection' ) ;
20
+ goog . provide ( 'cwc.mode.makeblock.mBot .Connection' ) ;
21
21
22
- goog . require ( 'cwc.protocol.makeblock.mbot.Api' ) ;
22
+ goog . require ( 'cwc.lib.protocol.makeblock.mBot.Api' ) ;
23
+ goog . require ( 'cwc.lib.protocol.bluetoothChrome.profile.Device' ) ;
24
+ goog . require ( 'cwc.lib.protocol.makeblock.mBot.Events' ) ;
23
25
goog . require ( 'cwc.utils.Events' ) ;
24
26
25
27
goog . require ( 'goog.Timer' ) ;
26
28
27
29
30
+ goog . scope ( function ( ) {
31
+ const Api = goog . module . get ( 'cwc.lib.protocol.makeblock.mBot.Api' ) ;
32
+ const BluetoothProfile =
33
+ goog . module . get ( 'cwc.lib.protocol.bluetoothChrome.profile.Device' ) ;
34
+ const Events = goog . module . get ( 'cwc.lib.protocol.makeblock.mBot.Events' ) ;
35
+
28
36
/**
29
37
* @constructor
30
38
* @param {!cwc.utils.Helper } helper
31
39
*/
32
- cwc . mode . makeblock . mbot . Connection = function ( helper ) {
40
+ cwc . mode . makeblock . mBot . Connection = function ( helper ) {
33
41
/** @type {string } */
34
42
this . name = 'mBot Connection' ;
35
43
@@ -39,32 +47,41 @@ cwc.mode.makeblock.mbot.Connection = function(helper) {
39
47
/** @type {!cwc.utils.Helper } */
40
48
this . helper = helper ;
41
49
42
- /** @type {!cwc.protocol.makeblock.mbot.Api } */
43
- this . api_ = new cwc . protocol . makeblock . mbot . Api ( ) ;
44
-
45
50
/** @type {goog.Timer } */
46
51
this . connectMonitor = null ;
47
52
48
53
/** @type {number } */
49
54
this . connectMonitorInterval = 5000 ;
50
55
56
+ /** @type {!cwc.protocol.makeblock.mBot.Api } */
57
+ this . api_ = new Api ( ) ;
58
+
59
+ /** @private {!goog.events.EventTarget} */
60
+ this . apiEvents_ = this . api_ . getEventTarget ( ) ;
61
+
51
62
/** @private {!cwc.utils.Events} */
52
63
this . events_ = new cwc . utils . Events ( this . name ) ;
64
+
65
+ /** @private {!cwc.lib.protocol.bluetoothChrome.profile.Device} */
66
+ this . device_ = BluetoothProfile . MAKEBLOCK_MBOT_RANGER ;
53
67
} ;
54
68
55
69
56
70
/**
57
71
* Connects the mbot unit.
58
72
* @export
59
73
*/
60
- cwc . mode . makeblock . mbot . Connection . prototype . init = function ( ) {
61
- if ( ! this . connectMonitor ) {
62
- this . connectMonitor = new goog . Timer ( this . connectMonitorInterval ) ;
63
- this . events_ . listen ( this . connectMonitor , goog . Timer . TICK ,
64
- this . connect . bind ( this ) ) ;
74
+ cwc . mode . makeblock . mBot . Connection . prototype . init = function ( ) {
75
+ this . handleConnecting_ ( {
76
+ 'data' : 'Connecting Sphero 2.0' ,
77
+ 'source' : 1 ,
78
+ } ) ;
79
+
80
+ if ( this . apiEvents_ ) {
81
+ this . events_ . listen ( this . apiEvents_ ,
82
+ Events . Type . CONNECT , this . handleConnecting_ . bind ( this ) ) ;
65
83
}
66
84
67
- // Unload event
68
85
let layoutInstance = this . helper . getInstance ( 'layout' ) ;
69
86
if ( layoutInstance ) {
70
87
this . events_ . listen ( layoutInstance . getEventTarget ( ) ,
@@ -78,36 +95,40 @@ cwc.mode.makeblock.mbot.Connection.prototype.init = function() {
78
95
false , this ) ;
79
96
}
80
97
98
+ if ( ! this . connectMonitor ) {
99
+ this . connectMonitor = new goog . Timer ( this . connectMonitorInterval ) ;
100
+ this . events_ . listen ( this . connectMonitor , goog . Timer . TICK ,
101
+ this . connect . bind ( this ) ) ;
102
+ }
103
+
81
104
this . connectMonitor . start ( ) ;
82
105
this . connect ( ) ;
83
106
} ;
84
107
85
108
86
109
/**
87
- * Connects the mbot ball .
110
+ * Connects the mBot robot .
88
111
* @param {Event= } opt_event
89
112
* @export
90
113
*/
91
- cwc . mode . makeblock . mbot . Connection . prototype . connect = function ( opt_event ) {
114
+ cwc . mode . makeblock . mBot . Connection . prototype . connect = function ( opt_event ) {
92
115
let bluetoothInstance = this . helper . getInstance ( 'bluetoothChrome' ) ;
93
116
if ( ! bluetoothInstance ) {
94
117
return ;
95
118
}
96
119
if ( ! this . isConnected ( ) ) {
97
- bluetoothInstance . autoConnectDevice ( this . autoConnectName , function ( device ) {
98
- if ( device ) {
99
- this . api_ . connect ( device ) ;
100
- }
101
- } . bind ( this ) ) ;
120
+ bluetoothInstance . autoConnectDevice ( this . autoConnectName ,
121
+ this . api_ . connect . bind ( this . api_ ) ) ;
102
122
}
123
+ this . api_ . monitor ( true ) ;
103
124
} ;
104
125
105
126
106
127
/**
107
128
* Stops the current executions.
108
129
* @export
109
130
*/
110
- cwc . mode . makeblock . mbot . Connection . prototype . stop = function ( ) {
131
+ cwc . mode . makeblock . mBot . Connection . prototype . stop = function ( ) {
111
132
let previewInstance = this . helper . getInstance ( 'preview' ) ;
112
133
if ( previewInstance ) {
113
134
previewInstance . stop ( ) ;
@@ -118,10 +139,9 @@ cwc.mode.makeblock.mbot.Connection.prototype.stop = function() {
118
139
119
140
/**
120
141
* Resets the connection.
121
- * @param {Event= } opt_event
122
142
* @export
123
143
*/
124
- cwc . mode . makeblock . mbot . Connection . prototype . reset = function ( opt_event ) {
144
+ cwc . mode . makeblock . mBot . Connection . prototype . reset = function ( ) {
125
145
if ( this . isConnected ( ) ) {
126
146
this . api_ . reset ( ) ;
127
147
}
@@ -132,36 +152,36 @@ cwc.mode.makeblock.mbot.Connection.prototype.reset = function(opt_event) {
132
152
* @return {boolean }
133
153
* @export
134
154
*/
135
- cwc . mode . makeblock . mbot . Connection . prototype . isConnected = function ( ) {
155
+ cwc . mode . makeblock . mBot . Connection . prototype . isConnected = function ( ) {
136
156
return this . api_ . isConnected ( ) ;
137
157
} ;
138
158
139
159
140
160
/**
141
- * @return {!cwc.protocol.makeblock.mbot .Api }
161
+ * @return {!cwc.protocol.makeblock.mBot .Api }
142
162
* @export
143
163
*/
144
- cwc . mode . makeblock . mbot . Connection . prototype . getApi = function ( ) {
164
+ cwc . mode . makeblock . mBot . Connection . prototype . getApi = function ( ) {
145
165
return this . api_ ;
146
166
} ;
147
167
148
168
149
169
/**
150
170
* @return {goog.events.EventTarget }
151
171
*/
152
- cwc . mode . makeblock . mbot . Connection . prototype . getEventTarget = function ( ) {
172
+ cwc . mode . makeblock . mBot . Connection . prototype . getEventTarget = function ( ) {
153
173
return this . api_ . getEventTarget ( ) ;
154
174
} ;
155
175
156
176
157
177
/**
158
178
* Cleans up the event listener and any other modification.
159
179
*/
160
- cwc . mode . makeblock . mbot . Connection . prototype . cleanUp = function ( ) {
180
+ cwc . mode . makeblock . mBot . Connection . prototype . cleanUp = function ( ) {
161
181
if ( this . connectMonitor ) {
162
182
this . connectMonitor . stop ( ) ;
163
183
}
164
- this . api_ . monitor ( false ) ;
184
+ this . api_ . cleanUp ( ) ;
165
185
this . stop ( ) ;
166
186
this . events_ . clear ( ) ;
167
187
} ;
@@ -171,9 +191,23 @@ cwc.mode.makeblock.mbot.Connection.prototype.cleanUp = function() {
171
191
* @param {Event|Object } e
172
192
* @private
173
193
*/
174
- cwc . mode . makeblock . mbot . Connection . prototype . handlePreviewStatus_ = function (
194
+ cwc . mode . makeblock . mBot . Connection . prototype . handleConnecting_ = function ( e ) {
195
+ let message = e . data ;
196
+ let step = e . source ;
197
+ let title = 'Connecting' + this . device_ . name ;
198
+ let connectScreenInstance = this . helper . getInstance ( 'connectScreen' ) ;
199
+ connectScreenInstance . showConnectingStep ( title , message , step ) ;
200
+ } ;
201
+
202
+
203
+ /**
204
+ * @param {Event|Object } e
205
+ * @private
206
+ */
207
+ cwc . mode . makeblock . mBot . Connection . prototype . handlePreviewStatus_ = function (
175
208
e ) {
176
209
if ( e . data === cwc . ui . PreviewState . STOPPED ) {
177
210
this . stop ( ) ;
178
211
}
179
212
} ;
213
+ } ) ;
0 commit comments