Skip to content

Instantly share code, notes, and snippets.

@kntyskw
Last active December 13, 2015 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kntyskw/b1c6a9066d1fe36db9ce to your computer and use it in GitHub Desktop.
Save kntyskw/b1c6a9066d1fe36db9ce to your computer and use it in GitHub Desktop.
ThingをDiscoverしてBeam ref: http://qiita.com/kntyskw/items/bf10475c045199b35982
$ npm install node-upnp-ssdp upnp-device-client
var ssdp = require('node-upnp-ssdp');
var UPnPDeviceClient = require('upnp-device-client');
ssdp.on('DeviceFound', function(info){
var client = new UPnPDeviceClient(info.location);
client.getDeviceDescription(function(err, device){
if (!err){
console.log('UPnP: Device ' + device.friendlyName + ' found');
} else {
console.error('UPnP: ' + err);
}
});
});
ssdp.mSearch('upnp:rootdevice');
var ssdp = require('node-upnp-ssdp');
var UPnPDeviceClient = require('upnp-device-client');
var mqtt = require('mqtt');
var mqttClient = mqtt.connect('mqtt://beam.soracom.io');
var publish = function(udn, device){
var event = {
reported: {
devices: {}
}
};
event.reported.devices[udn] = device;
mqtt.publish('$aws/things/kenta-upnp-gateway/shadow/update',
JSON.stringify(device));
};
ssdp.on('DeviceFound', function(info){
var client = new UPnPDeviceClient(info.location);
client.getDeviceDescription(function(err, device){
if (!err){
device.online = true;
publish(device.UDN, device);
} else {
console.error('UPnP: ' + err);
}
});
});
ssdp.mSearch('upnp:rootdevice');
ssdp.on('DeviceUnavailable:upnp:rootdevice', function(info){
var udn = info.usn.split('::')[0];
publish(createDevicePresense(udn, {online: false}));
);
var topicDevicesRoot = '$aws/things/kenta-upnp-gateway/devices'
mqttClient.on('connect', function () {
mqttClient.subscribe(topicDevicesRoot + '/#');
});
mqttClient.on('message', function (topic, buffer) {
if (topic.indexOf(topicDevicesRoot) === 0){
executeAction(getContextFromTopic(topic),
JSON.parse(buffer.toString()));
} else {
console.error('MQTT: Unknown message received on topic ' + topic);
console.error(buffer.toString());
}
});
$ mosquitto_pub -h beam.soracom.io -t '$aws/things/kenta-upnp-gateway/devices/<TVのUUID>/RenderingControl/SetVolume' -m '{"InstanceID": 0, "Channel": "Master", "DesiredVolume": 26}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment