Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
neardws committed Sep 27, 2018
1 parent 35640be commit d9c1fa8
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public class Message implements Serializable {
public static final String TYPE_MESSAGE = "TYPE_MESSAGE";
public static final String TYPE_TIME_SYNC_MESSAGE = "TYPE_TIME_SYNC_MESSAGE";
public static final String TYPE_WIFI_TIME_SYNC_RESULT = "TYPE_WIFI_TIME_SYNC_RESULT";
public static final String TYPE_WIFI_MESSAGE = "TYPE_WIFI_MESSAGE";
public static int ERROR_VALUE = -666;
private int id;
private long timeStamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Result implements Serializable {

public static final String TYPE_RESULT = "TYPE_RESULT";
public static final String TYPE_TIME_SYNC_RESULT = "TYPE_TIME_SYNC_RESULT";
public static final String TYPE_WIFI_TIME_SYNC = "TYPE_WIFI_TIME_SYNC";
public static int ERROR_VALUE = -666;
private int id;
private int time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@
import com.raizlabs.android.dbflow.list.IFlowCursorIterator;

import java.io.IOException;
import java.io.PrintStream;
import java.io.SyncFailedException;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -65,6 +69,8 @@ public class MainActivity extends AppCompatActivity {

public static final String ACTION_SEND_MESSAGE = "ACTION_SEND_MESSAGE";
public static final String ACTION_SQL = "ACTION_SQL";
public static final String ACTION_LTE_TIME_SYNC = "ACTION_LTE_TIME_SYNC";

private static final String SERVER_IP = "192.168.1.80";
private static final String SERVER_PORT = "4040";

Expand Down Expand Up @@ -152,7 +158,7 @@ protected void onCreate(Bundle savedInstanceState) {
intentFilter.addAction(InfoThread.ACTION_INFORMATION);
intentFilter.addAction(MainActivity.ACTION_SEND_MESSAGE);
intentFilter.addAction(MainActivity.ACTION_SQL);

intentFilter.addAction(MainActivity.ACTION_LTE_TIME_SYNC);

etIp = (EditText) findViewById(R.id.et_ipAdd);
btnPing = (Button) findViewById(R.id.btn_testPing);
Expand All @@ -177,7 +183,6 @@ protected void onCreate(Bundle savedInstanceState) {
tv_gps = (TextView) findViewById(R.id.tv_gps);
tv_sql = (TextView) findViewById(R.id.tv_sql);


img_warning = (ImageView) findViewById(R.id.img_warning);
img_warning.setImageDrawable(getResources().getDrawable(R.mipmap.ic_safe));

Expand All @@ -186,60 +191,6 @@ protected void onCreate(Bundle savedInstanceState) {
btnQuery = (Button) findViewById(R.id.btn_query);
btnBackup = (Button) findViewById(R.id.btn_backup);

btnPing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (etIp.getEditableText().toString().equals("")){ //使用默认IP地址
if (isConnectedServer(SERVER_IP)){
tvPingResult.setText("Ping "+SERVER_IP+ "Success.");
addLogToDB("Ping "+SERVER_IP+ "Success.");
} else {
tvPingResult.setText("Ping "+SERVER_IP+ "Failed.");
addLogToDB("Ping "+SERVER_IP+ "Failed.");
}
} else {
String ipAdd = (String) etIp.getEditableText().toString();
if (isIp(ipAdd)){ //ip匹配
setServer_Add(ipAdd);
if (isConnectedServer(ipAdd)){
tvPingResult.setText("Ping"+ipAdd+ "Success.");
addLogToDB("Ping"+ipAdd+ "Success.");
} else {
tvPingResult.setText("Ping"+ipAdd+ "Failed.");
addLogToDB("Ping"+ipAdd+ "Failed.");
}
} else { //ip不匹配
tvPingResult.setText("请重新输入IP.");
addLogToDB("请重新输入IP.");
}

}

}
});

btnBackup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addLogToDB("备份数据库");
Intent intent = new Intent(MainActivity.this, DBTool.class);
intent.setAction(DBTool.ACTION_COPY);
startService(intent);
}
});

/***********************************************
* 现为时间同步按钮
*/
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//只要一点发送,就有数据的录入,获取,转换,以及数据json格式的转化过程
sendTimeStampMessage();
addLogToDB("发起时间同步");
}
});


/**
* 注册接收线程、发送线程
*/
Expand Down Expand Up @@ -308,6 +259,58 @@ public void onClick(View view) {//只要一点发送,就有数据的录入,
infoThread.start();


btnPing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (etIp.getEditableText().toString().equals("")){ //使用默认IP地址
if (isConnectedServer(SERVER_IP)){
tvPingResult.setText("Ping "+SERVER_IP+ "Success.");
addLogToDB("Ping "+SERVER_IP+ "Success.");
} else {
tvPingResult.setText("Ping "+SERVER_IP+ "Failed.");
addLogToDB("Ping "+SERVER_IP+ "Failed.");
}
} else {
String ipAdd = (String) etIp.getEditableText().toString();
if (isIp(ipAdd)){ //ip匹配
setServer_Add(ipAdd);
if (isConnectedServer(ipAdd)){
tvPingResult.setText("Ping"+ipAdd+ "Success.");
addLogToDB("Ping"+ipAdd+ "Success.");
} else {
tvPingResult.setText("Ping"+ipAdd+ "Failed.");
addLogToDB("Ping"+ipAdd+ "Failed.");
}
} else { //ip不匹配
tvPingResult.setText("请重新输入IP.");
addLogToDB("请重新输入IP.");
}

}

}
});

