• Popular
  • Users
  • Login
Flic Home

Community

  • Login
  • Search
  • Popular
  • Users
  1. Home
  2. oskaremilsson
  • Profile
  • Following 0
  • Followers 0
  • Topics 5
  • Posts 15
  • Best 1
  • Controversial 0
  • Groups 0

oskaremilsson

@oskaremilsson

2
Reputation
52
Profile views
15
Posts
0
Followers
0
Following
Joined 20 Apr 2021, 07:36 Last Online 21 Oct 2024, 16:04
Website oskaremilsson.se/ Location Sweden

oskaremilsson Unfollow Follow

Best posts made by oskaremilsson

  • IRTCP v1.0.0 (Hub server for using IR module via HTTP)

    What is it?
    Hub TCP server for using the IR module via HTTP.
    Both to record of new signals with a given name and to play them back.
    Using net since SDK doesn't have the nodejs HTTP-module available.

    Why?
    I really wanted to be able to call my IR module from my phone homescreen or Google Home (via IFTTT). I got it working so I wanted to share it if anyone else find it helpful 🙂

    The code is what could be called a POC, so code feedback and/or changes are totally welcome.

    How does it work?

    1. Record a signal GET {ip}:1338?record={name_of_signal}
    2. Plays a signal GET {ip}:1338?cmd={name_of_signal}
    3. Cancel recording GET {ip}:1338?cancelRecord=true

    Example;

    1. call GET 192.168.0.100:1338?record=volume_up
    2. Press Volume Up on remote towards IR module
    3. returns 200 OK Signal volume_up stored!
    4. call GET 192.168.0.100:1338?cmd=volume_up
    5. returns 200 OK Signal sent!
    6. Volume goes up 🙂

    The code

    module.json

    {
    	"name": "IRTCP",
    	"version": "1.0.0"
    }
    

    main.js

    // main.js
    const net = require("net");
    const ir = require("ir");
    const datastore = require("datastore");
    
    const utils = require("./utils");
    
    const respondToClient = function(c, statusCode, message) {
    	c.write(
    		"HTTP/1.1 " + statusCode + "\r\n" +
    		"\r\n"
    	);
    	c.write(message);
    	c.end();
    }
    
    const handleCmd = function(c, cmd) {
    	datastore.get(cmd, function(err, strBuffer) {
    		if (!err && strBuffer) {
    			var signal = new Uint32Array(utils.str2ab(strBuffer));
    			if (!ArrayBuffer.isView(signal)) {
    				return respondToClient(c, "422 Unprocessable Entity", "Unknown signal");
    			}
    
    			ir.play(signal, function(err) {
    				if (!err) {
    					return respondToClient(c, "200 OK", "Signal sent");
    				}
    				return respondToClient(c, "500 Internal Server Error", "Couldn't send signal");
    			});
    		} else {
    			return respondToClient(c, "422 Unprocessable Entity", "Unknown cmd");
    		}
    	});
    }
    
    const handleRecord = function(c, name) {
    	/* TODO: add a timeout for listening for complete */
    	ir.record();
    	console.log("recording signal...");
    
    	ir.on("recordComplete", function(signal) {	
    		datastore.put(name, utils.ab2str(signal.buffer), function(err) {
    			if (!err) {
    				console.log("Signal " + name + " stored!");
    				return respondToClient(c, "200 OK", "Signal " + name + " stored!");
    			} else {
    				return respondToClient(c, "500 Internal Server Error", "Couldn't store signal");
    			}
    		});
    	});
    }
    
    
    var server = net.createServer(function(c) {
      console.log('server connected');
      c.on('end', function() {
        console.log('server disconnected');
      });
    
    	c.on('data', function(data) {
    		console.log(data);
    		var match = data.toString().match(/GET \/\?.[^ ]*/);
    
    		if (!match) {
    			return respondToClient(c, "403 Forbidden", "Forbidden method");
    		}
    
    		var params = utils.parseParams(match[0].split("GET /?")[1]);
    		
    		if (params.cmd) {
    			return handleCmd(c, params.cmd);
    		} else if (params.record && params.record.length > 0) {
    			return handleRecord(c, params.record);
    		} else if (params.cancelRecord) {
    			ir.cancelRecord();
    			console.log("recording canceled!");
    			return respondToClient(c, "200 OK", "Recording canceled!");
    		} else {
    			return respondToClient(c, "403 Forbidden", "Forbidden params");
    		}
    	});
    });
    
    server.listen(1338, "0.0.0.0", function() {
      console.log('server bound', server.address().port);
    });
    

    utils.js

    const ab2str = function(buf) {
      return String.fromCharCode.apply(null, new Uint16Array(buf));
    }
    
    const str2ab = function(str) {
      var buf = new ArrayBuffer(str.length*2);
      var bufView = new Uint16Array(buf);
      for (var i=0, strLen=str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
      }
      return buf;
    }
    
    const parseParams = function (s) {
    	var obj = {};
    	var arr = s.split("&");
    	var temp;
    
    	for (i = 0; i < arr.length; i++) {
    		temp = arr[i].split("=");
    		obj[temp[0]] = temp[1];
    	}
    	
    	return obj;
    };
    
    exports.ab2str = ab2str;
    exports.str2ab = str2ab;
    exports.parseParams = parseParams;
    
    posted in Flic Hub SDK
    oskaremilsson
    oskaremilsson
    6 May 2021, 12:51

