AddMeshActivity.java 5.58 KB
/*
 * Copyright 2017 wugian
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qnbar.smc;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.qnbar.smc.service.TelinkLightService;
import com.telink.bluetooth.light.model.Mesh;
import com.xgimi.gimicinema.R;
import com.xgimi.gimicinema.application.FangTangApplication;

public final class AddMeshActivity extends Activity {

    private ImageView backView;
    private Button btnSave;

    private FangTangApplication mApplication;
    private OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v == backView) {
                finish();
            } else if (v == btnSave) {
                saveMesh();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.activity_add_mesh);

        this.mApplication = (FangTangApplication) this.getApplication();

        this.backView = (ImageView) this
                .findViewById(R.id.img_header_menu_left);
        this.backView.setOnClickListener(this.clickListener);

        this.btnSave = (Button) this.findViewById(R.id.btn_save);
        this.btnSave.setOnClickListener(this.clickListener);

        this.updateGUI();

        TelinkLightService.Instance().idleMode(false);
    }

    private void updateGUI() {

        if (this.mApplication.isEmptyMesh())
            return;

        EditText txtMeshName = (EditText) this.findViewById(R.id.txt_mesh_name);
        EditText txtPassword = (EditText) this
                .findViewById(R.id.txt_mesh_password);

        EditText txtFactoryMeshName = (EditText) this
                .findViewById(R.id.txt_factory_name);
        EditText txtFactoryPassword = (EditText) this
                .findViewById(R.id.txt_factory_password);

        Mesh mesh = this.mApplication.getMesh();

        txtMeshName.setText(mesh.name);
        txtPassword.setText(mesh.password);
        txtFactoryMeshName.setText(mesh.factoryName);
        txtFactoryPassword.setText(mesh.factoryPassword);
    }

    @SuppressLint("ShowToast")
    private void saveMesh() {

        EditText txtMeshName = (EditText) this.findViewById(R.id.txt_mesh_name);
        EditText txtPassword = (EditText) this
                .findViewById(R.id.txt_mesh_password);

        EditText txtFactoryMeshName = (EditText) this
                .findViewById(R.id.txt_factory_name);
        EditText txtFactoryPassword = (EditText) this
                .findViewById(R.id.txt_factory_password);
        //EditText otaText = (EditText) this.findViewById(R.id.ota_device);
        Mesh mesh = this.mApplication.getMesh();

        String newfactoryName = txtMeshName.getText().toString().trim();
        String newfactoryPwd = txtPassword.getText().toString().trim();

        String factoryName = txtFactoryMeshName.getText().toString().trim();
        String factoryPwd = txtFactoryPassword.getText().toString().trim();

        if (mesh == null)
            mesh = new Mesh();

        if (!factoryName.equals(mesh.factoryName) || !factoryPwd.equals(mesh.factoryPassword)) {
            mesh.allocDeviceAddress = null;
            mesh.devices.clear();
        }

        mesh.name = newfactoryName;
        mesh.password = newfactoryPwd;
        mesh.factoryName = factoryName;
        mesh.factoryPassword = factoryPwd;
        //mesh.otaDevice = otaText.getText().toString().trim();

        if (mesh.saveOrUpdate()) {
            this.mApplication.setMesh(mesh);
            Toast.makeText(AddMeshActivity.this, "保存mesh成功", Toast.LENGTH_SHORT).show();

//            int connectMeshAddress = mesh.getDeviceAddress();
//
//            if (connectMeshAddress == -1) {
////                show("哎呦,网络里的灯泡太多了!目前可以有256灯");
//                this.finish();
//                return;
//            }
//
//            //更新参数
//            LeUpdateParameters params = Parameters.createUpdateParameters();
//            params.setOldMeshName(mesh.factoryName);
//            params.setOldPassword(mesh.factoryPassword);
//            params.setNewMeshName(mesh.name);
//            params.setNewPassword(mesh.password);
//
////            DeviceInfo deviceInfo = event.getArgs();
////            deviceInfo.meshAddress = connectMeshAddress;
////            params.setUpdateDeviceList(deviceInfo);
//            //params.set(Parameters.PARAM_DEVICE_LIST, deviceInfo);
//            TelinkLightService.Instance().idleMode(true);
//            //加灯
//            TelinkLightService.Instance().updateMesh(params);
        }
    }
}