clean code

This commit is contained in:
CiufudeanDani 2022-03-30 12:52:38 +03:00
parent cbeb912820
commit ca4255943a

View File

@ -1,7 +1,6 @@
package com.safemobile.services;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
@ -12,16 +11,14 @@ import com.safemobile.lib.SM;
import com.safemobile.lib.TCPmsg;
public class TCPmsgParser implements Runnable{
public class TCPmsgParser implements Runnable {
public boolean run = true;
private TCPmsg _msg;
private static List<ITCPListener> _listeners = new ArrayList<ITCPListener>();
private static final List<ITCPListener> _listeners = new ArrayList<>();
private Thread TCPmsgParserThread;
public TCPmsgParser()
{
public TCPmsgParser() {
TCPmsgParserThread = new Thread(this, "TCPmsgParserThread");
TCPmsgParserThread.start(); // (2) Start the thread.
}
@ -37,404 +34,349 @@ public class TCPmsgParser implements Runnable{
public synchronized void clearITCPListeners() {
_listeners.clear();
}
public int getListenersSize()
{
return _listeners.size();
}
private synchronized void _fireLoginEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onLoginReceived( event );
for (ITCPListener listener : _listeners) {
listener.onLoginReceived(event);
}
}
private synchronized void _fireGPSEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onGPSReceived(event);
for (ITCPListener listener : _listeners) {
listener.onGPSReceived(event);
}
}
private synchronized void _fireSMSEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onSMSReceived( event );
for (ITCPListener listener : _listeners) {
listener.onSMSReceived(event);
}
}
private synchronized void _fireLastSMSEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onLastSMSsReceived(event);
for (ITCPListener listener : _listeners) {
listener.onLastSMSsReceived(event);
}
}
private synchronized void _fireVehEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onVehiclesReceived( event );
for (ITCPListener listener : _listeners) {
listener.onVehiclesReceived(event);
}
}
private synchronized void _fireNewSMS() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onNewSMSReceived(event);
for (ITCPListener listener : _listeners) {
listener.onNewSMSReceived(event);
}
}
private synchronized void _fireSMSconfirm() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onSMSAckReceived(event);
for (ITCPListener listener : _listeners) {
listener.onSMSAckReceived(event);
}
}
private synchronized void _fireLastPos() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onLastPositionsReceived(event);
for (ITCPListener listener : _listeners) {
listener.onLastPositionsReceived(event);
}
}
private synchronized void _fireRadioEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onRadioMsgReceived(event);
for (ITCPListener listener : _listeners) {
listener.onRadioMsgReceived(event);
}
}
private synchronized void _fireHistPos() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onHistoryPositionsReceived(event);
for (ITCPListener listener : _listeners) {
listener.onHistoryPositionsReceived(event);
}
}
private synchronized void _fireHistCount() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onHistoryPositionsCountReceived(event);
for (ITCPListener listener : _listeners) {
listener.onHistoryPositionsCountReceived(event);
}
}
private synchronized void _fireAlarmList() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onAlarmsReceived(event);
for (ITCPListener listener : _listeners) {
listener.onAlarmsReceived(event);
}
}
private synchronized void _fireAlarmACK() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).onAlarmAckReceived(event);
for (ITCPListener listener : _listeners) {
listener.onAlarmAckReceived(event);
}
}
private synchronized void _fireAlarmLive() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next() ).alarmLiveReceived(event);
for (ITCPListener listener : _listeners) {
listener.alarmLiveReceived(event);
}
}
private synchronized void _fireRecordList() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onRecordingsListReceived(event);
for (ITCPListener listener : _listeners) {
listener.onRecordingsListReceived(event);
}
}
private synchronized void _fireRecordPlay() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onRecordingPlayReceived(event);
for (ITCPListener listener : _listeners) {
listener.onRecordingPlayReceived(event);
}
}
private synchronized void _firePOLLEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onPollReceived(event);
for (ITCPListener listener : _listeners) {
listener.onPollReceived(event);
}
}
private synchronized void _fireConnectionReplyEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onConnectionReplyReceived(event);
for (ITCPListener listener : _listeners) {
listener.onConnectionReplyReceived(event);
}
}
private synchronized void _fireContactsReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onContactsListReceived(event);
for (ITCPListener listener : _listeners) {
listener.onContactsListReceived(event);
}
}
private synchronized void _fireRecordingsReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onRecordingsListReceived(event);
for (ITCPListener listener : _listeners) {
listener.onRecordingsListReceived(event);
}
}
private synchronized void _fireTextMessagesReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onTextMessagesListReceived(event);
for (ITCPListener listener : _listeners) {
listener.onTextMessagesListReceived(event);
}
}
private synchronized void _fireRecordingPlayReceivedEvent() {
TCPEvent event = new TCPEvent( this, _msg );
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onRecordingPlayReceived(event);
for (ITCPListener listener : _listeners) {
listener.onRecordingPlayReceived(event);
}
}
public static synchronized void _fireTCPConnectionDownEvent(boolean previuosWasConnectionUp) {
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onTCPConnectionDown(previuosWasConnectionUp);
for (ITCPListener listener : _listeners) {
listener.onTCPConnectionDown(previuosWasConnectionUp);
}
}
public static synchronized void _fireTCPConnectionUpEvent(boolean previuosWasConnectionUp) {
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onTCPConnectionUp(previuosWasConnectionUp);
for (ITCPListener listener : _listeners) {
listener.onTCPConnectionUp(previuosWasConnectionUp);
}
}
public static synchronized void _fireonTCPConnectionStatusEvent(boolean isConnectionUp, boolean previuosWasConnectionUp) {
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onTCPConnectionStatusReceived(isConnectionUp, previuosWasConnectionUp);
for (ITCPListener listener : _listeners) {
listener.onTCPConnectionStatusReceived(isConnectionUp, previuosWasConnectionUp);
}
}
public static synchronized void _firePONGReceivedEvent() {
Iterator<ITCPListener> listeners = _listeners.iterator();
while( listeners.hasNext() ) {
( (ITCPListener) listeners.next()).onPONGReceived();
for (ITCPListener listener : _listeners) {
listener.onPONGReceived();
}
}
@Override
public void run()
{
while(run)
{
//SM.Debug("TCPmsgParser waiting for data...");
public void run() {
while(run) {
sleep(1);
if(TCPhandler.msgList==null)
if (TCPhandler.msgList == null)
continue;
if(!TCPhandler.msgList.isEmpty())
{
try
{
if(!TCPhandler.msgList.isEmpty()) {
try {
_msg = TCPhandler.msgList.poll();
if(_msg == null)
continue;
if(_msg.OK == false)
if(!_msg.OK)
continue;
//parse the rest of the message;
switch(_msg.opCode)
{
case 40:{
_fireLoginEvent();
break;
}
switch(_msg.opCode) {
case 40:{
_fireLoginEvent();
break;
}
case 41:{
_fireVehEvent();
break;
}
case 41:{
_fireVehEvent();
break;
}
case 42:{
_fireSMSEvent();
break;
}
case 42:{
_fireSMSEvent();
break;
}
case 43:{
_fireLastSMSEvent();
break;
}
case 43:{
_fireLastSMSEvent();
break;
}
case OperationCodes.TM_ACK:{
_fireSMSconfirm();
break;
}
case OperationCodes.TM_ACK_SD:{
_fireSMSconfirm();
break;
}
case OperationCodes.TM_ACK:
case OperationCodes.TM_ACK_SD: {
_fireSMSconfirm();
break;
}
case OperationCodes.RECEIVED_TM:{
_fireNewSMS();
break;
}
case OperationCodes.RECEIVED_TM:{
_fireNewSMS();
break;
}
case 45:{
_fireLastPos();
break;
}
case 45:{
_fireLastPos();
break;
}
case 131:{
_fireGPSEvent();
break;
}
case 131:{
_fireGPSEvent();
break;
}
case 231:{
_firePOLLEvent();
break;
}
case 231:{
_firePOLLEvent();
break;
}
case 50:{
_fireRadioEvent();
break;
}
case 50:{
_fireRadioEvent();
break;
}
case 46:{
_fireHistPos();
break;
}
case 46:{
_fireHistPos();
break;
}
case 86:{
_fireHistCount();
break;
}
case 86:{
_fireHistCount();
break;
}
case 47:{
_fireAlarmList();
break;
}
case 47:{
_fireAlarmList();
break;
}
case 48:{
_fireAlarmACK();
break;
}
case 48:{
_fireAlarmACK();
break;
}
case 49:{
_fireRecordList();
break;
}
case 49:{
_fireRecordList();
break;
}
case 38:{
_fireRecordPlay();
break;
}
case 38:{
_fireRecordPlay();
break;
}
case 135:
case 136:
case 137:
case 138:
case 140:{
_fireAlarmLive();
break;
}
case 135:
case 136:
case 137:
case 138:
case 140:{
_fireAlarmLive();
break;
}
case OperationCodes.PONG: {
//_firePONGReceivedEvent();
break;
}
case OperationCodes.PONG: {
//_firePONGReceivedEvent();
break;
}
case OperationCodes.CONNECTION_REP: {
_fireConnectionReplyEvent();
break;
}
case OperationCodes.CONNECTION_REP: {
_fireConnectionReplyEvent();
break;
}
case OperationCodes.CONTACTS_REP: {
sleep(15);
_fireContactsReceivedEvent();
break;
}
case OperationCodes.CONTACTS_REP: {
sleep(15);
_fireContactsReceivedEvent();
break;
}
case OperationCodes.TM_LIST_REP: {
_fireTextMessagesReceivedEvent();
break;
}
case OperationCodes.TM_LIST_REP: {
_fireTextMessagesReceivedEvent();
break;
}
case OperationCodes.RECORDINGS_LIST_REP: {
_fireRecordingsReceivedEvent();
break;
}
case OperationCodes.RECORDINGS_LIST_REP: {
_fireRecordingsReceivedEvent();
break;
}
case OperationCodes.RECORDING_REP: {
_fireRecordingPlayReceivedEvent();
break;
}
case OperationCodes.RECORDING_REP: {
_fireRecordingPlayReceivedEvent();
break;
}
default:
break;
}
}
catch(Exception ex)
{
try {
if(TCPhandler.msgList.size() > 0)
_msg = TCPhandler.msgList.remove(0);
}
catch(NoSuchElementException exe)
{
SM.Exception("TCP msg Parser", "NoSuchElementException");
}
}
}
}
/*
catch (Exception e)
{
SM.Debug("Error on fire Event:"+e.toString());
break;
}
}*/
default:
break;
}
} catch(Exception ex) {
try {
if(TCPhandler.msgList.size() > 0)
_msg = TCPhandler.msgList.remove(0);
} catch(NoSuchElementException exe) {
SM.Exception("TCP msg Parser", "NoSuchElementException");
}
}
}
}
SM.Debug("TCPmsgParser listen thread stoped.");
}
private void sleep(int miliseconds) {
try {
Thread.sleep(miliseconds);
} catch (InterruptedException e) {
}
} catch (InterruptedException ignored) { }
}
public void clearMsgList()
@ -442,17 +384,14 @@ public class TCPmsgParser implements Runnable{
TCPhandler.msgList.clear();
}
public void Stop()
{
public void Stop() {
run = false;
// stop thread
if(TCPmsgParserThread != null)
{
if(TCPmsgParserThread != null) {
Thread moribund = TCPmsgParserThread;
TCPmsgParserThread = null;
moribund.interrupt();
}
SM.Debug("Stoping TCPmsgParser");
}
}
}