Ejemplo n.º 1
0
        //TODO Update
        public void onResult(string result)
        {
            // TODO Auto-generated method stub
            if (result.Equals("CONNECT_TIMEOUT") ||
                result.Equals("NTCS_CLOSED") ||
                result.Equals("NTCS_UNKNOWN") ||
                result.Equals("FAILED"))
            {
                //TODO update
                //if (_remoteTunnel1 != null)
                //{


                //    Log.d(TAG, "_remoteTunnel1 : closeTunnels ");


                //    _remoteTunnel1.closeTunnels();
                //    _remoteTunnel1 = null;

                //    mHandler.removeMessages(400);
                //    mHandler.sendEmptyMessageDelayed(400, 10);
                //}
            }
            else
            {
                _devicePort = 3333;


                try
                {
                    SetSocket(ip, _devicePort);

                    var socketPrinterRunnable = new SocketPrinterRunnable(bThreadRunnig, socket);
                    thread = new Thread(socketPrinterRunnable.run);
                    thread.Start();


                    DoCommandStart();

                    Log.d(TAG, "doCommandStart =================== ");


                    DoCommandSecretCode();

                    Log.d(TAG, "doCommandSecretCode =================== ");
                }
                catch (Exception e1)
                {
                    Log.d(TAG, e1.Message);
                }
            }
        }
Ejemplo n.º 2
0
        public void initSocket()
        {
            Log.d(TAG, "initSocket()");

            CloseSocket();

            isWorking = false;

            NabtoClient nabto    = new NabtoClient(); // Get a reference to the Nabto client runtime and start it up.
            var         email    = "guest";
            var         password = "******";

            var threadStart = false;

            try
            {
                using (Session session = nabto.CreateSession(email, password))     // Create a session using the specified user credentials.
                {
                    Console.WriteLine("Creating tunnel...");
                    using (Nabto.Client.Tunneling.Tunnel tunnel = session.CreateTunnel(_deviceId, 8080, "127.0.0.1", 80))
                    {
                        //var device = session.CreateDeviceConnection(deviceId);

                        //device.
                        while (Console.KeyAvailable == false)
                        {
                            Console.Write("Tunnel created - current state: {0}\r", tunnel.State);
                            var remoteRelayMicroState = Nabto.Client.Tunneling.TunnelState.RemoteRelayMicro;
                            var RemotePeerToPeerState = Nabto.Client.Tunneling.TunnelState.RemotePeerToPeer;
                            if (threadStart == false && (tunnel.State == remoteRelayMicroState || tunnel.State == RemotePeerToPeerState))
                            {
                                SetSocket(ip, _devicePort);
                                DoCommandStart();
                                DoCommandSecretCode();
                                var socketPrinterRunnable = new SocketPrinterRunnable(bThreadRunnig, socket);
                                thread = new Thread(socketPrinterRunnable.run);
                                thread.Start();
                                threadStart = true;
                                int ccc = 1;
                                while (ccc > 0)
                                {
                                    int action = 0;
                                    Console.WriteLine("action:");
                                    action = int.Parse(Console.ReadLine());
                                    if (action == 1)
                                    {
                                        DoCommandTemp();
                                    }
                                    else if (action == 2)
                                    {
                                        string attachment_file_name = "20180912072713";
                                        byte[] array   = Encoding.UTF8.GetBytes(attachment_file_name);
                                        int    c       = array.Length + 3;
                                        byte[] cmdSend = new byte[c];
                                        cmdSend[0] = (byte)0xA5;
                                        cmdSend[1] = 0x06;
                                        for (int i = 0; i < array.Length; i++)
                                        {
                                            cmdSend[i + 2] = array[i];
                                        }
                                        cmdSend[c - 1] = (byte)0xFF;
                                        Send(cmdSend);
                                    }
                                    else
                                    {
                                        DoSendPrint();
                                    }
                                    Console.WriteLine("continue:");
                                    ccc = int.Parse(Console.ReadLine());
                                }
                            }
                        }
                        Console.WriteLine();
                        Console.WriteLine("Shutting down...");
                        Console.ReadKey(true);
                    }
                }
            }
            catch (Exception e)
            {
                Log.d(TAG, e.Message);
            }
            //}
        }