Skip to content

Instantly share code, notes, and snippets.

@kntyskw
Created June 26, 2018 22:36
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/4f06a40295fedf5eb3bb3cfb79edbdb9 to your computer and use it in GitHub Desktop.
Save kntyskw/4f06a40295fedf5eb3bb3cfb79edbdb9 to your computer and use it in GitHub Desktop.
DLNA対応TVをSORACOM Inventoryに登録してWebコンソールから操作する ref: https://qiita.com/kntyskw/items/bae6ef159ebd6af5c6e4
import org.fourthline.cling.UpnpService;
import org.fourthline.cling.UpnpServiceImpl;
import org.fourthline.cling.model.message.header.STAllHeader;
import org.yasukawa.inventory.upnp.InventoryUpnpRegistryListener;
import org.yasukawa.inventory.upnp.UpnpController;
public class App {
public static void main(String[] args) throws Exception {
UpnpService upnpService = new UpnpServiceImpl();
UpnpController.initialize(upnpService);
upnpService.getRegistry().addListener(new InventoryUpnpRegistryListener(upnpService));
upnpService.getControlPoint().search(new STAllHeader());
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'
repositories {
mavenCentral()
maven { url 'https://soracom.github.io/maven-repository/' }
maven { url 'http://4thline.org/m2/' } // <-追加
}
def INVENTORY_AGENT_VERSION="0.0.5"
dependencies {
compile "io.soracom:soracom-inventory-agent-for-java-core:$INVENTORY_AGENT_VERSION"
compile "org.fourthline.cling:cling-core:2.1.1" // <-追加
testCompile 'junit:junit:4.12'
}
jar {
manifest {
attributes "Main-Class": "App"
}
}
mainClassName = 'App'
$ ./bin/inventory-upnp
$ git clone https://github.com/kntyskw/inventory-upnp
$ cd inventory-upnp
$ gradle distZip
$ git add inventory-upnp.zip
$ git commit -m "add/update inventory-upnp.zip"
$ git push resin master
@Override
public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
logger.info("Device {} with type {} is added", device.getIdentity().toString(), device.getType().toString());
if (MediaRendererAgent.MEDIA_RENDERER_DEVICE_TYPE.equals(device.getType().toString())){
logger.info("Media renderer found: " + device.getDetails().getFriendlyName());
MediaRendererAgent agent = new MediaRendererAgent(device);
logger.info("Starting agent for {} and registering to SORACOM Inventory",
device.getIdentity().toString());
agent.start();
agentMap.put(device.getIdentity().toString(), agent);
}
}
@Override
public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
logger.info("Device {} with type {} is removed", device.getIdentity().toString(), device.getType().toString());
if (agentMap.containsKey(device.getIdentity().toString())){
logger.info("Stopping agent for {} and deregistering from SORACOM Inventory",
device.getIdentity().toString());
agentMap.remove(device.getIdentity().toString()).stop();
}
}
package models;
import io.soracom.inventory.agent.core.util.TypedAnnotatedObjectTemplateClassGenerator;
import java.io.File;
public class JavaSourceGenerator {
public static void main(String[] args) {
String javaPackage = "models";
File sourceFileDir = new File("src/main/java");
TypedAnnotatedObjectTemplateClassGenerator generator = new TypedAnnotatedObjectTemplateClassGenerator(
javaPackage, sourceFileDir);
File[] modelFiles = new File("src/main/resources").listFiles();
for (File modelFile : modelFiles) {
generator.generateTemplateClassFromObjectModel(modelFile);
}
System.out.println("Finished generating Java source.");
System.exit(0);
}
}
package org.yasukawa.inventory.upnp.media_renderer;
import org.fourthline.cling.model.meta.RemoteDevice;
import org.fourthline.cling.model.meta.Service;
import org.fourthline.cling.model.types.ServiceType;
import org.yasukawa.inventory.upnp.UpnpDeviceAgent;
public class MediaRendererAgent extends UpnpDeviceAgent {
public static final String MEDIA_RENDERER_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaRenderer:1";
public static final String RENDERING_CONTROL_SERVICE_TYPE = "urn:schemas-upnp-org:service:RenderingControl:1";
public static final String AV_TRANSPORT_SERVICE_TYPE = "urn:schemas-upnp-org:service:AVTransport:1";
public static final String ATTR_LAST_CHANGE = "LastChange";
public static final String ATTR_INSTANCE_ID = "InstanceID";
public MediaRendererAgent(RemoteDevice device){
super(device);
for ( Service service : device.getServices()){
System.out.println(service.getServiceType());
System.out.println(service.getServiceId());
}
super.initializer.addInstancesForObject(new UpnpRenderingControlServiceObject(
device.findService(ServiceType.valueOf(RENDERING_CONTROL_SERVICE_TYPE)))
);
super.initializer.addInstancesForObject(new UpnpAvTransportServiceObject(
device.findService(ServiceType.valueOf(AV_TRANSPORT_SERVICE_TYPE)))
);
}
}
<Item ID="2216">
<Name>Volume</Name>
<Operations>R</Operations>
<MultipleInstances>Single</MultipleInstances>
<Mandatory>Mandatory</Mandatory>
<Type>Integer</Type>
<RangeEnumeration />
<Units></Units>
<Description>Current audio volume of the renderer</Description>
</Item>
package models;
import io.soracom.inventory.agent.core.lwm2m.*;
import java.util.Date;
import org.eclipse.leshan.core.node.ObjectLink;
/**
* LwM2M device object model for UPnP Rendering Control service.
**/
@LWM2MObject(objectId = 30001, name = "UPnP Rendering Control Service", multiple = true)
public class UPnPRenderingControlServiceObject extends AnnotatedLwM2mInstanceEnabler {
/**
* Mute status of the renderer
**/
@Resource(resourceId = 2215, operation = Operation.Read)
public Boolean readMute() {
throw LwM2mInstanceResponseException.notFound();
}
/**
* Current audio volume of the renderer
**/
@Resource(resourceId = 2216, operation = Operation.Read)
public Integer readVolume() {
throw LwM2mInstanceResponseException.notFound();
}
/**
* Sets the renderer audio volume.
**/
@Resource(resourceId = 2430, operation = Operation.Execute)
public void executeSetVolume(String executeParameter) {
throw LwM2mInstanceResponseException.notFound();
}
}
/**
* Current audio volume of the renderer
**/
@Resource(resourceId = 2216, operation = Operation.Read)
public Long readVolume() {
ActionInvocation actionInvocation =
new ActionInvocation(renderingControlService.getAction(ACTION_GET_VOLUME));
actionInvocation.setInput(ARG_INSTANCE_ID, new UnsignedIntegerFourBytes(0));
actionInvocation.setInput(ARG_CHANNEL, VALUE_CHANNEL_MASTER);
Map<String, ActionArgumentValue> output = UpnpController.getInstance().executeUpnpAction(actionInvocation);
return ((UnsignedIntegerTwoBytes)output.get(ARG_CURRENT_VOLUME).getValue()).getValue();
}
/**
* Sets the renderer audio volume.
**/
@Resource(resourceId = 2430, operation = Operation.Execute)
public void executeSetVolume(String executeParameter) {
ActionInvocation setVolumeInvocation =
new ActionInvocation(renderingControlService.getAction(ACTION_SET_VOLUME));
setVolumeInvocation.setInput(ARG_INSTANCE_ID, new UnsignedIntegerFourBytes(0));
setVolumeInvocation.setInput(ARG_CHANNEL, VALUE_CHANNEL_MASTER);
setVolumeInvocation.setInput(ARG_DESIRED_VOLUME, new UnsignedIntegerTwoBytes(executeParameter));
UpnpController.getInstance().executeUpnpAction(setVolumeInvocation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment