logo听风亭

如风在野,山峦共听

近期实现基于thinkphp5 实现一个对接多门禁设备的工厂类扩展

一、设计思路:

        1.主要是以目前所对接设备类型为模板,开发出一个便于扩展、易维护的一个针对门禁设备执行动作的一个插件,这里主要是在 thinkphp5 的框架目录完成。

        2.以现有实现逻辑为标准,提取抽象类作为后期设备扩展的标准模板,将后期如何对接的设备使用同一输出模式,方便业务流程的对接,通过修改设备对应型号实例化对应设备的实现类。

二、实现流程

        1.建立基础目录

--device  扩展目录
----lib   工厂类目录
------Landevice.php   非联网设备工厂类
------Webdevice.php   联网设备GQC设备工厂类
----DeviceFactory.php  设备路由类
----DeviceHandle.php   设备工厂抽象类

        2.建立抽象类,定义设备接入规则

//DeviceHandle.php
<?php

namespace device;


abstract class DeviceHandle
{
    abstract function faceDistribute($data);
    abstract function icDistribute($data);
    abstract function passwordDistribute($data);
    abstract function qrcodeDistribute($data);
    abstract function openDoor($data);
}

        3.对应路由类,根据设备型号实例化对应设备操作类

//DeviceFactory.php
<?php
namespace device;

use device\lib\Landevice;
use device\lib\Webdevice;

class DeviceFactory
{
    protected $error ;

    public static $instance = null;

    public function getError(){
        return $this->error ? : '';
    }

    public function getDeviceHandle($type){

        switch ($type){
            case 'LAN':
                self::$instance = new Landevice();
                break;
            case 'WEB_JQC':
                self::$instance = new Webdevice();
                break;
            default:
                $this->error = '没有对接相关类型设备';
                self::$instance = null;
                break;
        }
        return self::$instance;
    }
}

        3.lib目录下实现具体设备接入类

//Webdevice.php 
<?php

namespace device\lib;


class Webdevice extends \device\DeviceHandle
{

    protected $error = '';
    protected $url = '';
    protected $return_data;
    public function __construct()
    {
    
    }

    function faceDistribute($data)
    {
        // TODO: Implement faceDistribute() method.

    }

    function icDistribute($data)
    {
        // TODO: Implement icDistribute() method.
    }

    function passwordDistribute($data)
    {
        // TODO: Implement passwordDistribute() method.
    }

    function qrcodeDistribute($data)
    {
        // TODO: Implement qrcodeDistribute() method.
    }
    function openDoor($data)
    {
        // TODO: Implement openDoor() method.
    }
}


总结:

        以上即这个设备对接吗,模块的实现流程,后期对接设备,只需根据实际设备支持的能力,设计相关实现类,通过定义设备型号,进行相关设备的实例化,当然路由类可以写的更加智能一些,这里没有继续完善,一会有机会的话可以再维护下。

Comments 0

0.135298s