PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/java/src/main/java/ola/OlaClient.java

https://code.google.com/
Java | 430 lines | 203 code | 71 blank | 156 comment | 11 complexity | 5206e14c66b304a411ec1174b5c1035c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. /***********************************************************************
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on
  11. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. *
  15. *************************************************************************/
  16. package ola;
  17. import java.util.logging.Logger;
  18. import ola.proto.Ola.DeviceConfigReply;
  19. import ola.proto.Ola.DeviceConfigRequest;
  20. import ola.proto.Ola.DeviceInfoReply;
  21. import ola.proto.Ola.DeviceInfoRequest;
  22. import ola.proto.Ola.DiscoveryRequest;
  23. import ola.proto.Ola.DmxData;
  24. import ola.proto.Ola.MergeMode;
  25. import ola.proto.Ola.MergeModeRequest;
  26. import ola.proto.Ola.OlaServerService;
  27. import ola.proto.Ola.OptionalUniverseRequest;
  28. import ola.proto.Ola.PatchAction;
  29. import ola.proto.Ola.PatchPortRequest;
  30. import ola.proto.Ola.PluginDescriptionReply;
  31. import ola.proto.Ola.PluginDescriptionRequest;
  32. import ola.proto.Ola.PluginListReply;
  33. import ola.proto.Ola.PluginListRequest;
  34. import ola.proto.Ola.PortPriorityRequest;
  35. import ola.proto.Ola.RDMRequest;
  36. import ola.proto.Ola.RDMResponse;
  37. import ola.proto.Ola.RegisterAction;
  38. import ola.proto.Ola.RegisterDmxRequest;
  39. import ola.proto.Ola.TimeCode;
  40. import ola.proto.Ola.TimeCodeType;
  41. import ola.proto.Ola.UID;
  42. import ola.proto.Ola.UIDListReply;
  43. import ola.proto.Ola.UniverseInfoReply;
  44. import ola.proto.Ola.UniverseNameRequest;
  45. import ola.proto.Ola.UniverseRequest;
  46. import ola.rpc.SimpleRpcController;
  47. import ola.rpc.StreamRpcChannel;
  48. import com.google.protobuf.ByteString;
  49. import com.google.protobuf.Message;
  50. import com.google.protobuf.RpcCallback;
  51. import com.google.protobuf.RpcChannel;
  52. import com.google.protobuf.RpcController;
  53. public class OlaClient {
  54. private static Logger logger = Logger.getLogger(OlaClient.class.getName());
  55. private OlaServerService serverService;
  56. private RpcController controller;
  57. private RpcChannel channel;
  58. public OlaClient() throws Exception {
  59. channel = new StreamRpcChannel();
  60. controller = new SimpleRpcController();
  61. serverService = OlaServerService.Stub.newStub(channel);
  62. }
  63. /**
  64. * Generic method for making Rpc Calls.
  65. *
  66. * @param method Name of te Rpc Method to call
  67. * @param inputMessage Input RpcMessage
  68. * @return Message result message or null if the call failed.
  69. */
  70. private Message callRpcMethod(String method, Message inputMessage) {
  71. final Message[] outputMessage = new Message[1];
  72. controller.reset();
  73. RpcCallback<Message> cb = new RpcCallback<Message>() {
  74. public void run(Message arg0) {
  75. outputMessage[0] = arg0;
  76. }
  77. };
  78. serverService.callMethod(serverService.getDescriptorForType().findMethodByName(method), controller, inputMessage, cb);
  79. if (controller.failed()) {
  80. logger.warning("RPC Call failed: " + controller.errorText());
  81. return null;
  82. }
  83. return outputMessage[0];
  84. }
  85. /**
  86. * Get a list of plugins from olad.
  87. *
  88. * @return The list of plugings.
  89. */
  90. public PluginListReply getPlugins() {
  91. return (PluginListReply) callRpcMethod("GetPlugins", PluginListRequest.newBuilder().build());
  92. }
  93. /**
  94. * Get a plugin description from olad.
  95. *
  96. * @param pluginId number of the plugin for which to receive the description
  97. * @return The list of plugings.
  98. */
  99. public PluginDescriptionReply getPluginDescription(int pluginId) {
  100. PluginDescriptionRequest request = PluginDescriptionRequest.newBuilder()
  101. .setPluginId(pluginId)
  102. .build();
  103. return (PluginDescriptionReply) callRpcMethod("GetPluginDescription", request);
  104. }
  105. /**
  106. * Get device info from olad.
  107. *
  108. * @return The Device Info.
  109. */
  110. public DeviceInfoReply getDeviceInfo() {
  111. return (DeviceInfoReply) callRpcMethod("GetDeviceInfo", DeviceInfoRequest.newBuilder().build());
  112. }
  113. /**
  114. * Get candidate ports for universe.
  115. *
  116. * @param universe the id of the universe.
  117. * @return device info
  118. */
  119. public DeviceInfoReply getCandidatePorts(int universe) {
  120. OptionalUniverseRequest request = OptionalUniverseRequest.newBuilder().setUniverse(universe).build();
  121. return (DeviceInfoReply) callRpcMethod("GetCandidatePorts", request);
  122. }
  123. /**
  124. * Configure device.
  125. *
  126. * @param device the id of the device to configure.
  127. * @param data device configuration data.
  128. * @return
  129. */
  130. public DeviceConfigReply configureDevice(int device, short[] data) {
  131. DeviceConfigRequest request = DeviceConfigRequest.newBuilder()
  132. .setDeviceAlias(device)
  133. .setData(convertToUnsigned(data))
  134. .build();
  135. return (DeviceConfigReply) callRpcMethod("ConfigureDevice", request);
  136. }
  137. /**
  138. * Get universe information.
  139. *
  140. * @param universe the id of the universe
  141. * @return UniverseInfo
  142. */
  143. public UniverseInfoReply getUniverseInfo(int universe) {
  144. OptionalUniverseRequest request = OptionalUniverseRequest.newBuilder().setUniverse(universe).build();
  145. return (UniverseInfoReply) callRpcMethod("GetUniverseInfo", request);
  146. }
  147. /**
  148. * Get UID's.
  149. *
  150. * @param universe the id of the universe
  151. * @return UIDListReply
  152. */
  153. public UIDListReply getUIDs(int universe) {
  154. UniverseRequest request = UniverseRequest.newBuilder().setUniverse(universe).build();
  155. return (UIDListReply) callRpcMethod("GetUIDs", request);
  156. }
  157. /**
  158. * Force discovery of a universe.
  159. *
  160. * @param universe the id of the universe
  161. * @param full
  162. * @return UID List
  163. */
  164. public UIDListReply forceDiscovery(int universe, boolean full) {
  165. DiscoveryRequest request = DiscoveryRequest.newBuilder()
  166. .setUniverse(universe)
  167. .setFull(full)
  168. .build();
  169. return (UIDListReply) callRpcMethod("ForceDiscovery", request);
  170. }
  171. /**
  172. * Retrieve dmx data from universe.
  173. * @param universe the id of the universe
  174. * @return
  175. */
  176. public DmxData getDmx(int universe) {
  177. return (DmxData) callRpcMethod("GetDmx", UniverseRequest.newBuilder().setUniverse(universe).build());
  178. }
  179. /**
  180. * Patch a port.
  181. *
  182. * @param device number
  183. * @param port number
  184. * @param action PachAction.PATCH or PatchAction.UNPATCH
  185. * @param universe number
  186. * @return true when succeeded.
  187. */
  188. public boolean patchPort(int device, int port, PatchAction action, int universe) {
  189. PatchPortRequest patchRequest = PatchPortRequest.newBuilder()
  190. .setPortId(port)
  191. .setAction(action)
  192. .setDeviceAlias(device)
  193. .setUniverse(universe)
  194. .setIsOutput(true)
  195. .build();
  196. return callRpcMethod("PatchPort", patchRequest) != null;
  197. }
  198. /**
  199. * Send dmx data to olad.
  200. *
  201. * @param universe number
  202. * @param values array of dmx data values
  203. * @return true when succeeded.
  204. */
  205. public boolean sendDmx(int universe, short[] values) {
  206. DmxData dmxData = DmxData.newBuilder()
  207. .setUniverse(universe)
  208. .setData(convertToUnsigned(values))
  209. .build();
  210. return callRpcMethod("UpdateDmxData", dmxData) != null;
  211. }
  212. /**
  213. * Set port priority.
  214. *
  215. * @return true if request succeeded.
  216. */
  217. public boolean setPortPriority(int device, int port, int priority, int mode, boolean output) {
  218. PortPriorityRequest request = PortPriorityRequest.newBuilder()
  219. .setDeviceAlias(device)
  220. .setPortId(port)
  221. .setPriority(priority)
  222. .setPriorityMode(mode)
  223. .setIsOutput(output)
  224. .build();
  225. return callRpcMethod("SetPortPriority", request) != null;
  226. }
  227. /**
  228. * Set universe name.
  229. *
  230. * @param universe id of universe for which to set the name.
  231. * @param name The name to set.
  232. * @return true if the call succeeded.
  233. */
  234. public boolean setUniverseName(int universe, String name) {
  235. UniverseNameRequest request = UniverseNameRequest.newBuilder()
  236. .setUniverse(universe)
  237. .setName(name)
  238. .build();
  239. return callRpcMethod("SetUniverseName", request) != null;
  240. }
  241. /**
  242. * Define merge mode for a universe.
  243. *
  244. * @param universe The id of the universe
  245. * @param mode, merge mode to use
  246. * @return true if call succeeded.
  247. */
  248. public boolean setMergeMode(int universe, MergeMode mode) {
  249. MergeModeRequest request = MergeModeRequest.newBuilder()
  250. .setUniverse(universe)
  251. .setMergeMode(mode)
  252. .build();
  253. return callRpcMethod("SetMergeMode", request) != null;
  254. }
  255. /**
  256. * Register for dmx
  257. * @param universe
  258. * @param action RegisterAction
  259. * @return true if call succeeded.
  260. */
  261. public boolean registerForDmx(int universe, RegisterAction action) {
  262. RegisterDmxRequest request = RegisterDmxRequest.newBuilder()
  263. .setUniverse(universe)
  264. .setAction(action)
  265. .build();
  266. return callRpcMethod("RegisterForDmx", request) != null;
  267. }
  268. /**
  269. * Set source UID for device.
  270. * @param device The id of the device
  271. * @param estaId the UID to set.
  272. * @return true if call succeeded.
  273. */
  274. public boolean setSourceUID(int device, int estaId) {
  275. UID request = UID.newBuilder()
  276. .setDeviceId(device)
  277. .setEstaId(estaId)
  278. .build();
  279. return callRpcMethod("SetSourceUID", request) != null;
  280. }
  281. /**
  282. * Send TimeCode.
  283. *
  284. * @param type TimeCodeType
  285. * @param frames number of frames
  286. * @param hours
  287. * @param minutes
  288. * @param seconds
  289. * @return true if call succeeded.
  290. */
  291. public boolean sendTimeCode(TimeCodeType type, int frames, int hours, int minutes, int seconds) {
  292. TimeCode request = TimeCode.newBuilder()
  293. .setFrames(frames)
  294. .setHours(hours)
  295. .setMinutes(minutes)
  296. .setSeconds(seconds)
  297. .setType(type)
  298. .build();
  299. return callRpcMethod("SendTimeCode", request) != null;
  300. }
  301. /**
  302. * Send RDM Command.
  303. *
  304. * @param uid
  305. * @param subDevice
  306. * @param paramId
  307. * @param isSet
  308. * @param includeRawResponse
  309. * @param universe
  310. * @param data
  311. * @return RDMResponse
  312. */
  313. public RDMResponse sendRDMCommand(UID uid, int subDevice, int paramId, boolean isSet, boolean includeRawResponse, int universe, short[] data) {
  314. RDMRequest request = RDMRequest.newBuilder()
  315. .setUid(uid)
  316. .setSubDevice(subDevice)
  317. .setParamId(paramId)
  318. .setIsSet(isSet)
  319. .setIncludeRawResponse(includeRawResponse)
  320. .setUniverse(universe)
  321. .setData(convertToUnsigned(data))
  322. .build();
  323. return (RDMResponse) callRpcMethod("RDMCommand", request);
  324. }
  325. /**
  326. * Send dmx data, but don't wait for response.
  327. *
  328. * @param universe the id of the universe
  329. * @param values dmx data
  330. */
  331. public void streamDmx(int universe, short[] values) {
  332. DmxData dmxData = DmxData.newBuilder()
  333. .setUniverse(universe)
  334. .setData(convertToUnsigned(values))
  335. .build();
  336. callRpcMethod("StreamDmxData", dmxData);
  337. }
  338. /**
  339. * Convert short array to bytestring
  340. */
  341. public ByteString convertToUnsigned(short[] values) {
  342. byte[] unsigned = new byte[values.length];
  343. for (int i = 0; i < values.length; i++) {
  344. unsigned[i] = (byte) values[i];
  345. }
  346. return ByteString.copyFrom(unsigned);
  347. }
  348. /**
  349. * Convert bytestring to short array.
  350. */
  351. public short[] convertFromUnsigned(ByteString data) {
  352. byte[] values = data.toByteArray();
  353. short[] signed = new short[values.length];
  354. for (int i = 0; i < values.length; i++) {
  355. signed[i] = (short) ((short) values[i] & 0xFF);
  356. }
  357. return signed;
  358. }
  359. }