AesController.php 8.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/12
 * Time: 11:02
 */

namespace app\portal\controller;



use aesutil\AESUtil;
use cmf\controller\HomeBaseController;
use think\Db;

/**
 * @title 蓝牙锁相关接口
 * @description 蓝牙锁相关接口
 * @group 蓝牙锁相关接口
 */
class AesController extends HomeBaseController{
    public function index(){
        $strKey='2222222222222222';
        $message='ff0ca23403de4f4aaf5500ef';
        $aes=new AESUtil($strKey);
        dump($aes->encryption($message));
        dump($aes->decryption('2857153e26d8dda190834c3a1a3f8f3c'));
    }
    /**
     * @title 开锁(加密)
     * @description 获取加密后的“开锁”命令
     * @author 董瑞恩
     * @url /portal/Aes/secretLock
     * @method GET
     *
     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
     *
     *
     * @return lockKey:加密的开锁指令(数组)
     */
    public function secretLock(){
        $MACAddress=$this->request->param('MACAddress');
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $key="ff0ca2".$MACAddress."5500ef";
            $aes=new AESUtil($strKey['key']);
            $lockKey=$aes->encryption($key);
            $this->apiResponse(200,'success',$lockKey);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }

    }

    /**
     * @title 开锁(不加密)
     * @description 获取“开锁”命令
     * @author 董瑞恩
     * @url /portal/Aes/lock
     * @method GET
     *
     * @param name:name type:String require:1 default:无 other: desc:设备名称
     *
     *
     * @return lockKey:加密的开锁指令(数组)
     */
    public function lock(){
        $name=$this->request->param('name');
        $strKey=Db::name('equipment')->where('name',$name)->find();
        if (!empty($strKey)){
            if ($strKey['electricity']==0){
                $this->apiResponse(302,'设备电量低');
            }else{
                $key="ff0ca2".$strKey['']."5500ef";
                $lockKey=$this->ToArray($key);
                $this->apiResponse(200,'success',$lockKey);
            }
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }

    }

    public function macToStr(){

    }

    /**
     * @title 获取电压、开关状态(加密)
     * @description 获得加密后“获取电压、开关状态”的命令
     * @author 董瑞恩
     * @url /portal/Aes/getSecretState
     * @method GET
     *
     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
     *
     *
     * @return lockKey:获取电压、开关状态的指令(数组)
     */

    public function getSecretState(){
        $MACAddress=$this->request->param('MACAddress');
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $key="ff05a301ef";
            $aes=new AESUtil($strKey['key']);
            $StateKey=$aes->encryption($key);
            $this->apiResponse(200,'success',$StateKey);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }
    }

    /**
     * @title 获取电压、开关状态(不加密)
     * @description 获得加密后“获取电压、开关状态”的命令
     * @author 董瑞恩
     * @url /portal/Aes/getState
     * @method GET
     *
     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
     *
     *
     * @return lockKey:获取电压、开关状态的指令(数组)
     */

    public function getState(){
        $MACAddress=$this->request->param('MACAddress');
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $key="ff05a301ef";
            $StateKey=$this->ToArray($key);
            $this->apiResponse(200,'success',$StateKey);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }
    }


    /**
     * @title 命令解密
     * @description 将设备的十六进制码进行解码,获得设备的返回信息
     * @author 董瑞恩
     * @url /portal/Aes/decryption
     * @method GET
     *
     * @param name:decryKey type:String require:1 default:无 other: desc:设备返回状态(加密)
     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
     * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
     *
     * @return code:1锁已开,0位锁已关,2为数据异常
     */
    public function decryption(){
        $MACAddress=$this->request->param('MACAddress');
        $decryKey=$this->request->param('decryKey');
        $type=$this->request->param('type');
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $aes=new AESUtil($strKey['key']);
            $key=$aes->decryption($decryKey);
            $this->state($MACAddress,$key,$type);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }
    }

    /**
     * @title 设备状态判断
     * @description 解析设备返回值,获取设备状态
     * @author 董瑞恩
     * @url /portal/Aes/decryption
     * @method GET
     *
     * @param name:name type:String require:1 default:无 other: desc:设备名称
     * @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密)
     * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
     *
     * @return code:1锁已开,0位锁已关,2为数据异常
     */
    public function state($name,$key,$type){
        if ($type==1){
            //判断命令是否合法
            if($key[0]=='0xff' && $key[4]=='0xef'){
                $this->open_deal_with($name,$key);
            }else{
                $this->apiResponse(2,'返回指令错误');
            }
        }else if($type==2){
            //判断命令是否合法
            if($key[0]=='0xff' && $key[7]=='0xef'){
                $this->lock_deal_with($name,$key);
            }else{
                $this->apiResponse(2,'返回指令错误');
            }
        }
    }

    //开锁状态处理
    public function open_deal_with($name,$key){
        $use=Db::name('equipment')->where('name',$name)->find()['use'];
        if ($use==1){
            $this->apiResponse(303,'此锁已被使用');
        }
        if ($key[3]=='0x01'){
            //修改数据库设备状态
            try{
                Db::name('equipment')->where('name',$name)->update(['use'=>1]);
            }catch (\Exception $exception){
                $this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage());
            }
            $this->apiResponse(200,'锁已开');
        }else if($key[3]=='0x00'){
            $this->apiResponse(302,'锁未开');
        }
    }
    //关锁
    public function lock_deal_with($name,$key){
        //查看数据库中设备状态
        $use=Db::name('equipment')->where('name',$name)->find()['use'];
        if ($use==0){
            $this->apiResponse(303,'此锁未被使用');
        }
        //电量过低,修改数据库状态
        if ($key[5]=='0x01'){
            Db::name('equipment')->where('name',$name)->update(['electricity'=>0]);
        }
        if ($key[6]=='0x01'){
            //修改数据库设备状态
            $this->apiResponse(301,'锁状态:开锁');//开着
        }else if($key[6]=='0x00'){
            //修改数据库设备状态
            try{
                Db::name('equipment')->where('name',$name)->update(['use'=>0]);
            }catch (\Exception $exception){
                $this->apiResponse(302,'数据库状态更改失败,错误信息:'.$exception->getMessage());
            }
            $this->apiResponse(200,'关锁成功');//关了
        }
    }


    //转成数组
    private function ToArray($data){
        $array = array();
        for ($i = 0; $i < strlen($data); $i += 2) {
            $array[]='0x'.substr($data, $i, 2);
        }
        return $array;
    }

}