智能农业项目简介

智能农业项目总结

首先,先在build.gradle添加项目所需要的所有的数据包

compile 'com.makeramen:roundedimageview:2.2.1'
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'

一.创建页面布局,实现界面转换(举例首页布局)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    tools:context="android.coolweather2.com.agriculturedemo.fragment.HomePageFragment">

    <TextView
        android:id="@+id/home_page_top_lin"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_horizontal"
        android:text="智能农业"
        android:textColor="#24bd3b"
        android:textSize="30dp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/main_bottom_lin"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/home_page_top_lin"
        android:overScrollMode="never"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/home_page_vp"
                android:layout_width="match_parent"
                android:layout_height="150dp">
            android.support.v4.view.ViewPager>

            <View
                android:layout_width="match_parent"
                android:layout_height="20dp" />

            <include layout="@layout/co2" />

            <View
                android:layout_width="match_parent"
                android:layout_height="20dp" />

            <include layout="@layout/light" />

            <View
                android:layout_width="match_parent"
                android:layout_height="20dp" />

            <include layout="@layout/soil" />

            <View
                android:layout_width="match_parent"
                android:layout_height="20dp" />

            <include layout="@layout/air" />

        LinearLayout>
    ScrollView>
RelativeLayout>

CO2界面代码(将方形图片转换成圆形图片)


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#716c6c"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.makeramen.roundedimageview.RoundedImageView
            app:riv_oval="true"
            android:id="@+id/co2_img"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:src="@mipmap/co2" />

        <TextView
            android:layout_toLeftOf="@+id/co2_warn_img"
            android:id="@+id/co2_concentation_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/co2_img"
            android:layout_margin="5dp"
            android:layout_toRightOf="@+id/co2_img"
            android:text="CO2浓度"
            android:textColor="#100909"
            android:textSize="25sp" />

        <RelativeLayout
            android:layout_toLeftOf="@+id/co2_right_img"
            android:id="@+id/co2_relative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/co2_concentation_tv"
            android:layout_margin="5dp"
            android:layout_toRightOf="@+id/co2_img">

            <TextView
                android:id="@+id/co2_co2_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text="CO2:"
                android:textColor="#100909"
                android:textSize="25sp" />

            <TextView
                android:id="@+id/co2_values_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_toRightOf="@+id/co2_co2_tv"
                android:gravity="center_vertical"
                android:text="200"
                android:textColor="#e80c0c"
                android:textSize="25sp" />
        RelativeLayout>

        <RelativeLayout
            android:layout_toLeftOf="@+id/co2_right_img"
            android:layout_alignBottom="@+id/co2_img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/co2_relative"
            android:layout_margin="5dp"
            android:layout_toRightOf="@+id/co2_img">

            <TextView
                android:id="@+id/co2_set_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text="设定值:"
                android:textColor="#1fa433"
                android:textSize="15sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_toRightOf="@+id/co2_set_tv"
                android:gravity="center_vertical"
                android:text="200"
                android:textColor="#605858"
                android:textSize="15sp" />
        RelativeLayout>

        <ImageView
            android:id="@+id/co2_right_img"
            android:layout_width="20dp"
            android:src="@mipmap/right"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_height="60dp" />

        <ImageView
            android:id="@+id/co2_warn_img"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@mipmap/p2"
            android:layout_width="50dp"
            android:layout_height="20dp" />
    RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#716c6c"/>
LinearLayout>

图片展示

智能农业项目简介_第1张图片

二.Java文件(fragment,gson,util)

实现网络请求,获取信息并进行解析得到实时信息,放到页面所属位置
获取各个开关的状态,并获取沙盘的数据,可以进行控制(操作界面中各个选项,跳转到具体设施操作界面,加载界面时获取服务器中各个设施状态,判断设施状态显示界面中按钮状态)
点击按钮控制,开关图片需要变化,需要用标识位来显示,转到其他页面,也不需要重新更新数据,依然可以获取

公共类与全局变量的使用

本项目需要网络请求很多次,为减轻代码量,可以写一个公共类,在里面写方法,然后进行调用。

使用OKHttp

创建OKHttp类

public class OkHttp {
//定义静态url路径
    private static final String BASE_URL = "http://"+ StartActivity.URL+":8890/type/jason/action/";
    private static OkHttpClient client = new OkHttpClient();
    public static final MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");

    public static void sendOkHttpRequest(String url, okhttp3.Callback callback) {
        Request request = new Request.Builder().url(getAbsoluteUrl(url)).build();
        client.newCall(request).enqueue(callback);
    }
//post方法,获取JSON格式的数据
    public static void postJSONByOkhttp(String url, String json,int number, Callback callback) {
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put(json,number);
            RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString());
            Request request = new Request.Builder().url(getAbsoluteUrl(url)).post(requestBody).build();
            client.newCall(request).enqueue(callback);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    //变换的URL
    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }
}

控制开关功能

首先设置监听,跳转到开关界面,接着进行点击图片实现控制开关,并改变图片的内容