Latest posts made by oskaremilsson

  • RE: IRTCP v1.0.0 (Hub server for using IR module via HTTP)

    @johan-0 This is the best thing I have ever encountered in my life of a developer. I just googled the issue I got in console and arrived at my own thread where the answer was already solved. hahah

    posted in Flic Hub SDK
    oskaremilsson
    oskaremilsson
    21 Oct 2024, 16:04
  • RE: Cant connect to hub with Fold 3

    @oskaremilsson yup. Erm. So.. this is awkward.
    The phones BLE scanner must have gotten stuck in some bad state.
    A restart of the phone worked 🤦‍♂️

    Didn't cross my mind that the issue could be that deep into the system.
    As a dev I should've tested that as #1 though 🙃

    Thanks, and sorry! 😅

    posted in Flic Hub
    oskaremilsson
    oskaremilsson
    14 Sept 2021, 09:20
  • RE: Cant connect to hub with Fold 3

    @Emil it was added first (I guess inherited from my account?). But I only got time out when trying to connect, so I deleted it in order to try to readd.

    posted in Flic Hub
    oskaremilsson
    oskaremilsson
    14 Sept 2021, 09:08
  • RE: Cant connect to hub with Fold 3

    @Emil
    The app don't show the hub at all in scanning. Other devices finds the hub in seconds.

    posted in Flic Hub
    oskaremilsson
    oskaremilsson
    13 Sept 2021, 07:25
  • Cant connect to hub with Fold 3

    Hi!
    I can't connect to my hub with a Samsung Galaxy Fold 3.
    The hub works with buttons, I can reach SDK, I can connect with an Samsung Galaxy Note 20 Ultra.
    But it fails with the Fold.

    I have bluetooth on, I have given all the permissions. I've reinstalled the app. I've restated the hub. I've tried anything I can think of.

    Any ideas?

    posted in Flic Hub
    oskaremilsson
    oskaremilsson
    12 Sept 2021, 17:54
  • RE: IRTCP v1.0.0 (Hub server for using IR module via HTTP)

    @jitmo simply amazing!
    If I were to put the original code on GitHub, would you be up to make these changes there so we get a nice history and stuff?

    posted in Flic Hub SDK
    oskaremilsson
    oskaremilsson
    19 Aug 2021, 20:07
  • RE: Promises instead of Callbacks?

    @Emil Ok, thanks for info 🙂

    posted in Flic Hub SDK
    oskaremilsson
    oskaremilsson
    7 May 2021, 18:06
  • Promises instead of Callbacks?

    Is there any way of using Promises for stuff like Datastore.get() instead of callback?

    posted in Flic Hub SDK
    oskaremilsson
    oskaremilsson
    7 May 2021, 12:40
  • RE: IRTCP v1.0.0 (Hub server for using IR module via HTTP)

    @Emil Thanks, and thank you for quick responses on questions

    posted in Flic Hub SDK
    oskaremilsson
    oskaremilsson
    7 May 2021, 12:38
  • RE: IRTCP v1.0.0 (Hub server for using IR module via HTTP)

    I forgot to add how I'm calling this from my homecreen of my phone.
    I'm using HTTP Shortcuts-app widget.

    posted in Flic Hub SDK
    oskaremilsson
    oskaremilsson
    6 May 2021, 20:01