package im.zhaojun.system.service;

import cn.hutool.json.JSONUtil;
import im.zhaojun.system.mapper.AlarmMapper;
import im.zhaojun.system.model.Alarm;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import javax.annotation.Resource;
import java.net.URI;

public class MyWebSocketClient extends WebSocketClient {

    public MyWebSocketClient(URI serverUri) {
        super(serverUri);
    }

    @Resource
    private AlarmMapper alarmMapper;


    @Override


    public void onOpen(ServerHandshake arg0) {
// TODO Auto-generated method stub
        System.out.println("open");


    }


    @Override


    public void onClose(int arg0, String arg1, boolean arg2) {
        // TODO Auto-generated method stu
    }


    @Override


    public void onError(Exception arg0) {
        // TODO Auto-generated method stub
    }


    @Override


    public void onMessage(String arg0) {
        // TODO Auto-generated method stub
//获得消息，调用service 放入表中‘
        try {
            Alarm alarm = JSONUtil.toBean(arg0, Alarm.class);
            alarmMapper.insert(alarm);
            if(alarm.getType().equalsIgnoreCase("TRAFFIC_INCIDENT_ALARM")) {
                alarmMapper.insert(alarm);
            }
        } catch (Exception ex) {


        }
        System.out.println(arg0);

    }


}