Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wifilights plugin #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/plugins/new-ui/public/webcomponents/switches.html
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions src/plugins/wifilights/index.js
Original file line number Diff line number Diff line change
@@ -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);
};

})();
78 changes: 78 additions & 0 deletions src/plugins/wifilights/public/js/wifilights.js
Original file line number Diff line number Diff line change
@@ -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);