package example.binarylight;
import org.teleal.cling.binding.annotations.UpnpAction;
import org.teleal.cling.binding.annotations.UpnpInputArgument;
import org.teleal.cling.binding.annotations.UpnpOutputArgument;
import org.teleal.cling.binding.annotations.UpnpService;
import org.teleal.cling.binding.annotations.UpnpServiceId;
import org.teleal.cling.binding.annotations.UpnpServiceType;
import org.teleal.cling.binding.annotations.UpnpStateVariable;
@UpnpService(
serviceId = @UpnpServiceId("ElectricfanService"), //serviceId
serviceType = @UpnpServiceType(value = "ElectricfanService", version = 1) //SERVICE TYPE 和版本
)
public class ElectricfanService {
@UpnpStateVariable(defaultValue = "0", sendEvents = false)
private boolean target = false;
@UpnpStateVariable(defaultValue = "0")
private boolean status = false;
@UpnpAction
public void setTarget(@UpnpInputArgument(name = "NewTargetValue")
boolean newTargetValue) {
target = newTargetValue;
status = newTargetValue;
System.out.println("myService is: " + status);
}
@UpnpAction(out = @UpnpOutputArgument(name = "RetTargetValue"))
public boolean getTarget() {
return target;
}
@UpnpAction(out = @UpnpOutputArgument(name = "ResultStatus"))
public boolean getStatus() {
return status;
}
@UpnpStateVariable(defaultValue = "0", sendEvents = false)
private String aa;
@UpnpAction
public void setAa(@UpnpInputArgument(name = "rr")
String rr) {
aa = rr;
System.out.println("myService setAa is: " + rr);
}
}
1、 target和status是官方文档里面例子,暂时不明白为什么需要两个参数。
2、@UpnpStateVariable(defaultValue = "0", sendEvents = false)
private String aa;
可以直接改为:
@UpnpStateVariable
private String aa;
不知道这样有什么影响。
3、aa是自己写的变量,使用string类型,提供一个action方法,要求符合java bean规范:setAa这种写法。rr是控制点传入参数Key值,输入输出使用key value的形式。
4、控制点调用如下:
基本使用官方demo
package example.binarylight;
import java.net.URI;
import java.net.URISyntaxException;
import org.teleal.cling.UpnpService;
import org.teleal.cling.UpnpServiceImpl;
import org.teleal.cling.controlpoint.ActionCallback;
import org.teleal.cling.model.action.ActionInvocation;
import org.teleal.cling.model.message.UpnpResponse;
import org.teleal.cling.model.message.header.STAllHeader;
import org.teleal.cling.model.meta.RemoteDevice;
import org.teleal.cling.model.meta.Service;
import org.teleal.cling.model.types.InvalidValueException;
import org.teleal.cling.model.types.ServiceId;
import org.teleal.cling.model.types.UDAServiceId;
import org.teleal.cling.registry.DefaultRegistryListener;
import org.teleal.cling.registry.Registry;
import org.teleal.cling.registry.RegistryListener;
public class ElectricfanClient implements Runnable {
public static void main(String[] args) throws Exception {
// Start a user thread that runs the UPnP stack
Thread clientThread = new Thread(new ElectricfanClient());
clientThread.setDaemon(false);
clientThread.start();
}
public void run() {
try {
UpnpService upnpService = new UpnpServiceImpl();
// Add a listener for device registration events
upnpService.getRegistry().addListener(
createRegistryListener(upnpService)
);
// Broadcast a search message for all devices
upnpService.getControlPoint().search(
new STAllHeader()
);
} catch (Exception ex) {
System.err.println("Exception occured: " + ex);
System.exit(1);
}
}
// DOC: REGISTRYLISTENER
RegistryListener createRegistryListener(final UpnpService upnpService) {
return new DefaultRegistryListener() {
ServiceId serviceId = new UDAServiceId("ElectricfanService");
@Override
public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
Service switchPower;
if ((switchPower = device.findService(serviceId)) != null) {
System.out.println("Service discovered: " + switchPower);
executeAction(upnpService, switchPower);
}
}
@Override
public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
Service switchPower;
if ((switchPower = device.findService(serviceId)) != null) {
System.out.println("Service disappeared: " + switchPower);
}
}
};
}
// DOC: REGISTRYLISTENER
// DOC: EXECUTEACTION
void executeAction(UpnpService upnpService, Service switchPowerService) {
ActionInvocation setTargetInvocation =
new SetTargetActionInvocation(switchPowerService);
// Executes asynchronous in the background
upnpService.getControlPoint().execute(
new ActionCallback(setTargetInvocation) {
@Override
public void success(ActionInvocation invocation) {
assert invocation.getOutput().length == 0;
System.out.println("Successfully called action!");
}
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation,
String defaultMsg) {
System.err.println(defaultMsg);
}
}
);
}
// class SetTargetActionInvocation extends ActionInvocation {
//
// SetTargetActionInvocation(Service service) {
// super(service.getAction("SetTarget"));
// try {
//
// // Throws InvalidValueException if the value is of wrong type
// setInput("NewTargetValue", true);
//
// } catch (InvalidValueException ex) {
// System.err.println(ex.getMessage());
// System.exit(1);
// }
// }
// }
class SetTargetActionInvocation extends ActionInvocation {
SetTargetActionInvocation(Service service) {
super(service.getAction("SetAa"));
try {
// Throws InvalidValueException if the value is of wrong type
setInput("rr", "1234");
} catch (InvalidValueException ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
}
}
// DOC: EXECUTEACTION
}
主要区别在于:
1、 ServiceId serviceId = new UDAServiceId("ElectricfanService");
定义为自己需要的service
2、 super(service.getAction("SetAa"));
调用自己定义的action
3、传入自己的key value
setInput("rr", "1234");
以上基本实现控制点控制设备的服务功能。
按照理解,UPnP-av-ContentDirectory-v1-Service应该也是通过这种方式的,无非自己定义通讯的key value,加上设备需要启动一个web容器,然后提供对外资源暴露的功能。
以控制电风扇例子为例,简单家电需要提供的服务如下:
1、开关功能。 (电风扇开关)
2、功能列表查询功能。 (档位控制,定时控制)
3、具体功能控制
a、档位增减
b、定时多少
一般设备功能:
1、开关
2、控制量的增减
3、定时
难点:
1、设备要求支持网络模块(upnp协议)以及服务的实现
2、控制点可以为电脑,智能手机。
分享到:
相关推荐
基于Cling库实现的DMC,可以访问upnp设备共享的图片,音乐,视频,并进行显示,播放
Android Upnp 库 查看示例项目以获取快速入门。 如何使用? 包括位于项目“out”目录中的 jar 文件。 编辑您的清单以提供适当的权限和服务描述(参见示例) 然后,您可以对 AndroidUpnpProvider 对象执行方法调用...
关于cling库的DLNA开发,对cling库有比较详细的解说,以及代码
cling-core-2.0.1及三个依赖包,用于Java对UPnP操作的实现 cling-core-2.0.1.jar seamless-http-1.1.1.jar seamless-util-1.1.1.jar seamless-xml-1.1.1.jar
使用cling 开发基于upnp协议的服务
android upnp client and server use cling 2.0
upnp开发,Android,java,引用cling
cling-core-2.1.2.jar和cling-support-2.1.2.jar 下载
upnp开源代码cling项目,基于java实现upnp协议,可用来开发移动设备和桌面设备的网络应用。
upnp开源代码cling项目,基于java实现upnp协议,可用来开发android移动设备、桌面设备和网络设备的网络应用。
cling-support-2.1.1.jar和cling-core-2.1.1.jar电视投屏相关工具
一个UPnP协议的java sdk,可以使用java语言快速开发UPnP应用
一个UPnP协议的java sdk,可以使用java语言快速开发UPnP应用。
利用cling实现Render播放器端框架简单例子,利用qq音乐app可以搜索到此例子 利用cling实现Render播放器端框架简单例子,利用qq音乐app可以搜索到此例子
收集最新的upnp开源库(2016-3-2)。 压缩包包含以下内容: cling-master cybergarage-upnp-master gupnp-0.20.16 libupnp-1.6.19 PlatinumKit-1-0-5
Android-Cling Example of use is available in Solid Launcher What is Cling library Cling is a library that allows you to create overlays that will allow the user to be informed of how your ...
cling-dlna非常好的开发软件,支持java开发,基础dlna开发包
cling-master.zip
Cling是欧洲核子研究中心(CERN)开发的C++交互式编译器。该PPT介绍了Cling的应用场景、实现以及如何在Cling中实现动态地检测空指针解引用错误。 关于作者:丁保增,中科院软件所博士在读,研究方向:系统安全,...
基于第三方cling库的安卓upnp投屏及其控制代码,适合初学者