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

Update sync_view.php in order to solve the issue with EmonCMSv10 #16

Merged
merged 1 commit into from May 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
268 changes: 147 additions & 121 deletions sync-module/sync_view.php
Expand Up @@ -40,14 +40,13 @@

<div id="time" style="float:right; padding-top:10px; padding-right:20px">Next update: 0s</div>


<div class="alert alert-info"></div>

<table id="sync-table" class="table hide feed-view">
<table class="table hide feed-view">
<tr><th>Location</th><th>Feed Name</th><th>Start time</th><th>Interval</th><th></th><th></th></tr>
<tbody id="feeds"></tbody>
<tbody id="all_feed_datas"></tbody>
</table>

<div class="input-view hide">
<p>Download remote emoncms inputs <button id="download-inputs" class="btn">Download</button></p>
<pre id="input-output"></pre>
Expand Down Expand Up @@ -75,144 +74,171 @@

var redis_enabled = <?php echo $redis_enabled; ?>;
var path = "<?php echo $path; ?>";
var remote = false;
//is remote var used and usefull ?
//var remote = false;
var feeds = [];
var next_update = 0;

$("#page").html(subaction.charAt(0).toUpperCase() + subaction.slice(1));

if (redis_enabled) {
$(".alert").show().html("Connecting to remote emoncms server...");
// Load remote details
$.ajax({ url: path+"sync/remote-load", dataType: 'json', async: false, success: function(result){
if (result.success!=undefined && !result.success) remote=false; else remote=result;
}});

$("#remote-host").val(remote.host);
$("#remote-username").val(remote.username);

if (remote) {
$(".alert").hide();

if (subaction=="feeds") {
$(".feed-view").show();
sync_list();
}

if (subaction=="inputs") {
$(".input-view").show();
}

if (subaction=="dashboards") {
$(".dashboard-view").show();
}


}

setInterval(sync_list,10000);
}

