Friday, 27 May 2022

callback is not a function - castv2

I'm following this http://siglerdev.us/blog/2021/02/26/google-home-message-broadcast-system-node-js/31 which uses this library castv2-client to send messages to my google home. It works. I get the messages no problem, but the code throws

C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72
    callback(null, response.status.volume);
    ^

TypeError: callback is not a function
    at C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72:5                                                                  ver.js:72
    at fn.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\request-response.js:27:7)
    at fn.emit (events.js:203:15)
    at Channel.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\controller.js:16:10)                                           s\receiver.js:72:5
    at Channel.emit (events.js:198:13)                                              lib\controllers\request-response.js:27:7)
    at Client.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2\lib\channel.js:23:10)                                                                  ient\lib\controllers\controller.js:16:10)
    at Client.emit (events.js:203:15)
    at PacketStreamWrapper.onpacket (C:\Users\Phil\Documents\google home\node_module\channel.js:23:10)s\castv2\lib\client.js:81:10)
    at PacketStreamWrapper.emit (events.js:198:13)                                  s\castv2\lib\client.js:81:10)
    at TLSSocket.<anonymous> (C:\Users\Phil\Documents\google home\node_modules\castv2\lib\packet-stream-wrapper.js:28:16)                     

What's wrong with the code that is throwing this AND/OR how can I fix it so it's either more graceful in catching error and doesn't throw since the message still delivers to google home or fix it to not throw this at all? I appreciate any help!

I believe it's here in the castv2-client library that it's referencing, but I haven't been able to make it happy.

ReceiverController.prototype.launch = function(appId, callback) {
  this.request({ type: 'LAUNCH', appId: appId }, function(err, response) {
    if(err) return callback(err);
    if(response.type === 'LAUNCH_ERROR') {
      return callback(new Error('Launch failed. Reason: ' + response.reason));
    }
    callback(null, response.status.applications || []);
  });
};

my code

var Client = require('castv2-client').Client;
var DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver;
const googleTTS = require('google-tts-api');

var App = {
  playin: false,
  DeviceIp: "",
  Player: null,
  GoogleHome: function (host, url) {
    var client = new Client();
    client.connect(host, function () {
      client.launch(DefaultMediaReceiver, function (err, player) {
        client.setVolume({ level: 1 });
        var media = {
            contentId: url,
            contentType: 'audio/mp3',
            streamType: 'BUFFERED'
        };
        App.Player = player;
        App.Player.load(media, { autoplay: true }, function (err, status) {
            App.Player.on('status', function (status) {
                if (status.playerState === "IDLE" && App.playin === false) {
                    client.close();
                }
            });
        });
    });
});
client.on('error', function (err) {
  console.log('Error: %s', err.message);
  client.close();
});
},
run: function (ip, text) {
  App.DeviceIp = ip;
  const url = googleTTS.getAudioUrl(text, {
      lang: 'en-US',
      slow: false,
      host: 'https://translate.google.com',
  });
  App.GoogleHome(App.DeviceIp, url, function (res) {
      console.log(res);
  })
},
broadcast: function(text){
  const ips = '192.168.0.15'.split(","); //From config, 192.168.68.105,192.168.68.107,192.168.68.124
  for (var s of ips){
      App.run(s, text);
  }
}
}

App.broadcast("Broadcasted to all of the devices"); //Only works if you did step 4.5


from callback is not a function - castv2

How do I use np.linspace() to change the pixels colors of a patch in an image

I drew a straight line by changing the color of 50 pixels in the image below. The line is the diagonal line shown on the image

enter image description here

Here is how I did it.

zeros = torch.zeros_like(img) #img is b,c,h,w

x1, y1, x2, y2, r, g,  b = 16, 50, 100, 100, 6, 2, 4
rgb = torch.tensor([r, g, b])
cx, cy = x2 - x1, y2 - y1

for t in np.linspace(0, 1, 50):
    px, py = x1 + t * cx, y1 + t * cy
    zeros[0, :, int(py), int(px)] = rgb

mask = (zeros == 0).float()
im = mask * img + (1 - mask) *zeros

It is obvious in np.linspace(0, 1, 50) that I am changing 50 pixels. I want to use the same method to change a specific portion of the image, like a 14 x 14 patch of my image if the image size is 224 x 224 or even more. The range of pixels to cover now will be 196 -> (since there are 196 pixels in a 14 x 14 patch) instead of 50. How do I go about it, please?



from How do I use np.linspace() to change the pixels colors of a patch in an image

Storybook: Render a component by clicking on a link (React)

I've got a component, 'Dialog', that renders a modal dialog box basically, when a user clicks on a link or button to invoke/open the dialog. I'm building a storybook component for the Dialog component, and so I need to be able to have a clickable HTML element (anchor tag or button, preferably anchor tag) and open the Dialog component when that clickable HTML element is clicked. I don't know how to go about this in Storybook.

Here is my storybook element as it exists so far:

const Template: ComponentStory<typeof Dialog> = (args) => <Dialog {...args} />;

export const Regular = Template.bind({});

Regular.args = {
    type:"regular",
    show: false,
    children: "Content of Dialog Box",
    dismissable: true,
    classNames: "",
    maxWidth: "1000px",
    displayBoxShadow: true,
    renderOnLoad: false,
}

An example of an implementation of this component/story would be a Log In button that, when clicked, would render a slide-out dialog with appropriate fields (children). I'm trying to implement the Dialog part of the story, as the Dialog component is invoked when another HTML object on page is clicked/interacted with.



from Storybook: Render a component by clicking on a link (React)