<commit_before>package cn.diaovision.omnicontrol.core.model.device.matrix;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.diaovision.omnicontrol.core.message.MatrixMessage;
import cn.diaovision.omnicontrol.core.model.device.endpoint.HiCamera;
import cn.diaovision.omnicontrol.core.model.device.matrix.io.Channel;
import cn.diaovision.omnicontrol.rx.RxMessage;
import cn.diaovision.omnicontrol.rx.RxSubscriber;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**矩阵控制器，这里用remoter，以区分controller
 * Created by liulingfeng on 2017/5/5.
 */

public class MediaMatrixRemoter {
    int a;
    private MediaMatrix matrix;

    public MediaMatrixRemoter(MediaMatrix matrix) {
        this.matrix = matrix;
    }

    public MediaMatrix getMatrix() {
        return matrix;
    }

    public void setMatrix(MediaMatrix matrix) {
        this.matrix = matrix;
    }

    public int switchVideo(final int portIn, final int[] portOut, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable())
            return -1;

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override
            public void subscribe(FlowableEmitter<RxMessage> e) throws Exception {
                byte[] bytes = MatrixMessage.buildMultiSwitchMessage(matrix.id, portIn, portOut).toBytes();
                byte[] recv = matrix.getController().send(bytes, bytes.length);
                if (recv.length > 0) {
                    e.onNext(new RxMessage(RxMessage.DONE));
                    e.onComplete();
                }
                else {
                    matrix.setReachable(false);
                    e.onError(new IOException());
                }
            }
        }, BackpressureStrategy.BUFFER)
        .doOnComplete(new Action() {
            @Override
            public void run() throws Exception {
                matrix.updateChannel(portIn, portOut, Channel.MOD_NORMAL);
            }
        })
        .subscribe(subscriber);
        return 0;
    }

    public int stitchVideo(final int portIn, final int columnCnt, final int rowCnt, final int[] portOut, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable())
            return -1;

        //send multi -> send stitch
        MatrixMessage multiSwithMsg = MatrixMessage.buildMultiSwitchMessage(matrix.id, portIn, portOut);
        MatrixMessage stitchMsg = MatrixMessage.buildStitchMessage(matrix.id, columnCnt, rowCnt, portOut);
        List<MatrixMessage> msgList = new ArrayList<>();
        msgList.add(multiSwithMsg);
        msgList.add(stitchMsg);

        Flowable.fromIterable(msgList)
                .map(new Function<MatrixMessage, RxMessage>() {
                    @Override
                    public RxMessage apply(MatrixMessage matrixMessage) throws Exception {
                        byte[] recv = matrix.getController().send(matrixMessage.toBytes(), matrixMessage.toBytes().length);
                        if (recv.length > 0) {
                            //4. send message
                            return new RxMessage(RxMessage.DONE);
                        }
                        else {
                            matrix.setReachable(false);
                            throw new IOException();
                        }
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        matrix.updateChannel(portIn, portOut, Channel.MOD_STITCH);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int getCameraIdx(final int portIdx, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }

        Flowable.just(portIdx)
                .map(new Function<Integer, RxMessage>() {
                    @Override
                    public RxMessage apply(Integer integer) throws Exception {
                        byte[] bytes = MatrixMessage.buildGetCameraInfoMessage(matrix.id, portIdx).toBytes();
                        byte[] recv = matrix.getController().send(bytes, bytes.length);
                        if (recv.length > 0){
                            int idx = 0;
                            if (recv[5] >= '9'){
                                idx += (recv[5] - 0x30-7)<<4;
                            }
                            else {
                                idx += (recv[5] - 0x30)<<4;
                            }

                            if (recv[6] >= '9'){
                                idx += (recv[6] - 0x30-7);
                            }
                            else {
                                idx += (recv[6] - 0x30);
                            }
                            return new RxMessage(RxMessage.DONE, idx);
                        }
                        else {
                            throw new IOException();
                        }
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int startCameraGo(final int portIdx, final int cmd, final int speed, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }
        final HiCamera cam = matrix.getCameras().get(portIdx);
        if (cam == null){
            return -1;
        }

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override
            public void subscribe(FlowableEmitter<RxMessage> e) throws Exception {

                final byte[] bytes = MatrixMessage.buildStartCameraGoMessage(matrix.id, cam.getBaudrate(), cam.getProto(), cam.getPortIdx(), cmd, speed).toBytes();
                byte[] recv = matrix.getController().send(bytes, bytes.length);
                if (recv.length > 0){
                    e.onNext(new RxMessage(RxMessage.DONE));
                    e.onComplete();
                }
                else {
                    matrix.setReachable(false);
                    e.onError(new IOException());
                }
            }
        }, BackpressureStrategy.BUFFER)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int stopCameraGo(final int portIdx, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }
        final HiCamera cam = matrix.getCameras().get(portIdx);
        if (cam == null){
            return -1;
        }

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override
            public void subscribe(FlowableEmitter<RxMessage> e) throws Exception {

                byte[] bytes = MatrixMessage.buildStopCameraGoMessage(matrix.id, cam.getBaudrate(), cam.getProto(), cam.getPortIdx()).toBytes();
                byte[] recv = matrix.getController().send(bytes, bytes.length);
                if (recv!= null || recv.length > 0){
                    e.onNext(new RxMessage(RxMessage.DONE));
                    e.onComplete();
                }
                else {
                    matrix.setReachable(false);
                    e.onError(new IOException());
                }
            }
        }, BackpressureStrategy.BUFFER)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int storeCameraPreset(final int portIdx, final int presetIdx, final String name, RxSubscriber subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }
        final HiCamera cam = matrix.getCameras().get(portIdx);
        if (cam == null){
            return -1;
        }

        if (matrix == null)
            return -1;

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override<commit_msg>added e.onNext for cameraGo<commit_after>package cn.diaovision.omnicontrol.core.model.device.matrix;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.diaovision.omnicontrol.core.message.MatrixMessage;
import cn.diaovision.omnicontrol.core.model.device.endpoint.HiCamera;
import cn.diaovision.omnicontrol.core.model.device.matrix.io.Channel;
import cn.diaovision.omnicontrol.rx.RxMessage;
import cn.diaovision.omnicontrol.rx.RxSubscriber;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**矩阵控制器，这里用remoter，以区分controller
 * Created by liulingfeng on 2017/5/5.
 */

public class MediaMatrixRemoter {
    int a;
    private MediaMatrix matrix;

    public MediaMatrixRemoter(MediaMatrix matrix) {
        this.matrix = matrix;
    }

    public MediaMatrix getMatrix() {
        return matrix;
    }

    public void setMatrix(MediaMatrix matrix) {
        this.matrix = matrix;
    }

    public int switchVideo(final int portIn, final int[] portOut, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable())
            return -1;

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override
            public void subscribe(FlowableEmitter<RxMessage> e) throws Exception {
                byte[] bytes = MatrixMessage.buildMultiSwitchMessage(matrix.id, portIn, portOut).toBytes();
                byte[] recv = matrix.getController().send(bytes, bytes.length);
                if (recv.length > 0) {
                    e.onNext(new RxMessage(RxMessage.DONE));
                    e.onComplete();
                }
                else {
                    matrix.setReachable(false);
                    e.onError(new IOException());
                }
            }
        }, BackpressureStrategy.BUFFER)
        .doOnComplete(new Action() {
            @Override
            public void run() throws Exception {
                matrix.updateChannel(portIn, portOut, Channel.MOD_NORMAL);
            }
        })
        .subscribe(subscriber);
        return 0;
    }

    public int stitchVideo(final int portIn, final int columnCnt, final int rowCnt, final int[] portOut, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable())
            return -1;

        //send multi -> send stitch
        MatrixMessage multiSwithMsg = MatrixMessage.buildMultiSwitchMessage(matrix.id, portIn, portOut);
        MatrixMessage stitchMsg = MatrixMessage.buildStitchMessage(matrix.id, columnCnt, rowCnt, portOut);
        List<MatrixMessage> msgList = new ArrayList<>();
        msgList.add(multiSwithMsg);
        msgList.add(stitchMsg);

        Flowable.fromIterable(msgList)
                .map(new Function<MatrixMessage, RxMessage>() {
                    @Override
                    public RxMessage apply(MatrixMessage matrixMessage) throws Exception {
                        byte[] recv = matrix.getController().send(matrixMessage.toBytes(), matrixMessage.toBytes().length);
                        if (recv.length > 0) {
                            //4. send message
                            return new RxMessage(RxMessage.DONE);
                        }
                        else {
                            matrix.setReachable(false);
                            throw new IOException();
                        }
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        matrix.updateChannel(portIn, portOut, Channel.MOD_STITCH);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int getCameraIdx(final int portIdx, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }

        Flowable.just(portIdx)
                .map(new Function<Integer, RxMessage>() {
                    @Override
                    public RxMessage apply(Integer integer) throws Exception {
                        byte[] bytes = MatrixMessage.buildGetCameraInfoMessage(matrix.id, portIdx).toBytes();
                        byte[] recv = matrix.getController().send(bytes, bytes.length);
                        if (recv.length > 0){
                            int idx = 0;
                            if (recv[5] >= '9'){
                                idx += (recv[5] - 0x30-7)<<4;
                            }
                            else {
                                idx += (recv[5] - 0x30)<<4;
                            }

                            if (recv[6] >= '9'){
                                idx += (recv[6] - 0x30-7);
                            }
                            else {
                                idx += (recv[6] - 0x30);
                            }
                            return new RxMessage(RxMessage.DONE, idx);
                        }
                        else {
                            throw new IOException();
                        }
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int startCameraGo(final int portIdx, final int cmd, final int speed, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }
        final HiCamera cam = matrix.getCameras().get(portIdx);
        if (cam == null){
            return -1;
        }

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override
            public void subscribe(FlowableEmitter<RxMessage> e) throws Exception {

                final byte[] bytes = MatrixMessage.buildStartCameraGoMessage(matrix.id, cam.getBaudrate(), cam.getProto(), cam.getPortIdx(), cmd, speed).toBytes();
                byte[] recv = matrix.getController().send(bytes, bytes.length);
                if (recv!= null && recv.length >= 0){
                    e.onNext(new RxMessage(RxMessage.DONE));
                    e.onComplete();
                }
                else {
                    matrix.setReachable(false);
                    e.onError(new IOException());
                }
            }
        }, BackpressureStrategy.BUFFER)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int stopCameraGo(final int portIdx, RxSubscriber<RxMessage> subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }
        final HiCamera cam = matrix.getCameras().get(portIdx);
        if (cam == null){
            return -1;
        }

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override
            public void subscribe(FlowableEmitter<RxMessage> e) throws Exception {

                byte[] bytes = MatrixMessage.buildStopCameraGoMessage(matrix.id, cam.getBaudrate(), cam.getProto(), cam.getPortIdx()).toBytes();
                byte[] recv = matrix.getController().send(bytes, bytes.length);
                if (recv!= null && recv.length >= 0){
                    e.onNext(new RxMessage(RxMessage.DONE));
                    e.onComplete();
                }
                else {
                    matrix.setReachable(false);
                    e.onError(new IOException());
                }
            }
        }, BackpressureStrategy.BUFFER)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
        return 0;
    }

    public int storeCameraPreset(final int portIdx, final int presetIdx, final String name, RxSubscriber subscriber){
        if (matrix == null || !matrix.isReachable()) {
            return -1;
        }
        final HiCamera cam = matrix.getCameras().get(portIdx);
        if (cam == null){
            return -1;
        }

        if (matrix == null)
            return -1;

        Flowable.create(new FlowableOnSubscribe<RxMessage>() {
            @Override
{'fraction_non_alphanumeric': 0.10169197898019287, 'fraction_numerical': 0.0060056591788416, 'mean_word_length': 1.5679122182680902, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492374', 'n_tokens_mistral': 4683, 'n_tokens_neox': 4350, 'n_words': 890}