diff --git a/src/plugins/new-ui/public/webcomponents/switches.html b/src/plugins/new-ui/public/webcomponents/switches.html index fd124a3a..beafd285 100644 --- a/src/plugins/new-ui/public/webcomponents/switches.html +++ b/src/plugins/new-ui/public/webcomponents/switches.html @@ -150,6 +150,22 @@ } } }, + + WFL: { + switchable: true, + state: false, + description: 'WiFi lights on/off', + events: [ + {message:'plugin.wifilights.state',fn: function(state){return state.level>0;}} + ], + switch: function (state) { + if (self.eventEmitter !== undefined){ + var level = 1; + if (state==true) level = 0; + self.eventEmitter.emit('plugin.wifilights.set',level); + } + } + }, XLE: { switchable: true, state: false, diff --git a/src/plugins/wifilights/index.js b/src/plugins/wifilights/index.js new file mode 100644 index 00000000..434b39a0 --- /dev/null +++ b/src/plugins/wifilights/index.js @@ -0,0 +1,61 @@ +(function() { + function wifiLights(name, deps) { + console.log('Wifi Lights plugin loaded'); + var wifilights = 0; + + // Cockpit + deps.cockpit.on('plugin.wifilights.adjust', function (value) { + adjustLights(value); + }); + + deps.cockpit.on('plugin.wifilights.set', function (value) { + setLights(value); + }); + + var adjustLights = function (value) { + if (wifilights === 0 && value < 0) { + wifilights = 0; + } else if (wifilights == 1 && value > 0) { + wifilights = 1; + } else { + wifilights += value; + } + setLights(wifilights); + }; + + var setLights = function (value) + { + wifilights = value; + if (wifilights >= 1){ + wifilights = 1; + } + + if (wifilights <= 0){ + wifilights = 0; + } + var val = wifilights*100; + var request = require('request'); + var options = { url: 'http://192.168.1.91',//change to ESP's IP address + method: 'POST', + headers: {'Val': val} + }; + + request(options,function(error,response,body) + { + if(!error){ + var json=JSON.stringify({level:body}) + json = JSON.parse(json); + deps.cockpit.emit('plugin.wifilights.state',json); + console.log('Server responded with:',body);} + else if(error){ + console.error('ERROR:',error);} + }); + + }; + + } + module.exports = function (name, deps) { + return new wifiLights(name,deps); + }; + +})(); diff --git a/src/plugins/wifilights/public/js/wifilights.js b/src/plugins/wifilights/public/js/wifilights.js new file mode 100644 index 00000000..25d1cc0d --- /dev/null +++ b/src/plugins/wifilights/public/js/wifilights.js @@ -0,0 +1,78 @@ +(function(window) { + 'use strict'; + var plugins = namespace('plugins'); + plugins.wifiLights = function(cockpit) { + var self = this; + self.cockpit = cockpit; + + }; + + plugins.wifiLights.prototype.getTelemetryDefintions = function getTelemetryDefintions() { + return([{name: 'WLIGP', description: 'WiFi lights percent of power'}]); + } + + plugins.wifiLights.prototype.inputDefaults = function inputDefaults() { + return [ + // lights increment + { + name: 'plugin.wifilights.adjust_increment', + description: 'Makes the ROV lights brighter.', + defaults: { keyboard: 'e', gamepad: 'DPAD_UP' },//gamepad button needs to be changed + down: function () { + cockpit.rov.emit('plugin.wifilights.adjust', 0.1); + } + }, + + // lights decrement + { + name: 'plugin.wifilights.adjust_increment', + description: 'Makes the ROV lights dimmer.', + defaults: { keyboard: 'd', gamepad: 'DPAD_DOWN' },//gamepad button needs to be changed + + down: function () { + cockpit.rov.emit('plugin.wifilights.adjust', -0.1); + } + }, + + // lights ON + { + name: 'plugin.wifilights.set', + description: 'WiFi lights on.', + defaults: { keyboard: 'w' }, + down: function () { + cockpit.rov.emit('plugin.wifilights.set',1); + } + }, + + // lights OFF + { + name: 'plugin.wifilights.set', + description: 'WiFi lights off.', + defaults: { keyboard: 's' }, + down: function () { + cockpit.rov.emit('plugin.wifilights.set',0); + } + }, + + ] + + }; + + //This pattern will hook events in the cockpit and pull them all back + //so that the reference to this instance is available for further processing + plugins.wifiLights.prototype.listen = function listen() { + var self = this; + + self.cockpit.rov.withHistory.on('plugin.wifilights.state', function(state) { + self.cockpit.emit('plugin.wifilights.state',state); + }); + + self.cockpit.on('plugin.wifilights.set',function(value){ + self.cockpit.rov.emit('plugin.wifilights.set',value); + }); + + }; + + window.Cockpit.plugins.push(plugins.wifiLights); + +})(window);