acc-send-util.ts 3.8 KB
Newer Older
neko's avatar
neko committed
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
// import moment from 'moment';
import { AppCommunicationsCenter } from './app-communications-center';

/**
 * 应用消息发送工具
 *
 * @export
 * @class AccSendUtil
 */
export class AccSendUtil {
    /**
     * 消息发送
     *
     * @protected
     * @memberof AccSendUtil
     */
    protected acc: AppCommunicationsCenter;

    /**
     * Creates an instance of AccSendUtil.
     * @memberof AccSendUtil
     */
    constructor(acc: AppCommunicationsCenter) {
        this.acc = acc;
    }

    /**
     * 指令-更新
     *
     * @param {*} data
     * @memberof ControlBase
     */
    public update(data: any): void {
        this.acc.sendMessage({
            type: 'COMMAND',
            subtype: 'OBJECTUPDATED',
            content: data
        });
    }

    /**
     * 指令-删除
     *
     * @param {*} data
     * @memberof ControlBase
     */
    public remove(data: any): void {
        this.acc.sendMessage({
            type: 'COMMAND',
            subtype: 'OBJECTREMOVED',
            content: data
        });
    }

    /**
     * 指令-新建
     *
     * @param {*} data
     * @memberof ControlBase
     */
    public create(data: any): void {
        this.acc.sendMessage({
            type: 'COMMAND',
            subtype: 'OBJECTCREATED',
            content: data
        });
    }

    /**
     * 向console区发送消息
     *
     * @param {string} message 消息内容
     * @param {('success' | 'error' | 'warning' | 'info')} [type='info'] 消息类型
     * @param {string} [subtype] 消息子类型用于分类
     * @returns {void}
     * @memberof AccSendUtil
     */
    public console(message: string, type: 'success' | 'error' | 'warning' | 'info' = 'info', subtype?: string): void {
        if (message) {
            switch (type) {
                case 'success':
                    return this.consoleSuccess(message, subtype);
                case 'error':
                    return this.consoleError(message, subtype);
                case 'warning':
                    return this.consoleWarning(message, subtype);
                case 'info':
                    return this.consoleInfo(message, subtype);
            }
        }
    }

    /**
     * 发送成功消息
     *
     * @protected
     * @param {string} message
     * @memberof AccSendUtil
     */
    protected consoleSuccess(message: string, subtype?: string): void {
        this.acc.sendMessage({
            type: 'CONSOLE',
            subtype,
            content: `${this.getLocalDate()} \x1b[32m${message} \x1b[0m`
        });
    }

    /**
     * 发送错误消息
     *
     * @protected
     * @param {string} message
     * @memberof AccSendUtil
     */
    protected consoleError(message: string, subtype?: string): void {
        this.acc.sendMessage({
            type: 'CONSOLE',
            subtype,
            content: `${this.getLocalDate()} \x1b[31m${message} \x1b[0m`
        });
    }

    /**
     * 发送警告消息
     *
     * @protected
     * @param {string} message
     * @memberof AccSendUtil
     */
    protected consoleWarning(message: string, subtype?: string): void {
        this.acc.sendMessage({
            type: 'CONSOLE',
            subtype,
            content: `${this.getLocalDate()} \x1b[33m${message} \x1b[0m`
        });
    }

    /**
     * 发送消息
     *
     * @protected
     * @param {string} message
     * @memberof AccSendUtil
     */
    protected consoleInfo(message: string, subtype?: string): void {
        this.acc.sendMessage({
            type: 'CONSOLE',
            subtype,
            content: message
        });
    }

    /**
     * 获取当前时间
     *
     * @protected
     * @returns {string}
     * @memberof AccSendUtil
     */
    protected getLocalDate(): string {
        // return moment().format('MM-DD HH:mm:ss');
        return '';
    }

}