398 lines
12 KiB
Java
398 lines
12 KiB
Java
package it.acxent.rd;
|
|
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
import it.acxent.common.Parm;
|
|
import it.acxent.common.StatusMsg;
|
|
import it.acxent.db.ApplParmFull;
|
|
import it.acxent.db.DBAdapter;
|
|
import it.acxent.db.ResParm;
|
|
import it.acxent.db.WcString;
|
|
import it.acxent.util.RandomString;
|
|
import it.acxent.util.StringTokenizer;
|
|
import it.acxent.util.Vectumerator;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.Serializable;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Timestamp;
|
|
import java.util.EnumMap;
|
|
import java.util.Map;
|
|
import javax.imageio.ImageIO;
|
|
import org.json.JSONObject;
|
|
|
|
public class RemoteDevice extends DBAdapter implements Serializable {
|
|
private static final long serialVersionUID = 1636646748813L;
|
|
|
|
public static final String P_RD_API_LOGIN = "RD_API_LOGIN";
|
|
|
|
public static final String P_RD_API_PWD = "RD_API_PWD";
|
|
|
|
public static final String P_RD_API_SERVER = "RD_API_SERVER";
|
|
|
|
public static final String P_RD_TEST_SMS = "RD_TEST_SMS";
|
|
|
|
private long id_remoteDevice;
|
|
|
|
private String descrizione;
|
|
|
|
private String token;
|
|
|
|
private String imei;
|
|
|
|
private String ipAddress;
|
|
|
|
private Timestamp updTmst;
|
|
|
|
private String tokenOld;
|
|
|
|
private String fcmToken;
|
|
|
|
private long flgAbilitato;
|
|
|
|
public static final String SMSGATEWAY_PROJECT_ID = "smsgateway-f05ec";
|
|
|
|
public RemoteDevice(ApplParmFull newApplParmFull) {
|
|
super(newApplParmFull);
|
|
}
|
|
|
|
public RemoteDevice() {}
|
|
|
|
private String getQrTokenString() {
|
|
JSONObject jo = new JSONObject();
|
|
jo.put("uri", getRemoteDeviceApiServer());
|
|
jo.put("token", getToken());
|
|
return jo.toString();
|
|
}
|
|
|
|
public String getRemoteDeviceApiServer() {
|
|
return getParm("RD_API_SERVER").getTesto();
|
|
}
|
|
|
|
public String getRemoteDeviceTestSms() {
|
|
return getParm("RD_TEST_SMS").getTesto();
|
|
}
|
|
|
|
public void setId_remoteDevice(long newId_remoteDevice) {
|
|
this.id_remoteDevice = newId_remoteDevice;
|
|
}
|
|
|
|
public void setDescrizione(String newDescrizione) {
|
|
this.descrizione = newDescrizione;
|
|
}
|
|
|
|
public void setToken(String newToken) {
|
|
this.token = newToken;
|
|
}
|
|
|
|
public void setImei(String newImei) {
|
|
this.imei = newImei;
|
|
}
|
|
|
|
public void setIpAddress(String newIpAddress) {
|
|
this.ipAddress = newIpAddress;
|
|
}
|
|
|
|
public void setUpdTmst(Timestamp newUpdTmst) {
|
|
this.updTmst = newUpdTmst;
|
|
}
|
|
|
|
public long getId_remoteDevice() {
|
|
return this.id_remoteDevice;
|
|
}
|
|
|
|
public String getDescrizione() {
|
|
return (this.descrizione == null) ? "" : this.descrizione.trim();
|
|
}
|
|
|
|
public String getToken() {
|
|
return (this.token == null) ? "" : this.token.trim();
|
|
}
|
|
|
|
public String getImei() {
|
|
return (this.imei == null) ? "" : this.imei.trim();
|
|
}
|
|
|
|
public String getIpAddress() {
|
|
return (this.ipAddress == null) ? "" : this.ipAddress.trim();
|
|
}
|
|
|
|
public Timestamp getUpdTmst() {
|
|
return this.updTmst;
|
|
}
|
|
|
|
public String getUpdTmstS() {
|
|
return (this.updTmst == null) ? "" : this.updTmst.toString();
|
|
}
|
|
|
|
protected ResParm checkDeleteCascade() {
|
|
return new ResParm(true);
|
|
}
|
|
|
|
protected void deleteCascade() {}
|
|
|
|
public Vectumerator<RemoteDevice> findByCR(RemoteDeviceCR CR, int pageNumber, int pageRows) {
|
|
String s_Sql_Find = "select A.* from REMOTE_DEVICE AS A";
|
|
String s_Sql_Order = "";
|
|
WcString wc = new WcString();
|
|
if (!CR.getSearchTxt().trim().isEmpty()) {
|
|
StringTokenizer st = new StringTokenizer(CR.getSearchTxt().trim(), " ");
|
|
StringBuffer txt = new StringBuffer("(");
|
|
while (st.hasMoreTokens()) {
|
|
String token = st.nextToken();
|
|
txt.append("(A.Cognome like '%" + token + "%' or A.Nome like '%" + token + "%')");
|
|
if (st.hasMoreTokens())
|
|
txt.append(" and ");
|
|
}
|
|
txt.append(")");
|
|
wc.addWc(txt.toString());
|
|
}
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
return findRows(stmt, pageNumber, pageRows);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
public String getQRCodeTokenFullFileName() {
|
|
if (getId_remoteDevice() > 0L) {
|
|
String targetdir = getDocBase() + "admin/rd/_qr/";
|
|
File fileTd = new File(targetdir);
|
|
if (!fileTd.exists())
|
|
fileTd.mkdirs();
|
|
return targetdir + targetdir;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public String getQRCodeTokenFileName() {
|
|
if (getId_remoteDevice() > 0L)
|
|
return "token_" + getId_remoteDevice() + "_" + getImgTmst() + ".jpg";
|
|
return "";
|
|
}
|
|
|
|
public ResParm generaNuovoToken() {
|
|
long l_id = getId_remoteDevice();
|
|
ResParm rp = new ResParm();
|
|
if (l_id > 0L) {
|
|
new File(getQRCodeTokenFullFileName()).delete();
|
|
setImgTmst(getTimeNameForFileUpload());
|
|
super.save();
|
|
String fileName = getQRCodeTokenFullFileName();
|
|
if (new File(fileName).exists());
|
|
RandomString randomString = new RandomString(256);
|
|
String token = randomString.nextString();
|
|
setToken(token);
|
|
rp = super.save();
|
|
if (rp.getStatus())
|
|
rp = creaQrCodeTokenFile(fileName, getQrTokenString(), 600);
|
|
} else {
|
|
rp.setStatus(false);
|
|
rp.setMsg("Salvare il record prima di generare il token");
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
public ResParm creaQrCodeTokenFile(String fileName, String qrmsg, int size) {
|
|
ResParm rp = new ResParm(true);
|
|
if (new File(fileName).exists());
|
|
try {
|
|
QRCodeWriter writer = new QRCodeWriter();
|
|
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
|
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
hints.put(EncodeHintType.MARGIN, Integer.valueOf(0));
|
|
if (size <= 0)
|
|
size = 600;
|
|
BitMatrix matrix = writer.encode(qrmsg, BarcodeFormat.QR_CODE, size, size, hints);
|
|
BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);
|
|
ImageIO.write(image, "JPEG", new File(fileName));
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
rp.setException(e);
|
|
}
|
|
return rp;
|
|
}
|
|
|
|
protected void initFields() {
|
|
super.initFields();
|
|
this.tokenOld = "";
|
|
}
|
|
|
|
protected void fillFields(ResultSet rst) {
|
|
super.fillFields(rst);
|
|
this.tokenOld = getToken();
|
|
}
|
|
|
|
public ResParm save() {
|
|
String l_tokenOld = this.tokenOld;
|
|
if (l_tokenOld != null &&
|
|
!l_tokenOld.equals(getToken())) {
|
|
new File(getQRCodeTokenFullFileName()).delete();
|
|
setImgTmst(getTimeNameForFileUpload());
|
|
}
|
|
ResParm rp = super.save();
|
|
if (rp.getStatus() &&
|
|
l_tokenOld != null &&
|
|
!l_tokenOld.equals(getToken()))
|
|
rp.append(creaQrCodeTokenFile(getQRCodeTokenFullFileName(), getQrTokenString(), 600));
|
|
return rp;
|
|
}
|
|
|
|
public Vectumerator<RemoteDevice> findAvailable() {
|
|
String s_Sql_Find = "select A.* from REMOTE_DEVICE AS A";
|
|
String s_Sql_Order = " order by A.updTmst desc";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.updTmst is not null");
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
return findRows(stmt);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
return AB_EMPTY_VECTUMERATOR;
|
|
}
|
|
}
|
|
|
|
public void findFirstAvailable() {
|
|
String s_Sql_Find = "select A.* from REMOTE_DEVICE AS A";
|
|
String s_Sql_Order = " order by A.updTmst desc";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.updTmst is not null");
|
|
wc.addWc("A.flgAbilitato=1");
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
findFirstRecord(stmt);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
}
|
|
}
|
|
|
|
public void findByToken(String l_token) {
|
|
String s_Sql_Find = "select A.* from REMOTE_DEVICE AS A";
|
|
String s_Sql_Order = " order by A.updTmst desc";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.token='" + l_token + "'");
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
findFirstRecord(stmt);
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
}
|
|
}
|
|
|
|
public static void initApplicationParms(ApplParmFull ap) {
|
|
boolean debug = false;
|
|
if (ap != null) {
|
|
DBAdapter.logDebug(debug, "RemoteDevice initParms: start");
|
|
String l_tipoParm = "";
|
|
Parm bean = new Parm(ap);
|
|
l_tipoParm = "REMOTE DEVICE";
|
|
StatusMsg.updateMsgByTag(ap, "INIT", l_tipoParm);
|
|
bean.findByCodice("RD_API_LOGIN");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("RD_API_LOGIN");
|
|
bean.setDescrizione("RD_API_LOGIN");
|
|
bean.setFlgTipo(0L);
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("login");
|
|
bean.setNota("LOGIN DI SCAMBIO TRA DEVICE E SERVER");
|
|
bean.save();
|
|
bean.findByCodice("RD_API_PWD");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("RD_API_PWD");
|
|
bean.setDescrizione("RD_API_PWD");
|
|
bean.setFlgTipo(0L);
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("pwd");
|
|
bean.setNota("PASSWORD DI SCAMBIO TRA DEVICE E SERVER");
|
|
bean.save();
|
|
bean.findByCodice("RD_API_SERVER");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("RD_API_SERVER");
|
|
bean.setDescrizione("RD_API_SERVER");
|
|
bean.setFlgTipo(0L);
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("http://10.10.0.50/cc/");
|
|
bean.setNota("INDIRIZZO DEL SERVER DI REGISTRAZIONE DEI DEVICE<br>Es: http://10.10.0.50/cc/");
|
|
bean.save();
|
|
bean.findByCodice("RD_TEST_SMS");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("RD_TEST_SMS");
|
|
bean.setDescrizione("RD_TEST_SMS");
|
|
bean.setFlgTipo(0L);
|
|
bean.setNota("NUMERO CELLULARE DI TEST PER INVIO DI PROVA SMS<BR>TUTTI I MESSAGGI VENGONO INVIATI A QUESTO CELLULARE");
|
|
bean.save();
|
|
bean.findByCodice("FIREBASE_SERVER_KEY");
|
|
if (bean.getId_parm() > 0L)
|
|
bean.delete();
|
|
bean.findByCodice("FIREBASE_JSON_AUTH_PRIVATE_KEY");
|
|
bean.setFlgAdmin(1L);
|
|
bean.setTipoParm(l_tipoParm);
|
|
bean.setCodice("FIREBASE_JSON_AUTH_PRIVATE_KEY");
|
|
bean.setDescrizione("FIREBASE_JSON_AUTH_PRIVATE_KEY");
|
|
bean.setFlgTipo(0L);
|
|
if (bean.getTesto().isEmpty())
|
|
bean.setTesto("/Users/acolzi/Downloads/smsgateway-f05ec-firebase-adminsdk-uicut-dbc52f0758.json");
|
|
bean.setNota("PERCORSO FULL AL FILE JSON CON LA CHIAVE DI AUTORIZZAZIONE DELLA APPLICAZIONE FIREBASE.<BR>\"/Users/acolzi/Downloads/smsgateway-f05ec-firebase-adminsdk-uicut-dbc52f0758.json\"");
|
|
bean.save();
|
|
DBAdapter.logDebug(debug, "RemoteDevice initParms: stop");
|
|
StatusMsg.deleteMsgByTag(ap, "INIT");
|
|
}
|
|
}
|
|
|
|
public String getFcmToken() {
|
|
return (this.fcmToken == null) ? "" : this.fcmToken.trim();
|
|
}
|
|
|
|
public void setFcmToken(String fcmToken) {
|
|
this.fcmToken = fcmToken;
|
|
}
|
|
|
|
public long getFlgAbilitato() {
|
|
return this.flgAbilitato;
|
|
}
|
|
|
|
public void setFlgAbilitato(long flgAbilitato) {
|
|
this.flgAbilitato = flgAbilitato;
|
|
}
|
|
|
|
public boolean isActive() {
|
|
if (getId_remoteDevice() == 0L || getFlgAbilitato() == 0L || getToken().isEmpty() || getFcmToken().isEmpty())
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
public boolean hasDeviceActive() {
|
|
String s_Sql_Find = "select A.* from REMOTE_DEVICE AS A";
|
|
String s_Sql_Order = "";
|
|
WcString wc = new WcString();
|
|
wc.addWc("A.flgAbilitato=1");
|
|
wc.addWc("A.token<>''");
|
|
wc.addWc("A.fcmToken<>''");
|
|
try {
|
|
PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString());
|
|
Vectumerator<RemoteDevice> vec = findRows(stmt, 1, 1);
|
|
if (vec.hasMoreElements())
|
|
return true;
|
|
return false;
|
|
} catch (SQLException e) {
|
|
removeCPConnection();
|
|
handleDebug(e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|