Advertisement
Guest User

Orvibo S20

a guest
Dec 1st, 2015
1,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. Program.cs
  2.  
  3. static void Main(string[] args)
  4.         {
  5.             string deviceIP = "192.168.xxx.xxx";
  6.             string deviceMAC = "ac-cf-23-xx-xx-xx";
  7.  
  8.  
  9.             // # Instance
  10.             OrviboClass asyn = new OrviboClass(deviceIP , deviceMAC);
  11.  
  12.             // # Subscribe to device and get current status
  13.             int status = asyn.Device_Subscribe();
  14.             Console.WriteLine("Device is : {0}", status == 1 ? "ON" : "OFF");
  15.             Thread.Sleep(1000);
  16.  
  17.             // # Turn OFF
  18.             Console.WriteLine("Turn OFF");
  19.             asyn.Device_ChangeStatus(false);
  20.             Thread.Sleep(1000);
  21.  
  22.             // # Turn ON
  23.             Console.WriteLine("Turn ON");
  24.             asyn.Device_ChangeStatus(true);
  25.            
  26.             Console.ReadKey();
  27.  
  28.             // # END
  29.         }
  30. </code>
  31.  
  32. <code>
  33. orvibo.cs
  34.  
  35. public class OrviboClass
  36.     {
  37.         ///
  38.         /// Class constructor
  39.         ///
  40.         /// Device IP Address
  41.         /// Device MAC Address
  42.         /// Device Port. Default: 10000
  43.         public OrviboClass(string deviceIP, string deviceMAC, int deviceport = 10000)
  44.         {
  45.             // # Device config
  46.             DeviceIPAddress = IPAddress.Parse(deviceIP);        // IP Address
  47.             DeviceMACAddress = deviceMAC.Replace("-", "");      // MAC Address
  48.  
  49.             string[] reverseMAC = deviceMAC.Split('-');         // Reversed MAC Address
  50.             Array.Reverse(reverseMAC);                          //
  51.             foreach (string item in reverseMAC)                 //
  52.                 DeviceMACAddressReverse += item;                //
  53.  
  54.             DevicePort = deviceport;                            // Port
  55.  
  56.             // # EndPoint
  57.             remoteEP = new IPEndPoint(DeviceIPAddress, DevicePort);  
  58.         }
  59.                
  60.         private IPAddress DeviceIPAddress;
  61.         private string DeviceMACAddress, DeviceMACAddressReverse;
  62.         private IPEndPoint remoteEP;
  63.         private int DevicePort;
  64.  
  65.         // # Constantes
  66.         const string _SUBSCRIBE_HEX = "H*6864001e636c";
  67.         const string _SUBSCRIBE_STR = "6864001e636c";
  68.  
  69.         const string _MESSAGE_HEX = "H*686400176463";
  70.         const string _MESSAGE_STR = "686400176463";
  71.         const string _DEVICEON = "0000000001";
  72.         const string _DEVICEOFF = "0000000000";
  73.  
  74.         const string _TWENTYS = "202020202020";
  75.  
  76.  
  77.         ///
  78.         /// Listener to get device response, only returns last character
  79.         ///
  80.         ///
  81.         private int StartListener()
  82.         {
  83.             //bool done = false;
  84.             int lastBit = 0;
  85.             UdpClient listener = new UdpClient(10000);
  86.             IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 10000);
  87.             try
  88.             {
  89.                 byte[] bytes = listener.Receive(ref groupEP);
  90.                 lastBit = int.Parse(bytes[bytes.Length - 1].ToString());
  91.  
  92.                 //Console.Write(BitConverter.ToString(bytes) + "\r\n\r\n");
  93.                
  94.             }
  95.             catch (Exception e)
  96.             {
  97.                 Console.WriteLine(e.ToString());
  98.             }
  99.             finally
  100.             {
  101.                 listener.Close();
  102.             }
  103.             return lastBit;
  104.         }
  105.  
  106.         ///
  107.         /// Function to subscribe to device
  108.         ///
  109.         /// Current device status:   1 ON - 0 OFF
  110.         public int Device_Subscribe()
  111.         {
  112.             // # Build message
  113.             byte[] message = PackH(_SUBSCRIBE_STR + DeviceMACAddress + _TWENTYS + DeviceMACAddressReverse + _TWENTYS);
  114.  
  115.             // # Create Socket
  116.             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  117.             IPAddress broadcast = DeviceIPAddress;
  118.             IPEndPoint ep = new IPEndPoint(broadcast, DevicePort);
  119.  
  120.             // # Send message
  121.             s.SendTo(message, ep);
  122.  
  123.             // # Listen to device
  124.             int deviceStatus = StartListener();
  125.  
  126.             // # Close Socket
  127.             s.Close();
  128.  
  129.             // # Return device status
  130.             return deviceStatus;
  131.         }
  132.  
  133.         ///
  134.         /// Function to change device status (ON/OFF)
  135.         ///
  136.         /// true: ON - false: OFF
  137.         public void Device_ChangeStatus(bool newStatus)
  138.         {
  139.             // # Build message
  140.             byte[] mensaje = PackH(_MESSAGE_STR + DeviceMACAddress + _TWENTYS + (newStatus == true ? _DEVICEON : _DEVICEOFF));
  141.  
  142.             // # Create Socket
  143.             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  144.             IPAddress broadcast = DeviceIPAddress;
  145.             IPEndPoint ep = new IPEndPoint(broadcast, DevicePort);
  146.  
  147.             // # Send message
  148.             s.SendTo(mensaje, ep);
  149.            
  150.             // # Close Socket
  151.             s.Close();
  152.         }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.         //
  160.         // # Aux functions
  161.         //
  162.  
  163.  
  164.         ///
  165.         /// Convert HEX String -&gt; byte[] . Like PHP Pack function
  166.         ///
  167.         /// HEX string
  168.         /// byte[]
  169.         private static byte[] PackH(string hex)
  170.         {
  171.             if ((hex.Length % 2) == 1) hex += '0';
  172.             byte[] bytes = new byte[hex.Length / 2];
  173.             for (int i = 0; i &lt; hex.Length; i += 2)
  174.             {
  175.                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  176.             }
  177.             return bytes;
  178.         }
  179.  
  180.         ///
  181.         /// Convert  byte[] -&gt; HEX String . Like PHP UnPack function
  182.         ///
  183.         /// byte[]
  184.         /// HEX string
  185.         private static string UnPackH(byte[] byteArray)
  186.         {
  187.             string sRes = ASCIIEncoding.ASCII.GetString(byteArray, 0, 4);
  188.             return sRes;
  189.         }
  190.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement