携帯端末から、ダイアルアップで家電制御webシステムの構築方法
以下のシステム限定で話を進めるために、他の環境では、多少の変更が必要かもしれません。| 設定項目 | 設定内容 |
| サーバーアドレス | 192.168.0.1 |
| クライアントアドレス | 192.168.0.7 |
| ピックネットワークインターフェイスカードアドレス | 192.168.0.200 |
CMD /C "C:\Program Files\Lynx for Win32\lynx.exe" -source http://192.168.0.200/modify.cgi?RB4=H
CMD /C ping localhost
CMD /C "C:\Program Files\Lynx for Win32\lynx.exe" -source http://192.168.0.200/modify.cgi?RB4=L
CMD /C "C:\Program Files\Lynx for Win32\lynx.exe" -source http://192.168.0.200/modify.cgi?RB4=H
CMD /C ping localhost
CMD /C "C:\Program Files\Lynx for Win32\lynx.exe" -source http://192.168.0.200/modify.cgi?RB4=L
CMD /C "C:\Program Files\Lynx for Win32\lynx.exe" -source http://192.168.0.200/modify.cgi?RB7=H
CMD /C ping localhost
CMD /C "C:\Program Files\Lynx for Win32\lynx.exe" -source http://192.168.0.200/modify.cgi?RB7=L
// These permissions apply to javac
grant codeBase "file:${java.home}/lib/-" {
permission java.security.AllPermission;
};
// These permissions apply to all shared system extensions
grant codeBase "file:${java.home}/jre/lib/ext/-" {
permission java.security.AllPermission;
};
// These permissions apply to javac when ${java.home] points at $JAVA_HOME/jre
grant codeBase "file:${java.home}/../lib/-" {
permission java.security.AllPermission;
};
// These permissions apply to all shared system extensions when
// ${java.home} points at $JAVA_HOME/jre
grant codeBase "file:${java.home}/lib/ext/-" {
permission java.security.AllPermission;
};
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>TimeControl</servlet-name>
<servlet-class>snet.timer.TimeControl</servlet-class>
</servlet>
<servlet>
<servlet-name>DeleteServlet</servlet-name>
<servlet-class>snet.delete.DeleteServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>BackupServlet</servlet-name>
<servlet-class>snet.delete.BackupServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ListServlet</servlet-name>
<servlet-class>snet.delete.ListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
TimeControl
</servlet-name>
<url-pattern>
/TimeControl
</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>
DeleteServlet
</servlet-name>
<url-pattern>
/DeleteServlet
</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>
BackupServlet
</servlet-name>
<url-pattern>
/BackupServlet
</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>
ListServlet
</servlet-name>
<url-pattern>
/ListServlet
</url-pattern>
</servlet-mapping>
</web-app>
package snet.timer;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Timer;
public class TimeControl extends HttpServlet {
final static String path = "/snet/TimeControl";
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doTask(request, response);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doTask(request, response);
}
public void doTask(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Calendar defaultCal = null;
// mode param
String param = request.getParameter("mode");
if( param == null ) param = "list";
if( param.equals("") ) param = "list";
String seq = request.getParameter("seq");
// schedule param
String place = request.getParameter("place");
if( place == null ) place = "";
String year = request.getParameter("year");
String mon = request.getParameter("mon");
String day = request.getParameter("day");
String h = request.getParameter("h");
String m = request.getParameter("m");
String s = request.getParameter("s");
StringBuffer sb = new StringBuffer();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
MyTimer myTimer = MyTimer.getInstance();
if( param.equals("list")) {
;
} else if( param.equals("reset")) {
MyTimer.reset();
} else if( param.equals("delete")) {
MyTimer.cancel(place, seq);
System.out.println(place+" "+seq);
} else if( param.equals("regist")) {
Calendar cl = Calendar.getInstance(Locale.JAPAN);
cl.set(Calendar.YEAR, Integer.parseInt(year));
cl.set(Calendar.MONTH, Integer.parseInt(mon));
cl.set(Calendar.DATE, Integer.parseInt(day));
cl.set(Calendar.HOUR_OF_DAY, Integer.parseInt(h));
cl.set(Calendar.MINUTE, Integer.parseInt(m));
cl.set(Calendar.SECOND, Integer.parseInt(s));
MyTask tm = new MyTask();
String result = MyTimer.set(place, seq, cl) ;
if( result != null ) {
// スケジュールの追加不可
sb.append(result + "\n");
}
} else if( param.equals("default_myroom_1")) {
defaultCal = Calendar.getInstance(Locale.JAPAN);
defaultCal.set(Calendar.HOUR_OF_DAY, 19);
defaultCal.set(Calendar.MINUTE, 3);
defaultCal.set(Calendar.SECOND, 0);
// place上書き
place = MyTimer.MYROOM;
} else if( param.equals("default_under_1")) {
defaultCal = Calendar.getInstance(Locale.JAPAN);
defaultCal.set(Calendar.HOUR_OF_DAY, 18);
defaultCal.set(Calendar.MINUTE, 50);
defaultCal.set(Calendar.SECOND, 0);
// place上書き
place = MyTimer.UNDERROOM;
}
sb.append("<html>\n");
sb.append("<head>\n");
sb.append("<title></title>\n");
sb.append("</head>\n");
sb.append("<body>\n");
sb.append("<form method='post' action='" + path + "?mode=regist'>\n");
sb.append("thread size:" + MyTimer.mapSize() + "<br>\n");
// display for list
MyTask[] myTask = MyTimer.sortedList();
for( int i=0; i<myTask.length; i++ ) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String value = formatter.format( new Date(myTask[i].scheduledExecutionTime()) );
sb.append("<a href='" + path + "?mode=delete&place=" + myTask[i].id + "&seq=" + myTask[i].seq + "' >DELETE</a> \n");
sb.append(myTask[i].id + " " + myTask[i].seq + " : " + value + "<br>\n");
}
sb.append("<a href='" + path + "?mode=list' >LIST</a><br>\n");
sb.append("<a href='" + path + "?mode=reset' >RESET</a><br>\n");
sb.append("<select name='place' size=1>\n");
sb.append("<option value='" + MyTimer.MYROOM + "' " +
( place.equals(MyTimer.MYROOM) ? "selected" : "" ) + ">" + MyTimer.MYROOM + "\n");
sb.append("<option value='" + MyTimer.UNDERROOM + "' " +
( place.equals(MyTimer.UNDERROOM) ? "selected" : "" ) + ">" + MyTimer.UNDERROOM + "\n");
sb.append("</select>\n");
// キー設定
sb.append("<select name='seq' size=1>\n");
sb.append("<option value='1'>KEY_1\n");
sb.append("<option value='2'>KEY_2\n");
sb.append("<option value='3'>KEY_3\n");
sb.append("<option value='4'>KEY_4\n");
sb.append("<option value='5'>KEY_5\n");
sb.append("<option value='6'>KEY_6\n");
sb.append("<option value='7'>KEY_7\n");
sb.append("<option value='8'>KEY_8\n");
sb.append("<option value='9'>KEY_9\n");
sb.append("<option value='10'>KEY_10\n");
sb.append("<option value='11'>KEY_11\n");
sb.append("<option value='12'>KEY_12\n");
sb.append("<option value='13'>KEY_13\n");
sb.append("<option value='14'>KEY_14\n");
sb.append("<option value='15'>KEY_15\n");
sb.append("<option value='16'>KEY_16\n");
sb.append("<option value='17'>KEY_17\n");
sb.append("<option value='18'>KEY_18\n");
sb.append("<option value='19'>KEY_19\n");
sb.append("<option value='20'>KEY_20\n");
sb.append("</select><br>\n");
Calendar cl = Calendar.getInstance(Locale.JAPAN);
// 年の設定
sb.append("<input type='text' size='4' name='year' value='" + cl.get(Calendar.YEAR) + "'/>\n");
// 月の設定
sb.append("<select name='mon' size=1>\n");
for( int i=0; i<=11; i++ ) {
sb.append("<option value='" + i + "' " + ( cl.get(Calendar.MONTH) == i ? "selected" : "" ) + " >" + (i+1) );
}
sb.append("</select>\n");
// 日の設定
sb.append("<select name='day' size=1>\n");
for( int i=1; i<=31; i++ ) {
sb.append("<option value='" + i + "' " + ( cl.get(Calendar.DATE) == i ? "selected" : "" ) + " >" + i );
}
sb.append("</select><br>\n");
// 時間の設定
sb.append("<select name='h' size=1>\n");
for( int i=0; i<24; i++ ) {
if( defaultCal == null )
sb.append("<option value='" + i + "' " + ( cl.get(Calendar.HOUR_OF_DAY) == i ? "selected" : "" ) + " >" + i );
else
sb.append("<option value='" + i + "' " + ( defaultCal.get(Calendar.HOUR_OF_DAY) == i ? "selected" : "" ) + " >" + i );
}
sb.append("</select>\n");
// 分の設定
sb.append("<select name='m' size=1>\n");
for( int i=0; i<60; i++ ) {
if( defaultCal == null )
sb.append("<option value='" + i + "' " + ( cl.get(Calendar.MINUTE) == i ? "selected" : "" ) + " >" + i );
else
sb.append("<option value='" + i + "' " + ( defaultCal.get(Calendar.MINUTE) == i ? "selected" : "" ) + " >" + i );
}
sb.append("</select>\n");
// 秒の設定
sb.append("<select name='s' size=1>\n");
for( int i=0; i<60; i++ ) {
if( defaultCal == null )
sb.append("<option value='" + i + "' " + ( cl.get(Calendar.SECOND) == i ? "selected" : "" ) + " >" + i );
else
sb.append("<option value='" + i + "' " + ( defaultCal.get(Calendar.SECOND) == i ? "selected" : "" ) + " >" + i );
}
sb.append("</select><br>\n");
sb.append("<input type='submit' /><br>\n");
sb.append("</form>\n");
sb.append("</body>\n");
sb.append("</html>\n");
out.println(sb.toString());
}
}
/*
* スケジュールコントローラー
*/
class MyTimer extends Timer {
static public String UNDERROOM = "UNDERROOM";
static public String MYROOM = "MYROOM";
static private MyTimer myTimer = null;
static private Map taskMap = null;
private MyTimer() { super(); }
private MyTimer(boolean isDaemon) { super(isDaemon); }
static public int mapSize() {
return taskMap.size();
}
/*
* インスタンスの取得
* Mapも一緒に生成していることに注意
*/
static public MyTimer getInstance() {
if( myTimer == null ) {
myTimer = new MyTimer();
taskMap = new HashMap();
}
return myTimer;
}
static public String set(String key, String seq, Calendar cl) {
if( Calendar.getInstance(Locale.JAPAN).getTime().getTime() > cl.getTime().getTime() ) {
System.out.println("Not Added Scheduled:");
return "Not Added Schedule !";
}
// すでに登録されている場合には、前のスケジュールをキャンセル
cancel(key, seq);
MyTask tm = new MyTask();
tm.id = key;
tm.seq = seq;
myTimer.schedule(tm, cl.getTime());
taskMap.put(( key + seq ), tm);
System.out.println("Scheduled:" + cl.getTime());
return null;
}
static public void cancel(String key, String seq) {
MyTask tm = (MyTask)taskMap.get(key+seq);
if( tm != null ) tm.cancel();
taskMap.remove(key+seq);
}
static public MyTask[] list() {
Collection col = taskMap.values();
if( col == null ) return null;
return (MyTask[])col.toArray(new MyTask[0]);
}
static public MyTask[] sortedList() {
Collection col = taskMap.values();
if( col == null ) return null;
MyTask[] ret = (MyTask[])col.toArray(new MyTask[0]);
for( int i=0; i<ret.length; i++ ) {
for( int k=i; k<ret.length; k++ ) {
if( ret[i].scheduledExecutionTime() > ret[k].scheduledExecutionTime() ) {
// 入れ替え
MyTask tmp = ret[i];
ret[i] = ret[k];
ret[k] = tmp;
}
}
}
return ret;
}
static public void reset() {
MyTask[] tm = list();
for( int i=0; i<tm.length; i++ ) {
cancel(tm[i].id, tm[i].seq);
}
}
}
/*
* タイマから起動されるタスク。
* 時刻を保持するTimeクラスを持ち、タイマから起動されるごとに、時刻を
* 刻むため、Timeメソッドのtick()を呼び出す。
*/
class MyTask extends java.util.TimerTask {
public String id = null;
public String seq = null;
public MyTask() {
super();
}
public void run() {
try {
if( id.equals(MyTimer.UNDERROOM) ) {
Process proc = Runtime.getRuntime().exec("C:\\public_html\\pic\\execute\\under.bat");
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ( (line = in.readLine()) != null )
{
System.out.println(line);
}
in.close();
proc.waitFor();
proc.destroy();
MyTimer myTimer = MyTimer.getInstance();
MyTimer.cancel(id, seq);
} else if( id.equals(MyTimer.MYROOM) ) {
Process proc = Runtime.getRuntime().exec("C:\\public_html\\pic\\execute\\myroom.bat");
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ( (line = in.readLine()) != null )
{
System.out.println(line);
}
in.close();
proc.waitFor();
proc.destroy();
MyTimer myTimer = MyTimer.getInstance();
MyTimer.cancel(id, seq);
}
} catch ( Exception e ) { e.printStackTrace(); }
}
}
package snet.delete;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BackupServlet extends HttpServlet {
final public String directDir = "C:\\public_html\\pic\\direct";
final public String shotDir = "C:\\public_html\\pic\\shot";
final public String backupDir = "C:\\public_html\\pic\\backup";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void destroy() {
}
private void searchFile(File[] listfile, HttpServletResponse response, PrintWriter pw) throws Exception {
pw.write("<html>\n");
pw.write("<head>\n");
pw.write("<title></title>\n");
pw.write("</head>\n");
pw.write("<body>\n");
pw.write("<form>\n");
Date cal = new Date();
pw.write("length=" + listfile.length + "\r\n");
for( int i=0; i<listfile.length; i++ ) {
if( listfile[i].isFile() ) {
if( cal.getTime() >= listfile[i].lastModified() + (1000*60*10) ) {
try {
File par = listfile[i].getParentFile();
File targetBackup = new File(backupDir + "\\" + par.getName());
if( !targetBackup.exists() ) { targetBackup.mkdirs(); }
listfile[i].renameTo(new File(backupDir + "\\" + par.getName() + "\\" + listfile[i].getName()));
}
catch( Exception e ) {
e.printStackTrace();
}
}
} else {
this.searchFile( listfile[i].listFiles(), response, pw );
}
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
try {
PrintWriter pw = response.getWriter();
File target = new File(directDir);
File[] listfile = target.listFiles();
this.searchFile(listfile, response, pw);
target = null;
target = new File(shotDir);
listfile = null;
listfile = target.listFiles();
this.searchFile(listfile, response, pw);
pw.write("<br>OK<br>");
pw.write("</form>\n");
pw.write("</body>\n");
pw.write("</html>\n");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
package snet.delete;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DeleteServlet extends HttpServlet {
final public String directDir = "C:\\public_html\\pic\\direct";
final public String shotDir = "C:\\public_html\\pic\\shot";
final public String backupDir = "C:\\public_html\\pic\\backup";
final public String logDir = "C:\\Program Files\\Apache Tomcat 4.0\\logs";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void destroy() {
}
private void searchFile(File[] listfile, HttpServletResponse response, PrintWriter pw, long timeCount) throws Exception {
pw.write("<html>\n");
pw.write("<head>\n");
pw.write("<title></title>\n");
pw.write("</head>\n");
pw.write("<body>\n");
pw.write("<form>\n");
Date cal = new Date();
if( listfile == null ) {
pw.write("length=0\r\n");
return;
} else {
pw.write("length=" + listfile.length + "\r\n");
}
for( int i=0; i<listfile.length; i++ ) {
if( listfile[i].isFile() ) {
if( cal.getTime() >= listfile[i].lastModified() + timeCount ) {
listfile[i].delete();
}
} else {
this.searchFile( listfile[i].listFiles(), response, pw, timeCount );
if( listfile[i].listFiles().length == 0 ) {
pw.write("dir " + listfile[i].toString() + "\r\n");
listfile[i].delete();
}
}
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
PrintWriter pw = response.getWriter();
File target = new File(directDir);
File[] listfile = target.listFiles();
this.searchFile(listfile, response, pw, 1000*60*10);
target = null;
target = new File(shotDir);
listfile = null;
listfile = target.listFiles();
this.searchFile(listfile, response, pw, 1000*60*10);
target = null;
target = new File(backupDir);
listfile = null;
listfile = target.listFiles();
this.searchFile(listfile, response, pw, 1000*60*10);
target = null;
target = new File(logDir);
listfile = null;
listfile = target.listFiles();
this.searchFile(listfile, response, pw, 1000*60*1);
pw.write("<br>OK<br>");
pw.write("</form>\n");
pw.write("</body>\n");
pw.write("</html>\n");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}
package snet.delete;
import java.util.*;
import java.io.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ListServlet extends HttpServlet {
final public String directDir = "C:\\public_html\\pic\\direct";
final public String shotDir = "C:\\public_html\\pic\\shot";
final public String directURL = "http://localhost:8080/snet/pic/direct";
final public String shotURL = "http://localhost:8080/snet/pic/shot";
final public String backURL = "/snet/BackupServlet";
final public String deleteURL = "/snet/DeleteServlet";
final public String timeURL = "/snet/TimeControl";
final public String listURL = "/snet/pic/";
final public String listServletURL = "/snet/ListServlet";
Vector newList = null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void destroy() {
}
private void searchFile(File[] listfile, HttpServletResponse response, PrintWriter pw) throws Exception {
Date cal = new Date();
pw.write("length=" + listfile.length + "<br>\r\n");
for( int i=0; i<listfile.length; i++ ) {
if( listfile[i].isFile() ) {
if( cal.getTime() >= listfile[i].lastModified() ) {
this.newList.add((File)listfile[i]);
}
} else {
this.searchFile( listfile[i].listFiles(), response, pw );
}
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
PrintWriter pw = response.getWriter();
pw.write("<html>\n");
pw.write("<head>\n");
pw.write("<title></title>\n");
pw.write("</head>\n");
pw.write("<body>\n");
pw.write("<form>\n");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
pw.write(formatter.format(new Date(System.currentTimeMillis())) + "<br>\r\n");
pw.write(directDir + " LIST <br>\r\n");
this.newList = null;
this.newList = new Vector();
File target = new File(directDir);
File[] listfile = target.listFiles();
this.searchFile(listfile, response, pw);
File[] sortedList = (File[])this.newList.toArray(new File[0]);
Arrays.sort(sortedList, new DateComparator());
int st = sortedList.length - 5;
if( st < 0 ) st = 0;
for( int i=st; i<sortedList.length; i++ ) {
File tmp = (File)sortedList[i];
pw.write(formatter.format( new Date(tmp.lastModified()) ));
StringBuffer sb = new StringBuffer();
sb.append(directURL);
sb.append(tmp.toString().substring(directDir.length(), tmp.toString().length()));
String bb = sb.toString();
bb = bb.replace('\\','/');
pw.write("<A href='" + bb + "'>" + tmp.getName() + "</A><br>\r\n");
}
pw.write("<br>\r\n" + shotDir + " LIST <br>\r\n");
this.newList = null;
this.newList = new Vector();
target = null;
target = new File(shotDir);
listfile = null;
listfile = target.listFiles();
this.searchFile(listfile, response, pw);
sortedList = null;
sortedList = (File[])this.newList.toArray(new File[0]);
Arrays.sort(sortedList, new DateComparator());
st = sortedList.length - 5;
if( st < 0 ) st = 0;
for( int i=st; i<sortedList.length; i++ ) {
File tmp = (File)sortedList[i];
pw.write(formatter.format( new Date(tmp.lastModified()) ));
StringBuffer sb = new StringBuffer();
sb.append(shotURL);
sb.append(tmp.toString().substring(shotDir.length(), tmp.toString().length()));
String bb = sb.toString();
bb = bb.replace('\\','/');
pw.write("<A href='" + bb + "'>" + tmp.getName() + "</A><br>\r\n");
}
pw.write("<A href='" + backURL + "'>backup</A><br>\r\n");
pw.write("<A href='" + deleteURL + "'>delete</A><br>\r\n");
pw.write("<A href='" + timeURL + "'>time</A><br>\r\n");
pw.write("<A href='" + listURL + "'>list</A><br>\r\n");
pw.write("<A href='" + listServletURL + "'>listSrv</A><br>\r\n");
pw.write("<br>OK<br>");
pw.write("</form>\n");
pw.write("</body>\n");
pw.write("</html>\n");
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
class DateComparator implements Comparator {
public int compare(Object o1, Object o2) {
File d1 = (File)o1;
File d2 = (File)o2;
if( d1.lastModified() > d2.lastModified() ) return 1;
if( d1.lastModified() == d2.lastModified() ) return 0;
return -1;
}
}
}
| 設定項目 | 設定内容 |
| 接続先電話番号 | NTTドコモ P-in M@ster (サーバー側) の電話番号 |