1 | (function() |
||
2 | { |
||
3 | const Listener = require( 'Listener' ); |
||
0 ignored issues
–
show
Backwards Compatibility
introduced
by
![]() |
|||
4 | |||
5 | class Example |
||
0 ignored issues
–
show
|
|||
6 | { |
||
7 | constructor(name, deps) |
||
8 | { |
||
9 | deps.logger.debug( 'Example plugin loaded!' ); |
||
10 | |||
11 | this.globalBus = deps.globalEventLoop; // This is the server-side messaging bus. The MCU sends messages to server plugins over this |
||
12 | this.cockpitBus = deps.cockpit; // This is the server<->client messaging bus. This is how the server talks to the browser |
||
13 | |||
14 | this.hasSaidHello = false; |
||
15 | |||
16 | var self = this; |
||
17 | |||
18 | // Pre-define all of the event listeners here. We defer enabling them until later. |
||
19 | // Look at src/libs/Listener.js to see how these work. |
||
20 | this.listeners = |
||
21 | { |
||
22 | // Listener for Settings updates |
||
23 | settings: new Listener( self.globalBus, 'settings-change.example', true, function( settings ) |
||
24 | { |
||
25 | // Apply settings |
||
26 | self.settings = settings.example; |
||
27 | |||
28 | // Emit settings update to cockpit |
||
29 | self.cockpitBus.emit( 'plugin.example.settingsChange', self.settings ); |
||
30 | }), |
||
31 | |||
32 | // Listener for MCU status messages |
||
33 | mcuStatus: new Listener( self.globalBus, 'mcu.status', false, function( data ) |
||
34 | { |
||
35 | // Check for the example field name in the MCU's status update |
||
36 | if( 'example' in data ) |
||
37 | { |
||
38 | // Get the message that the MCU sent to us |
||
39 | var message = data.example; |
||
40 | |||
41 | // Re-emit the message on the cockpit messaging bus (talks to the browser) |
||
42 | self.cockpitBus.emit( 'plugin.example.message', message ); |
||
43 | } |
||
44 | }), |
||
45 | |||
46 | sayHello: new Listener( self.cockpitBus, 'plugin.example.sayHello', false, function( powerIn ) |
||
47 | { |
||
48 | var command; |
||
49 | |||
50 | // Create a command in the format "command( parameters )" |
||
51 | if( self.hasSaidHello ) |
||
52 | { |
||
53 | command = 'ex_hello(' + 0 + ')'; |
||
54 | self.hasSaidHello = false; |
||
55 | } |
||
56 | else |
||
57 | { |
||
58 | command = 'ex_hello(' + 1 + ')'; |
||
59 | self.hasSaidHello = true; |
||
60 | } |
||
61 | |||
62 | // Send command to mcu |
||
63 | self.globalBus.emit( 'mcu.SendCommand', command ); |
||
64 | }) |
||
65 | } |
||
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: ![]() |
|||
66 | } |
||
67 | |||
68 | // This is automatically called when cockpit loads all of the plugins, and when a plugin is enabled |
||
69 | start() |
||
70 | { |
||
71 | // Enable the listeners! |
||
72 | this.listeners.settings.enable(); |
||
73 | this.listeners.mcuStatus.enable(); |
||
74 | this.listeners.sayHello.enable(); |
||
75 | } |
||
76 | |||
77 | // This is called when the plugin is disabled |
||
78 | stop() |
||
79 | { |
||
80 | // Disable listeners |
||
81 | this.listeners.settings.disable(); |
||
82 | this.listeners.mcuStatus.disable(); |
||
83 | this.listeners.sayHello.disable(); |
||
84 | } |
||
85 | |||
86 | // This is used to define user settings for the plugin. We populated some example properties below. |
||
87 | // The UI for changing the settings is automatically generated in the Settings applet. |
||
88 | getSettingSchema() |
||
89 | { |
||
90 | //from http://json-schema.org/examples.html |
||
91 | return [{ |
||
92 | 'title': 'Example Plugin', |
||
93 | 'type': 'object', |
||
94 | 'id': 'example', |
||
95 | 'properties': { |
||
96 | 'firstName': { |
||
97 | 'type': 'string', |
||
98 | 'default': 'Open' |
||
99 | }, |
||
100 | 'lastName': { |
||
101 | 'type': 'string', |
||
102 | 'default': 'Rov' |
||
103 | }, |
||
104 | 'age': { |
||
105 | 'description': 'Age in years', |
||
106 | 'type': 'integer', |
||
107 | 'minimum': 0 |
||
108 | } |
||
109 | }, |
||
110 | 'required': [ |
||
111 | 'firstName', |
||
112 | 'lastName' |
||
113 | ] |
||
114 | }]; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | module.exports = function(name, deps) |
||
119 | { |
||
120 | return new Example(name, deps); |
||
121 | }; |
||
122 | }()); |