FistJni.java 18.9 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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
package com.adroplat.fist_switch.jni;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import com.adroplat.fist_switch.SwitchApplication;
import com.adroplat.fist_switch.config.GlobalVar;
import com.adroplat.fist_switch.utils.ToastUtils;
import com.adroplat.fist_switch.utils.WifiUtils;
import com.adroplat.fist_switch.utils.protocol.one.FistProtocol;
import com.google.gson.Gson;
import com.qnbar.switchcontrol.model.MessageEvent;
import io.realm.Realm;
import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by WLJ on 2016/1/14.
 */
public class FistJni {
    private static final String TAG = FistJni.class.getSimpleName();

    public static final int DEVICE_HUBS_ONLINE_NOTIFY = 0x7101;
    private static final String DEVICE_HUBS_ONLINE_NOTIFY_STR = "DEVICE_HUBS_ONLINE_NOTIFY_STR";
    public static final int DEVICE_SWITCH_ONLINE_NOTIFY = 0x7102;
    private static final String DEVICE_SWITCH_ONLINE_NOTIFY_STR = "DEVICE_SWITCH_ONLINE_NOTIFY_STR";

    static {
        System.loadLibrary("fist_ndk");
    }

    private static FistJni instance;

    private FistJni() {
    }

    public static FistJni getInstance() {
        if (null == instance) {
            instance = new FistJni();
        }
        return instance;
    }

    private static Intent intent;

    public static Intent getIntent() {
        intent = new Intent();
        return intent;
    }

    public static final String CONFIG_DONE = "CONFIG_DONE";
    public static final String SEARCH_RESPONSE = "SEARCH_RESPONSE";
    public static final String LINK_STATUS_RESPONSE = "LINK_STATUS_RESPONSE";
    public static final String LINK_RESPONSE = "LINK_RESPONSE";
    public static final String CTRL_PWD_RESPONSE = "CTRL_PWD_RESPONSE";
    public static final String START_ADD = "START_ADD";
    public static final String STOP_ADD = "STOP_ADD";
    public static final String ADD_DONE = "ADD_DONE";
    public static final String CONTROL_REQUEST_TIMEOUT = "CONTROL_REQUEST_TIMEOUT";
    public static final String CONTROL_RESPONSE = "CONTROL_RESPONSE";
    public static final String CONTROL_DONE = "CONTROL_DONE";
    public static final String CONTROL_OVER = "CONTROL_OVER";
    public static final String BYTE_ERROR_CODE = "BYTE_ERROR_CODE";
    public static final String JSON_DEVICE = "JSON_DEVICE";

    private static void configDone(Device device) {
        sendJsonDevice(CONFIG_DONE, device);
    }

    private static ArrayList<Device> hubs;

    public static ArrayList<Device> getHubs() {
        if (hubs == null) {
            hubs = new ArrayList<>();
        }
        return hubs;
    }

    private static ArrayList<SubDevice> subDevices;

    public static ArrayList<SubDevice> getSubDevices() {
        if (subDevices == null) {
            subDevices = new ArrayList<>();
        }
        return subDevices;
    }

    private static void online(Device device, String jsonDevice) {
        boolean hasAdded = false;
        for (Device device1 : getHubs()) {
            if (Arrays.equals(device1.getDeviceIp(), device.getDeviceIp())) {
                hasAdded = true;
            }
        }
        if (!hasAdded) {
            getHubs().add(device);
        }
        sendMessage(DEVICE_HUBS_ONLINE_NOTIFY, DEVICE_HUBS_ONLINE_NOTIFY_STR);
    }

    private static void sendMessage(int type, String typeStr) {
        MessageEvent messageEvent = new MessageEvent();
        messageEvent.setEventId(type);
        messageEvent.setMessage(typeStr);
        EventBus.getDefault().post(messageEvent);
    }

