Jump to content

Recommended Posts

Posted (edited)

Hello everyone,

I need help with the extension of a library script "Scene Test with multiple Shellies"
Project:
The code should switch the output of multiple Shelly's in the same subnet (192.168.1.XXX)
Which would also work if the Shellys were not provided with a user and password.
The current response is "401 Unauthorized"


Can someone help me how to extend the code for an authentication with username and password?

The webhooks / HTTP(S) requests would be for example: http://admin:PASSWORD@192.168.1.13/relay/0?turn~3don

Quote

 

// Copyright 2021 Allterco Robotics EOOD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Shelly is a Trademark of Allterco Robotics

// Shelly Script example: Scene Test with multiple Shellies
//
// Playing a scene with four Shellies with that have a lamp as a load.
// Demonstration of a "Remote Shelly" wrapper object. object prototyping, and
// simple schene player

let CONFIG = {
  mac: 0,
};

Shelly.call(
  "sys.getstatus",
  {},
  function (result, error_code, error_message, user_data) {
    if (error_code === 0) {
      CONFIG.mac = result.mac;
    }
  },
  null
);

let RemoteShelly = {
  _cb: function (result, error_code, error_message, callback) {
    let rpcResult = JSON.parse(result.body);
    let rpcCode = result.code;
    let rpcMessage = result.message;
    callback(rpcResult, rpcCode, rpcMessage);
  },
  composeEndpoint: function (method) {
    print("hello");
    print(this.address);
    return "http://" + this.address + "/rpc/" + method
  },

  call: function (rpc, data, callback) {
    let postData = {
      url: this.composeEndpoint(rpc),
      body: data,
    };
    Shelly.call("HTTP.POST", postData, RemoteShelly._cb, callback);
  },
  getInstance: function (address) {
    let rs = Object.create(this);
    // remove static method
    rs.getInstance = null;
    rs.address = address;
    return rs;
  },
};

let blueShelly1 = RemoteShelly.getInstance("192.168.1.15");
let blueShelly2 = RemoteShelly.getInstance("192.168.1.14");
let blueShelly3 = RemoteShelly.getInstance("192.168.1.13");

function setSwitch(swObj, how) {
  swObj.call(
    "switch.set",
    { id: 0, on: how },
    function (result, error_code, message) {
      print(JSON.stringify(result), error_code, message);
    }
  );
}

let Scene = [
  {
    delay: 1000,
    fn: function () {
      setSwitch(Shelly, true);
    },
  },
  {
    delay: 1000,
    fn: function () {
      setSwitch(blueShelly1, true);
    },
  },
  {
    delay: 1000,
    fn: function () {
      setSwitch(blueShelly2, true);
    },
  },
  {
    delay: 1000,
    fn: function () {
      setSwitch(blueShelly3, true);
    },
  },
  {
    //cond will check the return value of fn to decide to continue
    type: "cond",
    fn: function () {
      return true;
    },
  },
  {
    delay: 1000,
    fn: function () {
      setSwitch(Shelly, false);
    },
  },
  {
    delay: 1000,
    fn: function () {
      setSwitch(blueShelly1, false);
    },
  },
  {
    delay: 1000,
    fn: function () {
      setSwitch(blueShelly2, false);
    },
  },
  {
    delay: 1000,
    fn: function () {
      setSwitch(blueShelly3, false);
    },
  },
  {
    //run will just execute fn
    fn: function () {
      print("end-of-scene");
    },
  },
];

let ScenePlayer = {
  currentScene: null,
  sceneCounter: -1,
  sceneTimer: null,
  loop: false,
  play: function (scene, loop) {
    if (typeof loop !== "undefined") {
      this.loop = loop;
    }
    Timer.clear(this.sceneTimer);
    this.sceneCounter = -1;
    this.currentScene = scene;
    this.next();
  },
  next: function () {
    if (!this.currentScene) {
      return;
    }
    this.sceneCounter++;
    let delay = 1;
    if (this.sceneCounter === this.currentScene.length) {
      if (this.loop) {
        this.sceneCounter = -1;
        this.step(delay);
      }
      return;
    }
    if (
      this.currentScene[this.sceneCounter].type === "cond" &&
      !this.currentScene[this.sceneCounter].fn()
    ) {
      return;
    }
    this.currentScene[this.sceneCounter].fn();
    if (typeof this.currentScene[this.sceneCounter].delay !== "undefined") {
      delay = this.currentScene[this.sceneCounter].delay;
    }
    this.step(delay);
  },
  step: function (delay) {
    this.sceneTimer = Timer.set(
      delay,
      false,
      function (sp) {
        sp.next();
      },
      this
    );
  },
  cancel: function () {
    Timer.clear(this.sceneTimer);
    this.sceneCounter = -1;
    this.currentScene = null;
  },
};

ScenePlayer.play(Scene, true);

 

 

Edited by lexnared

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...