3.Android 行人监控系统
请选手自行创建 Android 项目,通过云平台读取传感器或控制执行器,实现行人闯红
灯监控系统功能。程序界面如下
任务要求:
程序通过云平台,采集传感器值和控制执行器状态。
➢ 开机默认显示绿灯,可手动切换红、绿灯按钮。
➢ TCP 模式连接串口服务器 COM3 口。
➢ 红灯按钮时,三色灯仅红灯点亮,其他灯熄灭;同时通过串口服务器 COM3 口控制
“C-1”ZigBee 黑色开发板 D4 灯亮,D3 灯灭;
➢ 切换到绿灯按钮时,三色灯仅绿灯点亮,其他灯熄灭;同时通过串口服务器 COM3 口
控制“C-1”ZigBee 黑色开发板 D4 灯灭,D3 灯亮;
➢ 绿灯状态下,显示绿灯放行图,此时红外对射即使报警也不变;
➢ 红灯状态下,且红外对射不报警,显示红灯禁止图,报警灯熄灭;红外对射状态变
化同时通过串口服务器 COM3 口控制“C-1”ZigBee 黑色开发板 D6 灯灭;
➢ 红灯状态下,且红外对射报警,显示闯红灯图,报警灯报警;红外对射状态变化同
时通过串口服务器 COM3 口控制“C-1”ZigBee 黑色开发板 D6 灯亮;
➢ 退出 app 程序前,将红灯、绿灯、报警灯都熄灭,通过串口服务器 COM3 口控制
“C-1”ZigBee 黑色开发板 D4,D3,D6 灯皆灭。
完成以上任务后请做以下步骤:
◆ 开发完成后,请将程序以“行人监控”命名,发布到移动互联终端,没发布不得分,
移动互联终端开机,连接好网络。
◆ 把工程源码另存为“行人监控系统源码.rar”
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
new Thread(new Runnable() {
@Override
public void run() {
while (isStart){
SystemClock.sleep(1000);
new StartThread().start();
}
}
}).start();
}
StartThread
class StartThread extends Thread{
@Override
public void run() {
super.run();
if (login){
try {
login(account,pass);
login = false;
sw(device_gate,green,"1");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
HashMap<String,String> data = new HashMap<>(check(device_gate));
red = data.get("m_infrared");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Message msg = new Message();
msg = Message.obtain();
System.out.println(red);
System.out.println(flag_alarm);
if (flag_alarm == 1 & red.equals("true")){
try {
sw(device_gate,alarm,"1");
alarm_flag = true;
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
if (flag_alarm == 0 & red.equals("false")){
try {
sw(device_gate,alarm,"0");
alarm_flag = false;
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
handler.sendMessage(msg);
}
}
Handler
Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (alarm_flag){
iv_alarm.setBackgroundResource(R.drawable.pic_alarm_on);
ll_2.setBackgroundResource(R.drawable.lines_red);
bt_alarm.setBackgroundResource(R.drawable.btn_switch_on);
}
else {
iv_alarm.setBackgroundResource(R.drawable.pic_alarm_off);
bt_alarm.setBackgroundResource(R.drawable.btn_switch_off);
ll_2.setBackground(Drawable.createFromPath("#330000"));
}
switch (msg.what){
case 1:
bt_lamp.setBackgroundResource(R.drawable.btn_switch_green);
ll_1.setBackgroundResource(R.drawable.pic_bg_green);
iv_arrow.setBackgroundResource(R.drawable.pic_arrow_green);
break;
case 0:
bt_lamp.setBackgroundResource(R.drawable.btn_switch_red);
ll_1.setBackgroundResource(R.drawable.pic_bg_red);
iv_arrow.setBackgroundResource(R.drawable.pic_arrow_red);
break;
}
}
};
Init
private void Init() {
getSupportActionBar().hide();
ll_2 = findViewById(R.id.ll_2);
ll_1 = (LinearLayout) findViewById(R.id.ll_1);
iv_person = (ImageView) findViewById(R.id.iv_person);
iv_arrow = (ImageView) findViewById(R.id.iv_arrow);
bt_lamp = (Button) findViewById(R.id.bt_lamp);
bt_alarm = (Button) findViewById(R.id.bt_alarm);
iv_alarm = findViewById(R.id.iv_alarm);
ll_2.setBackground(Drawable.createFromPath("#330000"));
}
连接新大陆云平台操作
public boolean login(String loginname, String password) throws RuntimeException, IOException, JSONException {
String url = "http://api.nlecloud.com/users/login";
String json = "{'Account':'"+loginname+"','Password':'"+password+"','IsRememberMe':'true'}";
String response = co.post(url, json);
JSONObject jsonObj = new JSONObject(response);
String status = jsonObj.getString("Status");
if(status.equals("0")) {
co.accessToken = jsonObj.getJSONObject("ResultObj").getString("AccessToken");
return true;
}else {
throw new RuntimeException("连接云平台失败!");
}
}
public Map<String,String> check(String devIds) throws IOException, JSONException {
String url = "http://api.nlecloud.com/Devices/Datas" + "?devIds=" +devIds;
String content = co.get(url);
System.out.println(content);
JSONObject jsonObject = new JSONObject(content);
System.out.println(jsonObject);
String status = jsonObject.getString("Status");
if(!status.equals("0")) {
throw new RuntimeException("您的请求操作失败,请检查您传入的参数");
}
HashMap<String,String> data = new HashMap<>();
JSONArray deviceAry = jsonObject.getJSONArray("ResultObj");
JSONObject deviceObj = deviceAry.getJSONObject(0);
JSONArray sensorAry = deviceObj.getJSONArray("Datas");
int sensorSize = sensorAry.length();
for (int j=0;j<sensorSize;j++){
JSONObject sensorObj = sensorAry.getJSONObject(j);
String apiTag =sensorObj.getString("ApiTag");
String value =sensorObj.getString("Value");
data.put(apiTag,value);
}
return data;
}
public String sw(String deviceId,String apiTag,String json) throws IOException, JSONException {
String url = "http://api.nlecloud.com/Cmds"+"?deviceId="+deviceId +"&apiTag=" +apiTag;
String content = co.post(url, json);
JSONObject jsonObject = new JSONObject(content);
String status = jsonObject.getString("Status");
System.out.println(status);
return status;
}
swThread
class swThread extends Thread{
@Override
public void run() {
super.run();
if (flag_alarm == 0){
try {
sw(device_gate,green,"1");
sw(device_gate,hong,"0");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
else{
try {
sw(device_gate,hong,"1");
sw(device_gate,green,"0");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
bt_light
public void bt_light(View view){
Message msg = new Message();
msg = Message.obtain();
if (flag_alarm == 1){
flag_alarm = 0;
msg.what = 1;
}
else{
flag_alarm = 1;
msg.what = 0;
}
new swThread().start();
handler.sendMessage(msg);
}
onDestroy
@Override
protected void onDestroy() {
isStart =false;
try {
sw(device_gate,green,"0");
sw(device_gate,hong,"0");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
super.onDestroy();
}
layout代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_1"
android:background="@drawable/pic_bg_green"
tools:context=".MainActivity">
<include layout="@layout/title"></include>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:orientation="horizontal"
android:layout_weight="2"
android:id="@+id/ll_2"
android:background="@drawable/lines_red"
android:gravity="bottom">
<ImageView
android:id="@+id/iv_person"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@drawable/person_blue"></ImageView>
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="300dp"
android:layout_height="150dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="100dp"
android:background="@drawable/pic_arrow_green">
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="@drawable/bg_tab">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="14dp"
android:text="红路灯切换:">
</TextView>
<Button
android:id="@+id/bt_lamp"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:onClick="bt_light"
android:background="@drawable/btn_switch_green">
</Button>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:text="报警灯:">
</TextView>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/pic_alarm_off"
android:id="@+id/iv_alarm">
</ImageView>
<Button
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:id="@+id/bt_alarm"
android:background="@drawable/btn_switch_off">
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
页面title代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#0099CC"
android:layout_height="30dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="@drawable/icon_title_red_light">
</ImageView>
</LinearLayout>
任务还有要求: 通过串口服务器 COM3 口控制
“C-1”ZigBee 黑色开发板 D4 灯亮,D3 灯灭,以前写过这个,但是现在找不到,赖的复现了,串口控制是通过新大陆的包去实现,感兴趣可以去试一下。