ManagerCarActivity.java 17.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
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.EquipmentPropsBean;
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.Constant;
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;
import rx.Subscriber;

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);
            List<EquipmentPropsBean> equipmentPropsBeans = equipmentBean.getEquipmentProps();
            String status = null, exception = null, electric = null, location = null, taskNo = null;
            for(EquipmentPropsBean equipmentPropsBean : equipmentPropsBeans) {
                if(equipmentPropsBean.getEquipmentTypePropTemplateCode().equals("Status")) {
                    status = equipmentPropsBean.getValue();
                } else if(equipmentPropsBean.getEquipmentTypePropTemplateCode().equals("ExceptionCode")) {
                    exception = equipmentPropsBean.getValue();
                } else if(equipmentPropsBean.getEquipmentTypePropTemplateCode().equals("Electric")) {
                    electric = equipmentPropsBean.getValue();
                } else if(equipmentPropsBean.getEquipmentTypePropTemplateCode().equals("Location")) {
                    location = equipmentPropsBean.getValue();
                } else if(equipmentPropsBean.getEquipmentTypePropTemplateCode().equals("TaskNum")) {
                    taskNo = equipmentPropsBean.getValue();
                }
            }
            String name = equipmentBean.getName();
            addNTextView(tableRow, name);
            int lineId = equipmentBean.getLineId();
            addNTextView(tableRow, String.valueOf(lineId));
            addNTextView(tableRow, taskNo);
            addNTextView(tableRow, location);
            addNTextView(tableRow, getElectric(electric));
            addNTextView(tableRow, getException(Integer.parseInt(exception)));
            addNTextView(tableRow, getStatus(Integer.parseInt(status)));
            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.getCode()));
        }
        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);
                List<LocationBean> locationBeanList = new ArrayList<>();
                locationBeanList.addAll(locationBeans);
                locationBeanList.remove(locationBean);
                showWorkToDialog(locationBeanList, 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.getCode()));
        }
        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 String getException(int carStatus) {
        switch (carStatus) {
            case Constant.ERROR_NO:
                return mContext.getString(R.string.error_no);
            case Constant.ERROR_RGV:
                return mContext.getString(R.string.error_rgv);
            case Constant.ERROR_NO_CAR:
                return mContext.getString(R.string.error_no_car);
            case Constant.ERROR_GET_NOGOOD:
                return mContext.getString(R.string.error_get_nogood);
            case Constant.ERROR_PUT_NOGOOD:
                return mContext.getString(R.string.error_put_nogood);
            case Constant.ERROR_CHARGING:
                return mContext.getString(R.string.error_charging);
            case Constant.ERROR_ASSIGN_TASK:
                return mContext.getString(R.string.error_assigning_task);
        }
        return null;
    }

    private String getStatus(int carStatus) {
        switch (carStatus) {
            case Constant.STATUS_MAN:
                return mContext.getString(R.string.status_man);
            case Constant.STATUS_NOGOOD:
                return mContext.getString(R.string.status_nogood);
            case Constant.STATUS_GETGOOD:
                return mContext.getString(R.string.status_getgood);
            case Constant.STATUS_DOING_TASK:
                return mContext.getString(R.string.status_doing_task);
            case Constant.STATUS_COMPLETE_TASK:
                return mContext.getString(R.string.status_complete_task);
        }
        return null;
    }

    private String getElectric(String electric) {
        int elect = Integer.parseInt(electric);
        return elect/100 + "%";
    }

    private void getAllEquipments() {
        HttpInterface2.getInsstance().getAllEquipments(new Subscriber<List<EquipmentBean>>() {
            @Override
            public void onCompleted() {

            }

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

            @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();
                }
            }
        });
    }

    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;
            List<LocationBean> locationBeanList1 = new ArrayList<>();
            int size = locationBeanList.size();
            for (int i=0; i < size; i++) {
                LocationBean locationBean = locationBeanList.get(i);
                if(locationBean.isDisable()) {
                    locationBeanList1.add(locationBean);
                } else if(locationBean.getLocationType() == 0) {
                    locationBeanList1.add(locationBean);
                }
            }
            locationBeans.removeAll(locationBeanList1);
        }

        @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(mContext.getString(R.string.force_complete_success));
        }

        @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) {

        }
    };
}