2021-05-31

package com.saruan.xinfang.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.saruan.xinfang.mapper.LcdWindowMapper;
import com.saruan.xinfang.model.BipTWindow;
import com.saruan.xinfang.model.LcdWindow;
import com.saruan.xinfang.model.SysUser;
import com.saruan.xinfang.vo.SocketVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Component
public class SocketService {
    @Autowired
    public SysUserService userService;
    @Autowired
    public LcdWindowService lcdWindowService;
    @Autowired
    public WindowService windowService;
    @Autowired
    public LcdWindowMapper lcdWindowMapper;
    @Value("${socket.port}")
    Integer PORT;
    @Value("${socket.ip}")
    String ip ;
    private static PrintWriter out;
    @Value("${xinfang.picture}")
    private String pictureUrl;
    private static Map<Long, Socket> socketMap = new ConcurrentHashMap<Long, Socket>();
    public String ckId;  //lcd唯一码
    private static BufferedReader bufferedReader;
    @PostConstruct
    public void onOpen() throws Exception {
        //InetAddress ia = InetAddress.getByName(ip);
        ServerSocket server = new ServerSocket(PORT);
        System.out.println("socket success!");
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Socket clientSocket = server.accept();
                        System.out.println("新的socket连接进入:"+clientSocket);
                        //bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                        // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
                        InputStream inputStream = clientSocket.getInputStream();
                        byte[] bytes = new byte[1024];
                        int len;
                        StringBuilder sb = new StringBuilder();
                        while ((len = inputStream.read(bytes)) != -1) {
                            //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8
                            String s = new String(bytes, 0, len,"UTF-8");
                            sb.append(s);
                            if (s.endsWith("#")){
                                break;
                            }
                        }
                        ckId = sb.toString();
                        System.out.println("唯一码:"+ckId);
                        LcdWindow lcdWindow1 = new LcdWindow();
                        lcdWindow1.setLcdid(ckId);
                        List<LcdWindow> lcdWindows = lcdWindowService.selectAll(lcdWindow1);
                        for (LcdWindow lcdWindow : lcdWindows) {
                            socketMap.put(lcdWindow.getWindowid(),clientSocket);//保存窗口ID和socket
                        }
                        System.out.println("socketMap"+socketMap);
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("socket连接失败!");
                    }
                }
            }
        });
        t.start();
        Thread.sleep(1000);
    }

    public void SendMessage(SocketVo sv) throws IOException {
        Socket socket = socketMap.get(sv.getId());
        if (socket != null) {
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            out.println(JSON.toJSONString(sv));
            out.flush();
        }

    }

    public void SendPetitionPeople(SocketVo sv) throws IOException {
        //获取系统路径
        String url = System.getProperty("catalina.home");
        Socket socket = socketMap.get(sv.getId());
        if (socket != null) {
            //先存
            LcdWindow lcdWindow = new LcdWindow();
            lcdWindow.setWindowid(sv.getId());
            lcdWindow.setUserid(sv.getUserId());
            lcdWindowMapper.updateByWindowId(lcdWindow);
            //在通过窗口id查屏幕唯一码
            String lcdId = lcdWindowMapper.selectLcdIdByWindowId(sv.getId());
            System.out.println("lcdId"+lcdId);
            if (lcdId.equals(ckId)) {
                LcdWindow lcdWindow1 = new LcdWindow();
                lcdWindow1.setLcdid(lcdId);
                List<LcdWindow> lcdWindows = lcdWindowService.selectAll(lcdWindow1);
                System.out.println("lcdWindows"+lcdWindows);
                if (lcdWindows.size() == 1) {
                    for (LcdWindow window : lcdWindows) {
                        if (window.getUserid() != 0) {
                            SysUser user = userService.select(sv.getUserId());
                            //图片路径
                            window.setPicture(pictureUrl+user.getAvatar());//登记人员图片
                            window.setUsername(user.getUserName());//登记人员姓名
                            window.setZw("普通职员");//职位
                            window.setState("正在办理");//状态
                            BipTWindow select = windowService.select(window.getWindowid());
                            window.setPoweraffairsName(select.getFname());//责任单位
                        }
                    }
                }
                if (lcdWindows.size() == 2) {
                    for (LcdWindow window : lcdWindows) {
                        if (window.getUserid() != 0) {
                            SysUser user = userService.select(sv.getUserId());
                            //图片路径
                            window.setPicture(pictureUrl+user.getAvatar());//登记人员图片
                            window.setUsername(user.getUserName());//登记人员姓名
                            window.setZw("普通职员");//职位
                            window.setState("正在办理");//状态
                            BipTWindow select = windowService.select(window.getWindowid());
                            window.setPoweraffairsName(select.getFname());//责任单位
                        } else {
                            SysUser user = userService.select(sv.getUserId());
                            window.setPicture("");//登记人员图片
                            window.setUsername("");//登记人员姓名
                            window.setZw("普通职员");//职位
                            window.setState("暂停办理");//状态
                            System.out.println("window.getWindowid()"+window.getWindowid());
                            BipTWindow select = windowService.select(window.getWindowid());
                            window.setPoweraffairsName(select.getFname());//责任单位
                        }
                    }
                }
                JSONObject jo = new JSONObject();
                jo.put("type","login");
                jo.put("content",lcdWindows);
                out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                System.out.println(JSONArray.toJSONString(lcdWindows));
                out.println(jo.toString());
                out.flush();
            }else {
                out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                out.println("未找到对应的lcd屏幕");
                out.flush();
            }
        }else {
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            out.println("未创建socket连接");
            out.flush();
        }
    }
    public boolean Close(String id) throws IOException {
        boolean flag = false;
        Socket socket = socketMap.get(Long.valueOf(id));
        if (!socket.isClosed()) {
            socket.close();
            socketMap.remove(Long.valueOf(id));
            flag = true;
            System.out.println("窗口 - " + id + " 正常关闭");
            System.out.println("当前socket列表:" + socketMap.keySet());
        }
        return flag;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值