thingsboard 自定义静态部件

需要弄一个能发送http请求的组件,在源代码里捣鼓一圈,但是对angular实在不熟悉,最后还是在平台里创建吧

html:

<div class="cmBtns box">
    <h4 id="btnTitle" class="btnTitle">{{labelTitle}}</h4>
    <button id="btnOn" ng-click="deviceControl('on')"  class="simpleBtn">开</button>
    <button id="btnOff" ng-click="deviceControl('off')"  class="simpleBtn">关</button>
    <video preload="auto" name="media" hidden="true" class="myVideo">
        <source src="http://www.szqhhjbj.com/aigei_pao.mp3" type="audio/mpeg">
    </video>
</div>

css:

.box {
    text-align: center;
}

.simpleBtn {
    width: 90%;
    padding: 8px 0;
    background: #305680;
    border-radius: 10px;
    border: none;
    color:#fff;
    margin-bottom: 10px;
}
.simpleBtn:hover,.simpleBtn:active {
    background: #4171a7;
}
.btnTitle{margin-top:0;margin-bottom:0; font-size:20px;}

设置模式:

{

    "schema": {

        "type": "object",
        "title": "Settings",
        "properties": {
            "deviceId":{
                "title":"设备id",
                "type":"string"
            },
            "btntype":{
                "title":"是否批量(默认否)",
                "type":"boolean",
                "default":false
            },
            "labelTitle": {
                "title": "标题文字",
                "type": "string",
                "default": "标题文字"
            }
        },
        "required": ["deviceId"]
    },
    "form": [
        "btntype",
        "deviceId",
        "labelTitle"
    ]
}

js:

self.onInit = function() {
    var scope = self.ctx.$scope;
     
    self.ctx.$scope.labelTitle = self.ctx.settings.labelTitle || '标题文字';

    scope.deviceControl = function(status) { 
        var baseurl = "http://192.168.1.17:6405/service/code/action";
        var url="";
        var btnType = self.ctx.settings.btntype;
        var deviceId = self.ctx.settings.deviceId;
        if (deviceId) {
            if (btnType) {
                url = baseurl + "/batch?id=" +deviceId + "&cmd=" + status;
            } else {
                url = baseurl + "/single?id=" +deviceId + "&cmd=" + status;
            }
        }
        $('.myVideo')[0].load();
        $('.myVideo')[0].play();

        var xhr = new XMLHttpRequest();

        xhr.open('GET',url,true);
        xhr.send(null);
        //return promise
        return new Promise(function (resolve, reject) {
            //onload are executed just after the sync request is comple,
            //please use 'onreadystatechange' if need support IE9-
            xhr.onload = function () {
                if (xhr.status === 200) {
                    var result;
                    try {
                        result = JSON.parse(xhr.response);
                    } catch (e) {
                        result = xhr.response;
                    }
                    resolve(result);
                } else {
                    reject(xhr.response);
                }
            };

        });
        
    };
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值