    private static void saveRemote(Device device) {
        if (null == device) return;
        Realm realm = Realm.getDefaultInstance();
//        Hub host = realm.where(Hub.class)
//                .equalTo(Hub.Companion.getDEVICE_NUM(), device.getLongDeviceNum()).findFirst();
//        if (null != host) {//已存储,更新IP,查询子设备
//            realm.beginTransaction();
//            host.setMapIp(device.getIntMappedIp());
//            host.setMapPort(device.getMappedPort());
//            host.setPeerIp(device.getIntDeviceIp());
//            host.setPeerPort(device.getDevicePort());
//            host.setLAN(false);
//            host.setAlreadyHaveRemoteIP(true);
//            realm.commitTransaction();
//        }
        realm.close();
    }

    private static Gson gson;

    public static Gson getGson() {
        if (null == gson) {
            gson = new Gson();
        }
        return gson;
    }

    /**
     * 发送广播
     *
     * @param action
     * @param device
     */
    private static void sendJsonDevice(String action, Device device) {
        if (null == action || action.isEmpty() || null == device) return;
        Context context = SwitchApplication.getAppContext();
        if (null == context) return;
        Intent intent = getIntent();
        intent.setAction(action);
        intent.putExtra(JSON_DEVICE, device);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    }

    static String deviceJsonAA;