btnBackup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addLogToDB("备份数据库");
Intent intent = new Intent(MainActivity.this, DBTool.class);
intent.setAction(DBTool.ACTION_COPY);
startService(intent);
}
});

/***********************************************
* 现为时间同步按钮
*/
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//只要一点发送,就有数据的录入,获取,转换,以及数据json格式的转化过程
sendTimeStampMessage();
addLogToDB("发起时间同步");
}
});

btnStart.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -341,7 +344,6 @@ public void onClick(View v) {
}

public boolean isConnectedServer(String ip) {

Runtime runtime = Runtime.getRuntime();
try
{
Expand Down Expand Up @@ -490,6 +492,32 @@ public void onReceive(Context context, Intent intent) {
default:
break;
}
} else if (String.valueOf(id).equals("-666")){
String type = result.getType();
long sendTimeStamp = result.getSendTimeStamp();
long timeStamp = System.currentTimeMillis();
if (type.equals(Result.TYPE_WIFI_TIME_SYNC)){
Message messageWifiTimeSync = new Message();
messageWifiTimeSync.setId(Message.ERROR_VALUE);
messageWifiTimeSync.setType(Message.TYPE_WIFI_TIME_SYNC_RESULT);
messageWifiTimeSync.setLat(Message.ERROR_VALUE);
messageWifiTimeSync.setLon(Message.ERROR_VALUE);
messageWifiTimeSync.setAce(sendTimeStamp);
messageWifiTimeSync.setDirection(Message.ERROR_VALUE);
messageWifiTimeSync.setSpeed(Message.ERROR_VALUE);
// messageWifiTimeSync.setMac(String.valueOf(sendTimeStamp));
messageWifiTimeSync.setTimeStamp(timeStamp);
tvPingResult.setText("Type wifi time sync");
Intent sendIntent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动
sendIntent.setAction(SendService.ACTION_SEND_JSON);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。
sendIntent.putExtra(SendService.EXTRAS_HOST,"192.168.1.85");//将执行服务活动的限制,IP地址,端口号,还有
sendIntent.putExtra(SendService.EXTRAS_PORT,"4040");
sendIntent.putExtra(SendService.EXTRAS_JSON,messageWifiTimeSync);
startService(sendIntent);//现在真正执行服务

}else {
tvPingResult.setText("Result is from else id:"+String.valueOf(result.getId())+" "+type);
}
} else {
tvPingResult.setText("Result is from else id:"+String.valueOf(result.getId()));
}
Expand Down Expand Up @@ -548,6 +576,16 @@ public void onReceive(Context context, Intent intent) {
et_lon.setText("106.295269");
et_direction.setText("-74.463");
break;
case MainActivity.ACTION_LTE_TIME_SYNC:
long sendTimeStamp = intent.getExtras().getLong("sendTimeStamp");
long receiverTimeStamp = intent.getExtras().getLong("receiverTimeStamp");
long timeStamp = intent.getExtras().getLong("timeStamp");
long delay = (sendTimeStamp - receiverTimeStamp) /2;
long serverTimeStamp = timeStamp + delay;
setServerStamp(serverTimeStamp);
setBaseStamp(receiverTimeStamp);
tvPingResult.setText("Time Sync success");
break;
case MainActivity.ACTION_SEND_MESSAGE:

break;
Expand Down Expand Up @@ -641,6 +679,12 @@ private void addMessageToDB(){
startService(intent);
}


/************************************
*
* 发送Message, 给RSU, 给云服务器,给连接AP的PC
*
********************************/
private void sendMessage(){
String id = et_id.getEditableText().toString();//将输入的格式统一化为string型
String timeStamp = et_timeStamp.getEditableText().toString();
Expand Down Expand Up @@ -679,6 +723,127 @@ private void sendMessage(){
intent.putExtra(SendService.EXTRAS_JSON,message);
startService(intent);//现在真正执行服务

}
private void sendMessageToLTE(){
String id = et_id.getEditableText().toString();//将输入的格式统一化为string型
String timeStamp = et_timeStamp.getEditableText().toString();
String speed = et_speed.getEditableText().toString();
String direction = et_direction.getEditableText().toString();
String lat = et_lat.getEditableText().toString();
String lon = et_lon.getEditableText().toString();
String ace = et_ace.getEditableText().toString();

Message message = new Message();
message.setId(Integer.valueOf(id));//将上面得到的字符串类型的数据转化为具体相对应的类型
message.setTimeStamp(Long.valueOf(timeStamp));
if (!speed.equals("")){
message.setSpeed(Float.valueOf(speed));
}
if (!direction.equals("")){
message.setDirection(Float.valueOf(direction));
}
if (!lat.equals("")){
message.setLat(Double.valueOf(lat));
}
if (!lon.equals("")){
message.setLon(Double.valueOf(lon));
}
if (!ace.equals("")){
message.setAce(Double.valueOf(ace));
}
message.setType(Message.TYPE_MESSAGE); //普通消息
//现在Message里面已经有对应格式的数据,接下来是将数据转化为json格式。


Intent intent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动
intent.setAction(SendService.ACTION_SEND_TCP);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。
intent.putExtra(SendService.EXTRAS_HOST,"120.78.167.211");//将执行服务活动的限制,IP地址,端口号,还有
//intent.putExtra(SendService.EXTRAS_HOST,"192.168.123.186");//将执行服务活动的限制,IP地址,端口号,还有
intent.putExtra(SendService.EXTRAS_PORT,"4040");
intent.putExtra(SendService.EXTRAS_JSON,message);
startService(intent);//现在真正执行服务

}

private void sendMessageToLTETimeSync(){
String id = et_id.getEditableText().toString();//将输入的格式统一化为string型
String timeStamp = et_timeStamp.getEditableText().toString();
String speed = et_speed.getEditableText().toString();
String direction = et_direction.getEditableText().toString();
String lat = et_lat.getEditableText().toString();
String lon = et_lon.getEditableText().toString();
String ace = et_ace.getEditableText().toString();

Message message = new Message();
message.setId(Integer.valueOf(id));//将上面得到的字符串类型的数据转化为具体相对应的类型
message.setTimeStamp(Long.valueOf(timeStamp));
if (!speed.equals("")){
message.setSpeed(Float.valueOf(speed));
}
if (!direction.equals("")){
message.setDirection(Float.valueOf(direction));
}
if (!lat.equals("")){
message.setLat(Double.valueOf(lat));
}
if (!lon.equals("")){
message.setLon(Double.valueOf(lon));
}
if (!ace.equals("")){
message.setAce(Double.valueOf(ace));
}
message.setType(Message.TYPE_TIME_SYNC_MESSAGE); //普通消息
//现在Message里面已经有对应格式的数据,接下来是将数据转化为json格式。


Intent intent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动
intent.setAction(SendService.ACTION_SEND_TCP);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。
intent.putExtra(SendService.EXTRAS_HOST,"120.78.167.211");//将执行服务活动的限制,IP地址,端口号,还有
//intent.putExtra(SendService.EXTRAS_HOST,"192.168.123.186");//将执行服务活动的限制,IP地址,端口号,还有
intent.putExtra(SendService.EXTRAS_PORT,"4040");
intent.putExtra(SendService.EXTRAS_JSON,message);
startService(intent);//现在真正执行服务

}

private void sendMessageToWIFI(){
String id = et_id.getEditableText().toString();//将输入的格式统一化为string型
String timeStamp = String.valueOf(System.currentTimeMillis());
String speed = et_speed.getEditableText().toString();
String direction = et_direction.getEditableText().toString();
String lat = et_lat.getEditableText().toString();
String lon = et_lon.getEditableText().toString();
String ace = et_ace.getEditableText().toString();

Message message = new Message();
message.setId(Integer.valueOf(id));//将上面得到的字符串类型的数据转化为具体相对应的类型
message.setTimeStamp(Long.valueOf(timeStamp));
if (!speed.equals("")){
message.setSpeed(Float.valueOf(speed));
}
if (!direction.equals("")){
message.setDirection(Float.valueOf(direction));
}
if (!lat.equals("")){
message.setLat(Double.valueOf(lat));
}
if (!lon.equals("")){
message.setLon(Double.valueOf(lon));
}
if (!ace.equals("")){
message.setAce(Double.valueOf(ace));
}
message.setType(Message.TYPE_WIFI_MESSAGE); //普通消息
//现在Message里面已经有对应格式的数据,接下来是将数据转化为json格式。


Intent intent = new Intent(MainActivity.this, SendService.class);//跳转到SendService活动
intent.setAction(SendService.ACTION_SEND_JSON);//将执行服务的活动,现在并不执行,只是告诉android,我们要调用哪个功能。
intent.putExtra(SendService.EXTRAS_HOST,"192.168.1.85");//将执行服务活动的限制,IP地址,端口号,还有
intent.putExtra(SendService.EXTRAS_PORT,"4040");
intent.putExtra(SendService.EXTRAS_JSON,message);
startService(intent);//现在真正执行服务

}

/************************************
Expand Down Expand Up @@ -757,7 +922,9 @@ public void run() {
stop = false;
while (!stop){
try {
sendMessage();
// sendMessage();
// sendMessageToWIFI();
sendMessageToLTETimeSync();
addMessageToDB();
sleep(FREQUENCY);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public DBTool(){
super("DBTool");
}
public static final String TAG = "DBTool";
public static final String DATABASE_PATH = "/mnt/sdcard/Downloads/";
public static final String DATABASE_PATH = "/mnt/sdcard/Download/";
public static final String DATABASE_FLIENAME = "databasebackup.db";

public static final String ACTION_ADD = "ACTION_ADD";
Expand Down
Loading

0 comments on commit d9c1fa8

Please sign in to comment.