|
@@ -1,11 +1,13 @@
|
|
|
package com.huimv.wine.ws;
|
|
|
|
|
|
-import com.fasterxml.jackson.databind.util.BeanUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.huimv.wine.common.EventWsErrUtil;
|
|
|
import com.huimv.wine.config.VipCofing;
|
|
|
import com.huimv.wine.entity.Advertise;
|
|
|
import com.huimv.wine.entity.Device;
|
|
|
import com.huimv.wine.entity.Params;
|
|
|
import com.huimv.wine.entity.Wine;
|
|
|
+import com.huimv.wine.entity.vo.QrcodeParam;
|
|
|
import com.huimv.wine.entity.vo.WsEvent;
|
|
|
import com.huimv.wine.mapper.AdvertiseMapper;
|
|
|
import com.huimv.wine.mapper.DeviceMapper;
|
|
@@ -13,8 +15,7 @@ import com.huimv.wine.mapper.ParamsMapper;
|
|
|
import com.huimv.wine.mapper.WineMapper;
|
|
|
import com.huimv.wine.utils.SpringContextUtil;
|
|
|
import com.huimv.wine.utils.WebsocketSellerUtil;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
@@ -23,15 +24,14 @@ import javax.websocket.*;
|
|
|
import javax.websocket.server.PathParam;
|
|
|
import javax.websocket.server.ServerEndpoint;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* websocket接口处理类
|
|
|
*/
|
|
|
@Component
|
|
|
@ServerEndpoint(value = "/seller/{seq}")
|
|
|
+@Log4j2
|
|
|
public class SellerController extends TextWebSocketHandler {
|
|
|
|
|
|
|
|
@@ -73,10 +73,38 @@ public class SellerController extends TextWebSocketHandler {
|
|
|
|
|
|
|
|
|
@OnMessage
|
|
|
- public void onMessage( Session session, String message) {
|
|
|
+ public void onMessage(@PathParam(value = "seq") String seq, Session session, String message) {
|
|
|
// RemoteEndpoint.Async asyncRemote = session.getAsyncRemote();
|
|
|
+ if ("".equals(seq)){
|
|
|
+ WebsocketSellerUtil.sendMessage(session, EventWsErrUtil.getWsErr("device seq is required"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //获取设备
|
|
|
+ DeviceMapper deviceMapper = SpringContextUtil.getBean(DeviceMapper.class);
|
|
|
+ Device device = deviceMapper.getByDeviceId(seq);
|
|
|
+ if (ObjectUtils.isEmpty(device)){
|
|
|
+ WebsocketSellerUtil.sendMessage(session, EventWsErrUtil.getWsErr("device not fount"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isEmpty(message)){
|
|
|
+ WebsocketSellerUtil.sendMessage(session, EventWsErrUtil.getWsErr("data not fount"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- System.out.println(message);
|
|
|
+
|
|
|
+ WsEvent wsEvent = JSON.parseObject(message, WsEvent.class);
|
|
|
+ String event = wsEvent.getEvent();
|
|
|
+ Object data = wsEvent.getData();
|
|
|
+
|
|
|
+ switch (event){
|
|
|
+ case "pin":
|
|
|
+ keepAlive(session,wsEvent.getData());
|
|
|
+ break;
|
|
|
+ case "getQrcode":
|
|
|
+ getQrcode(device, session, data);
|
|
|
+ default:
|
|
|
+ WebsocketSellerUtil.sendMessage(session,new WsEvent("__Error_Event__","unrecognized event"));
|
|
|
+ }
|
|
|
//下发数据
|
|
|
// asyncRemote.sendText(message);
|
|
|
|
|
@@ -85,22 +113,36 @@ public class SellerController extends TextWebSocketHandler {
|
|
|
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 处理用户活连接异常
|
|
|
- *
|
|
|
- * @param session
|
|
|
- * @param throwable
|
|
|
- */
|
|
|
- @OnError
|
|
|
- public void onError(Session session, Throwable throwable) {
|
|
|
- try {
|
|
|
- session.close();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ //生成支付码
|
|
|
+ private void getQrcode(Device device, Session session, Object data) {
|
|
|
+ WineMapper wineMapper = SpringContextUtil.getBean(WineMapper.class);
|
|
|
+ QrcodeParam qrcodeParam = (QrcodeParam) data;
|
|
|
+ int weight = qrcodeParam.getWeight();
|
|
|
+ int id = qrcodeParam.getId();
|
|
|
+ int cash = qrcodeParam.getCash();
|
|
|
+ int cell = qrcodeParam.getCell();
|
|
|
+
|
|
|
+ Wine wine = wineMapper.selectWineById(id);
|
|
|
+ if (ObjectUtils.isEmpty(wine)){
|
|
|
+ WebsocketSellerUtil.sendMessage(session,EventWsErrUtil.getWsErr(String.format("no such wine: %d", qrcodeParam.getId())));
|
|
|
+ return;
|
|
|
}
|
|
|
- throwable.printStackTrace();
|
|
|
+ int nowCash = weight * wine.getPrice();
|
|
|
+ if (nowCash != cash){
|
|
|
+ WebsocketSellerUtil.sendMessage(session,EventWsErrUtil.getWsErr(String.format("got a wrong cash[you: %d, real: %d]",cash,nowCash)));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void keepAlive(Session session, Object data) {
|
|
|
+ WebsocketSellerUtil.sendMessage(session,new WsEvent("advResult",data));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public void infoOfDevice(Device device,Session session, DeviceMapper deviceMapper) {
|
|
|
|
|
|
WineMapper wineMapper = SpringContextUtil.getBean(WineMapper.class);
|
|
@@ -125,19 +167,26 @@ public class SellerController extends TextWebSocketHandler {
|
|
|
WebsocketSellerUtil.sendMessage(session,new WsEvent("runParamResult",paramsList));
|
|
|
|
|
|
//生成二维码
|
|
|
- String url = vipCofing.getVipScanApi() + "&device="+device.getId()+"&callback=" +vipCofing.getVipScanApi();
|
|
|
- System.out.println("url---->"+url);
|
|
|
+// String codeBase = CodeImgUtil.generateQRCode(vipCofing.getVipScanApi() + "&device=" + device.getId() + "&callback=" + vipCofing.getVipScanApi(), 200, 200);
|
|
|
+// WebsocketSellerUtil.sendMessage(session,new WsEvent("vipQrcodeResult",codeBase));
|
|
|
+ WebsocketSellerUtil.sendMessage(session,new WsEvent("initFinish",null));
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理用户活连接异常
|
|
|
+ *
|
|
|
+ * @param session
|
|
|
+ * @param throwable
|
|
|
+ */
|
|
|
+ @OnError
|
|
|
+ public void onError(Session session, Throwable throwable) {
|
|
|
+ try {
|
|
|
+ session.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ throwable.printStackTrace();
|
|
|
}
|
|
|
- //url := utils.Format("%s?device=%s&callback=%s", utils.VipScanApi, device.Id, utils.VipCallback)
|
|
|
- // img, err := qrcode.Encode(url, qrcode.Medium, 512)
|
|
|
- // if err != nil {
|
|
|
- // utils.Logger.Println(err)
|
|
|
- // _ = conn.WriteJSON(utils.WsError("Can't Access to Vip Qrcode"))
|
|
|
- // return
|
|
|
- // }
|
|
|
- // _ = conn.WriteJSON(utils.WsEvent("vipQrcodeResult", img))
|
|
|
- //
|
|
|
- // _ = conn.WriteJSON(utils.WsEvent("initFinish", nil))
|
|
|
+
|
|
|
|
|
|
}
|