Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Windows.ApplicationModel.Core;
- using Windows.Media.Capture;
- using Windows.ApplicationModel;
- using System.Threading.Tasks;
- using Windows.System.Display;
- using Windows.Graphics.Display;
- using Urho;
- using Urho.SharpReality;
- using Urho.Gui;
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using System.Text;
- namespace HelloWorld
- {
- internal class Program
- {
- [MTAThread]
- static void Main() => CoreApplication.Run(
- new UrhoAppViewSource<HelloWorldApplication>(
- new ApplicationOptions("Data")));
- }
- public class HelloWorldApplication : StereoApplication
- {
- Text yawText;
- Text pitchText;
- Text readyText;
- Text posisiText;
- Text tesrmq;
- Sprite crosshair;
- RMQ rmq = new RMQ();
- MediaCapture accessWebcam;
- bool isPreviewing;
- MediaCapture mediaCapture;
- DisplayRequest displayRequest;
- Text mediaText;
- //rabbitmq
- string inputmsg;
- string tujuan;
- string outputmsg;
- public HelloWorldApplication(ApplicationOptions opts) : base(opts) { }
- protected override void Start()
- {
- // base.Start() creates a basic scene
- base.Start();
- EnableGestureTapped = true;
- //Show crosshair
- ShowCrosshair();
- //Show Yaw & Pitch
- ShowYaw();
- ShowPitch();
- //Show turret info
- ShowReady();
- //rabbitmq
- //RMQ rmq = new RMQ();
- rmq.InitRMQConnection();
- rmq.CreateRMQConnection();
- //string queue_publish = "publishrangga";
- inputmsg = yawText.Value;
- tujuan = "rangga";
- string queue_consume = "consumerangga";
- rmq.CreateRMQChannel(queue_consume);
- rmq.WaitingMessage(queue_consume);
- //rmq.SendMessage(tujuan, inputmsg);
- //Menampilkan tes rabbitmq
- tesrmq = new Text(Context);
- tesrmq.Value = rmq.GetMessage();
- tesrmq.HorizontalAlignment = HorizontalAlignment.Right;
- tesrmq.VerticalAlignment = VerticalAlignment.Bottom;
- tesrmq.SetColor(Color.White);
- tesrmq.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
- UI.Root.AddChild(tesrmq);
- //Buat posisi tank
- posisiText = new Text();
- posisiText.Value = "0" + " X; " + "0" + " Y; " + "0" + " Z; ";
- posisiText.HorizontalAlignment = HorizontalAlignment.Right;
- posisiText.VerticalAlignment = VerticalAlignment.Top;
- posisiText.Position = new IntVector2(0, 50);
- posisiText.SetColor(Color.White);
- posisiText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
- UI.Root.AddChild(posisiText);
- accessWebcam = new MediaCapture();
- DisplayRequest displayRequest = new DisplayRequest();
- }
- //membuat fungsi OnUpdate agar fungsi aktif ketika ada update terjadi
- protected override void OnUpdate(float timeStep)
- {
- //mengambil nilai yaw dan pitch dari rightcamera hololens
- yawText.Value = (RightCamera.Node.WorldRotation.YawAngle.ToString());
- pitchText.Value = (RightCamera.Node.WorldRotation.PitchAngle.ToString());
- //mengambil nilai posisi dari rightcamera hololens
- posisiText.Value = (RightCamera.Node.WorldPosition.X) + " X; " +
- (RightCamera.Node.WorldPosition.Y) + " Y; " +
- (RightCamera.Node.WorldPosition.Z) + " Z; ";
- //terima input
- tesrmq.Value = rmq.GetMessage();
- }
- private async Task StartPreviewAsync()
- {
- try
- {
- mediaCapture = new MediaCapture();
- await mediaCapture.InitializeAsync();
- displayRequest.RequestActive();
- DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
- }
- catch (UnauthorizedAccessException)
- {
- // This will be thrown if the user denied access to the camera in privacy settings
- mediaText = new Text();
- mediaText.Value = "The app was denied access to the camera";
- mediaText.VerticalAlignment = VerticalAlignment.Bottom;
- mediaText.HorizontalAlignment = HorizontalAlignment.Left;
- mediaText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
- mediaText.SetColor(Color.White);
- UI.Root.AddChild(mediaText);
- return;
- }
- }
- public override async void OnGestureDoubleTapped()
- {
- readyText.Value = "FIRE";
- readyText.SetColor(Color.Red);
- await Delay(5f);
- readyText.Value = "READY";
- readyText.SetColor(Color.White);
- rmq.SendMessage(tujuan, inputmsg);
- base.OnGestureDoubleTapped();
- }
- void ShowYaw()
- {
- //Menampilkan yaw
- yawText = new Text(Context);
- yawText.Value = "0";
- yawText.HorizontalAlignment = HorizontalAlignment.Center;
- yawText.VerticalAlignment = VerticalAlignment.Top;
- yawText.SetColor(Color.White);
- yawText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
- UI.Root.AddChild(yawText);
- }
- void ShowPitch()
- {
- //Menampilkan pitch
- pitchText = new Text(Context);
- pitchText.Value = "";
- pitchText.HorizontalAlignment = HorizontalAlignment.Left;
- pitchText.VerticalAlignment = VerticalAlignment.Center;
- pitchText.SetColor(Color.White);
- pitchText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
- UI.Root.AddChild(pitchText);
- }
- void ShowReady()
- {
- readyText = new Text(Context);
- readyText.Value = "READY";
- //"0" + " X; " + "0" + " Y; " + "0" + " Z; ";
- readyText.HorizontalAlignment = HorizontalAlignment.Right;
- readyText.VerticalAlignment = VerticalAlignment.Top;
- readyText.SetColor(Color.White);
- readyText.SetFont(CoreAssets.Fonts.AnonymousPro, 30);
- UI.Root.AddChild(readyText);
- }
- void ShowCrosshair()
- {
- //buat crosshair
- crosshair = new Sprite(Context);
- crosshair.Texture = ResourceCache.GetTexture2D(@"Gif/earth.gif");
- crosshair.SetAlignment(HorizontalAlignment.Center, VerticalAlignment.Center);
- crosshair.HotSpot = new IntVector2(0, 0);
- crosshair.Position = new IntVector2(-50, -50);
- crosshair.SetSize(100, 100); //GetSprite("2D/crosshair1.png");
- UI.Root.AddChild(crosshair);
- }
- }
- class RMQ
- {
- public ConnectionFactory connectionFactory;
- public IConnection connection;
- public IModel channel;
- string varglob;
- string message;
- byte[] body;
- public void InitRMQConnection(string host = "10.0.45.126", int port = 5672, string user = "miftah.rangga20",
- string pass = "123")
- {
- connectionFactory = new ConnectionFactory();
- connectionFactory.HostName = host;
- //connectionFactory.Port = port;
- connectionFactory.UserName = user;
- connectionFactory.Password = pass;
- }
- public void CreateRMQConnection()
- {
- connection = connectionFactory.CreateConnection();
- //Console.WriteLine("Koneksi " + (connection.IsOpen ? "Berhasil!" : "Gagal!"));
- }
- public void CreateRMQChannel(string queue_name, string routingKey = "", string exchange_name =
- "")
- {
- if (connection.IsOpen)
- {
- channel = connection.CreateModel();
- //Console.WriteLine("Channel " + (channel.IsOpen ? "Berhasil!" : "Gagal!"));
- }
- if (channel.IsOpen)
- {
- channel.QueueDeclare(queue: queue_name,
- durable: true,
- exclusive: false,
- autoDelete: false,
- arguments: null);
- //Console.WriteLine("Queue telah dideklarasikan..");
- }
- }
- public void SendMessage(string tujuan, string msg = "send")
- {
- byte[] responseBytes = Encoding.UTF8.GetBytes(msg);// konversi pesan dalam bentuk string menjadi byte
- channel.BasicPublish(exchange: "",
- routingKey: tujuan,
- basicProperties: null,
- body: responseBytes);
- //Console.WriteLine("Pesan: '" + msg + "' telah dikirim.");
- }
- public void WaitingMessage(string queue_name)
- {
- using (channel = connection.CreateModel())
- {
- channel.QueueDeclare(queue: queue_name,
- durable: true,
- exclusive: false,
- autoDelete: false,
- arguments: null);
- var consumer = new EventingBasicConsumer(channel);
- consumer.Received += (model, ea) =>
- {
- body = ea.Body;
- message = Encoding.UTF8.GetString(body);
- //varglob = message; //message adalah variable global (ga ke-assign)
- //Console.WriteLine(" [x] Pesan diterima: {0}", message);
- };
- //varglob = Encoding.UTF8.GetString(body);
- channel.BasicConsume(queue: queue_name,
- noAck: true,
- consumer: consumer);
- //Console.WriteLine(" Tekan [enter] untuk memutus koneksi.");
- //Console.ReadLine();
- //Disconnect();
- }
- }
- public string GetMessage()
- {
- return varglob;
- }
- public void Disconnect()
- {
- channel.Close();
- channel = null;
- //Console.WriteLine("Channel ditutup!");
- if (connection.IsOpen)
- {
- connection.Close();
- }
- //Console.WriteLine("Koneksi diputus!");
- connection.Dispose();
- connection = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement