Authored by chenchao

support xml config

1 package com.yoho.rfid.controller; 1 package com.yoho.rfid.controller;
2 2
  3 +import com.yoho.rfid.model.ClientConfig;
3 import com.yoho.rfid.model.ManageCommand; 4 import com.yoho.rfid.model.ManageCommand;
4 import com.yoho.rfid.service.ManageService; 5 import com.yoho.rfid.service.ManageService;
5 import com.yoho.rfid.util.ApiResponse; 6 import com.yoho.rfid.util.ApiResponse;
  7 +
6 import org.apache.commons.lang3.StringUtils; 8 import org.apache.commons.lang3.StringUtils;
7 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.stereotype.Controller; 10 import org.springframework.stereotype.Controller;
9 import org.springframework.web.bind.annotation.PathVariable; 11 import org.springframework.web.bind.annotation.PathVariable;
  12 +import org.springframework.web.bind.annotation.RequestBody;
10 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.ResponseBody; 14 import org.springframework.web.bind.annotation.ResponseBody;
12 15
@@ -55,8 +58,8 @@ public class ManageController { @@ -55,8 +58,8 @@ public class ManageController {
55 58
56 @RequestMapping("/setBussinessDate") 59 @RequestMapping("/setBussinessDate")
57 @ResponseBody 60 @ResponseBody
58 - public ApiResponse setBussinessDate(int open,int close){  
59 - manageService.doBusinessDate(open, close); 61 + public ApiResponse setBussinessDate(@RequestBody ClientConfig clientConfig){
  62 + manageService.doBusinessDate(clientConfig);
60 return new ApiResponse.ApiResponseBuilder() 63 return new ApiResponse.ApiResponseBuilder()
61 .code(200).message("setBussinessDate success").build(); 64 .code(200).message("setBussinessDate success").build();
62 } 65 }
  1 +package com.yoho.rfid.model;
  2 +/**
  3 + *@author chenchao
  4 + *@date 2018年1月24日
  5 + */
  6 +public class ClientConfig {
  7 + private boolean needFilter = true;
  8 +
  9 + private int sendMailBeginHour = 7;
  10 +
  11 + private int sendMailEndHour = 8;
  12 +
  13 + /**
  14 + * 客户端关机时间
  15 + */
  16 + private int closeHour = 23;
  17 + /**
  18 + * 客户端开机时间
  19 + */
  20 + private int openHour = 9;
  21 +
  22 +
  23 + public int getSendMailBeginHour() {
  24 + return sendMailBeginHour;
  25 + }
  26 +
  27 + public void setSendMailBeginHour(int sendMailBeginHour) {
  28 + this.sendMailBeginHour = sendMailBeginHour;
  29 + }
  30 +
  31 + public int getSendMailEndHour() {
  32 + return sendMailEndHour;
  33 + }
  34 +
  35 + public void setSendMailEndHour(int sendMailEndHour) {
  36 + this.sendMailEndHour = sendMailEndHour;
  37 + }
  38 +
  39 + public int getCloseHour() {
  40 + return closeHour;
  41 + }
  42 +
  43 + public void setCloseHour(int closeHour) {
  44 + this.closeHour = closeHour;
  45 + }
  46 +
  47 + public int getOpenHour() {
  48 + return openHour;
  49 + }
  50 +
  51 + public void setOpenHour(int openHour) {
  52 + this.openHour = openHour;
  53 + }
  54 +
  55 +
  56 +
  57 + public boolean isNeedFilter() {
  58 + return needFilter;
  59 + }
  60 +
  61 + public void setNeedFilter(boolean needFilter) {
  62 + this.needFilter = needFilter;
  63 + }
  64 +
  65 + @Override
  66 + public String toString() {
  67 + return "ClientConfig [needFilter=" + needFilter
  68 + + ", sendMailBeginHour=" + sendMailBeginHour
  69 + + ", sendMailEndHour=" + sendMailEndHour + ", closeHour="
  70 + + closeHour + ", openHour=" + openHour + "]";
  71 + }
  72 +
  73 +
  74 +
  75 +}
@@ -15,14 +15,9 @@ public class SystemConfig { @@ -15,14 +15,9 @@ public class SystemConfig {
15 private int sendMailBeginHour = 10; 15 private int sendMailBeginHour = 10;
16 16
17 private int sendMailEndHour = 22; 17 private int sendMailEndHour = 22;
18 - /**  
19 - * 客户端关机时间  
20 - */  
21 - private int closeHour = 23;  
22 - /**  
23 - * 客户端开机时间  
24 - */  
25 - private int openHour = 9; 18 +
  19 +
  20 + private ClientConfig clientConfig;
26 21
27 private List<IpV4Host> hostWhiteList; 22 private List<IpV4Host> hostWhiteList;
28 23
@@ -32,6 +27,9 @@ public class SystemConfig { @@ -32,6 +27,9 @@ public class SystemConfig {
32 ipV4Host.setSegment1(ipV4Host.getSegment1B()); 27 ipV4Host.setSegment1(ipV4Host.getSegment1B());
33 ipV4Host.setType(ipV4Host.getTypeB()); 28 ipV4Host.setType(ipV4Host.getTypeB());
34 hostWhiteList.add(ipV4Host); 29 hostWhiteList.add(ipV4Host);
  30 +
  31 + //
  32 + clientConfig = new ClientConfig();
35 } 33 }
36 34
37 public boolean isSendMail() { 35 public boolean isSendMail() {
@@ -66,28 +64,23 @@ public class SystemConfig { @@ -66,28 +64,23 @@ public class SystemConfig {
66 private static SystemConfig systemConfig = new SystemConfig(); 64 private static SystemConfig systemConfig = new SystemConfig();
67 } 65 }
68 66
69 - public int getCloseHour() {  
70 - return closeHour;  
71 - }  
72 67
73 - public void setCloseHour(int closeHour) {  
74 - this.closeHour = closeHour; 68 + public List getHostWhiteList() {
  69 + return hostWhiteList;
75 } 70 }
76 71
77 - public int getOpenHour() {  
78 - return openHour; 72 + public void setHostWhiteList(List hostWhiteList) {
  73 + this.hostWhiteList = hostWhiteList;
79 } 74 }
80 75
81 - public void setOpenHour(int openHour) {  
82 - this.openHour = openHour;  
83 - }  
84 76
85 - public List getHostWhiteList() {  
86 - return hostWhiteList; 77 +
  78 + public ClientConfig getClientConfig() {
  79 + return clientConfig;
87 } 80 }
88 81
89 - public void setHostWhiteList(List hostWhiteList) {  
90 - this.hostWhiteList = hostWhiteList; 82 + public void setClientConfig(ClientConfig clientConfig) {
  83 + this.clientConfig = clientConfig;
91 } 84 }
92 85
93 @Override 86 @Override
@@ -96,8 +89,6 @@ public class SystemConfig { @@ -96,8 +89,6 @@ public class SystemConfig {
96 .append("sendMail", sendMail) 89 .append("sendMail", sendMail)
97 .append("sendMailBeginHour", sendMailBeginHour) 90 .append("sendMailBeginHour", sendMailBeginHour)
98 .append("sendMailEndHour", sendMailEndHour) 91 .append("sendMailEndHour", sendMailEndHour)
99 - .append("closeHour", closeHour)  
100 - .append("openHour", openHour)  
101 .append("hostWhiteList", hostWhiteList) 92 .append("hostWhiteList", hostWhiteList)
102 .toString(); 93 .toString();
103 } 94 }
  1 +package com.yoho.rfid.service;
  2 +
  3 +
  4 +import com.yoho.rfid.model.ClientConfig;
  5 +import com.yoho.rfid.util.DateUtil;
  6 +
  7 +import java.util.Date;
  8 +
  9 +/**
  10 + *@author chenchao
  11 + *@date 2018年1月24日
  12 + */
  13 +public abstract class AbstractMonitorService {
  14 + //崩溃
  15 + protected static final String CRASH = "crash",
  16 + //重启完毕
  17 + REBOOTED = "rebooted";
  18 +
  19 + protected boolean isTimeout(long timeout, long lastUpdateDT){
  20 + long currentDT = System.currentTimeMillis();
  21 + long diff = currentDT - lastUpdateDT;
  22 + return diff > timeout;
  23 + }
  24 +
  25 + protected boolean clientCanSendMail(ClientConfig clientConfig){
  26 + boolean cansend = true;
  27 + int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
  28 + if(clientConfig.isNeedFilter()){
  29 + cansend = currentHour >= clientConfig.getSendMailBeginHour()
  30 + && currentHour<= clientConfig.getSendMailEndHour();
  31 + }
  32 + return cansend;
  33 + }
  34 +}
1 package com.yoho.rfid.service; 1 package com.yoho.rfid.service;
2 2
3 -import com.yoho.rfid.model.SystemConfig;  
4 -import com.yoho.rfid.util.DateUtil;  
5 -import org.apache.commons.lang3.StringUtils;  
6 -import org.slf4j.Logger;  
7 -import org.slf4j.LoggerFactory;  
8 -import org.springframework.stereotype.Service; 3 +import java.util.Date;
  4 +import java.util.Properties;
9 5
  6 +import javax.annotation.Resource;
10 import javax.mail.MessagingException; 7 import javax.mail.MessagingException;
11 import javax.mail.NoSuchProviderException; 8 import javax.mail.NoSuchProviderException;
12 import javax.mail.Session; 9 import javax.mail.Session;
13 import javax.mail.Transport; 10 import javax.mail.Transport;
14 import javax.mail.internet.InternetAddress; 11 import javax.mail.internet.InternetAddress;
15 import javax.mail.internet.MimeMessage; 12 import javax.mail.internet.MimeMessage;
16 -import java.util.Date;  
17 -import java.util.Properties; 13 +
  14 +import org.apache.commons.lang3.StringUtils;
  15 +import org.slf4j.Logger;
  16 +import org.slf4j.LoggerFactory;
  17 +import org.springframework.beans.factory.annotation.Value;
  18 +import org.springframework.stereotype.Service;
18 19
19 /** 20 /**
20 * Created by sailing on 2017/10/17. 21 * Created by sailing on 2017/10/17.
@@ -22,19 +23,24 @@ import java.util.Properties; @@ -22,19 +23,24 @@ import java.util.Properties;
22 @Service 23 @Service
23 public class MailService { 24 public class MailService {
24 25
25 - private Logger logger = LoggerFactory.getLogger(getClass()); 26 + private final Logger logger = LoggerFactory.getLogger(getClass());
26 27
27 - private String username = "no-reply@yoho.cn";// 邮局用户名(请填写完整的email地址)  
28 - private String Password = "Yoho@9646"; //邮局密码 28 + private String username = "no-reply@yoho.cn";// 邮件用户名(请填写完整的email地址)
  29 + private String Password = "Yoho@9646"; //邮件密码
29 private String From = "no-reply@yoho.cn";//邮件发送者email地址 30 private String From = "no-reply@yoho.cn";//邮件发送者email地址
30 private static String Host = "smtp.exmail.qq.com"; //企业邮局域名 31 private static String Host = "smtp.exmail.qq.com"; //企业邮局域名
31 32
32 private boolean SMTPAuth = true;// 启用SMTP验证功能 33 private boolean SMTPAuth = true;// 启用SMTP验证功能
33 private static String CharSet = "utf-8";//设置邮件编码 34 private static String CharSet = "utf-8";//设置邮件编码
34 35
35 - private String toAccount = "ben.li@yoho.cn";  
36 - private String[] ccAccounts = {"frank.fu@yoho.cn", "xiuchun.luo@yoho.cn", "chao.chen@yoho.cn",  
37 - "rongjun.zhou@yoho.cn", "qiqi.zhou@yoho.cn", "jon.huang@yoho.cn"}; 36 + @Value(value="${email.toAccount:ben.li@yoho.cn}")
  37 + private String toAccount;
  38 +
  39 + //{"frank.fu@yoho.cn", "xiuchun.luo@yoho.cn", "chao.chen@yoho.cn",
  40 + //"rongjun.zhou@yoho.cn", "qiqi.zhou@yoho.cn", "jon.huang@yoho.cn"};
  41 + @Resource(name="ccAccounts")
  42 + private String[] ccAccounts;
  43 +
38 44
39 private Session getSession(){ 45 private Session getSession(){
40 return EmailManager.session; 46 return EmailManager.session;
@@ -55,21 +61,7 @@ public class MailService { @@ -55,21 +61,7 @@ public class MailService {
55 } 61 }
56 } 62 }
57 63
58 - public void send(String msg, boolean forceSend){  
59 - if(!forceSend){  
60 - SystemConfig systemConfig = SystemConfig.getInstance();  
61 - if(!systemConfig.isSendMail()){//不发送一般发生在测试环境  
62 - logger.warn("systemConfig.sendMail is set {},u can use manage/sendMail/open to control", systemConfig.isSendMail());  
63 - return;  
64 - }  
65 - //上午10点到晚上22点发送,其他时间不发  
66 - int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);  
67 - if(currentHour<systemConfig.getSendMailBeginHour() || currentHour >=systemConfig.getSendMailEndHour()){  
68 - logger.warn("it's too late, don't need to send mail, thanks developer sailing");  
69 - return;  
70 - }  
71 - }  
72 - 64 + public void send(String msg){
73 // 3. 创建一封邮件 65 // 3. 创建一封邮件
74 RecipientAccount ra = new RecipientAccount(); 66 RecipientAccount ra = new RecipientAccount();
75 ra.ccAccounts = ccAccounts; 67 ra.ccAccounts = ccAccounts;
@@ -81,8 +73,6 @@ public class MailService { @@ -81,8 +73,6 @@ public class MailService {
81 Session session = getSession(); 73 Session session = getSession();
82 MimeMessage message = createMimeMessage(session, username, ra, msg); 74 MimeMessage message = createMimeMessage(session, username, ra, msg);
83 transport = session.getTransport(); 75 transport = session.getTransport();
84 -  
85 -  
86 // 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错 76 // 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
87 // 77 //
88 // PS_01: 成败的判断关键在此一句, 如果连接服务器失败, 都会在控制台输出相应失败原因的 log, 78 // PS_01: 成败的判断关键在此一句, 如果连接服务器失败, 都会在控制台输出相应失败原因的 log,
@@ -3,6 +3,7 @@ package com.yoho.rfid.service; @@ -3,6 +3,7 @@ package com.yoho.rfid.service;
3 import java.util.concurrent.locks.Lock; 3 import java.util.concurrent.locks.Lock;
4 import java.util.concurrent.locks.ReentrantLock; 4 import java.util.concurrent.locks.ReentrantLock;
5 5
  6 +import com.yoho.rfid.model.ClientConfig;
6 import com.yoho.rfid.model.SystemConfig; 7 import com.yoho.rfid.model.SystemConfig;
7 8
8 import org.slf4j.Logger; 9 import org.slf4j.Logger;
@@ -29,7 +30,12 @@ public class ManageService { @@ -29,7 +30,12 @@ public class ManageService {
29 } 30 }
30 } 31 }
31 32
32 - public void doBusinessDate(int openHour, int closeHour){ 33 + public void doBusinessDate(ClientConfig clientConfig){
  34 + int openHour = clientConfig.getOpenHour();
  35 + int closeHour = clientConfig.getCloseHour();
  36 + boolean needFilter = clientConfig.isNeedFilter();
  37 + int beginHour = clientConfig.getSendMailBeginHour();
  38 + int endHour = clientConfig.getSendMailEndHour();
33 final Lock writelock = lock; 39 final Lock writelock = lock;
34 writelock.lock(); 40 writelock.lock();
35 try{ 41 try{
@@ -39,8 +45,11 @@ public class ManageService { @@ -39,8 +45,11 @@ public class ManageService {
39 return; 45 return;
40 } 46 }
41 SystemConfig systemConfig = SystemConfig.getInstance(); 47 SystemConfig systemConfig = SystemConfig.getInstance();
42 - systemConfig.setOpenHour(openHour);  
43 - systemConfig.setCloseHour(closeHour); 48 + systemConfig.getClientConfig().setOpenHour(openHour);
  49 + systemConfig.getClientConfig().setCloseHour(closeHour);
  50 + systemConfig.getClientConfig().setNeedFilter(needFilter);
  51 + systemConfig.getClientConfig().setSendMailBeginHour(beginHour);
  52 + systemConfig.getClientConfig().setSendMailEndHour(endHour);
44 }finally{ 53 }finally{
45 writelock.unlock(); 54 writelock.unlock();
46 } 55 }
@@ -2,6 +2,7 @@ package com.yoho.rfid.service; @@ -2,6 +2,7 @@ package com.yoho.rfid.service;
2 2
3 import com.yoho.rfid.constant.BackWorkerType; 3 import com.yoho.rfid.constant.BackWorkerType;
4 import com.yoho.rfid.helper.MonitorHelper; 4 import com.yoho.rfid.helper.MonitorHelper;
  5 +import com.yoho.rfid.model.ClientConfig;
5 import com.yoho.rfid.model.MonitorFuture; 6 import com.yoho.rfid.model.MonitorFuture;
6 import com.yoho.rfid.model.SystemConfig; 7 import com.yoho.rfid.model.SystemConfig;
7 import com.yoho.rfid.model.TreadNode; 8 import com.yoho.rfid.model.TreadNode;
@@ -9,6 +10,7 @@ import com.yoho.rfid.model.req.AppReportReq; @@ -9,6 +10,7 @@ import com.yoho.rfid.model.req.AppReportReq;
9 import com.yoho.rfid.model.req.InfoScreenHeartBeatPacket; 10 import com.yoho.rfid.model.req.InfoScreenHeartBeatPacket;
10 import com.yoho.rfid.thread.MonitorThreadFactory; 11 import com.yoho.rfid.thread.MonitorThreadFactory;
11 import com.yoho.rfid.util.DateUtil; 12 import com.yoho.rfid.util.DateUtil;
  13 +
12 import org.apache.commons.collections.CollectionUtils; 14 import org.apache.commons.collections.CollectionUtils;
13 import org.apache.commons.collections.MapUtils; 15 import org.apache.commons.collections.MapUtils;
14 import org.apache.commons.lang3.StringUtils; 16 import org.apache.commons.lang3.StringUtils;
@@ -19,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -19,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Service; 21 import org.springframework.stereotype.Service;
20 22
21 import javax.annotation.PostConstruct; 23 import javax.annotation.PostConstruct;
  24 +
22 import java.text.SimpleDateFormat; 25 import java.text.SimpleDateFormat;
23 import java.util.*; 26 import java.util.*;
24 import java.util.concurrent.*; 27 import java.util.concurrent.*;
@@ -27,7 +30,7 @@ import java.util.concurrent.*; @@ -27,7 +30,7 @@ import java.util.concurrent.*;
27 * Created by chenchao on 2017/10/16. 30 * Created by chenchao on 2017/10/16.
28 */ 31 */
29 @Service 32 @Service
30 -public class MonitorService { 33 +public class MonitorService extends AbstractMonitorService{
31 34
32 private Logger logger = LoggerFactory.getLogger(getClass()); 35 private Logger logger = LoggerFactory.getLogger(getClass());
33 // 36 //
@@ -41,10 +44,7 @@ public class MonitorService { @@ -41,10 +44,7 @@ public class MonitorService {
41 BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(); 44 BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
42 private ExecutorService executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 45 private ExecutorService executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
43 0L, TimeUnit.MILLISECONDS, workQueue, monitorThreadFactory); 46 0L, TimeUnit.MILLISECONDS, workQueue, monitorThreadFactory);
44 - //崩溃  
45 - private static final String CRASH = "crash",  
46 - //重启完毕  
47 - REBOOTED = "rebooted"; 47 +
48 48
49 49
50 @Autowired 50 @Autowired
@@ -227,19 +227,20 @@ public class MonitorService { @@ -227,19 +227,20 @@ public class MonitorService {
227 continue; 227 continue;
228 } 228 }
229 long lastUpdateDT = req.getUpdateDateTime(); 229 long lastUpdateDT = req.getUpdateDateTime();
230 - long currentDT = System.currentTimeMillis();  
231 - long diff = currentDT - lastUpdateDT;  
232 -  
233 - if(diff > timeout){  
234 - logger.warn("AppReportMap find reeboot, req {},currentDT {}, diff {}, timeout {}",  
235 - req, currentDT, diff, timeout); 230 + boolean isTimeout = isTimeout(timeout, lastUpdateDT);
  231 + if(isTimeout){
  232 + logger.warn("AppReportMap find reboot, req {} timeout {}",
  233 + req, timeout);
236 //reboot 234 //reboot
237 //infoScreenService.reboot(req.getIp(), req.getScreenType()); 235 //infoScreenService.reboot(req.getIp(), req.getScreenType());
238 //TODO use queue to split send mail function, use a thread to send mail 236 //TODO use queue to split send mail function, use a thread to send mail
239 - mailService.send(buildMailContent4AppCrash(req.getIp(), req.getScreenType()).toString(),false); 237 + boolean cansend = canSend4GlobalScope() && canSend4AppReport();
  238 + if(cansend){
  239 + mailService.send(buildMailContent4AppCrash(req.getIp(), req.getScreenType()).toString());
240 needRemove.add(req.getIp()); 240 needRemove.add(req.getIp());
241 } 241 }
242 } 242 }
  243 + }
243 //remove from map 244 //remove from map
244 if(CollectionUtils.isNotEmpty(needRemove)){ 245 if(CollectionUtils.isNotEmpty(needRemove)){
245 needRemove.stream().forEach(ip -> AppReportMap.remove(ip)); 246 needRemove.stream().forEach(ip -> AppReportMap.remove(ip));
@@ -286,19 +287,20 @@ public class MonitorService { @@ -286,19 +287,20 @@ public class MonitorService {
286 continue; 287 continue;
287 } 288 }
288 long lastUpdateDT = packet.getUpdateDateTime(); 289 long lastUpdateDT = packet.getUpdateDateTime();
289 - long currentDT = System.currentTimeMillis();  
290 - long diff = currentDT - lastUpdateDT;  
291 - 290 + boolean isTimeout = isTimeout(timeout, lastUpdateDT);
292 //todo 合并多个,只发送一封邮件 291 //todo 合并多个,只发送一封邮件
293 - if(diff > timeout){ 292 + if(isTimeout){
294 //reboot 293 //reboot
295 - logger.warn("HeartBeatMap find no heartbeat, req {},currentDT {}, diff {}, timeout {}",  
296 - packet, currentDT, diff, timeout); 294 + logger.warn("HeartBeatMap find no heartbeat, req {}, timeout {}",
  295 + packet, timeout);
297 //infoScreenService.reboot(req.getIp(), req.getScreenType()); 296 //infoScreenService.reboot(req.getIp(), req.getScreenType());
298 - mailService.send(buildMailContent(packet.getIp(), packet.getScreenType()).toString(), false); 297 + boolean cansend = canSend4GlobalScope() && canSend4HeartBeat();
  298 + if(cansend){
  299 + mailService.send(buildMailContent(packet.getIp(), packet.getScreenType()).toString());
299 needRemove.add(packet.getIp()); 300 needRemove.add(packet.getIp());
300 } 301 }
301 } 302 }
  303 + }
302 //remove from map 304 //remove from map
303 removeAll(needRemove); 305 removeAll(needRemove);
304 //客户端到23点,早上9点还没有关机就邮件报警 306 //客户端到23点,早上9点还没有关机就邮件报警
@@ -314,34 +316,42 @@ public class MonitorService { @@ -314,34 +316,42 @@ public class MonitorService {
314 } 316 }
315 317
316 private void sendMailIfExistOpenClient(){ 318 private void sendMailIfExistOpenClient(){
317 - int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour); 319 + int currentHour;
318 SystemConfig systemConfig = SystemConfig.getInstance(); 320 SystemConfig systemConfig = SystemConfig.getInstance();
319 - boolean isNotClose = (currentHour >= systemConfig.getCloseHour() || currentHour < systemConfig.getOpenHour())  
320 - && MapUtils.isNotEmpty(HeartBeatMap) && !isNoticedOpenClient;  
321 - if (isNotClose){ 321 + ClientConfig clientConfig = systemConfig.getClientConfig();
  322 + //尚未发送通知
  323 + if(!isNoticedOpenClient){
  324 + currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
  325 + boolean hasData = MapUtils.isNotEmpty(HeartBeatMap);
  326 + boolean cansend = clientCanSendMail(clientConfig);
  327 + boolean isNotClose = (currentHour >= clientConfig.getCloseHour() || currentHour < clientConfig.getOpenHour());
  328 + if (isNotClose && hasData && cansend){
322 isNoticedOpenClient = true; 329 isNoticedOpenClient = true;
323 List<String> ips = new ArrayList<String>(HeartBeatMap.keySet()); 330 List<String> ips = new ArrayList<String>(HeartBeatMap.keySet());
324 - mailService.send(buildMailContentOfExistOpenClient(ips).toString(), true); 331 + mailService.send(buildMailContentOfExistOpenClient(ips).toString());
325 //移除的意义不大,心跳会不间断地上报 332 //移除的意义不大,心跳会不间断地上报
326 this.removeAll(ips); 333 this.removeAll(ips);
327 logger.info("in sendMailIfExistOpenClient, ips {}", ips); 334 logger.info("in sendMailIfExistOpenClient, ips {}", ips);
328 } 335 }
  336 + }
  337 + //已经发送通知后,需要恢复标识
  338 + if(isNoticedOpenClient){
  339 + currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
329 //auto wakeup, reset isNoticedOpenClient if it's false 340 //auto wakeup, reset isNoticedOpenClient if it's false
330 - boolean inBussiness = currentHour < systemConfig.getCloseHour()  
331 - && currentHour >= systemConfig.getOpenHour()  
332 - && isNoticedOpenClient; 341 + boolean inBussiness = currentHour < clientConfig.getCloseHour()
  342 + && currentHour >= clientConfig.getOpenHour();
333 if(inBussiness){ 343 if(inBussiness){
334 isNoticedOpenClient = false; 344 isNoticedOpenClient = false;
335 logger.info("in sendMailIfExistOpenClient, reset isNoticedOpenClient success"); 345 logger.info("in sendMailIfExistOpenClient, reset isNoticedOpenClient success");
336 } 346 }
337 } 347 }
  348 + }
338 349
339 public long getAliveDatetime(){ 350 public long getAliveDatetime(){
340 return aliveDatetime; 351 return aliveDatetime;
341 } 352 }
342 } 353 }
343 354
344 -  
345 StringBuilder buildMailContentOfExistOpenClient(List<String> ips){ 355 StringBuilder buildMailContentOfExistOpenClient(List<String> ips){
346 StringBuilder sb = new StringBuilder(); 356 StringBuilder sb = new StringBuilder();
347 String ipsStr = StringUtils.join(ips, ",") ; 357 String ipsStr = StringUtils.join(ips, ",") ;
@@ -350,6 +360,41 @@ public class MonitorService { @@ -350,6 +360,41 @@ public class MonitorService {
350 } 360 }
351 361
352 362
  363 +
  364 + private boolean canSend4AppReport(){
  365 + SystemConfig systemConfig = SystemConfig.getInstance();
  366 + //上午10点到晚上22点发送,其他时间不发
  367 + int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
  368 + if(currentHour<systemConfig.getSendMailBeginHour() || currentHour >=systemConfig.getSendMailEndHour()){
  369 + logger.warn("in canSend4AppReport,it's too late, don't need to send mail, thanks developer sailing");
  370 + return false;
  371 + }
  372 + return true;
  373 + }
  374 +
  375 + private boolean canSend4GlobalScope(){
  376 + SystemConfig systemConfig = SystemConfig.getInstance();
  377 + if(!systemConfig.isSendMail()){//不发送一般发生在测试环境
  378 + logger.warn("in canSend4GlobalScope,systemConfig.sendMail is set {},u can use manage/sendMail/open to control", systemConfig.isSendMail());
  379 + return false;
  380 + }
  381 + return true;
  382 + }
  383 +
  384 + private boolean canSend4HeartBeat(){
  385 + SystemConfig systemConfig = SystemConfig.getInstance();
  386 + //上午10点到晚上22点发送,其他时间不发
  387 + int currentHour = DateUtil.getPart(new Date(), DateUtil.TimeUnit.hour);
  388 + if(currentHour<systemConfig.getSendMailBeginHour() || currentHour >=systemConfig.getSendMailEndHour()){
  389 + logger.warn("in canSend4HeartBeat,it's too late, don't need to send mail, thanks developer sailing");
  390 + return false;
  391 + }
  392 + return true;
  393 + }
  394 +
  395 +
  396 +
  397 +
353 public static void main(String[] args) { 398 public static void main(String[] args) {
354 long cnt = 1 << 31 -1 ; 399 long cnt = 1 << 31 -1 ;
355 Boolean flag = cnt == Integer.MAX_VALUE; 400 Boolean flag = cnt == Integer.MAX_VALUE;
@@ -18,7 +18,15 @@ @@ -18,7 +18,15 @@
18 <context:property-placeholder ignore-resource-not-found="true" location="classpath*:config.properties" /> 18 <context:property-placeholder ignore-resource-not-found="true" location="classpath*:config.properties" />
19 19
20 <context:component-scan base-package="com.yoho.rfid" /> 20 <context:component-scan base-package="com.yoho.rfid" />
21 - 21 + <!-- warn email cc accounts -->
  22 + <util:list id="ccAccounts" value-type="java.lang.String">
  23 + <value>frank.fu@yoho.cn</value>
  24 + <value>xiuchun.luo@yoho.cn</value>
  25 + <value>rongjun.zhou@yoho.cn</value>
  26 + <value>qiqi.zhou@yoho.cn</value>
  27 + <value>jon.huang@yoho.cn</value>
  28 + <value>chao.chen@yoho.cn</value>
  29 + </util:list>
22 <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> 30 <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
23 <mvc:annotation-driven> 31 <mvc:annotation-driven>
24 <mvc:message-converters register-defaults="false"> 32 <mvc:message-converters register-defaults="false">
@@ -11,3 +11,7 @@ portal.gateway.url=http://instore.yohobuy.com @@ -11,3 +11,7 @@ portal.gateway.url=http://instore.yohobuy.com
11 web.context=rfid 11 web.context=rfid
12 12
13 deploy.home=/home/store/rfid 13 deploy.home=/home/store/rfid
  14 +
  15 +toAccount=ben.li@yoho.cn
  16 +#test or dev
  17 +#email.toAccount=chao.chen@yoho.cn