public class AirActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView openFanImg;
    private ImageView openLightImg;
    private ImageView openPumpImg;
    private ImageView openAlertImg;
    private TextView airTemp;
    private TextView airWet;

    private  void modifierBlowerStatus() {
        if (Status.Blower == 0) {
            openFanImg.setImageResource(R.mipmap.dakaifengshan);
            DeviceControl.Result = 0;
        } else if (Status.Blower == 1) {
            openFanImg.setImageResource(R.mipmap.dakaifengshan2);
            DeviceControl.Result = 1;
        }
    }

    private  void modifierBuzzerStatus() {
        //这时候全局变量就有作用了
        if (Status.Buzzer == 0) {
            openAlertImg.setImageResource(R.mipmap.dakaibaojing);
            //通过状态来改变标志位,用标志位来改变以后再这个页面图片的转换
            //标志位依然是全局的变量,会在实现设备开关的类进行定义
            DeviceControl.alertResult = 0;
        } else if (Status.Buzzer == 1) {
            openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
            DeviceControl.alertResult = 1;
        }
    }

    private  void modifierLightStatus() {
        if (Status.Roadlamp == 0) {
            openLightImg.setImageResource(R.mipmap.dakaiguangzhao);
            DeviceControl.lightResult = 0;
        } else if (Status.Roadlamp == 1) {
            openLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
            DeviceControl.lightResult = 1;
        }
    }

    private  void modifierPumpStatus() {
        if (Status.WaterPump == 0) {
            openPumpImg.setImageResource(R.mipmap.dakaishui);
            DeviceControl.pumpResult = 0;
        } else if (Status.WaterPump == 1) {
            openPumpImg.setImageResource(R.mipmap.dakaishui2);
            DeviceControl.pumpResult = 1;
        }
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_air);

        bangID();
        //下面四个方法都是判断沙盘上各个控件的状态,如果有开的,就需要改变图片
        modifierBlowerStatus();
        modifierBuzzerStatus();
        modifierLightStatus();
        modifierPumpStatus();

        airTemp.setText(MainActivity.airTemperature+"");
        airWet.setText(MainActivity.airHumidity+"");

    }

    private void bangID() {
        openFanImg = findViewById(R.id.air_fan_img);
        openLightImg = findViewById(R.id.air_light_img);
        openPumpImg = findViewById(R.id.air_pump_img);
        openAlertImg = findViewById(R.id.air_alert_img);
        airTemp = findViewById(R.id.air_temp_values);
        airWet = findViewById(R.id.air_wet_values);

        openFanImg.setOnClickListener(this);
        openLightImg.setOnClickListener(this);
        openPumpImg.setOnClickListener(this);
        openAlertImg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.air_alert_img:
                if (DeviceControl.alertResult == 0) {
                    openAlertImg.setImageResource(R.mipmap.dakaibaojing2);
                    //调用开报警的方法
                    DeviceControl.openAlert(MainActivity.basicUrl, AirActivity.this);
                    Toast.makeText(this, "开警报", Toast.LENGTH_SHORT).show();
                } else if (DeviceControl.alertResult == 1) {
                    openAlertImg.setImageResource(R.mipmap.dakaibaojing);
                    Intent intent = getIntent();
                    String basicUrl = intent.getStringExtra("basicUrl");
                    //调用关报警的方法
                    DeviceControl.closeAlert(basicUrl, AirActivity.this);
                    Toast.makeText(this, "关警报", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.air_fan_img:
                if (DeviceControl.Result == 0) {
                    openFanImg.setImageResource(R.mipmap.dakaifengshan2);
                    DeviceControl.openFan(MainActivity.basicUrl, AirActivity.this);
                    Toast.makeText(this, "开风扇", Toast.LENGTH_SHORT).show();
                } else if (DeviceControl.Result == 1) {
                    openFanImg.setImageResource(R.mipmap.dakaifengshan);
                    DeviceControl.closeFan(MainActivity.basicUrl, AirActivity.this);
                    Toast.makeText(this, "关风扇", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.air_light_img:
                if(DeviceControl.lightResult==0){
                    openLightImg.setImageResource(R.mipmap.dakaiguangzhao2);
                    DeviceControl.openLight(MainActivity.basicUrl,AirActivity.this);
                    Toast.makeText(this, "开灯", Toast.LENGTH_SHORT).show();
                }else if( DeviceControl.lightResult==1){
                    openLightImg.setImageResource(R.mipmap.dakaiguangzhao);
                    DeviceControl.closeLight(MainActivity.basicUrl,AirActivity.this);
                    Toast.makeText(this, "关灯", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.air_pump_img:
                if (DeviceControl.pumpResult == 0) {
                    openPumpImg.setImageResource(R.mipmap.dakaishui2);
                    DeviceControl.openPump(MainActivity.basicUrl, AirActivity.this);
                    Toast.makeText(this, "开水泵", Toast.LENGTH_SHORT).show();
                } else if (DeviceControl.pumpResult == 1) {
                    openPumpImg.setImageResource(R.mipmap.dakaishui);
                    DeviceControl.closePump(MainActivity.basicUrl, AirActivity.this);
                    Toast.makeText(this, "关水泵", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }
}

智能农业项目简介_第2张图片

你可能感兴趣的:(智能农业项目简介)