Thingsboard 创建RCP小部件

一、参考

官方给到的RPC固件,只能固定method和param,因为有测试需求,需要网页可以发送随意的rpc命令。鉴于不太懂前端的知识,找到对应的大佬做好的例子
https://www.jianshu.com/p/0adc8965da45

二、创建小部件

  1. 创建自己的部件库

  2. 添加新部件,分别添加HTMLJS设置架构的代码
    HTML

    <div class="tb-rpc-button" fxLayout="column">
        <div fxFlex="20" class="title-container" fxLayout="row"
            fxLayoutAlign="center center" [fxShow]="showTitle">
            <span class="button-title">{{title}}</span>
        </div>
        <div fxFlex="{{showTitle ? 80 : 100}}" [ngStyle]="{paddingTop: showTitle ? '5px': '10px'}"
            class="button-container" fxLayout="column" fxLayoutAlign="center center">
            <div>
                <button mat-button (click)="sendCommand()"
                        [class.mat-raised-button]="styleButton?.isRaised"
                        [color]="styleButton?.isPrimary ? 'primary' : ''"
                        [ngStyle]="customStyle">
                    {{buttonLable}}
                </button>
            </div>
        </div>
        <div class="error-container" [ngStyle]="{'background': error?.length ? 'rgba(255,255,255,0.25)' : 'none'}"
             fxLayout="row" fxLayoutAlign="center center">
            <span class="button-error">{{ error }}</span>
        </div>
    </div>
    

    JS

    self.onInit = function() {
        
        self.ctx.$scope.sendCommand = function() {
            var rpcMethod = self.ctx.$scope.rpcMethod;
            var rpcParams = self.ctx.$scope.rpcParams;
            var timeout = self.ctx.settings.requestTimeout;
            var oneWayElseTwoWay = self.ctx.settings.oneWayElseTwoWay ? true : false;
    
            var commandObservable;
            if (oneWayElseTwoWay) {
                commandObservable = self.ctx.controlApi.sendOneWayCommand(rpcMethod, rpcParams, timeout);
            } else {
                commandObservable = self.ctx.controlApi.sendTwoWayCommand(rpcMethod, rpcParams, timeout);
            }
            commandObservable.subscribe(
                function (response) {
                    if (oneWayElseTwoWay) {
                        self.ctx.$scope.rpcCommandResponse = "Command was successfully received by device.<br> No response body because of one way command mode.";
                    } else {
                        self.ctx.$scope.rpcCommandResponse = "Response from device:<br>";                    
                        self.ctx.$scope.rpcCommandResponse += JSON.stringify(response, undefined, 2);
                    }
                    self.ctx.detectChanges();
                },
                function (rejection) {
                    self.ctx.$scope.rpcCommandResponse = "Failed to send command to the device:<br>"
                    self.ctx.$scope.rpcCommandResponse += "Status: " + rejection.status + "<br>";
                    self.ctx.$scope.rpcCommandResponse += "Status text: '" + rejection.statusText + "'";
                    self.ctx.detectChanges();
                }
                
            );
        }
        }
    

    设置架构

    {
        "schema": {
            "type": "object",
            "title": "Settings",
            "properties": {
                "oneWayElseTwoWay": {
                    "title": "Is One Way Command",
                    "type": "boolean",
                    "default": true
                },
                "requestTimeout": {
                    "title": "RPC request timeout",
                    "type": "number",
                    "default": 500
                }
            },
            "required": []
        },
        "form": [
            "oneWayElseTwoWay",
            "requestTimeout"
        ]}
    

三、实测

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值