// myTVServlet.java
package com.nazca.px;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Parcel;
@WebServlet("/ZigbeeCloudDemo")
public class myTVServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String proofOfLife ;
private myProxy pProxy = null ;
public void init(ServletConfig config) throws ServletException {
super.init(config);
android.content.Context androidContext =
(android.content.Context)config.getServletContext().getAttribute
("org.mortbay.ijetty.context");
proofOfLife = androidContext.getApplicationInfo().packageName;
androidContext.startService (
new Intent("com.google.android.ZigbeeApp.REMOTE_SERVICE"));
androidContext.bindService (
new Intent("com.google.android.ZigbeeApp.REMOTE_SERVICE"),
mConnection, android.content.Context.BIND_AUTO_CREATE);
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder ibinder){
pProxy = new myProxy(ibinder);
}
public void onServiceDisconnected(ComponentName className) {}
};
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String zigbeecmd = request.getParameter("zigbeecmd");
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<h1>智能家庭 – 移动互联网<br></h1>" );
out.println("<h3>控制操作: <br></h3>");
if(Integer.valueOf(zigbeecmd)==1){
pProxy.setLedStatus(1);
out.println("<h3><li>你已开启电源插座</li></h3>");
out.println("<h3><A HREF=\"index.html\">返回</a></h3>");
}
if(Integer.valueOf(zigbeecmd)==0){
pProxy.setLedStatus(0);
out.println("<h3><li>你已关闭电源插座</li></h3>");
out.println("<h3><A HREF=\"index.html\">返回</a></h3>");
}
out.println("</html>");
out.flush();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {}
//---------------------------------------------------------------------------------------------------
private class myProxy {
private IBinder ib ;
private String mStatus,mLedStatus ;
myProxy(IBinder ibinder) {
ib = ibinder ;
}
public void setLedStatus(int key){
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try{ ib.transact(key, data, replay, 0);
}catch (Exception e) { e.printStackTrace(); }
}
}
}