Advertisement
Guest User

Untitled

a guest
Feb 1st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. using System;
  2. using Windows.ApplicationModel.Core;
  3. using Windows.Media.Capture;
  4. using Windows.ApplicationModel;
  5. using System.Threading.Tasks;
  6. using Windows.System.Display;
  7. using Windows.Graphics.Display;
  8.  
  9. using Urho;
  10. using Urho.SharpReality;
  11. using Urho.Gui;
  12.  
  13. using RabbitMQ.Client;
  14. using RabbitMQ.Client.Events;
  15. using System.Text;
  16.  
  17. namespace HelloWorld
  18. {
  19. internal class Program
  20. {
  21. [MTAThread]
  22. static void Main() => CoreApplication.Run(
  23. new UrhoAppViewSource<HelloWorldApplication>(
  24. new ApplicationOptions("Data")));
  25. }
  26.  
  27. public class HelloWorldApplication : StereoApplication
  28. {
  29. Text yawText;
  30. Text pitchText;
  31. Text readyText;
  32. Text posisiText;
  33. Text tesrmq;
  34. Sprite crosshair;
  35. RMQ rmq = new RMQ();
  36.  
  37. MediaCapture accessWebcam;
  38. bool isPreviewing;
  39.  
  40. MediaCapture mediaCapture;
  41. DisplayRequest displayRequest;
  42. Text mediaText;
  43.  
  44. //rabbitmq
  45. string inputmsg;
  46. string tujuan;
  47. string outputmsg;
  48.  
  49. public HelloWorldApplication(ApplicationOptions opts) : base(opts) { }
  50.  
  51. protected override void Start()
  52. {
  53. // base.Start() creates a basic scene
  54. base.Start();
  55.  
  56. EnableGestureTapped = true;
  57.  
  58. //Show crosshair
  59. ShowCrosshair();
  60. //Show Yaw & Pitch
  61. ShowYaw();
  62. ShowPitch();
  63.  
  64. //Show turret info
  65. ShowReady();
  66.  
  67. //rabbitmq
  68. //RMQ rmq = new RMQ();
  69. rmq.InitRMQConnection();
  70. rmq.CreateRMQConnection();
  71. //string queue_publish = "publishrangga";
  72.  
  73. inputmsg = yawText.Value;
  74. tujuan = "rangga";
  75. string queue_consume = "consumerangga";
  76. rmq.CreateRMQChannel(queue_consume);
  77. rmq.WaitingMessage(queue_consume);
  78.  
  79.  
  80. //rmq.SendMessage(tujuan, inputmsg);
  81.  
  82. //Menampilkan tes rabbitmq
  83. tesrmq = new Text(Context);
  84. tesrmq.Value = rmq.GetMessage();
  85. tesrmq.HorizontalAlignment = HorizontalAlignment.Right;
  86. tesrmq.VerticalAlignment = VerticalAlignment.Bottom;
  87. tesrmq.SetColor(Color.White);
  88. tesrmq.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
  89. UI.Root.AddChild(tesrmq);
  90.  
  91.  
  92.  
  93. //Buat posisi tank
  94. posisiText = new Text();
  95. posisiText.Value = "0" + " X; " + "0" + " Y; " + "0" + " Z; ";
  96.  
  97. posisiText.HorizontalAlignment = HorizontalAlignment.Right;
  98. posisiText.VerticalAlignment = VerticalAlignment.Top;
  99. posisiText.Position = new IntVector2(0, 50);
  100. posisiText.SetColor(Color.White);
  101. posisiText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
  102. UI.Root.AddChild(posisiText);
  103.  
  104. accessWebcam = new MediaCapture();
  105. DisplayRequest displayRequest = new DisplayRequest();
  106.  
  107.  
  108. }
  109.  
  110. //membuat fungsi OnUpdate agar fungsi aktif ketika ada update terjadi
  111. protected override void OnUpdate(float timeStep)
  112. {
  113. //mengambil nilai yaw dan pitch dari rightcamera hololens
  114. yawText.Value = (RightCamera.Node.WorldRotation.YawAngle.ToString());
  115. pitchText.Value = (RightCamera.Node.WorldRotation.PitchAngle.ToString());
  116.  
  117. //mengambil nilai posisi dari rightcamera hololens
  118. posisiText.Value = (RightCamera.Node.WorldPosition.X) + " X; " +
  119. (RightCamera.Node.WorldPosition.Y) + " Y; " +
  120. (RightCamera.Node.WorldPosition.Z) + " Z; ";
  121.  
  122. //terima input
  123. tesrmq.Value = rmq.GetMessage();
  124.  
  125. }
  126.  
  127. private async Task StartPreviewAsync()
  128. {
  129. try
  130. {
  131.  
  132. mediaCapture = new MediaCapture();
  133. await mediaCapture.InitializeAsync();
  134.  
  135. displayRequest.RequestActive();
  136. DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
  137. }
  138. catch (UnauthorizedAccessException)
  139. {
  140. // This will be thrown if the user denied access to the camera in privacy settings
  141. mediaText = new Text();
  142. mediaText.Value = "The app was denied access to the camera";
  143. mediaText.VerticalAlignment = VerticalAlignment.Bottom;
  144. mediaText.HorizontalAlignment = HorizontalAlignment.Left;
  145. mediaText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
  146. mediaText.SetColor(Color.White);
  147. UI.Root.AddChild(mediaText);
  148. return;
  149. }
  150. }
  151.  
  152. public override async void OnGestureDoubleTapped()
  153. {
  154. readyText.Value = "FIRE";
  155. readyText.SetColor(Color.Red);
  156. await Delay(5f);
  157. readyText.Value = "READY";
  158. readyText.SetColor(Color.White);
  159. rmq.SendMessage(tujuan, inputmsg);
  160.  
  161. base.OnGestureDoubleTapped();
  162. }
  163.  
  164.  
  165. void ShowYaw()
  166. {
  167. //Menampilkan yaw
  168. yawText = new Text(Context);
  169. yawText.Value = "0";
  170. yawText.HorizontalAlignment = HorizontalAlignment.Center;
  171. yawText.VerticalAlignment = VerticalAlignment.Top;
  172. yawText.SetColor(Color.White);
  173. yawText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
  174. UI.Root.AddChild(yawText);
  175. }
  176.  
  177. void ShowPitch()
  178. {
  179. //Menampilkan pitch
  180. pitchText = new Text(Context);
  181. pitchText.Value = "";
  182. pitchText.HorizontalAlignment = HorizontalAlignment.Left;
  183. pitchText.VerticalAlignment = VerticalAlignment.Center;
  184. pitchText.SetColor(Color.White);
  185. pitchText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
  186. UI.Root.AddChild(pitchText);
  187. }
  188.  
  189. void ShowReady()
  190. {
  191. readyText = new Text(Context);
  192. readyText.Value = "READY";
  193. //"0" + " X; " + "0" + " Y; " + "0" + " Z; ";
  194. readyText.HorizontalAlignment = HorizontalAlignment.Right;
  195. readyText.VerticalAlignment = VerticalAlignment.Top;
  196. readyText.SetColor(Color.White);
  197. readyText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
  198. UI.Root.AddChild(readyText);
  199. }
  200.  
  201. void ShowCrosshair()
  202. {
  203. //buat crosshair
  204. crosshair = new Sprite(Context);
  205. crosshair.Texture = ResourceCache.GetTexture2D(@"Gif/earth.gif");
  206. crosshair.SetAlignment(HorizontalAlignment.Center, VerticalAlignment.Center);
  207. crosshair.HotSpot = new IntVector2(0, 0);
  208. crosshair.Position = new IntVector2(-50, -50);
  209. crosshair.SetSize(100, 100); //GetSprite("2D/crosshair1.png");
  210. UI.Root.AddChild(crosshair);
  211. }
  212.  
  213.  
  214.  
  215. }
  216.  
  217. class RMQ
  218. {
  219. public ConnectionFactory connectionFactory;
  220. public IConnection connection;
  221. public IModel channel;
  222.  
  223. string varglob;
  224. string message;
  225. byte[] body;
  226.  
  227. public void InitRMQConnection(string host = "10.0.45.126", int port = 5672, string user = "miftah.rangga20",
  228. string pass = "123")
  229. {
  230. connectionFactory = new ConnectionFactory();
  231. connectionFactory.HostName = host;
  232. //connectionFactory.Port = port;
  233. connectionFactory.UserName = user;
  234. connectionFactory.Password = pass;
  235. }
  236. public void CreateRMQConnection()
  237. {
  238. connection = connectionFactory.CreateConnection();
  239. //Console.WriteLine("Koneksi " + (connection.IsOpen ? "Berhasil!" : "Gagal!"));
  240. }
  241. public void CreateRMQChannel(string queue_name, string routingKey = "", string exchange_name =
  242. "")
  243. {
  244. if (connection.IsOpen)
  245. {
  246. channel = connection.CreateModel();
  247. //Console.WriteLine("Channel " + (channel.IsOpen ? "Berhasil!" : "Gagal!"));
  248. }
  249. if (channel.IsOpen)
  250. {
  251. channel.QueueDeclare(queue: queue_name,
  252. durable: true,
  253. exclusive: false,
  254. autoDelete: false,
  255. arguments: null);
  256. //Console.WriteLine("Queue telah dideklarasikan..");
  257. }
  258. }
  259. public void SendMessage(string tujuan, string msg = "send")
  260. {
  261. byte[] responseBytes = Encoding.UTF8.GetBytes(msg);// konversi pesan dalam bentuk string menjadi byte
  262. channel.BasicPublish(exchange: "",
  263. routingKey: tujuan,
  264. basicProperties: null,
  265. body: responseBytes);
  266. //Console.WriteLine("Pesan: '" + msg + "' telah dikirim.");
  267. }
  268.  
  269. public void WaitingMessage(string queue_name)
  270. {
  271. using (channel = connection.CreateModel())
  272. {
  273. channel.QueueDeclare(queue: queue_name,
  274. durable: true,
  275. exclusive: false,
  276. autoDelete: false,
  277. arguments: null);
  278. var consumer = new EventingBasicConsumer(channel);
  279. consumer.Received += (model, ea) =>
  280. {
  281. body = ea.Body;
  282. message = Encoding.UTF8.GetString(body);
  283. //varglob = message; //message adalah variable global (ga ke-assign)
  284. //Console.WriteLine(" [x] Pesan diterima: {0}", message);
  285. };
  286.  
  287. //varglob = Encoding.UTF8.GetString(body);
  288. channel.BasicConsume(queue: queue_name,
  289. noAck: true,
  290. consumer: consumer);
  291. //Console.WriteLine(" Tekan [enter] untuk memutus koneksi.");
  292. //Console.ReadLine();
  293. //Disconnect();
  294. }
  295. }
  296.  
  297. public string GetMessage()
  298. {
  299. return varglob;
  300. }
  301.  
  302. public void Disconnect()
  303. {
  304. channel.Close();
  305. channel = null;
  306. //Console.WriteLine("Channel ditutup!");
  307. if (connection.IsOpen)
  308. {
  309. connection.Close();
  310. }
  311. //Console.WriteLine("Koneksi diputus!");
  312. connection.Dispose();
  313. connection = null;
  314. }
  315. }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement