<commit_before><?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 2017-09-25
 * Time: 15:52
 */

namespace sunsun\transfer_station\controller;


use GatewayWorker\Lib\Gateway;
use sunsun\helper\LogHelper;
use sunsun\helper\ResultHelper;

class DeviceTransferCtrl
{
    public function process($client_id,$message){
        $jsonDecode = json_decode($message, JSON_OBJECT_AS_ARRAY);
        if($jsonDecode === false){
            return ResultHelper::fail('unknown message');
        }
        LogHelper::logDebug($client_id,json_encode($jsonDecode),'transfer process');
        if(array_key_exists('t',$jsonDecode)){
            $t = $jsonDecode['t'];
            return $this->innerProcess($client_id,strval($t),$jsonDecode);
        }

        return ResultHelper::fail('cant process',[],-101);
    }

    private function innerProcess($client_id,$type,$data){
        switch ($type){
            case ReqMsgType::Hb:
                return $this->hb($client_id,$data);
            case ReqMsgType::Login:
                return $this->login($client_id,$data);
                break;
            case ReqMsgType::SwitchGroup:
                return $this->switchGroup($client_id,$data);
                break;
            default:
                return ResultHelper::fail('unknown message type');
                break;
        }
    }

    private function hb($client_id,$data){
        return ResultHelper::success('100');
    }

    private function login($client_id, $data){
        $did = array_key_exists('did',$data) ? $data['did']:'';
        $token = array_key_exists('token',$data) ? $data['token']:'';
        $uid = array_key_exists('uid',$data) ? $data['uid']:'';
        // TODO token暂时不用
        if(empty($did) || empty($token) || empty($uid)){
            return ResultHelper::fail('did|token|uid invalid');
        }
        // client_id 加入到 did
        Gateway::joinGroup($client_id,$did);
        return ResultHelper::success('login success');
    }

    private function switchGroup($client_id,$data){
        $did = array_key_exists('did',$data) ? $data['did']:'';
        $pre_did = array_key_exists('pre_did',$data) ? $data['pre_did']:'';
        $token = array_key_exists('token',$data) ? $data['token']:'';
        $uid = array_key_exists('uid',$data) ? $data['uid']:'';
        if(empty($did) || empty($pre_did) || empty($token) || empty($uid)){
            return ResultHelper::fail('did|token|uid invalid');
        }

        //1. 登出上一个组
        Gateway::leaveGroup($client_id,$pre_did);
        //2. 登入新的组
        Gateway::joinGroup($client_id,$did);

        return ResultHelper::success('switch success');
    }

}<commit_msg>【完善】新增定时器<commit_after><?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 2017-09-25
 * Time: 15:52
 */

namespace sunsun\transfer_station\controller;


use GatewayWorker\Lib\Gateway;
use sunsun\helper\LogHelper;
use sunsun\helper\ResultHelper;

class DeviceTransferCtrl
{
    public function process($client_id,$message){
        $jsonDecode = json_decode($message, JSON_OBJECT_AS_ARRAY);
        if($jsonDecode === false){
            return ResultHelper::fail('unknown message');
        }
        LogHelper::logDebug($client_id,json_encode($jsonDecode),'transfer process');
        if(array_key_exists('t',$jsonDecode)){
            $t = $jsonDecode['t'];
            return $this->innerProcess($client_id,strval($t),$jsonDecode);
        }

        return ResultHelper::fail('cant process',[],-101);
    }

    private function innerProcess($client_id,$type,$data){
        switch ($type){
            case ReqMsgType::Hb:
                return $this->hb($client_id,$data);
            case ReqMsgType::Login:
                return $this->login($client_id,$data);
                break;
            case ReqMsgType::SwitchGroup:
                return $this->switchGroup($client_id,$data);
                break;
            default:
                return ResultHelper::fail('unknown message type');
                break;
        }
    }

    private function hb($client_id,$data){
        return ResultHelper::success('100');
    }

    private function login($client_id, $data){
        $did = array_key_exists('did',$data) ? $data['did']:'';
        $pre_did = array_key_exists('pre_did',$data) ? $data['pre_did']:'';
        $token = array_key_exists('token',$data) ? $data['token']:'';
        $uid = array_key_exists('uid',$data) ? $data['uid']:'';
        // TODO token暂时不用
        if(empty($did) || empty($token) || empty($uid)){
            return ResultHelper::fail('did|token|uid invalid');
        }
        if(!empty($pre_did)){
            Gateway::leaveGroup($client_id, $pre_did);
        }
        // client_id 加入到 did
        Gateway::joinGroup($client_id,$did);
        return ResultHelper::success('login success');
    }

}
{'fraction_non_alphanumeric': 0.1608695652173913, 'fraction_numerical': 0.007867494824016562, 'mean_word_length': 2.668185269552012, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4042859', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1464, 'n_words': 235}