package com.power.spcomm;
|
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.InetSocketAddress;
|
import java.net.Socket;
|
import java.nio.ByteBuffer;
|
import java.util.ArrayList;
|
import java.util.Date;
|
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
import com.power.alarm.Alarm_Config;
|
import com.power.alarm.Alarm_Config_SQL;
|
import com.power.mysql.MysqlConnPool;
|
|
public class SocketClientComm_Thread extends Thread {
|
|
Logger logger = LogManager.getLogger(SPCommModule.class);
|
|
private Socket socketClient;
|
private InputStream inData;
|
private OutputStream outData;
|
|
private MysqlConnPool m_Conn_Pool;
|
|
private int Alarm_way;
|
|
private Alarm_Config Alarm_config;
|
|
private boolean socketCommOK = false;
|
|
private ByteBuffer CommRxBuffer = ByteBuffer.allocate(512);
|
|
private ArrayList<String> phoneNumList = new ArrayList<String>();
|
private ArrayList<Integer> devIdList = new ArrayList<Integer>();
|
|
|
public Date last_Alarm_Time = null;
|
public Date sms_AlarmCheck_Time = null;
|
|
|
public SocketClientComm_Thread(MysqlConnPool pool,int alarm_way) {
|
m_Conn_Pool = pool;
|
|
Alarm_way = alarm_way;
|
|
}
|
|
public void readMsg(ByteBuffer bbf_rx) {
|
|
if(false == socketCommOK)
|
return;
|
|
bbf_rx.clear();
|
int time_out = 0;
|
byte[] rx_buf_t = new byte[512];
|
|
try {
|
while(true) {
|
if(inData.available() > 0) {
|
time_out = 0;
|
int rx_cnt_t = inData.read(rx_buf_t);
|
if((bbf_rx.position()+rx_cnt_t) < (bbf_rx.capacity()-1)) {
|
bbf_rx.put(rx_buf_t, 0, rx_cnt_t);
|
} else {
|
break;
|
}
|
} else {
|
sleep(5);
|
time_out++;
|
if(0 == bbf_rx.position()) {
|
if(time_out >= 2000) {
|
break;
|
}
|
} else {
|
if(time_out >= 5) {
|
break;
|
}
|
}
|
}
|
}
|
|
bbf_rx.flip();
|
}
|
catch(Exception e) {
|
logger.error(e.toString(), e);
|
|
try {
|
inData.close();
|
outData.close();
|
socketClient.close();
|
}catch(Exception e1) {
|
logger.error(e1.toString(), e1);
|
}
|
|
socketCommOK = false;
|
}
|
}
|
|
private boolean sendShortMessage(String phonenumber) {
|
|
logger.info("****************** sendShortMessage ***********");
|
|
boolean send_success = false;
|
|
if(false == socketCommOK)
|
return send_success;
|
|
String msg_str = "86" + phonenumber + ":0:" + Alarm_config.SMS_Alarm_Text;
|
|
try {
|
outData.write(msg_str.getBytes("GBK"),0,msg_str.getBytes("GBK").length);
|
outData.flush();
|
}catch (Exception e) {
|
logger.error(e.toString(), e);
|
|
try {
|
inData.close();
|
outData.close();
|
socketClient.close();
|
}catch(Exception e1) {
|
logger.error(e1.toString(), e1);
|
}
|
|
socketCommOK = false;
|
}
|
|
readMsg(CommRxBuffer);
|
|
String rx_str = null;
|
|
CommRxBuffer.position(0);
|
|
byte[] bt_dat = new byte[CommRxBuffer.remaining()];
|
|
for(int n=0; n<bt_dat.length; n++) {
|
bt_dat[n] = CommRxBuffer.get();
|
}
|
|
rx_str = new String(bt_dat);
|
|
//logger.info("Recv Message: " + rx_str);
|
|
if(rx_str.contains("SMS_SEND_SUCESS")) {
|
send_success = true;
|
logger.info("SMS_SEND_SUCESS");
|
}
|
else if(rx_str.contains("SMS_SEND_FAIL")) {
|
logger.info("SMS_SEND_FAIL");
|
}
|
else {
|
logger.info("Unkwon return text...");
|
}
|
|
return send_success;
|
}
|
|
private void buzzerBeepAndLedOn() { //´ò¿ª µÆ ºÍ ÉùÒô
|
byte[] bt_dat = new byte[8];
|
|
bt_dat[0] = 0x7E;
|
bt_dat[1] = (byte)0xFF;
|
bt_dat[2] = 0x06;
|
bt_dat[3] = 0x3A;
|
bt_dat[4] = 0x00;
|
|
bt_dat[5] = 0x01;
|
bt_dat[6] = 0x00;
|
|
bt_dat[7] = (byte)0xEF;
|
|
try {
|
outData.write(bt_dat,0,8);
|
outData.flush();
|
}catch (Exception e) {
|
logger.error(e.toString(), e);
|
|
try {
|
inData.close();
|
outData.close();
|
socketClient.close();
|
}catch(Exception e1) {
|
logger.error(e1.toString(), e1);
|
}
|
|
socketCommOK = false;
|
}
|
}
|
|
private void turnOffBuzzerBeep() { //¹Ø±Õ ÉùÒô£¬ ±£Áô µÆ
|
byte[] bt_dat = new byte[8];
|
|
bt_dat[0] = 0x7E;
|
bt_dat[1] = (byte)0xFF;
|
bt_dat[2] = 0x06;
|
bt_dat[3] = 0x3A;
|
bt_dat[4] = 0x00;
|
|
bt_dat[5] = 0x00;
|
bt_dat[6] = 0x00;
|
|
bt_dat[7] = (byte)0xEF;
|
|
try {
|
outData.write(bt_dat,0,8);
|
outData.flush();
|
}catch (Exception e) {
|
logger.error(e.toString(), e);
|
|
try {
|
inData.close();
|
outData.close();
|
socketClient.close();
|
}catch(Exception e1) {
|
logger.error(e1.toString(), e1);
|
}
|
|
socketCommOK = false;
|
}
|
}
|
|
private void turnOffBuzzerAndLed() { //¹Ø±Õ ÉùÒô ºÍ µÆ
|
byte[] bt_dat = new byte[8];
|
|
bt_dat[0] = 0x7E;
|
bt_dat[1] = (byte)0xFF;
|
bt_dat[2] = 0x06;
|
bt_dat[3] = 0x3A;
|
bt_dat[4] = 0x00;
|
|
bt_dat[5] = 0x00;
|
bt_dat[6] = 0x01;
|
|
bt_dat[7] = (byte)0xEF;
|
|
try {
|
outData.write(bt_dat,0,8);
|
outData.flush();
|
}catch (Exception e) {
|
logger.error(e.toString(), e);
|
|
try {
|
inData.close();
|
outData.close();
|
socketClient.close();
|
}catch(Exception e1) {
|
logger.error(e1.toString(), e1);
|
}
|
|
socketCommOK = false;
|
}
|
}
|
|
private void query_phone_number() {
|
//String ph = "18627009360";
|
|
//phoneNumList.clear();
|
|
//phoneNumList.add(ph);
|
|
Alarm_Config_SQL.query_phonenumber_SQL(m_Conn_Pool, devIdList, phoneNumList);
|
}
|
|
@Override
|
public void run() {
|
|
socketCommOK = false;
|
|
last_Alarm_Time = new Date();
|
sms_AlarmCheck_Time = new Date();
|
|
for(int i=0;i<60;i++) {
|
try {
|
Thread.sleep(1000);
|
} catch (InterruptedException e) {
|
logger.error(e.toString(), e);
|
}
|
}
|
|
if(Alarm_Config.ALARM_WAY_BEEPER==Alarm_way) {
|
logger.info("Beeper Alarm Thread Start....");
|
}
|
else {
|
logger.info("SMS Alarm Thread Start....");
|
}
|
|
if(Alarm_Config.ALARM_WAY_SMS == Alarm_way) {
|
|
boolean new_alarm_exists = false;
|
|
while(true) {
|
long timelong =((new Date()).getTime()-sms_AlarmCheck_Time.getTime())/1000;
|
|
if(timelong >= 60) { //60 seconds
|
|
sms_AlarmCheck_Time = new Date();
|
|
new_alarm_exists = Alarm_Config_SQL.check_if_new_alarm(m_Conn_Pool, last_Alarm_Time,devIdList);
|
}
|
|
if(new_alarm_exists) {
|
|
Alarm_config = Alarm_Config_SQL.query_alarm_config(m_Conn_Pool);
|
|
if(Alarm_config.SMS_Alarm_EN>0) {
|
|
if(false == socketCommOK) {
|
|
try {
|
socketClient = new Socket();
|
socketClient.connect(new InetSocketAddress(Alarm_config.AlarmDevIP, Alarm_config.SMS_Alarm_ipPort), 3000);
|
|
if(socketClient.isConnected()) {
|
|
inData = socketClient.getInputStream();
|
outData = socketClient.getOutputStream();
|
|
socketCommOK = true;
|
}
|
}catch (IOException e) {
|
logger.error("SMS socket error...");
|
}
|
}
|
|
if(socketCommOK) {
|
|
last_Alarm_Time = new Date();
|
new_alarm_exists = false;
|
|
query_phone_number();
|
|
for(int i=0;i<phoneNumList.size();i++) {
|
if(sendShortMessage(phoneNumList.get(i)) == false) {
|
try {
|
Thread.sleep(20000); //·¢ËͶÌÐÅʧ°Ü£¬20SºóÔÙ¼ÌÐø
|
} catch (InterruptedException e) {
|
logger.error(e.toString(), e);
|
}
|
|
sendShortMessage(phoneNumList.get(i));
|
}
|
|
if(false == socketCommOK)
|
break;
|
}
|
}
|
}
|
}
|
|
try {
|
Thread.sleep(1000);
|
} catch (InterruptedException e) {
|
logger.error(e.toString(), e);
|
}
|
}
|
}
|
else if(Alarm_Config.ALARM_WAY_BEEPER == Alarm_way) {
|
|
boolean alarm_exists = false;
|
|
while(true) {
|
|
alarm_exists = Alarm_Config_SQL.check_if_alarm_exists(m_Conn_Pool);
|
|
|
Alarm_config = Alarm_Config_SQL.query_alarm_config(m_Conn_Pool);
|
|
//logger.info("Alarm_config.AlarmDevIP, Alarm_config.Beeper_Alarm_ipPort"+Alarm_config.AlarmDevIP+"---"+ Alarm_config.Beeper_Alarm_ipPort);
|
|
/*if(alarm_exists)*/ {
|
|
if(false == socketCommOK) {
|
|
try {
|
socketClient = new Socket();
|
socketClient.connect(new InetSocketAddress(Alarm_config.AlarmDevIP, Alarm_config.Beeper_Alarm_ipPort), 3000);
|
|
//logger.info("Alarm_config.AlarmDevIP, Alarm_config.Beeper_Alarm_ipPort"+Alarm_config.AlarmDevIP+"---"+ Alarm_config.Beeper_Alarm_ipPort);
|
|
|
if(socketClient.isConnected()) {
|
|
inData = socketClient.getInputStream();
|
outData = socketClient.getOutputStream();
|
|
socketCommOK = true;
|
}
|
}catch (IOException e) {
|
logger.error("Beeper socket error...");
|
}
|
}
|
|
//Alarm_config = Alarm_Config_SQL.query_alarm_config(m_Conn_Pool);
|
|
if(Alarm_config.Beeper_Alarm_EN>0) {
|
|
/*
|
if(false == socketCommOK) {
|
|
try {
|
socketClient = new Socket();
|
socketClient.connect(new InetSocketAddress(Alarm_config.AlarmDevIP, Alarm_config.Beeper_Alarm_ipPort), 3000);
|
|
if(socketClient.isConnected()) {
|
|
inData = socketClient.getInputStream();
|
outData = socketClient.getOutputStream();
|
|
socketCommOK = true;
|
}
|
}catch (IOException e) {
|
logger.error("Beeper socket error...");
|
}
|
}
|
*/
|
|
if(socketCommOK) {
|
|
if(alarm_exists) {
|
if(Alarm_config.Beeper_Sound_OFF>0) {
|
turnOffBuzzerBeep();
|
}
|
else {
|
buzzerBeepAndLedOn();
|
}
|
}
|
else {
|
turnOffBuzzerAndLed();
|
}
|
}
|
}
|
else {
|
if(socketCommOK) {
|
turnOffBuzzerAndLed();
|
}
|
}
|
}
|
|
try {
|
Thread.sleep(5000);
|
} catch (InterruptedException e) {
|
logger.error(e.toString(), e);
|
}
|
}
|
}
|
|
|
}
|
}
|