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

Modify device dps using web browser #244

Merged
merged 1 commit into from
Dec 27, 2022
Merged
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
143 changes: 143 additions & 0 deletions server/web/device_dps.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>TinyTuya API Server - Device Details</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>

<body>
<div class="title"></div>
<p>Device Details</p>
<div class="device"></div>
<p>Device DPS</p>
<div class="dps"></div>

<script>

$(document).on('click', '.edit', function() {
$(this).parent().siblings('td.data').each(function() {
var content = $(this).html();
$(this).html('<input value="' + content + '" />');
});

$(this).siblings('.save').show();
$(this).hide();
});

$(document).on('click', '.save', function() {
var key = $(this).parent().siblings('td.key').html();
var val = "";

$('input').each(function() {
var content = $(this).val();
$(this).html(content);
$(this).contents().unwrap();

val = $(this).val()
});

$(this).siblings('.edit').show();
$(this).hide();

updateDPS(key,val);
});

function buildHtmlTable(json) {
var table = "<table class=\"table data\">";

table = table + "<th>Index</th><th>Value</th>";

for (var key in json) {
table = table + "<tr>";

var cellValue = key;
table = table + "<td class=\"key\">" + cellValue + "</td>";

var cellValue = json[key];
table = table + "<td class=\"data\">" + cellValue + "</td>";
table = table + "<td><button class=\"edit\">Edit</button><button class=\"save\" hidden>Save</button></td>";
table = table + "<tr>";
}

table = table + "</table>";
return table;
}

// Get Version
function showversion() {
var pwurl = window.location.protocol + "//" + window.location.hostname + ":8888/stats";
$.getJSON(pwurl, function(data) {
var text = `TinyTuya API Server ${data.tinytuya}`
$(".title").html(text);
});
setTimeout(showversion, 10000);
}

// Device DPS Details
function device_dps() {
// Grab the GET variables
var $_GET = {};
if(document.location.toString().indexOf('?') !== -1) {
var query = document.location
.toString()
// get the query string
.replace(/^.*?\?/, '')
// and remove any existing hash string (thanks, @vrijdenker)
.replace(/#.*$/, '')
.split('&');

for(var i=0, l=query.length; i<l; i++) {
var aux = decodeURIComponent(query[i]).split('=');
$_GET[aux[0]] = aux[1];
}
}
id = $_GET['id'];

// Device Metadata
var pwurl = window.location.protocol + "//" + window.location.hostname + ":8888/device/" + id;
$.getJSON(pwurl, function(data) {
var ip = data.ip;
var name = data.name;
var version = data.version;
var key = data.key;
var product = data.productKey;
var mac = data.mac;
var output =
"Name: " + name + "<br>" +
"Device ID: " + id + "<br>" +
"IP: " + ip + "<br>" +
"MAC: " + mac + "<br>" +
"Version: " + version + "<br>" +
"Key: " + key + "<br>" +
"Product: " + product + "<br>";
$(".device").html(output);
});

// Device DPS
var pwurl = window.location.protocol + "//" + window.location.hostname + ":8888/status/" + id;
$.getJSON(pwurl, function(data,textStatus,jqXHR) {
var dps_json = jqXHR.responseJSON["dps"];
$(".dps").html(buildHtmlTable(dps_json));
});
}

// Get Version
function updateDPS(key, val) {
var pwurl = window.location.protocol + "//" + window.location.hostname + ":8888/set/" + id + "/" + key + "/" + val;
$.getJSON(pwurl, function(data) {
window.location.reload();
});
}

// Display
showversion();
device_dps();

</script>

<div class="controls">
<p><a href="/">Back</a></p>
</div>

</body>
</html>
2 changes: 1 addition & 1 deletion server/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
output = output + rownum +
" - <a href='device.html?id=" + sortedDevices[x].id + "'>" +
(sortedDevices[x].name.length > 0 ? sortedDevices[x].name : '[' + sortedDevices[x].id + ']') + "</a> - " +
sortedDevices[x].id + " - " +
"<a href='device_dps.html?id=" + sortedDevices[x].id + "'>" + sortedDevices[x].id + "</a> -" +
sortedDevices[x].ip + "<br>\n";
rownum++;
}
Expand Down