UBAINS

 找回密码
 立即注册
搜索
热搜: UBAINS
查看: 2252|回复: 0

[设备模板] IdeaHub或Board的简单控制

[复制链接]

10

主题

11

帖子

447

积分

版主

Rank: 7Rank: 7Rank: 7

积分
447
QQ
发表于 2021-6-16 20:06:58 | 显示全部楼层 |阅读模式
本帖最后由 pidtfork 于 2021-6-17 16:04 编辑



  1. /**
  2. * @description IdeaHub 和 Board 的简单控制,控制设备休眠/唤醒/关机/信号切换功能
  3. *              使用此方法前必须设备WEB后台打开HTTP,知道api 用户名和密码
  4. *                                 如果使用网络唤醒也需要在WEB页面打开(有些设备默认关闭网络唤醒)
  5. *                                 wakeUp 是休眠唤醒,设备关机时无法使用此方法唤醒设备
  6. *         2021-6-17 16:02:54        新增 sleep2 方法,原 sleep 方法的api在 21.0.5 版本不起作用
  7. *                                                 可能在 21.0.0 版本即以后版本改了 休眠的 API
  8. * @author pidtfork
  9. * @date 2021-06-16
  10. * @param {Object} conf 设备的信息项
  11. *                 ip                         设备IP                必须
  12. *                 user                 API用户名        必须
  13. *                 password        API密码                必须
  14. *                 mac                        设备MAC地址        非必须
  15. * @param {Function} callback
  16. */
  17. function IdeaHub(conf, callback) {
  18.         this.conf = conf;
  19.         this.httpVCSession = HttpSession();
  20.         this.acCSRFToken = "";
  21.         this.httpPost = function (vAPI, vData) {
  22.                 var header = { 'Content-Type': 'application/json' };
  23.                 return HttpPost('http://' + this.conf.ip + '/action.cgi?ActionID=' + vAPI, JSON.stringify(vData), JSON.stringify(header), this.httpVCSession, 1);
  24.         }
  25.         this.login = function () {
  26.                 try {
  27.                         var receive = this.httpPost('WEB_RequestSessionIDAPI', '');
  28.                         if (receive.indexOf("success") != -1) {
  29.                                 receive = this.httpPost('WEB_RequestCertificateAPI', '{"user":"' + this.conf.user + '","password":"' + this.conf.password + '"}');
  30.                                 if (receive.indexOf("success") != -1) {
  31.                                         var msg = JSON.parse(receive);
  32.                                         msg = JSON.parse(msg.data);
  33.                                         this.acCSRFToken = msg.acCSRFToken;
  34.                                         receive = this.httpPost('WEB_ChangeSessionIDAPI', '');
  35.                                         if (receive.indexOf("success") != -1) {
  36.                                                 return true;
  37.                                         }
  38.                                 }
  39.                         }
  40.                 }
  41.                 catch (err) {

  42.                 }
  43.                 if (typeof callback == 'function') {
  44.                         callback("操作失败");
  45.                 }
  46.                 return false;
  47.         }
  48.         this.sleep = function () {
  49.                 if (this.login()) {
  50.                         this.httpPost('WEB_StartTermSleepAPI', '{"acCSRFToken":"' + this.acCSRFToken + '"}');
  51.                 }
  52.         }
  53.         this.sleep2 = function () {
  54.                 if (this.login()) {
  55.                         this.httpPost('WEB_StartSystemSleepAPI', '{"acCSRFToken":"' + this.acCSRFToken + '"}');
  56.                 }
  57.         }
  58.         this.wakeUp = function () {
  59.                 if (this.login()) {
  60.                         this.httpPost('WEB_SystemWakeUpAPI', '{"acCSRFToken":"' + this.acCSRFToken + '"}');
  61.                 }
  62.         }
  63.         this.reboot = function () {
  64.                 if (this.login()) {
  65.                         this.httpPost('WEB_RequestRebootAPI', '{"acCSRFToken":"' + this.acCSRFToken + '"}');
  66.                 }
  67.         }
  68.         this.powerDown = function () {
  69.                 if (this.login()) {
  70.                         this.httpPost('WEB_RequestPowerDownAPI', '{"acCSRFToken":"' + this.acCSRFToken + '"}');
  71.                 }
  72.         }
  73.         this.switchOPS = function () {
  74.                 if (this.login()) {
  75.                         this.httpPost('WEB_SwitchPageForPcCam', '{"viewStatus":0,"acCSRFToken":"' + this.acCSRFToken + '"}');
  76.                 }
  77.         }
  78.         this.switchAndroid = function () {
  79.                 if (this.login()) {
  80.                         this.httpPost('WEB_SwitchPageForPcCam', '{"viewStatus":1,"acCSRFToken":"' + this.acCSRFToken + '"}');
  81.                 }
  82.         }
  83.         this.switchAUX = function () {
  84.                 if (this.login()) {
  85.                         this.httpPost('WEB_SwitchPageForPcCam', '{"viewStatus":2,"acCSRFToken":"' + this.acCSRFToken + '"}');
  86.                 }
  87.         }
  88.         this.devWakeLan = new _UDP(46426, '255.255.255.255:7')
  89.         this.wakeLan = function () {
  90.                 if (this.conf.mac) {
  91.                         var magicPack = '\xFF\xFF\xFF\xFF\xFF\xFF';
  92.                         for (var i = 0; i < 16; i++) {
  93.                                 magicPack = magicPack + this.conf.mac;
  94.                         }
  95.                         this.devWakeLan.sendCodeString(magicPack);
  96.                 }

  97.         }
  98. }

  99. // 操作不成功时提示 例如连接失败,设备未上电等
  100. function popupTip(msg) {
  101.         sendLog("ideHub message", msg);
  102.         setPageShow("提示");
  103. }

  104. // 实例化终端1
  105. var ideaHub1 = new IdeaHub({
  106.         ip: "192.168.1.103",
  107.         user: "api",
  108.         password: "Change_Me",
  109.         mac: "\x01\x02\x03\x04\x05\x06"
  110. }, popupTip)

  111. // 主要的几个功能
  112. // ideaHub1.sleep(); //休眠
  113. // ideaHub1.sleep2(); //休眠 21.0.0 后版本使用此方法
  114. // ideaHub1.reboot(); //重启
  115. // ideaHub1.wakeUp(); //唤醒
  116. // ideaHub1.powerDown(); //关机
  117. // ideaHub1.switchOPS(); //切换到OPS
  118. // ideaHub1.switchAndroid(); //切换到安卓
  119. // ideaHub1.switchAUX(); //切换到辅流
  120. // ideaHub1.wakeLan();  //网络唤醒


  121. // 实例化第二台  不需要网络唤醒可以不传入 mac
  122. var ideaHub2 = new IdeaHub({
  123.         ip: "192.168.1.104",
  124.         user: "api",
  125.         password: "Change_Me",
  126. }, popupTip)

  127. // 主要的几个功能
  128. // ideaHub2.sleep(); //休眠
  129. // ideaHub2.sleep2(); //休眠 21.0.0 后版本使用此方法
  130. // ideaHub2.reboot(); //重启
  131. // ideaHub2.wakeUp(); //唤醒
  132. // ideaHub2.powerDown(); //关机
  133. // ideaHub2.switchOPS(); //切换到OPS
  134. // ideaHub2.switchAndroid(); //切换到安卓
  135. // ideaHub2.switchAUX(); //切换到辅流


  136. // 实例化第三台 ......
  137. // 实例化第四台 ......
复制代码


高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则

Archiver|手机版|小黑屋|BBS.UBAINSYUN.COM

GMT+8, 2024-4-24 23:30 , Processed in 0.034872 second(s), 19 queries .

Powered by UBAINS! X3.4

© 2001-2017 UBAINS Inc.

快速回复 返回顶部 返回列表