加热器换水提醒推送

This commit is contained in:
yiyan 2018-09-29 19:23:18 +08:00
parent 2dd7bc01be
commit 9ca321be3d
3 changed files with 214 additions and 6 deletions

View File

@ -0,0 +1,191 @@
package com.ifish.job;
import com.ifish.enums.PhoneTypeEnum;
import com.ifish.jpush.JPushNotification;
import com.ifish.netease.NeteaseIM;
import com.ifish.util.IfishUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.util.logging.Logger;
/**
* @author: yan.y
* @Description:
* @Date: Created in 19:47 2018/9/18
* @Modified by:
*/
@Component
@EnableScheduling
@Lazy(false)
public class HeaterJob {
@Autowired
private NeteaseIM neteaseIM;
@Autowired
private JPushNotification jPushNotification2;
private Connection connection = null;
private Statement stmt = null;
private PreparedStatement prest = null;
private PreparedStatement prestDevice = null;
private static final Logger log = Logger.getLogger("job");
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int i = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println(i);
}
public void heraterChangeWater() {
log.info("智能加热棒换水提醒推送begin.....");
log.info(IfishUtil.format2(new Date())+"开始任务");
String nowDate = IfishUtil.format1(new Date());
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myfishdb?characterEncoding=UTF-8", "ifish", "ifish7pwd");
stmt = connection.createStatement();
prest = connection.prepareStatement("update tbl_device_heater_detail set herter_remind_date=? where heater_mac_address=?");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
//结果集
ResultSet result = null;
try {
//推送提醒数
int rowCount = 0;
//按分页来获取数据
int pageNo = 0;
//云信限制每次最多500条
int pageSize = 500;
//查询总共需要推送的用户数
String countSql = "select count(1) as countRow from tbl_device_heater_detail where herter_remind_date = '" + nowDate + "'";
result = stmt.executeQuery(countSql);
if(result.next()){
rowCount=result.getInt("countRow");
pageNo = (rowCount+pageSize-1)/pageSize;
}
log.info("pageNo : " + pageNo);
for (int i = 0; i < pageNo; i++) {
//查询出推送日期推送周期 未推送 及当天
result = stmt.executeQuery("select tdhd.heater_mac_address,tdhd.heater_cycle,tdu.user_id from tbl_device_heater_detail tdhd,tbl_device td,tbl_device_user tdu where tdhd.heater_mac_address = td.mac_address and td.device_id = tdu.device_id and tdhd.herter_remind_date = '"+ nowDate +"' and tdhd.heater_reminder_time = '" + hour + "' limit 0,"+pageSize);
//推送的用户
List<String> androidUser = new ArrayList<String>();
List<String> iosUser = new ArrayList<String>();
List<String> ids = new ArrayList<String>();
//更新提醒过的用户
while(result.next()){
Integer userId = result.getInt("user_id");
String macAddress = result.getString("heater_mac_address");
String remindCycle = result.getString("heater_cycle");
//设置下次提醒日期
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR) + Integer.parseInt(remindCycle));
String nextRemindDate = IfishUtil.format1(calendar.getTime());
prest.setString(1, nextRemindDate);
prest.setString(2, macAddress);
prest.addBatch();
ids.add(userId.toString());
//极光
iosUser.add(userId.toString());
}
//发送云信消息
if(ids.size()>0){
neteaseIM.sendBatchMsg("ifish", JSONArray.fromObject(ids).toString(), "【换水提醒】您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置");
}
//极光推送
Integer iosSize = iosUser.size();
Integer androidSize = androidUser.size();
if(iosSize>0){
push(PhoneTypeEnum.ios,iosUser.toArray(new String[iosSize]));
}
if(androidSize>0){
push(PhoneTypeEnum.android,androidUser.toArray(new String[androidSize]));
}
//批量提交
prest.executeBatch();
//云信一分钟访问不超过120次
Thread.sleep(600);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(result!=null){
try {
result.close();
result = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(prest!=null){
try {
prest.close();
prest = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
connection = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
log.info("智能加热棒换水提醒推送end.....");
}
/**
* 推送提醒
* @param type
* @param ids
*/
public void push(PhoneTypeEnum type,String[] ids){
try {
Map<String,String> map = new HashMap<String, String>();
map.put("timestamp", IfishUtil.format2(new Date()));
//推送android
/*if(type.equals(PhoneTypeEnum.android)){
System.out.println(new Date()+"android推送"+ids.length+"");
jPushNotification.sendAndroidNotification(ids, "换水提醒", "您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置", map);
}*/
//推送IOS
if(type.equals(PhoneTypeEnum.ios)){
System.out.println(new Date()+"ios推送"+ids.length+"");
jPushNotification2.sendIosNotification(ids, "换水提醒", "您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置", map);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -8,8 +8,6 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import com.ifish.enums.NeteaseEnum;
import com.ifish.jpush.utils.StringUtils;
import com.ifish.netease.CheckSumBuilder;
import net.sf.json.JSONArray;
@ -20,7 +18,6 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import com.ifish.enums.PhoneTypeEnum;
@ -113,7 +110,6 @@ public class job{
}
}
//发送云信消息
if(ids.size()>0){
neteaseIM.sendBatchMsg("ifish", JSONArray.fromObject(ids).toString(), "【换水提醒】您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置");
@ -226,7 +222,6 @@ public class job{
HttpResponse response = httpClient.execute(httpPost);
//执行结果
String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject json = new JSONObject(responseStr);
System.out.println(responseStr);
}

View File

@ -13,6 +13,7 @@
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
<ref bean="myHeaterJobTrigger" />
</list>
</property>
<property name="quartzProperties">
@ -34,6 +35,17 @@
<value>0 0 10 * * ?</value>
</property>
</bean>
<bean id="myHeaterJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="heaterJobDetail" />
</property>
<property name="cronExpression">
<!-- 每小时监测一遍 -->
<value>0 0 0/1 * * ?</value>
</property>
</bean>
<!-- 调度的配置结束 -->
<!-- job的配置开始 -->
@ -45,11 +57,21 @@
<property name="targetMethod">
<value>pushRemind</value>
</property>
</bean>
<bean id="heaterJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="heaterJob" />
</property>
<property name="targetMethod">
<value>heraterChangeWater</value>
</property>
</bean>
<!-- job的配置结束 -->
<!-- 工作的bean -->
<bean id="myJob" class=" com.ifish.job.job" />
<bean id="heaterJob" class=" com.ifish.job.HeaterJob" />
<!-- 注册识别注解 -->
<context:annotation-config />