package com.pm.sms;

import java.io.FileOutputStream;
import java.io.OutputStream;

// 手机卡通道 - GSM Modem
public class GsmSmsSender{

//    private SerialPort serialPort;
//
//    public GsmSmsSender(String comPort) throws Exception {
//        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(comPort);
//        serialPort = (SerialPort) portId.open("SMS_Modem", 2000);
//        serialPort.setSerialPortParams(9600, 8, 1, 0);
//    }

    public boolean sendAlert(String phone, String message) {
//        try (OutputStream os = serialPort.getOutputStream()) {
        try{
            OutputStream os =new FileOutputStream("sms_alert.txt");
            // AT指令集操作
            String cmd = "AT+CMGF=1\r";  // 设置为文本模式
            os.write(cmd.getBytes());
            Thread.sleep(1000);

            cmd = "AT+CMGS=\"" + phone + "\"\r";
            os.write(cmd.getBytes());
            Thread.sleep(1000);

            os.write(message.getBytes());
            os.write(0x1A);  // Ctrl+Z结束
            os.flush();

            return true;
        } catch (Exception e) {
           // log.error("GSM短信发送失败", e);
            return false;
        }
    }
}