    public static void fistListener(String jsonDevice) {
        if (null == jsonDevice || jsonDevice.isEmpty()) return;
//        GlobalVar.getLogUtils().i("fistListener jsonDevice =" + jsonDevice);
        Log.d(TAG, jsonDevice);
        Gson gson = getGson();
        Device device = gson.fromJson(jsonDevice, Device.class);
        char cmdTpye = (char) device.getCmdType();
        if (TextUtils.isEmpty(deviceJsonAA)) {
            deviceJsonAA = jsonDevice;
        }
        if (FistProtocol.CommondType.CMD_TYPE_SEARCH_RESPONSE == cmdTpye) {//查询响应
            removeId(device);
            online(device, jsonDevice);
            sendJsonDevice(SEARCH_RESPONSE, device);

        } else if (FistProtocol.CommondType.CMD_TYPE_CONFIG_DONE == cmdTpye) {//配置完成处理
            configDone(device);

        } else if (FistProtocol.CommondType.CMD_TYPE_ONLINE == cmdTpye) {//上线通知
            removeId(device);
            online(device, jsonDevice);
            configDone(device);

        } else if (FistProtocol.CommondType.CMD_TYPE_QUERY_RESPONSE == cmdTpye) {
            sendJsonDevice(ADD_DONE, device);
            ArrayList<SubDevice> subDevices = device.getSubDevices();
            if (!subDevices.isEmpty()) {//查询结果不为空
                getSubDevices().addAll(subDevices);
                sendMessage(DEVICE_SWITCH_ONLINE_NOTIFY, DEVICE_SWITCH_ONLINE_NOTIFY_STR);
            }
//            sendJsonDevice(LightingGroupAdapter.REFRESH, device);

        } else if (FistProtocol.CommondType.CMD_TYPE_ADD_RESPONSE == cmdTpye) {
            sendJsonDevice(START_ADD, device);

        } else if (FistProtocol.CommondType.CMD_TYPE_ADD_OVER == cmdTpye) {//子设备结束入网
//            if (0 == device.getIntDeviceIp()) return;
//            FistJni.stopAddSubDevice(jsonDevice);

        } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL == cmdTpye) {//控制超时
            sendJsonDevice(CONTROL_REQUEST_TIMEOUT, device);

        } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_RESPONSE == cmdTpye) {//控制应答
            sendJsonDevice(CONTROL_RESPONSE, device);

        } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_DONE_RESPONSE == cmdTpye) {//单个控制结果
            sendJsonDevice(CONTROL_DONE, device);

        } else if (FistProtocol.CommondType.CMD_TYPE_CONTROL_OVER == cmdTpye) {
            sendJsonDevice(CONTROL_OVER, device);
            GlobalVar.getLogUtils().i("CONTROL_OVER");
        } else if (FistProtocol.CommondType.CMD_NET_LINK_STATUS_RESPONSE == cmdTpye) {
            sendJsonDevice(LINK_STATUS_RESPONSE, device);

        } else if (FistProtocol.CommondType.CMD_LINK_RESPONSE == cmdTpye) {
            sendJsonDevice(LINK_RESPONSE, device);

        } else if (FistProtocol.CommondType.CMD_MODIFY_CTRL_PWD_RESPONSE == cmdTpye) {
            GlobalVar.getLogUtils().i("FistProtocol.CommondType.CMD_MODIFY_CTRL_PWD_RESPONSE");
            sendJsonDevice(CTRL_PWD_RESPONSE, device);

        } /*else if (RemoteProtocol.MsgType.SET_SUCCESS == cmdTpye) {//密码设置成功
            GlobalVar.getLogUtils().i("SET_SUCCESS jsonDevice =" + jsonDevice);
            long deviceNum = device.getLongDeviceNum();
            Realm realm = Realm.getDefaultInstance();
            Hub host = realm.where(Hub.class).equalTo(Hub.Companion.getDEVICE_NUM(), deviceNum).findFirst();
            if (null != host) {
                realm.beginTransaction();
                host.setWanState(WanState.ON);
                realm.commitTransaction();
            }
            realm.close();
        } else if (RemoteProtocol.MsgType.SET_FAILED == cmdTpye) {//密码设置失败
            GlobalVar.getLogUtils().i("SET_FAILED jsonDevice =" + jsonDevice);

        } else if (RemoteProtocol.MsgType.QUERY_SUCCESS == cmdTpye) {//查询对端成功
            final long deviceNum = device.getLongDeviceNum();
            ArrayList<Long> ids = HeService.getLocIds();
            for (long id : ids) {
                if (deviceNum == id) {
                    saveRemote(device);
                    device.setWan(true);
                    jsonDevice = gson.toJson(device);
                    querySubDevice(jsonDevice);
                    break;
                }
            }
            removeId(device);

        } else if (RemoteProtocol.MsgType.QUERY_FAILED == cmdTpye) {//查询对端失败

        }*/
    }

    /**
     * 开始
     */
    public static native void start();

    /**
     * 启动SKD
     */
    public void startSdk() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                start();
                Log.d(TAG, "start sdk");
                return null;
            }
        }.execute();
    }

    /**
     * 移除查询到的主机ID
     *
     * @param device
     */
    private static void removeId(Device device) {
//        ArrayList<Long> ids = HeService.getLocIds();
//        final long deviceNum = device.getLongDeviceNum();
//        for (long id : ids) {
//            if (deviceNum == id) {
//                ids.remove(id);
//                break;
//            }
//        }
    }

    /**
     * 搜索设备
     */
    public static native void searchDevice();

    /**
     * 查询子设备
     */
    public static native void querySubDevice(String jsonDevice);

    public void query(final String jsonDevice) {
//        if (!HeService.isConn()) {
//            return;
//        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                querySubDevice(jsonDevice);
                return null;
            }
        }.execute();
    }

    public void query() {
//        if (!HeService.isConn()) {
//            return;
//        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                querySubDevice(deviceJsonAA);
                return null;
            }
        }.execute();
    }

    /**
     * 开始添加子设备
     */
    public static native void startAddSubDevice(String jsonDevice);

    public void startAdd(final String jsonDevice) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                startAddSubDevice(jsonDevice);
                return null;
            }
        }.execute();
    }

    /**
     * 停止添加子设备
     */
    public static native void stopAddSubDevice(String jsonDevice);

    public void stopAdd(final String jsonDevice) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                stopAddSubDevice(jsonDevice);
                return null;
            }
        }.execute();
    }

    /**
     * 删除子设备
     */
    public static native void deleteSubDevices(String jsonDevice);

    public void delSub(final String jsonDevice) {
//        if (!HeService.isConn()) {
//            ToastUtils.toastShort("网络未连接");
//            return;
//        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                deleteSubDevices(jsonDevice);
                return null;
            }
        }.execute();
    }

    /**
     * 查询软件版本
     */
    public static native void querySoftwareVer(String jsonDevice);

    /**
     * 查询工作模式
     */
    public static native void queryModel(String jsonDevice);

    public void sceneC(final String jsonDevice) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                GlobalVar.getLogUtils().i("sceneC jsonDevice =" + jsonDevice);
                sceneControl(jsonDevice);
                return null;
            }
        }.execute();
    }

    /**
     * 情景控制
     */
    public static native void sceneControl(String jsonDevice);

    public void normalC(final String jsonDevice) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                GlobalVar.getLogUtils().i("normalC jsonDevice =" + jsonDevice);
                normalControl(jsonDevice);
                return null;
            }
        }.execute();
    }

    /**
     * 普通控制
     */
    private static native void normalControl(String jsonDevice);


    /**
     * 设置远程密码
     *
     * @param jsonDevice
     * @param pwd
     */
    private static native void setRemotePwd(String jsonDevice, String pwd);

    public void setPwd(final String jsonDevice, final String pwd) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                setRemotePwd(jsonDevice, pwd);
                return null;
            }
        }.execute();
    }

    private static native void getRemoteCtrlIp(String jsonDevice, String random, String pwd);

    public void searchRecordDevice() {
//        final ArrayList<Long> ids = HeService.getLocIds();
//        if (ids.isEmpty()) return;
//        new AsyncTask<Void, Void, Void>() {
//
//            @Override
//            protected Void doInBackground(Void... params) {
//                searchDevice();
//                Realm realm = Realm.getDefaultInstance();
//                for (long id : ids) {
//                    Hub host = realm.where(Hub.class).equalTo(Hub.Companion.getDEVICE_NUM(), id).findFirst();
//                    if (null != host) {
//                        if (WanState.ON == host.getWanState()) {
//                            realm.beginTransaction();
//                            host.setAlreadyHaveRemoteIP(false);
//                            realm.commitTransaction();
//                            Device device = Device.getInstance();
//                            device.setDeviceNum(ByteUtils.longToMacArray(id));
//                            device.setDeviceKey(host.getWanPwd().getBytes());
//                            Gson gson = getGson();
//                            String jsonDevice = gson.toJson(device);
//                            GlobalVar.getLogUtils().i("searchRecordDevice = " + jsonDevice);
//                            String strRandom = RandomUtil.getRandomStr();
//                            String strPwd = host.getWanPwd();
//                            getRemoteCtrlIp(jsonDevice, strRandom, strPwd);
//                        }
//                    }
//                }
//                realm.close();
//                return null;
//            }
//        }.execute();
    }

    public void searchLocalDevice() {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                searchDevice();
                Log.d(TAG, "search local device");
                return null;
            }
        }.execute();
    }

    /**
     * 链接路由
     *
     * @param jsonDevice
     * @param ssid
     * @param pwd
     */
    public static native void linkRouter(String jsonDevice, String ssid, String pwd);

    public void getlinkRouter(final String jsonDevice, final String ssid, final String pwd) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                GlobalVar.getLogUtils().i("getlinkRouter jsonDevice =" + jsonDevice);
                linkRouter(jsonDevice, ssid, pwd);
                return null;
            }
        }.execute();
    }

    /**
     * 修改密码或SSID
     *
     * @param jsonDevice
     * @param ssid
     * @param pwd
     */
    public static native void modifyAp(String jsonDevice, String ssid, String pwd);

    /**
     * 链接状态
     *
     * @param jsonDevice
     */
    public static native void linkStatus(String jsonDevice);

    public void getLinkStatus(final String jsonDevice) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                GlobalVar.getLogUtils().i("getLinkStatus jsonDevice =" + jsonDevice);
                linkStatus(jsonDevice);
                return null;
            }
        }.execute();
    }


    /**
     * 修改控制密码
     *
     * @param jsonDevice
     * @param pwd
     */
    public static native void modifyCtrlPWd(String jsonDevice, String pwd);

    public void modifyPwd(final String jsonDevice, final String pwd) {
        if (!(WifiUtils.isWifiConnected())) {
            ToastUtils.toastShort("网络未连接");
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                GlobalVar.getLogUtils().i("modifyPwd jsonDevice =" + jsonDevice);
                modifyCtrlPWd(jsonDevice, pwd);
                return null;
            }
        }.execute();
    }
}