ManagerCarActivity.java 14.4 KB
package com.huaheng.robot.task;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.huaheng.robot.R;
import com.huaheng.robot.bean.ChargingPileBean;
import com.huaheng.robot.bean.EquipmentBean;
import com.huaheng.robot.bean.LocationBean;
import com.huaheng.robot.bean.RGVBean;
import com.huaheng.robot.bean.TaskBean;
import com.huaheng.robot.https.HttpInterface2;
import com.huaheng.robot.https.Subscribers.ProgressSubscriber;
import com.huaheng.robot.https.Subscribers.SubscriberOnNextListener;
import com.huaheng.robot.util.CommonActivity;
import com.huaheng.robot.util.WMSLog;
import com.huaheng.robot.util.WMSUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ManagerCarActivity extends CommonActivity {

    private final int WC= ViewGroup.LayoutParams.WRAP_CONTENT;
    private final int FP= ViewGroup.LayoutParams.WRAP_CONTENT;
    private Context mContext;
    private List<EquipmentBean> equipmentBeans;
    private List<LocationBean> locationBeans;
    private List<ChargingPileBean> chargingPileBeans;
    private EquipmentBean mChooseEquipmentBean;
    private int mRow = 0;
    private MyHandler myHandler = new MyHandler();
    private int chooseTask = 0;

    @Override
    protected void initActivityOnCreate(Bundle savedInstanceState) {
        super.initActivityOnCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_car);

        ButterKnife.bind(this);
        mContext = this;
        setTitle(getString(R.string.manager_car));
        getAllEquipments();
    }

    @Override
    protected void onPause() {
        super.onPause();
        myHandler.removeMessages(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        myHandler.removeMessages(0);
        myHandler.sendEmptyMessageDelayed(0, 10 * 1000);
    }

    private void createTable() {
        TableLayout tableLayout = (TableLayout) findViewById(R.id.car_tableLayout);
        tableLayout.setStretchAllColumns(true);     //设置指定列号的列属性为Stretchable
        if(equipmentBeans == null || equipmentBeans.size() ==0) {
            tableLayout.setVisibility(View.GONE);
            return;
        }
        tableLayout.setVisibility(View.VISIBLE);
        if(mRow != 0) {
            tableLayout.removeViews(1, mRow);
        }
        int size = equipmentBeans.size();
        mRow = size;
        for (int row = 0; row < size; row++) {     //产生的行数
            TableRow tableRow = new TableRow(ManagerCarActivity.this);
            tableRow.setBackgroundColor(Color.rgb(222, 220, 210));      //设置表格背景
            final EquipmentBean equipmentBean = equipmentBeans.get(row);
            int id = equipmentBean.getId();
            addNTextView(tableRow, String.valueOf(id));
            String name = equipmentBean.getName();
            addNTextView(tableRow, name);
            int lineId = equipmentBean.getLineId();
            addNTextView(tableRow, String.valueOf(lineId));
            int fromLocationId = equipmentBean.getFromSequence();
            addNTextView(tableRow, String.valueOf(fromLocationId));
            int toLocationId = equipmentBean.getToSequence();
            addNTextView(tableRow, String.valueOf(toLocationId));
            int electric = equipmentBean.getLimitElectric();
            addNTextView(tableRow, String.valueOf(electric));
            int chargingpileId = equipmentBean.getChargingpileId();
            addNTextView(tableRow, String.valueOf(chargingpileId));
            tableLayout.addView(tableRow, new LinearLayout.LayoutParams(FP, WC));
            final int finalRow = row;
            tableRow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showChooseTaskDialog(finalRow, equipmentBean.getId());
                }
            });
        }
    }

    private void addNTextView(TableRow tableRow, String view) {
        TextView tv = new TextView(ManagerCarActivity.this);
        tv.setBackgroundResource(R.drawable.shape);     //设置背景
        tv.setGravity(Gravity.CENTER);
        tv.setText(view);
        tableRow.addView(tv);
    }

    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            getAllEquipments();
        }
    }

    private void showChooseTaskDialog(int row, int id){
        chooseTask = id;
        mChooseEquipmentBean = equipmentBeans.get(row);
        List<String> lists = new ArrayList<>();
        lists.add(mContext.getString(R.string.assign_charging_task));
        lists.add(mContext.getString(R.string.assign_work_task));
        lists.add(mContext.getString(R.string.force_complete_task));
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(ManagerCarActivity.this);
        normalDialog.setTitle(R.string.choose_task);
        String[] strs1= lists.toArray(new String[lists.size()]);
        normalDialog.setItems(strs1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        showChargingToDialog();
                        break;
                    case 1:
                        showWorkFromDialog();
                        break;
                    case 2:
                        forceCompleteTask(chooseTask);
                        break;
                }
            }
        });
        normalDialog.show();
    }

    private void showWorkFromDialog(){
        List<String> lists = new ArrayList<>();
        for(LocationBean locationBean: locationBeans) {
            lists.add(String.valueOf(locationBean.getSequence()));
        }
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(ManagerCarActivity.this);
        normalDialog.setTitle(R.string.fromIsLand);
        String[] strs1= lists.toArray(new String[lists.size()]);
        normalDialog.setItems(strs1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LocationBean locationBean = locationBeans.get(which);
                locationBeans.remove(locationBean);
                showWorkToDialog(locationBeans, locationBean);
            }
        });
        normalDialog.show();
    }

    private void showWorkToDialog(final List<LocationBean> locationBeanList, final LocationBean fromLocationBean){
        List<String> lists = new ArrayList<>();
        for(LocationBean locationBean: locationBeanList) {
            lists.add(String.valueOf(locationBean.getSequence()));
        }
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(ManagerCarActivity.this);
        normalDialog.setTitle(R.string.toIsLand);
        String[] strs1= lists.toArray(new String[lists.size()]);
        normalDialog.setItems(strs1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LocationBean toLocationBean = locationBeanList.get(which);
                showWorkDialog(mChooseEquipmentBean, fromLocationBean, toLocationBean);
            }
        });
        normalDialog.show();
    }

    private void showWorkDialog(EquipmentBean equipmentBean, final LocationBean fromLocationBean, final LocationBean toLocationBean){
        String str = getResources().getString(R.string.work_message);
        String message = String.format(str, fromLocationBean.getSequence(), toLocationBean.getSequence());
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(mContext);
        normalDialog.setTitle(R.string.transfer_title);
        normalDialog.setMessage(message);
        normalDialog.setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        createWorkTask(mChooseEquipmentBean, fromLocationBean, toLocationBean);
                    }
                });
        normalDialog.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        normalDialog.show();
    }

    private void showChargingToDialog(){
        List<String> lists = new ArrayList<>();
        for(ChargingPileBean chargingPileBean: chargingPileBeans) {
            lists.add(String.valueOf(chargingPileBean.getName()));
        }
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(ManagerCarActivity.this);
        normalDialog.setTitle(R.string.toIsLand);
        String[] strs1= lists.toArray(new String[lists.size()]);
        normalDialog.setItems(strs1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ChargingPileBean chargingPileBean = chargingPileBeans.get(which);
                showChargingDialog(mChooseEquipmentBean, chargingPileBean);
            }
        });
        normalDialog.show();
    }

    private void showChargingDialog(EquipmentBean equipmentBean, final ChargingPileBean chargingPileBean){
        String str = getResources().getString(R.string.charging_message);
        String message = String.format(str, chargingPileBean.getName());
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(mContext);
        normalDialog.setTitle(R.string.charging_title);
        normalDialog.setMessage(message);
        normalDialog.setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        createChargingTask(mChooseEquipmentBean, chargingPileBean);
                    }
                });
        normalDialog.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        normalDialog.show();
    }

    private void getAllEquipments() {
        HttpInterface2.getInsstance().getAllEquipments(new ProgressSubscriber<List<EquipmentBean>>(this, allEquipmentsListener));
    }

    SubscriberOnNextListener allEquipmentsListener = new SubscriberOnNextListener<List<EquipmentBean>>() {

        @Override
        public void onNext(List<EquipmentBean>  equipmentBeanList) {
            equipmentBeans = equipmentBeanList;
            if(equipmentBeans == null || equipmentBeans.size() == 0) {
                WMSUtils.showShort(ManagerCarActivity.this.getString(R.string.no_car));
            }
            createTable();
            myHandler.removeMessages(0);
            myHandler.sendEmptyMessageDelayed(0, 10 * 1000);
            if(locationBeans == null) {
                getAllLocation();
            }
            if(chargingPileBeans == null) {
                getAllChargingPiles();
            }
        }

        @Override
        public void onError(String str) {
            myHandler.removeMessages(0);
            myHandler.sendEmptyMessageDelayed(0, 10 * 1000);
        }
    };

    private void getAllLocation() {
        HttpInterface2.getInsstance().getAllLocation(new ProgressSubscriber<List<LocationBean>>(this, allLocationListener));
    }

    SubscriberOnNextListener allLocationListener = new SubscriberOnNextListener<List<LocationBean>>() {

        @Override
        public void onNext(List<LocationBean>  locationBeanList) {
            locationBeans = locationBeanList;
        }

        @Override
        public void onError(String str) {

        }
    };

    private void getAllChargingPiles() {
        HttpInterface2.getInsstance().getAllChargingPiles(new ProgressSubscriber<List<ChargingPileBean>>(this, chargingPileListener));
    }

    SubscriberOnNextListener chargingPileListener = new SubscriberOnNextListener<List<ChargingPileBean>>() {

        @Override
        public void onNext(List<ChargingPileBean>  chargingPileBeanList) {
            chargingPileBeans = chargingPileBeanList;

        }

        @Override
        public void onError(String str) {

        }
    };

    private void forceCompleteTask(int id) {
        HttpInterface2.getInsstance().forceCompleteTask(new ProgressSubscriber<String>(this, forceCompleteListener), id);
    }

    SubscriberOnNextListener forceCompleteListener = new SubscriberOnNextListener<String>() {

        @Override
        public void onNext(String msg) {
            WMSUtils.showShort(msg);
        }

        @Override
        public void onError(String str) {

        }
    };

    private void createWorkTask(EquipmentBean equipmentBean, LocationBean fromLocation, LocationBean toLocation) {
        HttpInterface2.getInsstance().createWorkTask(new ProgressSubscriber<TaskBean>(this, createWorkTaskListener), equipmentBean, fromLocation, toLocation);
    }

    SubscriberOnNextListener createWorkTaskListener = new SubscriberOnNextListener<TaskBean>() {

        @Override
        public void onNext(TaskBean msg) {
            WMSUtils.showShort(mContext.getString(R.string.success_work_task));
        }

        @Override
        public void onError(String str) {

        }
    };

    private void createChargingTask(EquipmentBean equipmentBean, ChargingPileBean chargingPileBean) {
        HttpInterface2.getInsstance().createChargingTask(new ProgressSubscriber<TaskBean>(this, createChargingTaskListener), equipmentBean, chargingPileBean);
    }

    SubscriberOnNextListener createChargingTaskListener = new SubscriberOnNextListener<TaskBean>() {

        @Override
        public void onNext(TaskBean msg) {
            WMSUtils.showShort(mContext.getString(R.string.success_charging_task));
        }

        @Override
        public void onError(String str) {

        }
    };
}