1 | const Periodic = require( 'Periodic' ); |
||
0 ignored issues
–
show
Backwards Compatibility
introduced
by
![]() |
|||
2 | const Listener = require( 'Listener' ); |
||
0 ignored issues
–
show
|
|||
3 | |||
4 | // Encoding helper functions |
||
5 | function encode( floatIn ) |
||
6 | { |
||
7 | return parseInt( floatIn * 1000 ); |
||
8 | } |
||
9 | |||
10 | function decode( intIn ) |
||
11 | { |
||
12 | return ( intIn * 0.001 ); |
||
13 | } |
||
14 | |||
15 | class DiveProfile |
||
0 ignored issues
–
show
|
|||
16 | { |
||
17 | constructor(name, deps) |
||
18 | { |
||
19 | deps.logger.debug( 'DiveProfile plugin loaded' ); |
||
20 | var self = this; |
||
21 | |||
22 | // Comm buses |
||
23 | this.globalBus = deps.globalEventLoop; |
||
24 | this.cockpitBus = deps.cockpit; |
||
25 | |||
26 | // Plugin settings |
||
27 | this.settings = {}; |
||
28 | |||
29 | // Target settings |
||
30 | this.targetWaterType = 0; // 0 = Fresh, 1 = Salt |
||
31 | |||
32 | // MCU's reported settings |
||
33 | this.mcuWaterType = NaN; |
||
34 | this.zeroDepthAck = false; |
||
35 | this.clearDepthOffsetAck = false; |
||
36 | |||
37 | // State information |
||
38 | this.depth = 0; // meters |
||
39 | this.pressure = 0; // kPa |
||
40 | this.temperature = 0; // celsius |
||
41 | this.waterType = "Fresh"; |
||
42 | |||
43 | this.SyncSettings = new Periodic( 100, "timeout", function() |
||
44 | { |
||
45 | let synced = true; |
||
0 ignored issues
–
show
|
|||
46 | |||
47 | // Send water type until synced |
||
48 | if( self.mcuWaterType !== self.targetWaterType ) |
||
49 | { |
||
50 | synced = false; |
||
51 | |||
52 | // Emit command to mcu |
||
53 | var command = 'depth_water(' + self.targetWaterType + ')'; |
||
54 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
55 | } |
||
56 | |||
57 | // TODO: Max Attempts |
||
58 | if( synced ) |
||
59 | { |
||
60 | // Successfully synced |
||
61 | self.SyncSettings.stop(); |
||
62 | |||
63 | // Enable API |
||
64 | self.listeners.zeroDepth.enable(); |
||
65 | self.listeners.clearDepthOffset.enable(); |
||
66 | } |
||
67 | }); |
||
68 | |||
69 | // Periodic function that commands the MCU to zero the depth value of the depth sensor |
||
70 | this.SyncZeroDepth = new Periodic( 100, "timeout", () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
71 | { |
||
72 | // TODO: Max Attempts |
||
73 | if( self.zeroDepthAck !== true ) |
||
74 | { |
||
75 | // Emit command to mcu |
||
76 | var command = 'depth_zero()'; |
||
77 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | // Stop syncing |
||
82 | self.SyncZeroDepth.stop(); |
||
83 | } |
||
84 | }); |
||
85 | |||
86 | this.SyncClearDepthOffset = new Periodic( 100, "timeout", () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
87 | { |
||
88 | // TODO: Max Attempts |
||
89 | if( self.clearDepthOffsetAck !== true ) |
||
90 | { |
||
91 | // Emit command to mcu |
||
92 | var command = 'depth_clroff()'; |
||
93 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
94 | } |
||
95 | else |
||
96 | { |
||
97 | // Stop syncing |
||
98 | self.SyncClearDepthOffset.stop(); |
||
99 | } |
||
100 | }); |
||
101 | |||
102 | this.listeners = |
||
103 | { |
||
104 | settings: new Listener( this.globalBus, 'settings-change.diveProfile', true, ( settings ) => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
105 | { |
||
106 | // Apply settings |
||
107 | self.settings = settings.diveProfile; |
||
108 | |||
109 | // Set target water type |
||
110 | if( self.settings.waterType == "Fresh" ) |
||
111 | { |
||
112 | self.targetWaterType = 0; |
||
113 | } |
||
114 | else if( self.settings.waterType == "Salt" ) |
||
115 | { |
||
116 | self.targetWaterType = 1; |
||
117 | } |
||
118 | |||
119 | // Enable MCU Status listener |
||
120 | self.listeners.mcuStatus.enable(); |
||
121 | |||
122 | // Start syncing the current settings with the MCU |
||
123 | self.SyncSettings.start(); |
||
124 | }), |
||
125 | |||
126 | mcuStatus: new Listener( this.globalBus, 'mcu.status', false, ( data ) => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
127 | { |
||
128 | // Depth |
||
129 | if( 'depth_d' in data ) |
||
130 | { |
||
131 | self.depth = decode( parseInt( data.depth_d ) ); |
||
132 | self.globalBus.emit( "plugin.diveProfile.depth", self.depth ); |
||
133 | } |
||
134 | |||
135 | // Pressure |
||
136 | if( 'depth_p' in data ) |
||
137 | { |
||
138 | self.pressure = decode( parseInt( data.depth_p ) ); |
||
139 | self.globalBus.emit( "plugin.diveProfile.pressure", self.pressure ); |
||
140 | } |
||
141 | |||
142 | // Temperature |
||
143 | if( 'depth_t' in data ) |
||
144 | { |
||
145 | self.temperature = decode( parseInt( data.depth_t ) ); |
||
146 | self.globalBus.emit( "plugin.diveProfile.temp", self.temperature ); |
||
147 | } |
||
148 | |||
149 | // Water type |
||
150 | if( 'depth_water' in data ) |
||
151 | { |
||
152 | self.mcuWaterType = parseInt( data.depth_water ); |
||
153 | |||
154 | if( self.mcuWaterType == 0 ) |
||
0 ignored issues
–
show
It is recommended to use
=== to compare with 0 .
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator. ![]() |
|||
155 | { |
||
156 | this.waterType = "Fresh"; |
||
157 | } |
||
158 | else |
||
159 | { |
||
160 | this.waterType = "Salt"; |
||
161 | } |
||
162 | |||
163 | self.cockpitBus.emit( "plugin.diveProfile.waterType", self.waterType ); |
||
164 | } |
||
165 | |||
166 | // Depth zero ack |
||
167 | if( 'depth_zero' in data ) |
||
168 | { |
||
169 | if( data.depth_zero == "ack" ) |
||
170 | { |
||
171 | // Done syncing |
||
172 | self.zeroDepthAck = true; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | // Depth offset clear ack |
||
177 | if( 'depth_clroff' in data ) |
||
178 | { |
||
179 | if( data.depth_clroff == "ack" ) |
||
180 | { |
||
181 | // Done syncing |
||
182 | self.clearDepthOffsetAck = true; |
||
183 | } |
||
184 | } |
||
185 | }), |
||
186 | |||
187 | zeroDepth: new Listener( this.cockpitBus, 'plugin.diveProfile.zeroDepth', false, () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
188 | { |
||
189 | // Zero the depth value by using the current value as the offset |
||
190 | self.setZeroDepth(); |
||
191 | }), |
||
192 | |||
193 | clearDepthOffset: new Listener( this.cockpitBus, 'plugin.diveProfile.clearDepthOffset', false, () => |
||
0 ignored issues
–
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
194 | { |
||
195 | // Zero the depth value by using the current value as the offset |
||
196 | self.clearDepthOffset(); |
||
197 | }) |
||
198 | } |
||
0 ignored issues
–
show
There should be a semicolon.
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers. Further Readings: ![]() |
|||
199 | } |
||
200 | |||
201 | setZeroDepth() |
||
202 | { |
||
203 | // Reset the ack and start syncing state |
||
204 | this.zeroDepthAck = false; |
||
205 | this.SyncZeroDepth.start(); |
||
206 | } |
||
207 | |||
208 | clearDepthOffset() |
||
209 | { |
||
210 | // Reset the ack and start syncing state |
||
211 | this.clearDepthOffsetAck = false; |
||
212 | this.SyncClearDepthOffset.start(); |
||
213 | } |
||
214 | |||
215 | start() |
||
216 | { |
||
217 | this.listeners.settings.enable(); |
||
218 | } |
||
219 | |||
220 | stop() |
||
221 | { |
||
222 | this.listeners.settings.disable(); |
||
223 | this.listeners.mcuStatus.disable(); |
||
224 | this.listeners.zeroDepth.disable(); |
||
225 | this.listeners.clearDepthOffset.disable(); |
||
226 | |||
227 | this.SyncSettings.stop(); |
||
228 | this.SyncZeroDepth.stop(); |
||
229 | this.SyncClearDepthOffset.stop(); |
||
230 | } |
||
231 | |||
232 | getSettingSchema() |
||
233 | { |
||
234 | return [{ |
||
235 | 'title': 'Dive Profile', |
||
236 | 'id': 'diveProfile', |
||
237 | 'type': 'object', |
||
238 | 'properties': { |
||
239 | 'waterType': { |
||
240 | 'type': 'string', |
||
241 | 'enum': [ |
||
242 | 'Fresh', |
||
243 | 'Salt' |
||
244 | ], |
||
245 | 'title': 'Water Type', |
||
246 | 'default': 'Freshwater' |
||
247 | } |
||
248 | }, |
||
249 | 'required': [ |
||
250 | 'waterType' |
||
251 | ] |
||
252 | }]; |
||
253 | } |
||
254 | } |
||
255 | |||
256 | module.exports = function (name, deps) |
||
257 | { |
||
258 | return new DiveProfile(name, deps); |
||
259 | }; |