-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.motor-controller.js
184 lines (154 loc) · 5.82 KB
/
app.motor-controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Motor Control
// =============
// Client application to control the motors
// Modules
// =======
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var W = require( 'w-js' );
var JSONSocketConnection = require( 'w-js/node/json-socket-connection' );
var MotorInterface = require( './lib/motor-interface' );
var RestesqueUtil = require( './lib/restesque/example/restesque-util-node.js' );
// Make & Init
// ===========
var makeApp = function () {
return {
wsPort: W.isDefined( process.env.WS_PORT ) ? process.env.WS_PORT : 7080,
isEnabled: true
};
};
var initApp = W.composePromisers( makeMotorInterface,
// doRunMotorInterfaceTestSequence,
makeWebSocketClient(
subscribeToIsEnabled,
subscribeRotationDirection,
subscribeRotationSpeed
),
doStartPostingHeartBeats,
doPostMotorDirectionToNone
);
initApp( makeApp() )
.success( function ( app ) {
report( 'OK', 'Application created' );
});
// Promisers
// =========
function makeMotorInterface ( app ) {
return W.promise( function ( resolve, reject ) {
MotorInterface
.init( MotorInterface.make() )
.success( function ( motorInterface ) {
app.motorInterface = motorInterface;
resolve( app );
})
.error( reject );
});
}
function doRunMotorInterfaceTestSequence ( app ) {
return W.promise( function ( resolve, reject ) {
MotorInterface.doTestSequence( motorInterface );
resolve( app );
});
}
// Takes a sequence of promiser to connect upon a successful reconnection.
// Even after a reconnect.
function makeWebSocketClient ( onConnectPromisers ) {
onConnectPromisers = W.toArray( arguments );
return function ( app ) {
return W.promise( function ( resolve, reject ) {
var socketUrl = 'wss://localhost:' + app.wsPort + '/';
report( 'CONNECTING', 'attempting to connect to:', socketUrl );
app.wsClient = new JSONSocketConnection( {
socketUrl: socketUrl
});
var resolveOnFirstConnect = once( function () {
report( 'CONNECTED', 'to:', socketUrl );
resolve( app );
});
app.wsClient.on( 'open', resolveOnFirstConnect );
app.wsClient.on( 'open', function () {
W.composePromisers.apply( this, onConnectPromisers )( app );
});
app.wsClient.on( 'error', makeReporter( 'Web Socket Error' ) );
app.wsClient.openSocketConnection();
});
}
}
function doPostMotorDirectionToNone ( app ) {
return W.promise( function ( resolve, reject ) {
RestesqueUtil.post( app.wsClient, '/motor/rotation/direction/', 'none' ).success( W.partial( resolve, app ) );
});
}
function subscribeToIsEnabled ( app ) {
return W.promise( function ( resolve, reject ) {
RestesqueUtil.subscribeWithInitialGet( app.wsClient, '/motor/controller/is-enabled/', function ( packet ) {
MotorInterface.doSetEnabled( app.motorInterface, packet.getBody() === 'yes' ? true : false );
}).success( W.partial( resolve, app ) );
});
}
function doStartPostingHeartBeats ( app ) {
return W.promise( function ( resolve, reject ) {
var resolveOnce = once( W.partial( resolve, app ) );
(function loop () {
RestesqueUtil
.now( app.wsClient, '/motor/controller/heartbeat/')
.success( function () {
resolveOnce();
setTimeout( loop, 3000 );
});
}());
});
}
function subscribeRotationDirection ( app ) {
return W.promise( function ( resolve, reject ) {
RestesqueUtil.subscribeWithInitialGet( app.wsClient, '/motor/rotation/direction/', function ( packet ) {
if ( packet.getBody() === 'forward' ) {
MotorInterface.doSetRotationForward( app.motorInterface );
} else if ( packet.getBody() === 'backward' ) {
MotorInterface.doSetRotationBackward( app.motorInterface );
} else if ( packet.getBody() === 'none' ) {
MotorInterface.doSetRotationNone( app.motorInterface );
}
}).success( W.partial( resolve, app ) );
});
}
function subscribeRotationSpeed ( app ) {
return W.promise( function ( resolve, reject ) {
RestesqueUtil.subscribeWithInitialGet( app.wsClient, '/motor/rotation/speed/', function ( packet ) {
if ( packet.getBody() === 'high' ) {
MotorInterface.doSetSpeedHigh( app.motorInterface );
} else if ( packet.getBody() === 'low' ) {
MotorInterface.doSetSpeedLow( app.motorInterface );
} else if ( packet.getBody() === 'medium' ) {
MotorInterface.doSetSpeedMedium( app.motorInterface );
}
}).success( W.partial( resolve, app ) );
});
}
// Utils
// =====
// Function
// --------
function once ( fn ) {
var hasTriggered = false;
return function () {
if ( ! hasTriggered ) {
fn();
hasTriggered = true;
}
};
}
// Reporting
// ---------
function report( status, str ) {
console.log( '[', status, ']', W.rest( W.toArray( arguments ) ).join( ' ' ) );
}
function makeReporter( status, str ) {
var reportArgs = arguments;
return function () {
report.apply( this, reportArgs );
var calleeArgs = arguments;
return W.promise( function ( resolve, reject ) {
resolve.apply( this, calleeArgs );
});
};
}