我正在制作一个Web应用程序,我想让一个状态按钮根据文本文件中的“打开”或“关闭”将颜色从红色更改为绿色。我正在使用Raspberry Pi和Ubuntu来“控制” Raspberry Pi。文本文件位于Raspberry Pi上,我让Web服务器通过代码读取Raspberry Pi上的内容。
要打开/关闭电视,我需要以下几行代码(在本例中为打开)
screen = screenDAO.findById(screenCode);
String[] args = new String[] { "/bin/bash", "-c", "ssh pi@172.19.xx.xxx 'echo \"on 0\" | cec-client -s'",
"with", "args" };
try {
Process proc = new ProcessBuilder(args).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f = new File("/tmp/status.txt");
if(f.exists()){
f.delete();
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
File file = new File("/tmp/status.txt");
FileWriter fr = null;
BufferedWriter br = null;
try {
// to append to file, you need to initialize FileWriter using below constructor
fr = new FileWriter(file, true);
br = new BufferedWriter(fr);
for (int i = 0; i < 1; i++) {
br.newLine();
// you can use write or append method
br.write("on");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return SUCCESS;
}
要更新我当前拥有的按钮状态
screen = screenDAO.findById(screenCode);
String[] args = new String[] { "/bin/bash", "-c",
"ssh pi@172.19.67.177 /tmp/status.txt", "with", "args" };
try {
Process proc = new ProcessBuilder(args).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String content = "";
try {
content = new String(Files.readAllBytes(Paths.get("/tmp/status.txt")));
if (content.contains("on")) {
System.out.println("The screen is turned on.");
Boolean screenStatusOn = true;
} else if (content.contains("off")) {
System.out.println("The screen is turned off.");
Boolean screenStatusOn = false;
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return SUCCESS;
}
这是我在js中拥有的
$('.screen-status').on('click', function(event) {
var $onButton= $(this);
$.ajax({
url : 'updateScreenStatus',
type : 'POST',
data : "screenCode=" + $onButton.data('code'),
success : function(data) {
console.log(data);
},
error : function(data) {
},
});
});
}
function initScreenControls() {
$('.screen-on').on('click', function(event) {
var $onButton= $(this);
$.ajax({
url : 'turnOnScreen',
type : 'POST',
data : "screenCode=" + $onButton.data('code'),
success : function(data) {
console.log(data);
},
error : function(data) {
},
});
});
$('.screen-off').on('click', function(event) {
var $onButton= $(this);
$.ajax({
url : 'turnOffScreen',
type : 'POST',
data : "screenCode=" + $onButton.data('code'),
success : function(data) {
console.log(data);
},
error : function(data) {
},
});
});
}
我希望关闭电视时按钮变为红色,而当电视打开时按钮变为绿色。目前,我只能发送“屏幕已打开”。 (或关闭)到控制台。
答案 0 :(得分:0)
您可以在javascript代码的“单击”部分(在登录控制台的下方)中详细说明成功后会发生什么。
例如:(关闭示例)
$('.screen-off').on('click', function(event) {
var $onButton= $(this);
$.ajax({
url : 'turnOffScreen',
type : 'POST',
data : "screenCode=" + $onButton.data('code'),
success : function(data) {
console.log(data);
/*document.getElementById("OnButton").style.backgroundColor = "red"; */
$(this).css('background-color', 'red');
},
error : function(data) {
},
});
});