Compare commits
No commits in common. "fe5a19157ad154de5e95770bc137d58a1d8fbec0" and "ca01d17e9addc27e98bbeeae7f37577e1731267d" have entirely different histories.
fe5a19157a
...
ca01d17e9a
|
@ -1,7 +1,6 @@
|
||||||
package com.agileboot.api.controller;
|
package com.agileboot.api.controller;
|
||||||
|
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import cn.hutool.http.useragent.UserAgentUtil;
|
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.agileboot.api.response.ShopGoodsResponse;
|
import com.agileboot.api.response.ShopGoodsResponse;
|
||||||
import com.agileboot.common.constant.WeixinConstants;
|
import com.agileboot.common.constant.WeixinConstants;
|
||||||
|
@ -67,12 +66,6 @@ public class ShopController {
|
||||||
return ResponseDTO.ok(new ShopGoodsResponse(goodsWithCabinetList, categoryList, "0"));
|
return ResponseDTO.ok(new ShopGoodsResponse(goodsWithCabinetList, categoryList, "0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 微信端商店入口,用于微信端授权登录
|
|
||||||
* 参数中包含token为汇邦云登录
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@GetMapping("/wechatAuth")
|
@GetMapping("/wechatAuth")
|
||||||
public RedirectView wechatAuthRedirect(HttpServletRequest request) {
|
public RedirectView wechatAuthRedirect(HttpServletRequest request) {
|
||||||
/*java.util.StringJoiner joiner = new java.util.StringJoiner("&");
|
/*java.util.StringJoiner joiner = new java.util.StringJoiner("&");
|
||||||
|
@ -110,11 +103,6 @@ public class ShopController {
|
||||||
return new RedirectView(authUrl);
|
return new RedirectView(authUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 企业微信推送消息点击商品图片后跳转的地址
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@GetMapping("/qy/wechatAuth")
|
@GetMapping("/qy/wechatAuth")
|
||||||
public RedirectView qyWechatAuthRedirect(HttpServletRequest request) {
|
public RedirectView qyWechatAuthRedirect(HttpServletRequest request) {
|
||||||
/*java.util.StringJoiner joiner = new java.util.StringJoiner("&");
|
/*java.util.StringJoiner joiner = new java.util.StringJoiner("&");
|
||||||
|
@ -134,11 +122,6 @@ public class ShopController {
|
||||||
return new RedirectView(authUrl);
|
return new RedirectView(authUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 企业微信推送消息点击商品图片后跳转的地址
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@GetMapping("/approvalRedirect")
|
@GetMapping("/approvalRedirect")
|
||||||
public RedirectView approvalRedirect(HttpServletRequest request) {
|
public RedirectView approvalRedirect(HttpServletRequest request) {
|
||||||
UriComponentsBuilder builder = UriComponentsBuilder
|
UriComponentsBuilder builder = UriComponentsBuilder
|
||||||
|
@ -155,77 +138,8 @@ public class ShopController {
|
||||||
return new RedirectView(builder.build().encode().toUriString());
|
return new RedirectView(builder.build().encode().toUriString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 企业微信聊天界面点击进入主页后跳转的地址
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@GetMapping("/qy/wechatAuth/home")
|
|
||||||
public RedirectView qyWechatAuthHomeRedirect(HttpServletRequest request) {
|
|
||||||
String userAgent = request.getHeader("User-Agent");
|
|
||||||
boolean isMobile = UserAgentUtil.parse(userAgent).isMobile();
|
|
||||||
String redirectPath = isMobile ? "homeRedirect" : "adminRedirect";
|
|
||||||
|
|
||||||
String authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize"
|
|
||||||
+ "?appid=" + WeixinConstants.corpid
|
|
||||||
+ "&redirect_uri=http%3A%2F%2Fwxshop.ab98.cn%2Fshop-api%2Fapi%2Fshop%2F" + redirectPath
|
|
||||||
+ "&response_type=code"
|
|
||||||
+ "&scope=snsapi_base"
|
|
||||||
+ "&state=STATE"
|
|
||||||
+ "&agentid=" + WeixinConstants.agentid
|
|
||||||
+ "#wechat_redirect";
|
|
||||||
return new RedirectView(authUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 企业微信聊天界面点击进入主页后跳转的地址
|
|
||||||
* 移动端
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@GetMapping("/homeRedirect")
|
|
||||||
public RedirectView homeRedirect(HttpServletRequest request) {
|
|
||||||
UriComponentsBuilder builder = UriComponentsBuilder
|
|
||||||
.fromHttpUrl("http://wxshop.ab98.cn/shop")
|
|
||||||
.queryParam("corpid", WeixinConstants.corpid)
|
|
||||||
.queryParam("device", "APP");
|
|
||||||
|
|
||||||
request.getParameterMap().forEach((key, values) -> {
|
|
||||||
if (!"corpid".equals(key) && !"device".equals(key)) {
|
|
||||||
builder.queryParam(key, (Object[]) values);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return new RedirectView(builder.build().encode().toUriString());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 企业微信聊天界面点击进入主页后跳转的地址
|
|
||||||
* PC端
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@GetMapping("/adminRedirect")
|
|
||||||
public RedirectView adminRedirect(HttpServletRequest request) {
|
|
||||||
UriComponentsBuilder builder = UriComponentsBuilder
|
|
||||||
.fromHttpUrl("http://wxshop.ab98.cn/shop-admin")
|
|
||||||
.queryParam("corpid", WeixinConstants.corpid)
|
|
||||||
.queryParam("device", "PC");
|
|
||||||
|
|
||||||
request.getParameterMap().forEach((key, values) -> {
|
|
||||||
if (!"corpid".equals(key) && !"device".equals(key)) {
|
|
||||||
builder.queryParam(key, (Object[]) values);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return new RedirectView(builder.build().encode().toUriString());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) throws UnsupportedEncodingException {
|
public static void main(String[] args) throws UnsupportedEncodingException {
|
||||||
String res = URLEncoder.encode("token=12312", StandardCharsets.UTF_8.name());
|
String res = URLEncoder.encode("token=12312", StandardCharsets.UTF_8.name());
|
||||||
System.out.println(res);
|
System.out.println(res);
|
||||||
|
|
||||||
String url = URLUtil.encode("http://wxshop.ab98.cn/shop-api/api/shop/");
|
|
||||||
System.out.println(url);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,9 +1,6 @@
|
||||||
package com.agileboot.domain.mqtt;
|
package com.agileboot.domain.mqtt;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -11,46 +8,25 @@ import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.eclipse.paho.client.mqttv3.*;
|
import org.eclipse.paho.client.mqttv3.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class MqttService implements MqttCallback {
|
public class MqttService implements MqttCallback {
|
||||||
private final List<ClientConfig> clientConfigs = new ArrayList<>();
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
|
||||||
private static class ClientConfig {
|
|
||||||
private final MqttClient client;
|
|
||||||
private final MqttConfig config;
|
|
||||||
}
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Getter
|
|
||||||
private enum MqttConfig {
|
|
||||||
LOCK("lock", "lock@ys#6785$", "lock/up/S4202414S", "lock/down/S4202414S"),
|
|
||||||
SELL("sell", "sell@ys#6785$", "lock/up/S5200184S", "lock/down/S5200184S");
|
|
||||||
|
|
||||||
private final String username;
|
|
||||||
private final String password;
|
|
||||||
private final String topicFilter;
|
|
||||||
private final String publishTopic;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String SERVER_URL = "tcp://221.7.159.46:1883";
|
private static final String SERVER_URL = "tcp://221.7.159.46:1883";
|
||||||
// private static final String USERNAME = "sell";
|
// private static final String USERNAME = "lock";
|
||||||
// private static final String PASSWORD = "sell@ys#6785$";
|
// private static final String PASSWORD = "lock@ys#6785$";
|
||||||
// private static final String TOPIC_FILTER = "lock/up/S5200184S";
|
// private static final String TOPIC_FILTER = "lock/up/S4202414S";
|
||||||
// private static final String TOPIC = "lock/down/S5200184S";
|
// private static final String TOPIC = "lock/down/S4202414S";
|
||||||
|
private static final String USERNAME = "sell";
|
||||||
|
private static final String PASSWORD = "sell@ys#6785$";
|
||||||
|
private static final String TOPIC_FILTER = "lock/up/S5200184S";
|
||||||
|
private static final String TOPIC = "lock/down/S5200184S";
|
||||||
// private static final String USERNAME = "iot";
|
// private static final String USERNAME = "iot";
|
||||||
// private static final String PASSWORD = "iot@ys#9963$";
|
// private static final String PASSWORD = "iot@ys#9963$";
|
||||||
// private static final String TOPIC_FILTER = "hongfa/2401310026C50D24FE/upload/";
|
// private static final String TOPIC_FILTER = "hongfa/2401310026C50D24FE/upload/";
|
||||||
// private static final String TOPIC = "hongfa/2401310026C50D24FE/download/";
|
// private static final String TOPIC = "hongfa/2401310026C50D24FE/download/";
|
||||||
|
|
||||||
// private MqttClient client;
|
private MqttClient client;
|
||||||
@Setter
|
|
||||||
private List<String> activeTopics = new ArrayList<>();
|
|
||||||
// 设置自定义消息处理器
|
// 设置自定义消息处理器
|
||||||
@Setter
|
@Setter
|
||||||
private MessageHandler messageHandler; // 自定义消息处理器接口
|
private MessageHandler messageHandler; // 自定义消息处理器接口
|
||||||
|
@ -62,7 +38,7 @@ public class MqttService implements MqttCallback {
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() throws MqttException {
|
public void init() throws MqttException {
|
||||||
connect();
|
connect();
|
||||||
// subscribe(TOPIC_FILTER);
|
subscribe(TOPIC_FILTER);
|
||||||
|
|
||||||
setMessageHandler((String topic, String hexPayload) -> {
|
setMessageHandler((String topic, String hexPayload) -> {
|
||||||
log.info("收到消息 topic: {}, hexPayload: {}", topic, hexPayload);
|
log.info("收到消息 topic: {}, hexPayload: {}", topic, hexPayload);
|
||||||
|
@ -70,54 +46,31 @@ public class MqttService implements MqttCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect() throws MqttException {
|
public void connect() throws MqttException {
|
||||||
for (MqttConfig config : MqttConfig.values()) {
|
client = new MqttClient(SERVER_URL, MqttClient.generateClientId(), new MemoryPersistence());
|
||||||
MqttClient client = new MqttClient(SERVER_URL, MqttClient.generateClientId(), new MemoryPersistence());
|
MqttConnectOptions options = new MqttConnectOptions();
|
||||||
MqttConnectOptions options = new MqttConnectOptions();
|
options.setUserName(USERNAME);
|
||||||
options.setUserName(config.getUsername());
|
options.setPassword(PASSWORD.toCharArray());
|
||||||
options.setPassword(config.getPassword().toCharArray());
|
options.setCleanSession(true);
|
||||||
options.setCleanSession(true);
|
options.setAutomaticReconnect(true);
|
||||||
options.setAutomaticReconnect(true);
|
|
||||||
|
|
||||||
client.setCallback(new MqttCallback() {
|
|
||||||
@Override
|
|
||||||
public void messageArrived(String topic, MqttMessage message) {
|
|
||||||
MqttService.this.messageArrived(topic, message);
|
|
||||||
}
|
|
||||||
@Override public void connectionLost(Throwable cause) {}
|
|
||||||
@Override public void deliveryComplete(IMqttDeliveryToken token) {}
|
|
||||||
});
|
|
||||||
client.connect(options);
|
|
||||||
client.subscribe(config.getTopicFilter());
|
|
||||||
clientConfigs.add(new ClientConfig(client, config));
|
|
||||||
log.info("成功连接MQTT账号:{}", config.getUsername());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
client.setCallback(this);
|
||||||
|
client.connect(options);
|
||||||
log.info("连接 MQTT 服务器 {}", SERVER_URL);
|
log.info("连接 MQTT 服务器 {}", SERVER_URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public void subscribe(String topic) throws MqttException {
|
public void subscribe(String topic) throws MqttException {
|
||||||
client.subscribe(topic);
|
client.subscribe(topic);
|
||||||
}*/
|
}
|
||||||
|
|
||||||
public void publish(String data) throws MqttException {
|
public void publish(String data) throws MqttException {
|
||||||
// lockCmd((byte) 0x8A, (byte) 0x01, (byte) 0x01, (byte) 0x33, null);
|
// lockCmd((byte) 0x8A, (byte) 0x01, (byte) 0x01, (byte) 0x33, null);
|
||||||
String bcc = BCCCalculator.calculateBCC(data);
|
String bcc = BCCCalculator.calculateBCC(data);
|
||||||
MqttMessage message = new MqttMessage(BCCCalculator.hexStringToByteArray(data + bcc));
|
MqttMessage message = new MqttMessage(BCCCalculator.hexStringToByteArray(data + bcc));
|
||||||
clientConfigs.forEach(cc -> {
|
client.publish(TOPIC, message);
|
||||||
try {
|
|
||||||
cc.client.publish(cc.config.getPublishTopic(), message);
|
|
||||||
} catch (MqttException e) {
|
|
||||||
log.error("消息发送失败", e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() throws MqttException {
|
public void disconnect() throws MqttException {
|
||||||
for (ClientConfig cc : clientConfigs) {
|
client.disconnect();
|
||||||
MqttClient client = cc.client;
|
|
||||||
client.disconnect();
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -155,7 +108,7 @@ public class MqttService implements MqttCallback {
|
||||||
指令0x9E:读取所有锁状态
|
指令0x9E:读取所有锁状态
|
||||||
指令中未标注cmdSub指令参数的固定为0x33
|
指令中未标注cmdSub指令参数的固定为0x33
|
||||||
*******************************************************/
|
*******************************************************/
|
||||||
/*private boolean lockCmd(byte cmdNo, byte boardNo, byte lockNo, byte cmdSub, String[] rsMsg)
|
private boolean lockCmd(byte cmdNo, byte boardNo, byte lockNo, byte cmdSub, String[] rsMsg)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -168,18 +121,12 @@ public class MqttService implements MqttCallback {
|
||||||
|
|
||||||
//发送数据并获取返回值
|
//发送数据并获取返回值
|
||||||
MqttMessage message = new MqttMessage(sendData);
|
MqttMessage message = new MqttMessage(sendData);
|
||||||
clientConfigs.forEach(cc -> {
|
client.publish(TOPIC, message);
|
||||||
try {
|
|
||||||
cc.client.publish(cc.config.getPublishTopic(), message);
|
|
||||||
} catch (MqttException e) {
|
|
||||||
log.error("消息发送失败", e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
log.error("lockCmd", e);
|
log.error("lockCmd", e);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}*/
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue