00001 00009 package com.rosalfred.core.ia; 00010 00011 import org.ros.node.ConnectedNode; 00012 import org.ros.node.topic.Publisher; 00013 import org.rosbuilding.common.IModule; 00014 00015 import com.rosalfred.core.ia.rivescript.RiveScript; 00016 00017 import smarthome_comm_msgs.Command; 00018 00024 public abstract class CommandPublisher { 00025 00026 protected final Object lockInstance = new Object(); 00027 00028 protected ConnectedNode node; 00029 protected volatile RosRiveScript rivescript; 00030 protected Publisher<Command> publisher; 00031 00032 public CommandPublisher(RiveScript rivescript) { 00033 this.rivescript = RosRiveScript.getRosRiveScript(rivescript); 00034 this.initialize(); 00035 } 00036 00041 protected synchronized void initialize() { 00042 if (node == null) { 00043 synchronized (this.lockInstance) { 00044 if (node == null) { 00045 this.node = this.rivescript.getNode(); 00046 this.publisher = this.rivescript.getPublisherSay(); 00047 00048 if ((this.node == null) || (this.publisher == null)) 00049 throw new RuntimeException(); 00050 } 00051 } 00052 } 00053 } 00054 00059 protected void publish(String method) { 00060 this.publish(method, ""); 00061 } 00062 00067 protected void publish(String method, String uri) { 00068 this.node.getLog().info("send sub command : " + method); 00069 00070 Command message = 00071 this.node.getTopicMessageFactory().newFromType(Command._TYPE); 00072 message.setAction(IModule.SEP + method); 00073 message.setSubject(uri); 00074 00075 this.publish(message); 00076 } 00077 00082 protected void publish(Command msg) { 00083 msg.getContext().setWho(IaNode.botname); 00084 msg.getContext().setWhere( // TODO Mapping by knowedge relation 00085 this.getUserParam(IaNode.VAR_CONTEXT_WHERE)); 00086 this.publisher.publish(msg); 00087 00088 try { 00089 Thread.sleep(1); 00090 } catch (InterruptedException e) { } 00091 } 00092 00098 protected String getUserParam(String key) { 00099 return this.rivescript.getUtils().getUserParam(key); 00100 } 00101 00107 protected void setUserParam(String key, String value) { 00108 this.rivescript.getUtils().setUserParam(key, value); 00109 } 00110 }