Last active
December 13, 2015 17:56
-
-
Save kntyskw/b1c6a9066d1fe36db9ce to your computer and use it in GitHub Desktop.
ThingをDiscoverしてBeam ref: http://qiita.com/kntyskw/items/bf10475c045199b35982
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install node-upnp-ssdp upnp-device-client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ssdp.on('DeviceUnavailable:upnp:rootdevice', function(info){ | |
var udn = info.usn.split('::')[0]; | |
publish(createDevicePresense(udn, {online: false})); | |
); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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