Skip to content

Commit

Permalink
Merge pull request #64 from 22hours/Server/Part_of_json
Browse files Browse the repository at this point in the history
v1.2.8.2 (서버로그 구분자추가, msg와 powerOff Timer에러뜨는 것 해결)
  • Loading branch information
jongwuner authored Mar 1, 2020
2 parents 65c1e6d + cb5f2ba commit 45ef7d8
Show file tree
Hide file tree
Showing 12 changed files with 417 additions and 91 deletions.
12 changes: 12 additions & 0 deletions dev/Server/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
<display-name>system_monitor_ver1.1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2 changes: 1 addition & 1 deletion dev/Server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'com.hours22'
version = '1.2-SNAPSHOT'
version = '1.2.8.2-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package com.hours22.system_monitor_ver11;

import java.nio.charset.Charset;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;


@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public HttpMessageConverter<String> responseBodyConverter() {
return new StringHttpMessageConverter(Charset.forName("UTF-8"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hours22.system_monitor_ver11.client;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

@Controller
public class ClientInfoController {
public static String getClientIp(HttpServletRequest req) {
String ip = req.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getRemoteAddr();
}
return ip;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.hours22.system_monitor_ver11.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hours22.system_monitor_ver11.client.ClientInfoController;
import com.hours22.system_monitor_ver11.db.DataService;
import com.hours22.system_monitor_ver11.db.LettuceController;
import com.hours22.system_monitor_ver11.vo.PcData;
Expand All @@ -32,22 +38,44 @@ public class CommonController {
@Autowired
LettuceController lc;

@Autowired
ClientInfoController cic;
SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

@RequestMapping(value = "/", method = RequestMethod.GET)
public void GetIndex(HttpServletResponse response) throws IOException {
response.getWriter().print("Welcome 22Hours!");
System.out.println("Input : / <- GET method ");
public void GetIndex(HttpServletRequest req, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println("Welcome 22Hours!<br>");
response.getWriter().println("This is system-monitor project.<br>");
response.getWriter().println("서버작업중 by jongchu. 2020-03-01 15:30<br><br><br><br>");

response.getWriter().println("O response : 값 전해주기.<br>" +
"△ 예외처리 test.<br>" +
"O request ip 추가하기<br>" +
"O nohup on/off.<br>" +
"O message 글씨 깨짐현상. (v1.2.7<br>" +
"O pc/연장신청, db에 반영이 안됌. (v1.2.7)<br>" +
"O url로만 받으면 될것 같은데, endtime 굳이 json으로 받을 필요 없지 않는가. ----> Client 규약 명확하게 하면 문제발생 X<br>" +
"O method에 현재시간 추가해주기.<br>" +
"O pc 언제꺼 ??? db에 반영 됌.<br>" +
"- observer pattern 적용 필요\r\n" +
"- 비동기 처리 필요");
System.out.println("---------------------------------------------------------------------");
System.out.println("Input : / <- GET method [Client Ip : "+ cic.getClientIp(req) +" ] at " + transFormat.format(new Date()));
}

@RequestMapping(value = "/testpage", method = RequestMethod.GET)
public @ResponseBody String GetTestPage(HttpServletResponse response) throws IOException {
public @ResponseBody String GetTestPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
dss.test();
System.out.println("Input : /testpage <- GET method ");
System.out.println("---------------------------------------------------------------------");
System.out.println("Input : /testpage <- GET method [Client Ip : " +cic.getClientIp(request)+" ] at " + transFormat.format(new Date()));
return "Test Page";
}

@RequestMapping(value = "/pc/{id}", method = RequestMethod.GET)
public @ResponseBody String GetPcInstanceData(HttpServletResponse response, @PathVariable String id) throws IOException {
System.out.println("Input : /pc/"+ id +" <- GET method ");
public @ResponseBody String GetPcInstanceData(HttpServletRequest req, HttpServletResponse response, @PathVariable String id) throws IOException {
System.out.println("---------------------------------------------------------------------");
System.out.println("Input : /pc/"+ id +" <- GET method [Client Ip : "+ cic.getClientIp(req) + " ] at " + transFormat.format(new Date()));

lc.getConnection();
String res = lc.getConnectionHgetall(id);
Expand All @@ -57,16 +85,18 @@ public void GetIndex(HttpServletResponse response) throws IOException {
}

@RequestMapping(value = "/pc/{id}", method = RequestMethod.POST)
public void PostPcInstanceData(HttpServletRequest request, @RequestBody Map<String, String> map, @PathVariable String id) throws IOException {
public void PostPcInstanceData(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> map, @PathVariable String id) throws IOException {
String json = ojm.writeValueAsString(map);
System.out.println("Input : "+ json +" <- POST method ");
System.out.println("---------------------------------------------------------------------");
System.out.println("Input : "+ json +" <- POST method [Client : "+ cic.getClientIp(request) + " ] at "+transFormat.format(new Date()));


lc.getConnection();
lc.getConnectionHsetAllData(id, map);
lc.getConnectionHset(id, map);
lc.getConnectionExit();


response.getWriter().print("Success /pc/"+id+" <- POST method !!");

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

Expand All @@ -16,13 +18,16 @@
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hours22.system_monitor_ver11.client.ClientInfoController;
import com.hours22.system_monitor_ver11.db.DataService;
import com.hours22.system_monitor_ver11.db.LettuceController;
import com.hours22.system_monitor_ver11.vo.PcData;
Expand All @@ -39,85 +44,115 @@ public class MobileController extends HttpServlet{
@Autowired
DataService dss;

Timer timer;
@Autowired
ClientInfoController cic;

Timer OffTimer, MsgTimer;
SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

@RequestMapping(value = "/mobile/pc", method = RequestMethod.GET)
public void GetPcData(HttpServletResponse response) throws IOException {
public void GetPcData(HttpServletRequest request, HttpServletResponse response) throws IOException {
// RedisLoad_JsonToObj();
// HttpResponse_ObjToJson();
System.out.println("Input : /mobile/pc <- GET method ");
//String json = ojm.writeValueAsString(dss.GetAllPcDataRedis());
System.out.println("---------------------------------------------------------------------");
System.out.println("Input : /mobile/pc <- GET method [Client Ip : "+ cic.getClientIp(request)+"] at "+transFormat.format(new Date()) );

lc.getConnection();
String json = ojm.writerWithDefaultPrettyPrinter().writeValueAsString(dss.GetAllPcDataRedis());

json = json.replaceAll("\\\\r\\\\n", "");
json = json.replaceAll("\\\\", "");

String json = ojm.writeValueAsString(dss.GetAllPcDataRedis());
json = dss.PrettyPrinter(json);
System.out.println(json);

lc.getConnectionExit();
response.getWriter().print(json);
}

@RequestMapping(value = "/mobile/pc/{id}/data", method = RequestMethod.GET)
public void GetPcRamCpuData(HttpServletResponse response, @PathVariable String id) throws IOException {
public void GetPcRamCpuData(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) throws IOException {
// RedisLoad_JsonToObj();
// HttpResponse_ObjToJson();

lc.getConnection();
System.out.println("Input : /mobile/pc/"+id+" <- GET method ");
//String json = ojm.writeValueAsString(dss.GetAllPcDataRedis());
System.out.println("---------------------------------------------------------------------");
System.out.println("Input : /mobile/pc/"+id+"/data <- GET method [Client Ip : "+ cic.getClientIp(request) +" ] at " + transFormat.format(new Date()));

String json = ojm.writerWithDefaultPrettyPrinter().writeValueAsString(lc.getConnectionHget(id));

json = json.replaceAll("\\\\r\\\\n", "");
json = json.replaceAll("\\\\", "");
String json = ojm.writeValueAsString(dss.GetAllPcDataRedis());
json = dss.PrettyPrinter(json);
System.out.println(json);

response.getWriter().print(json);
lc.getConnectionExit();
}

@RequestMapping(value = "/mobile/pc/{id}/power/{endTime}", method = RequestMethod.POST)
public void PostPcPowerOff(HttpServletRequest request, HttpServletResponse response, @PathVariable String id, @PathVariable String endTime) throws IOException, InterruptedException, ParseException {

response.setCharacterEncoding("UTF-8");

final AsyncContext asyncContext = request.startAsync(request, response);
asyncContext.setTimeout(900000000);
System.out.println("---------------------------------------------------------------------");
System.out.println("Input : /mobile/pc/"+id+"/power/"+endTime+" <- POST method [Client Ip : "+cic.getClientIp(request) +" ] at "+transFormat.format(new Date()));

if(timer != null) {
System.out.println("기존 종료시간이 변경되었습니다!");
timer.cancel();
timer = null;
if(OffTimer != null) {

System.out.println("기존 종료시간이 변경되었습니다!");
OffTimer.cancel();
OffTimer = null;
}
timer = new Timer();
OffTimer = new Timer();

TimerTask task = new TimerTask() {
@Override
public void run() {
JSONObject jsonObject = new JSONObject();

System.out.println("PC 전원을 끕니다.");
jsonObject.put("id", id);
jsonObject.put("powerStatus", "OFF");
System.out.println("response : " + jsonObject.toString());
timer = null;
Date nowTime = new Date();
SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println("---------------------------------------------------------------------");
System.out.println("PC 전원을 끕니다. [종료시각 : " + transFormat.format(nowTime)+"]");
OffTimer = null;

try {
response.getWriter().print(jsonObject.toString());
asyncContext.complete();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String> jsonObjectExit = new HashMap<String, String>();
jsonObjectExit.put("id", id);
jsonObjectExit.put("powerStatus", "OFF");
lc.getConnection();
lc.getConnectionHset(id, jsonObjectExit);
lc.getConnectionExit();

asyncContext.complete();
}
};

lc.getConnection();
String jsonStringForAndroid = lc.getConnectionHgetall(id);
System.out.println(jsonStringForAndroid);
// to send android!
// 어플이 먼저켜지고나서, 어플에서 long polling으로 업데이트.


Date now = new Date();
String form = endTime;
SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println("PC가 켜졌습니다!" + transFormat.format(now));
String [] seq = form.split("-");
form = seq[0] + "-" + seq[1] + "-" + seq[2] +" "+seq[3] +":"+seq[4];
System.out.println("종료예약 설정 [시간 : "+form+"]");
System.out.println("종료예약 설정 [시간 : "+form+"]");

Map<String, String> jsonObject = new HashMap<String, String>();
jsonObject.put("id", id);
jsonObject.put("powerStatus", "ON");
jsonObject.put("endTime", endTime);
String jsonString = ojm.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);

//lc.getConnectionHsetAllData(id, jsonObject);
lc.getConnectionHset(id, jsonObject);
// response
response.getWriter().print(jsonString);
System.out.println("hget 디버깅 결과 : " + jsonString);

Date to = transFormat.parse(form);
timer.schedule(task, to);
OffTimer.schedule(task, to);

System.out.println("Input : /mobile/pc/"+id+"/power <- POST method ");
lc.getConnectionExit();
}
}
Loading

0 comments on commit 45ef7d8

Please sign in to comment.