function sync_list()
//given the json feeds list, generate the html string
function jsonfeedsTohtml(feeds)
{
next_update = 10;

$(".alert").html("Connected. Fetching emoncms feeds...");
$.ajax({ url: path+"sync/feed-list", dataType: 'json', async: false, success: function(result){
feeds = result;
}});

var out = "";
var out = [];
for (var name in feeds) {

var action = "";
var status = "";
var feedlocation = "";
var interval = 0;
var start_time = 0;
var tr = "<tr>";
var action = "";
var status = "";
var feedlocation = "";
var interval = 0;
var start_time = 0;
var tr = "<tr>";

if (!feeds[name].local.exists && feeds[name].remote.exists) {
feedlocation = "Remote";
action = "<button class='btn btn-small download' name='"+name+"'><i class='icon-arrow-left' ></i> Download</button>";
if (!feeds[name].local.exists && feeds[name].remote.exists) {
feedlocation = "Remote";
action = "<button class='btn btn-small download' name='"+name+"'><i class='icon-arrow-left' ></i> Download</button>";

start_time = feeds[name].remote.start_time;
interval = feeds[name].remote.interval;
}
start_time = feeds[name].remote.start_time;
interval = feeds[name].remote.interval;
}

if (feeds[name].local.exists && !feeds[name].remote.exists) {
feedlocation = "Local";
action = "<button class='btn btn-small upload' name='"+name+"'><i class='icon-arrow-right' ></i> Upload</button>";
if (feeds[name].local.exists && !feeds[name].remote.exists) {
feedlocation = "Local";
action = "<button class='btn btn-small upload' name='"+name+"'><i class='icon-arrow-right' ></i> Upload</button>";

start_time = feeds[name].local.start_time;
interval = feeds[name].local.interval;
}
start_time = feeds[name].local.start_time;
interval = feeds[name].local.interval;
}

if (feeds[name].local.start_time==feeds[name].remote.start_time && feeds[name].local.interval==feeds[name].remote.interval) {
feedlocation = "Both";
if (feeds[name].local.start_time==feeds[name].remote.start_time && feeds[name].local.interval==feeds[name].remote.interval) {
feedlocation = "Both";

start_time = feeds[name].local.start_time;
interval = feeds[name].local.interval;
}
start_time = feeds[name].local.start_time;
interval = feeds[name].local.interval;
}

if (feeds[name].local.start_time==feeds[name].remote.start_time && feeds[name].local.interval==feeds[name].remote.interval) {
if (feeds[name].local.npoints>feeds[name].remote.npoints) {
tr = "<tr class='info'>";
if (feeds[name].local.start_time==feeds[name].remote.start_time && feeds[name].local.interval==feeds[name].remote.interval) {
if (feeds[name].local.npoints>feeds[name].remote.npoints) {
tr = "<tr class='info'>";

status = "Local ahead of Remote by "+(feeds[name].local.npoints-feeds[name].remote.npoints)+" points";
action = "<button class='btn btn-small upload' name='"+name+"'><i class='icon-arrow-right' ></i> Upload</button>";
status = "Local ahead of Remote by "+(feeds[name].local.npoints-feeds[name].remote.npoints)+" points";
action = "<button class='btn btn-small upload' name='"+name+"'><i class='icon-arrow-right' ></i> Upload</button>";

} else if (feeds[name].local.npoints<feeds[name].remote.npoints) {
tr = "<tr class='warning'>";
} else if (feeds[name].local.npoints<feeds[name].remote.npoints) {
tr = "<tr class='warning'>";

status = "Local behind Remote by "+(feeds[name].remote.npoints-feeds[name].local.npoints)+" points";
action = "<button class='btn btn-small download' name='"+name+"'><i class='icon-arrow-left' ></i> Download</button>";
status = "Local behind Remote by "+(feeds[name].remote.npoints-feeds[name].local.npoints)+" points";
action = "<button class='btn btn-small download' name='"+name+"'><i class='icon-arrow-left' ></i> Download</button>";

} else {
tr = "<tr class='success'>";
} else {
tr = "<tr class='success'>";

status = "Local and Remote are the same";
action = "";
}
status = "Local and Remote are the same";
action = "";
}
}

//out += "<td><input class='feed-select-checkbox' type=checkbox></td>";
//out += "<td><input class='feed-select-checkbox' type=checkbox></td>";

out += tr;
out += "<td>"+feedlocation+"</td>";
out += "<td>"+name+"</td>";
out.push(tr);
out.push("<td>"+feedlocation+"</td>");
out.push("<td>"+name+"</td>");

if (interval!=undefined) {
out += "<td>"+timeConverter(start_time)+"</td>";
} else {
out += "<td>n/a</td>";
}
if (interval!=undefined) {
out.push("<td>"+timeConverter(start_time)+"</td>");
} else {
out.push("<td>n/a</td>");
}

if (interval!=undefined) {
out += "<td>"+interval+"s</td>";
} else {
out += "<td>n/a</td>";
}
if (interval!=undefined) {
out.push("<td>"+interval+"s</td>");
} else {
out.push("<td>n/a</td>");
}

out += "<td class='status' name='"+name+"'>"+status+"</td>";
out.push("<td class='status' name='"+name+"'>"+status+"</td>");

//out += "<td>"+feeds[name].local.start_time+":"+feeds[name].local.interval+":"+feeds[name].local.npoints+"</td>";
out += "<td>"+action+"</td>";
//out += "<td>"+feeds[name].local.start_time+":"+feeds[name].local.interval+":"+feeds[name].local.npoints+"</td>";
out.push("<td>"+action+"</td>");

//out += "<td>"+feeds[z].remote.start_time+":"+feeds[z].remote.interval+":"+feeds[z].remote.npoints+"</td>";
//out += "<td><div class='syncprogress' style='width:"+Math.round(feeds[name].remote.npoints*0.0001)+"px'>"+feeds[name].local.start_time+":"+feeds[name].local.interval+":"+feeds[name].remote.npoints+"</div></td>";
out += "</tr>";
//out += "<td>"+feeds[z].remote.start_time+":"+feeds[z].remote.interval+":"+feeds[z].remote.npoints+"</td>";
//out += "<td><div class='syncprogress' style='width:"+Math.round(feeds[name].remote.npoints*0.0001)+"px'>"+feeds[name].local.start_time+":"+feeds[name].local.interval+":"+feeds[name].remote.npoints+"</div></td>";
out.push("</tr>");
}
$("#feeds").html(out);
$(".alert").hide();
return out.join("");

}

//interrogate the API and update the list
//update the gloabl variable new_update
//update the global variable feeds
function syncList()
{
next_update = 10;

$(".alert").html("Connected. Fetching emoncms feeds...");
$.ajax({
url: path+"sync/feed-list",
dataType: 'json',
async: true,
success(result){
out=jsonfeedsTohtml(result);
feeds=result;
$("#all_feed_datas").html(out);
$(".feed_view").show();
$(".alert").hide();
}
});
}

// interrogate the API and Load all the remote details
function remoteLoad()
{
$.ajax({
url: path+"sync/remote-load",
dataType: 'json',
async: true,
success(result){
//don't really understand this test as remote-load do not return any success field
//if (result.success!=undefined && !result.success) {
// //remote=false;
//} else {
//remote=result;
$(".alert").hide();
$("#remote-host").val(result.host);
$("#remote-username").val(result.username);
if (subaction=="feeds") {
$(".feed-view").show();
syncList();
}
if (subaction=="inputs") {
$(".input-view").show();
}
if (subaction=="dashboards") {
$(".dashboard-view").show();
}
//}
},
error(xhr) {
var errorMessage = xhr.status + ": " + xhr.statusText;
alert("Error - " + errorMessage);
}
});
}

$("#page").html(subaction.charAt(0).toUpperCase() + subaction.slice(1));

if (redis_enabled) {
$(".alert").show().html("Connecting to remote emoncms server...");
remoteLoad();
setInterval(syncList,10000);
}


$("#remote-save").click(function(){
var host = $("#remote-host").val();
var username = $("#remote-username").val();
var password = encodeURIComponent($("#remote-password").val());

$("#sync-table").hide();
$(".feed-view").hide();
$(".alert").show().html("Connecting to remote emoncms server...");

$.ajax({
Expand All @@ -221,13 +247,11 @@ function sync_list()
data: "host="+host+"&username="+username+"&password="+password,
dataType: 'json',
async: true,
success: function(result){
success(result){
if (result.success) {
$("#sync-table").show();
remote = result;
//remote = result;
// feed list scan
sync_list();

remoteLoad();
} else {
alert(result.message);
}
Expand All @@ -247,15 +271,16 @@ function sync_list()
});
});

$("#feeds").on("click",".download",function(){
$("#all_feed_datas").on("click",".download",function(){
var name = $(this).attr("name");
$(".status[name='"+name+"']").html("Downloading...");
var request = "name="+name+"&tag="+feeds[name].remote.tag+"&remoteid="+feeds[name].remote.id+"&interval="+feeds[name].remote.interval+"&engine="+feeds[name].remote.engine+"&datatype="+feeds[name].remote.datatype;
$.ajax({
url: path+"sync/download",
data: "name="+name+"&tag="+feeds[name].remote.tag+"&remoteid="+feeds[name].remote.id+"&interval="+feeds[name].remote.interval+"&engine="+feeds[name].remote.engine+"&datatype="+feeds[name].remote.datatype,
data: request,
dataType: 'json',
async: true,
success: function(result){
success(result){
if (result.success) {
// success
} else {
Expand All @@ -266,15 +291,16 @@ function sync_list()
});
});

$("#feeds").on("click",".upload",function(){
$("#all_feed_datas").on("click",".upload",function(){
var name = $(this).attr("name");
$(".status[name='"+name+"']").html("Uploading...");
var request = "name="+name+"&tag="+feeds[name].local.tag+"&localid="+feeds[name].local.id+"&interval="+feeds[name].local.interval+"&engine="+feeds[name].local.engine+"&datatype="+feeds[name].local.datatype;
$.ajax({
url: path+"sync/upload",
data: "name="+name+"&tag="+feeds[name].local.tag+"&localid="+feeds[name].local.id+"&interval="+feeds[name].local.interval+"&engine="+feeds[name].local.engine+"&datatype="+feeds[name].local.datatype,
data: request,
dataType: 'json',
async: true,
success: function(result){
success(result){
if (result.success) {
// success
} else {
Expand All @@ -290,7 +316,7 @@ function sync_list()
url: path+"sync/download-inputs",
dataType: 'text',
async: true,
success: function(result){
success(result){
$("#input-output").html(result);
}
});
Expand All @@ -301,7 +327,7 @@ function sync_list()
url: path+"sync/download-dashboards",
dataType: 'text',
async: true,
success: function(result){
success(result){
$("#dashboard-output").html(result);
}
});
Expand Down