调整时间通知为每秒
新增wifi设备连入退出判断 为下发开门指令做准备
This commit is contained in:
parent
d35431a35d
commit
7b1050b4e3
7
pom.xml
7
pom.xml
@ -95,6 +95,13 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.15</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,78 @@
|
||||
package com.yutou.qqbot.Controllers;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.qqbot.models.XiaoMi.MiRouter;
|
||||
import com.yutou.qqbot.utlis.HttpTools;
|
||||
import com.yutou.qqbot.utlis.RedisTools;
|
||||
import com.yutou.qqbot.utlis.XiaoMiRouter;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/router/")
|
||||
public class MiRouterDevices {
|
||||
@RequestMapping("device/add.do")
|
||||
@ResponseBody
|
||||
public String addDevice(HttpServletRequest request,String qq) {
|
||||
String data = RedisTools.get(MiRouter.redis_key);
|
||||
JSONArray array;
|
||||
if (data == null) {
|
||||
array = new JSONArray();
|
||||
} else {
|
||||
array = JSONArray.parseArray(data);
|
||||
}
|
||||
for (Object o : array) {
|
||||
JSONObject item = (JSONObject) o;
|
||||
if (item.getString("mac").equals(getRemoteAddress(request.getRemoteAddr()))) {
|
||||
return "已经添加过了";
|
||||
}
|
||||
}
|
||||
JSONObject item=new JSONObject();
|
||||
item.put("qq",qq);
|
||||
item.put("mac",getRemoteAddress(request.getRemoteAddr()));
|
||||
item.put("online",false);
|
||||
array.add(item);
|
||||
RedisTools.set(MiRouter.redis_key,array.toString());
|
||||
return "添加成功,关闭当前页面即可";
|
||||
}
|
||||
@ResponseBody
|
||||
@RequestMapping("device/del.do")
|
||||
public String delDevice(HttpServletRequest request,String qq){
|
||||
String data = RedisTools.get(MiRouter.redis_key);
|
||||
JSONArray array;
|
||||
if (data == null) {
|
||||
array = new JSONArray();
|
||||
} else {
|
||||
array = JSONArray.parseArray(data);
|
||||
}
|
||||
JSONArray _array= (JSONArray) array.clone();
|
||||
for (Object o : array) {
|
||||
JSONObject item = (JSONObject) o;
|
||||
if (item.getString("mac").equals(getRemoteAddress(request.getRemoteAddr()))) {
|
||||
_array.remove(item);
|
||||
RedisTools.set(MiRouter.redis_key,_array.toString());
|
||||
return "已成功删除";
|
||||
}
|
||||
}
|
||||
return "未找到该设备";
|
||||
}
|
||||
|
||||
private String getRemoteAddress(String ip) {
|
||||
JSONObject data = JSONObject.parseObject(HttpTools.get(XiaoMiRouter.getDeviceListUrl()));
|
||||
if (data.getInteger("code") == 0) {
|
||||
JSONArray array=data.getJSONArray("list");
|
||||
for (Object o : array) {
|
||||
JSONObject item= (JSONObject) o;
|
||||
if(item.getJSONArray("ip").getJSONObject(0).getString("ip").equals(ip)){
|
||||
return item.getString("mac");
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ package com.yutou.qqbot.Listeners;
|
||||
import com.yutou.qqbot.MessageEvent.AdminMessage;
|
||||
import com.yutou.qqbot.QQNumberManager;
|
||||
import com.yutou.qqbot.models.Model;
|
||||
import com.yutou.qqbot.utlis.Log;
|
||||
import kotlin.coroutines.CoroutineContext;
|
||||
import net.mamoe.mirai.event.EventHandler;
|
||||
import net.mamoe.mirai.event.ListeningStatus;
|
||||
|
@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class QQBotApplication {
|
||||
public static final String version="QQBot v.1.1.2";
|
||||
public static final String version="QQBot v.1.2.0";
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(QQBotApplication.class, args);
|
||||
RedisTools.initRedisPoolSub();
|
||||
|
@ -7,6 +7,7 @@ import com.yutou.qqbot.models.Commands.Bangumi;
|
||||
import com.yutou.qqbot.models.Commands.System.*;
|
||||
import com.yutou.qqbot.models.Model;
|
||||
import com.yutou.qqbot.models.WebSign.Tsdm;
|
||||
import com.yutou.qqbot.models.XiaoMi.MiRouter;
|
||||
import com.yutou.qqbot.models.setu.QQSetu;
|
||||
import com.yutou.qqbot.utlis.*;
|
||||
import net.mamoe.mirai.Bot;
|
||||
@ -42,6 +43,7 @@ public class QQBotManager {
|
||||
Model.classList.add(TurnipProphet.class);
|
||||
Model.classList.add(Tsdm.class);
|
||||
Model.classList.add(BaiduDown.class);
|
||||
Model.classList.add(MiRouter.class);
|
||||
}
|
||||
private static QQBotManager botManager = null;
|
||||
private Bot bot;
|
||||
|
@ -127,7 +127,7 @@ public class Bangumi extends Model {
|
||||
public void onTime(String time) {
|
||||
super.onTime(time);
|
||||
switch (time) {
|
||||
case "08:00", "20:00" -> QQBotManager.getInstance().sendMessage(QQBotManager.defGroup,BangumiTools.reportToDayBangumi());
|
||||
case "08:00:00", "20:00:00" -> QQBotManager.getInstance().sendMessage(QQBotManager.defGroup,BangumiTools.reportToDayBangumi());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,8 @@ public abstract class Model implements ModelInterface {
|
||||
public static final String TSDM_PAY = "!tsdm";
|
||||
public static final String TSDM_SIGN = "!tsdm签到";
|
||||
public static final String BAIDU_DOWN = "!bd";
|
||||
public static final String ROUTER_ADD="!添加设备";
|
||||
public static final String ROUTER_DEL="!删除设备";
|
||||
}
|
||||
|
||||
|
||||
|
@ -26,7 +26,7 @@ public class BaiHeHui extends Model {
|
||||
@Override
|
||||
public void onTime(String time) {
|
||||
super.onTime(time);
|
||||
if ("08:10".equals(time)) {
|
||||
if ("08:10:00".equals(time)) {
|
||||
Log.i("开始百合会签到");
|
||||
try {
|
||||
sign();
|
||||
|
@ -40,7 +40,7 @@ public class Tsdm extends Model {
|
||||
@Override
|
||||
public void onTime(String time) {
|
||||
super.onTime(time);
|
||||
if ("08:00".equals(time)) {
|
||||
if ("08:00:00".equals(time)) {
|
||||
Log.i("开始天使动漫签到");
|
||||
try {
|
||||
tsdmSign();
|
||||
|
106
src/main/java/com/yutou/qqbot/models/XiaoMi/MiRouter.java
Normal file
106
src/main/java/com/yutou/qqbot/models/XiaoMi/MiRouter.java
Normal file
@ -0,0 +1,106 @@
|
||||
package com.yutou.qqbot.models.XiaoMi;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.qqbot.QQBotManager;
|
||||
import com.yutou.qqbot.models.Model;
|
||||
import com.yutou.qqbot.utlis.HttpTools;
|
||||
import com.yutou.qqbot.utlis.RedisTools;
|
||||
import com.yutou.qqbot.utlis.XiaoMiRouter;
|
||||
import net.mamoe.mirai.event.events.MessageEvent;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class MiRouter extends Model {
|
||||
public static String redis_key = "MiRouterDevices";
|
||||
|
||||
@Override
|
||||
public boolean isUserPublic() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getUsePowers() {
|
||||
return new String[]{
|
||||
QQFromCommands.ROUTER_ADD,
|
||||
QQFromCommands.ROUTER_DEL
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(Long qq, MessageEvent event, boolean isGroup) {
|
||||
super.onMessage(qq, event, isGroup);
|
||||
if(msg.equals(QQFromCommands.ROUTER_ADD)){
|
||||
String url="http://192.168.31.88:8002/router/device/add.do?qq="+qq;
|
||||
QQBotManager.getInstance().sendMessage(qq,"点击该链接完成设备添加:\n"+url);
|
||||
}else if(msg.equals(QQFromCommands.ROUTER_DEL)){
|
||||
String url="http://192.168.31.88:8002/router/device/del.do?qq="+qq;
|
||||
QQBotManager.getInstance().sendMessage(qq,"点击该删除设备:\n"+url);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTime(String time) {
|
||||
super.onTime(time);
|
||||
String url = XiaoMiRouter.getDeviceListUrl();
|
||||
JSONObject json = JSONObject.parseObject(HttpTools.get(url));
|
||||
if (json.getInteger("code") == 0) {
|
||||
String _tmp = RedisTools.get(redis_key);
|
||||
if (_tmp == null) {
|
||||
return;
|
||||
}
|
||||
JSONArray devices = JSONArray.parseArray(_tmp);
|
||||
JSONArray devs = (JSONArray) devices.clone();
|
||||
JSONArray array = json.getJSONArray("list");
|
||||
Set<String> links = new HashSet<>();
|
||||
for (Object o : array) {
|
||||
JSONObject item = (JSONObject) o;
|
||||
for (Object device : devices) {
|
||||
JSONObject _device = (JSONObject) device;
|
||||
devs.remove(_device);
|
||||
if (_device.getString("mac").equals(item.getString("mac")) && !_device.getBoolean("online")) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(item.getString("oname"));
|
||||
builder.append(" 欢迎回家!").append("\n");
|
||||
builder.append("点击该链接开门:").append("\n");
|
||||
QQBotManager.getInstance().sendMessage(_device.getLong("qq"), builder.toString());
|
||||
_device.put("online", true);
|
||||
_device.put("name", item.getString("oname"));
|
||||
}
|
||||
if (_device.getString("mac").equals(item.getString("mac")) && _device.getBoolean("online")) {
|
||||
links.add(_device.getString("mac"));
|
||||
_device.put("linkTime", Long.parseLong(item.getJSONObject("statistics").getString("online")));
|
||||
}
|
||||
devs.add(_device);
|
||||
}
|
||||
}
|
||||
devices = (JSONArray) devs.clone();
|
||||
for (Object device : devices) {
|
||||
JSONObject item = (JSONObject) device;
|
||||
if (!links.contains(item.getString("mac"))&&item.getLong("linkTime")!=0) {
|
||||
devs.remove(device);
|
||||
String builder = "你的设备:" +
|
||||
item.getString("name") +
|
||||
"已下线" + "\n" +
|
||||
"在线时长:" + getOnLineTime(item.getString("linkTime")) + "\n";
|
||||
QQBotManager.getInstance().sendMessage(item.getLong("qq"), builder);
|
||||
item.put("online", false);
|
||||
item.put("linkTime",0);
|
||||
devs.add(item);
|
||||
}
|
||||
}
|
||||
RedisTools.set(redis_key, devs.toJSONString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private String getOnLineTime(String online) {
|
||||
long time = Long.parseLong(online);
|
||||
long day = time / 24 / 60 / 60;
|
||||
long h = time / 60 / 60 % 24;
|
||||
long m = time / 60 % 60;
|
||||
long s = time % 60;
|
||||
return String.format("%02d天%02d时%02d分%02d秒", day, h, m, s);
|
||||
}
|
||||
}
|
@ -222,7 +222,7 @@ public class QQSetu extends Model {
|
||||
@Override
|
||||
public void onTime(String time) {
|
||||
super.onTime(time);
|
||||
if("23:59".equals(time)){
|
||||
if("23:59:00".equals(time)){
|
||||
printTodaySetu();
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class ApplicationInit implements ApplicationRunner {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String time = new SimpleDateFormat("HH:mm").format(new Date());
|
||||
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
|
||||
if (time.equals(oldTime)) {
|
||||
return;
|
||||
}
|
||||
@ -36,6 +36,6 @@ public class ApplicationInit implements ApplicationRunner {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
},0, 35 * 1000);
|
||||
},0, 1000);
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.qqbot.interfaces.DownloadInterface;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.*;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -102,11 +99,8 @@ public class HttpTools {
|
||||
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36";
|
||||
}
|
||||
|
||||
private static String getKuKuUA() {
|
||||
return "/KUKU_APP(Android/#/cn.kuku.sdk/ttsdk17228/29401/A-2.9.4.01.KUSDK/868139039134314/fcddf839c8c135fa/F4:60:E2:AB:25:1A/460019406520644/+8618569400341/#/9/Redmi 6 Pro/xiaomi/1736/76fda4d6-cd6b-485f-987b-8d347b007f24/#/KUKU/Native/92972ea9651fbd2e)";
|
||||
}
|
||||
|
||||
public String toUrlParams(JSONObject json) {
|
||||
public static String toUrlParams(JSONObject json) {
|
||||
StringBuilder string = new StringBuilder();
|
||||
Set<String> keys = json.keySet();
|
||||
for (String key : keys) {
|
||||
@ -257,4 +251,24 @@ public class HttpTools {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static String getLocalMacAddress(){
|
||||
try {
|
||||
InetAddress address=InetAddress.getLocalHost();
|
||||
byte[] bytes=NetworkInterface.getByInetAddress(address).getHardwareAddress();
|
||||
StringBuilder builder=new StringBuilder();
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
if (i != 0) {
|
||||
builder.append(":");
|
||||
}
|
||||
String tmp=Integer.toHexString(bytes[i]&0xFF);
|
||||
builder.append(tmp.length()==1?0+tmp:tmp);
|
||||
|
||||
}
|
||||
return builder.toString();
|
||||
} catch (UnknownHostException | java.net.SocketException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public class RedisTools {
|
||||
Jedis jedis = getRedis();
|
||||
jedis.select(dbIndex);
|
||||
String ret = jedis.set(key, value);
|
||||
Log.i("Redis set =" + ret,"key = "+key+" value = "+value);
|
||||
//Log.i("Redis set =" + ret,"key = "+key+" value = "+value);
|
||||
jedis.close();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
|
40
src/main/java/com/yutou/qqbot/utlis/XiaoMiRouter.java
Normal file
40
src/main/java/com/yutou/qqbot/utlis/XiaoMiRouter.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.yutou.qqbot.utlis;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
public class XiaoMiRouter {
|
||||
private final static String key = "a2ffa5c9be07488bbb04a3a47d3c5f6a";
|
||||
|
||||
public static String getToken() {
|
||||
String nonce = nonceCreat();
|
||||
String oldPwd = DigestUtils.sha1Hex(nonce + DigestUtils.sha1Hex("34864394" + key));
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("username", "admin");
|
||||
json.put("password", oldPwd);
|
||||
json.put("logtype", 2);
|
||||
json.put("nonce", nonce);
|
||||
json = JSONObject.parseObject(HttpTools.http_post("http://192.168.31.1/cgi-bin/luci/api/xqsystem/login", HttpTools.toUrlParams(json).getBytes(StandardCharsets.UTF_8), 1, null));
|
||||
if (json.getInteger("code") == 0) {
|
||||
return json.getString("token");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getDeviceListUrl() {
|
||||
return "http://192.168.31.1/cgi-bin/luci/;stok=" + getToken() + "/api/misystem/devicelist";
|
||||
}
|
||||
|
||||
private static String nonceCreat() {
|
||||
return String.format("%s_%s_%s_%s", 0, HttpTools.getLocalMacAddress(), (int) (System.currentTimeMillis() / 1000), (int) (Math.random() * 10000));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user