Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs
- static void Main(string[] args)
- {
- string deviceIP = "192.168.xxx.xxx";
- string deviceMAC = "ac-cf-23-xx-xx-xx";
- // # Instance
- OrviboClass asyn = new OrviboClass(deviceIP , deviceMAC);
- // # Subscribe to device and get current status
- int status = asyn.Device_Subscribe();
- Console.WriteLine("Device is : {0}", status == 1 ? "ON" : "OFF");
- Thread.Sleep(1000);
- // # Turn OFF
- Console.WriteLine("Turn OFF");
- asyn.Device_ChangeStatus(false);
- Thread.Sleep(1000);
- // # Turn ON
- Console.WriteLine("Turn ON");
- asyn.Device_ChangeStatus(true);
- Console.ReadKey();
- // # END
- }
- </code>
- <code>
- orvibo.cs
- public class OrviboClass
- {
- ///
- /// Class constructor
- ///
- /// Device IP Address
- /// Device MAC Address
- /// Device Port. Default: 10000
- public OrviboClass(string deviceIP, string deviceMAC, int deviceport = 10000)
- {
- // # Device config
- DeviceIPAddress = IPAddress.Parse(deviceIP); // IP Address
- DeviceMACAddress = deviceMAC.Replace("-", ""); // MAC Address
- string[] reverseMAC = deviceMAC.Split('-'); // Reversed MAC Address
- Array.Reverse(reverseMAC); //
- foreach (string item in reverseMAC) //
- DeviceMACAddressReverse += item; //
- DevicePort = deviceport; // Port
- // # EndPoint
- remoteEP = new IPEndPoint(DeviceIPAddress, DevicePort);
- }
- private IPAddress DeviceIPAddress;
- private string DeviceMACAddress, DeviceMACAddressReverse;
- private IPEndPoint remoteEP;
- private int DevicePort;
- // # Constantes
- const string _SUBSCRIBE_HEX = "H*6864001e636c";
- const string _SUBSCRIBE_STR = "6864001e636c";
- const string _MESSAGE_HEX = "H*686400176463";
- const string _MESSAGE_STR = "686400176463";
- const string _DEVICEON = "0000000001";
- const string _DEVICEOFF = "0000000000";
- const string _TWENTYS = "202020202020";
- ///
- /// Listener to get device response, only returns last character
- ///
- ///
- private int StartListener()
- {
- //bool done = false;
- int lastBit = 0;
- UdpClient listener = new UdpClient(10000);
- IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 10000);
- try
- {
- byte[] bytes = listener.Receive(ref groupEP);
- lastBit = int.Parse(bytes[bytes.Length - 1].ToString());
- //Console.Write(BitConverter.ToString(bytes) + "\r\n\r\n");
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- finally
- {
- listener.Close();
- }
- return lastBit;
- }
- ///
- /// Function to subscribe to device
- ///
- /// Current device status: 1 ON - 0 OFF
- public int Device_Subscribe()
- {
- // # Build message
- byte[] message = PackH(_SUBSCRIBE_STR + DeviceMACAddress + _TWENTYS + DeviceMACAddressReverse + _TWENTYS);
- // # Create Socket
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- IPAddress broadcast = DeviceIPAddress;
- IPEndPoint ep = new IPEndPoint(broadcast, DevicePort);
- // # Send message
- s.SendTo(message, ep);
- // # Listen to device
- int deviceStatus = StartListener();
- // # Close Socket
- s.Close();
- // # Return device status
- return deviceStatus;
- }
- ///
- /// Function to change device status (ON/OFF)
- ///
- /// true: ON - false: OFF
- public void Device_ChangeStatus(bool newStatus)
- {
- // # Build message
- byte[] mensaje = PackH(_MESSAGE_STR + DeviceMACAddress + _TWENTYS + (newStatus == true ? _DEVICEON : _DEVICEOFF));
- // # Create Socket
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- IPAddress broadcast = DeviceIPAddress;
- IPEndPoint ep = new IPEndPoint(broadcast, DevicePort);
- // # Send message
- s.SendTo(mensaje, ep);
- // # Close Socket
- s.Close();
- }
- //
- // # Aux functions
- //
- ///
- /// Convert HEX String -> byte[] . Like PHP Pack function
- ///
- /// HEX string
- /// byte[]
- private static byte[] PackH(string hex)
- {
- if ((hex.Length % 2) == 1) hex += '0';
- byte[] bytes = new byte[hex.Length / 2];
- for (int i = 0; i < hex.Length; i += 2)
- {
- bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
- }
- return bytes;
- }
- ///
- /// Convert byte[] -> HEX String . Like PHP UnPack function
- ///
- /// byte[]
- /// HEX string
- private static string UnPackH(byte[] byteArray)
- {
- string sRes = ASCIIEncoding.ASCII.GetString(byteArray, 0, 4);
- return sRes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement