optionsSet.html 13.8 KB
<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>参数设定</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
</head>

<body class="options-set-view repair-view">

    <div class="aui-content aui-margin-b-15">
        <header class="aui-bar aui-bar-nav" id="header">
            <a class="aui-btn aui-pull-left" tapmode onclick="closeWin()">
                <span class="aui-iconfont aui-icon-left"></span>
            </a>
            <div class="aui-title">参数设定</div>
            <span class="aui-btn aui-pull-right" tapmode onclick="cmd();">确定</span>
        </header>
        <ul>
            <li class="phone-input">
                <label id="configLabel"></label>
                <input id="configTxt" class="aui-pull-right" value="" type="number" placeholder="请输入值" />
            </li>
        </ul>
    </div>
</body>

</html>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    var header = $api.byId('header');
    fixStatusBar_API(header);
    var kind = '';
    var imei = '';
    apiready = function() {
        api.setStatusBarStyle({
            style: 'dark',
            color: 'rgba(0,0,0,0)'
        });
        api.parseTapmode();

        var pageParam = api.pageParam;
        if(!pageParam || !pageParam.kind || pageParam.kind == '' || !pageParam.imei || pageParam.imei == ''){
          api.toast({
              msg: '参数错误',
              duration: 4000,
              location: 'bottom'
          });
          setTimeout(function(){
            api.closeWin();
          }, 3000);
          return false;
        }
        kind = pageParam.kind;
        imei = pageParam.imei;
        showSetDetail(kind, imei);
        // api.openWin({ slidBackEnabled:false,
        //     name: 'index',
        //     url: './html/index.html',
        //     pageParam: {}
        // });
    }

    var configArr = {}
    function showSetDetail(kind, imei){
      api.showProgress({
          style: 'default',
          animationType: 'fade',
          title: '努力加载中...',
          text: '',
          modal: true
      });
      var token = $api.getStorage('userToken');

      if(token && token != ''){
          $.ajax({
            url: HOST + '/iot_api/v1/app/get_param_range?imei='+imei,
            type: 'GET',
            dataType: 'json',
            headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'token': token
            },
            complete:function(){
              api.hideProgress();

            },
            success: function(ret) {
                var ok = false;
                if (ret) {
                    if (ret.code == 0 && ret.data) {
                        ok = true;
                        switch (kind) {
                          case 'guozaidianliu':
                            $api.html($api.byId("configLabel"), "过载电流动作值("+ret.data.over_current_min+"~"+ret.data.over_current_max+" A):");
                            configArr[kind] = {
                              "cmd":'&S2+',
                              "min":ret.data.over_current_min,
                              "max":ret.data.over_current_max
                            }
                            break;
                          case 'guozaishijian':
                            $api.html($api.byId("configLabel"), "过载时间动作值("+ret.data.over_duration_min+"~"+ret.data.over_duration_max+" s):");
                            configArr[kind] = {
                              "cmd":'&S3+',
                              "min":ret.data.over_duration_min,
                              "max":ret.data.over_duration_max
                            }
                            break;
                          case 'duanludianliu':
                            $api.html($api.byId("configLabel"), "短路电流动作值("+ret.data.over_short_current_min+"~"+ret.data.over_short_current_max+" A):");
                            configArr[kind] = {
                              "cmd":'&S4+',
                              "min":ret.data.over_short_current_min,
                              "max":ret.data.over_short_current_max
                            }
                            break;
                          case 'loudiandianliu':
                            $api.html($api.byId("configLabel"), "漏电电流动作值("+ret.data.over_rest_current_min+"~"+ret.data.over_rest_current_max+" mA):");
                            configArr[kind] = {
                              "cmd":'&S5+',
                              "min":ret.data.over_rest_current_min,
                              "max":ret.data.over_rest_current_max
                            }
                            break;
                          case 'guoya':
                            $api.html($api.byId("configLabel"), "过压报警值("+ret.data.over_high_voltage_min+"~"+ret.data.over_high_voltage_max+" V):");
                            configArr[kind] = {
                              "cmd":'&S6+',
                              "min":ret.data.over_high_voltage_min,
                              "max":ret.data.over_high_voltage_max
                            }
                            break;
                          case 'qianya':
                            $api.html($api.byId("configLabel"), "欠压报警值("+ret.data.over_low_voltage_min+"~"+ret.data.over_low_voltage_max+" V):");
                            configArr[kind] = {
                              "cmd":'&S7+',
                              "min":ret.data.over_low_voltage_min,
                              "max":ret.data.over_low_voltage_max
                            }
                            break;
                          case 'chaowen':
                            $api.html($api.byId("configLabel"), "超温报警值("+ret.data.over_temp_min+"~"+ret.data.over_temp_max+" ℃):");
                            configArr[kind] = {
                              "cmd":'&S8+',
                              "min":ret.data.over_temp_min,
                              "max":ret.data.over_temp_max
                            }
                            break;
                          default:
                            ok = false;
                        }

                    }
                }
                if(!ok){
                  api.toast({
                      msg: '获取设备参数范围失败!',
                      duration: 4000,
                      location: 'bottom'
                  });
                  setTimeout(function(){
                    api.closeWin();
                  }, 3000)
                }
            },
            error: function() {
              api.toast({
                  msg: '获取设备参数范围失败!',
                  duration: 4000,
                  location: 'bottom'
              });
              setTimeout(function(){
                api.closeWin();
              }, 3000)
            }
        });
      }
    }

    function cmd(){
      var config = configArr[kind];
      $("#configTxt").blur();
      var current = $api.val($api.byId("configTxt"));
      if(config && current != ''){
        //验证范围
        var val = parseFloat(current);
        if(val<config['min'] || val > config['max']){
          api.toast({
              msg: '参数设置不在有效范围!',
              duration: 2000,
              location: 'bottom'
          });
        }else{
          opCommand(config["cmd"], val);
        }

      }
    }

    function opCommand(cmd, val){
      var token = $api.getStorage('userToken');
      var userId = $api.getStorage('userID');
      if(token && token != '' && imei != '' && userId && userId != ''){
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '操作请求中...',
            text: '',
            modal: true
        });
        $.ajax({
          url: HOST + '/iot_api/v1/app/set_device',
          type: 'POST',
          dataType: 'json',
          headers: {
              'Content-Type': 'application/json;charset=utf-8',
              'token': token
          },
          data:JSON.stringify({
            imei:imei,
            command:cmd,
            value:val.toString(),
            user_id:userId
          }),
          complete:function(){

          },
          success: function(ret) {
              if (ret) {
                  if (ret.code == 0) {

                        //
                        $.ajax({
                          url: HOST + '/iot_api/v1/app/sync_device',
                          type: 'POST',
                          dataType: 'json',
                          headers: {
                              'Content-Type': 'application/json;charset=utf-8',
                              'token': token
                          },
                          data:JSON.stringify({
                            "imei":imei
                          }),
                          complete:function(){
                            api.hideProgress();
                          },
                          success: function(ret) {
                            api.toast({
                                msg: '操作成功',
                                duration: 3000,
                                location: 'bottom'
                            });
                            api.sendEvent({
                                name: 'change_config'
                            });
                            setTimeout(function(){
                              api.closeWin();
                            }, 2000)
                              },
                              error:function(){

                              }
                            });

                      }else{
                        api.hideProgress();
                        api.toast({
                            msg: '操作失败,请稍候再试!',
                            duration: 3000,
                            location: 'bottom'
                        });
                      }
                  }else{
                    api.hideProgress();
                    api.toast({
                        msg: '操作失败,请稍候再试!',
                        duration: 3000,
                        location: 'bottom'
                    });
                  }
              },
          error: function() {
            api.hideProgress();
            api.toast({
                msg: '操作失败,请稍候再试!',
                duration: 3000,
                location: 'bottom'
            });
          }
        });
      }

    }

    function selectNumber(obj) {
        var UIMultiSelector = api.require('UIMultiSelector');
        UIMultiSelector.open({
            rect: {
                h: 244
            },
            text: {
                title: '设备编号',
                leftBtn: '取消',
                rightBtn: '确定',
                selectAll: '全选'
            },
            max: 1,
            singleSelection: true,
            maskClose: false,
            styles: {
                mask: 'rgba(0,0,0,0.3)',
                title: {
                    bg: '#f5f5f5',
                    color: 'rgb(0,0,0)',
                    size: 14,
                    h: 44
                },
                leftButton: {
                    w: 80,
                    h: 35,
                    marginT: 5,
                    marginL: 8,
                    color: 'rgb(0,0,0)',
                    bg: 'rgb(200,200,200)',
                    size: 14,
                },
                rightButton: {
                    w: 80,
                    h: 35,
                    marginT: 5,
                    marginR: 8,
                    color: 'rgb(255,255,255)',
                    bg: '#5D93E4',
                    size: 14,
                },
                item: {
                    h: 40,
                    bg: '#fff',
                    bgActive: '#57A0EC',
                    bgHighlight: '#57A0EC',
                    color: '#666',
                    active: '#FFF',
                    highlight: '#FFF',
                    size: 14,
                    lineColor: '#ccc',
                    textAlign: 'center',
                },
                // icon: {
                //         w: 0
                // }
            },
            animation: true,
            maskClose: true,
            items: [{
                status: 'normal',
                text: "KHFKLLKJ"
            }, {
                status: 'normal',
                text: "KHFKLLKJ"
            }, {
                status: 'selected',
                text: "KHFKLLKJ"
            }, {
                status: 'normal',
                text: "KHFKLLKJ"
            }, {
                status: 'normal',
                text: "KHFKLLKJ"
            }, {
                status: 'normal',
                text: "KHFKLLKJ"
            }]
        }, function(ret, err) {
            //console.log(JSON.stringify(ret));
            if (ret.eventType == 'clickLeft' || ret.eventType == 'clickRight') {
                UIMultiSelector.hide();
            }
            if (ret.eventType == 'clickItem') {
                value = ret.items[0];
            }
        });
    }

    function login() {
        api.openWin({ slidBackEnabled:false,
            name: 'index',
            url: 'index.html',
            pageParam: {}
        });
    }
</script>