public void Close()
 {
     if (controller != null)
     {
         Console.WriteLine("Close controller: {0}", getPortName());
         controller.Close();
         controller = null;
     }
 }
Beispiel #2
0
 public void Close()
 {
     if (controller != null)
     {
         Console.WriteLine("Close controller: {0}", getPortName());
         controller.Close();
         controller = null;
     }
 }
Beispiel #3
0
        public Boolean Initialize()
        {
            Dictionary <int, Scheduler.Classes.DeviceClass> device_map = new Dictionary <int, Scheduler.Classes.DeviceClass>();
            //            Dictionary<int, JObject> json_map = new Dictionary<int, JObject>();
            Dictionary <int, bool> processed = new Dictionary <int, bool>();

            //config = new JObject();
            rootDev = getHSRootDevice();
            var extraData = rootDev.get_PlugExtraData_Get(HS);

            if (extraData == null)
            {
                extraData = new PlugExtraData.clsPlugExtraData();
            }
            var typeStr = (string)extraData.GetNamed("EnOcean Type");

            if (typeStr == null)
            {
                Console.WriteLine("No type on device - adding");
                extraData.AddNamed("EnOcean Type", "Controller");
                rootDev.set_PlugExtraData_Set(HS, extraData);
            }
            if (config["nodes"] == null)
            {
                config.Add("nodes", new JObject());
            }

            setControllerStatus("Initializing ctrl instance on port " + getPortName());
            controller = new EnOceanFrameLayer();
            if (controller.Open(getPortName()))
            {
                controller.PacketEventHandler += controller_PacketEvent;
                setControllerStatus("Active");
            }
            else
            {
                setControllerStatus("Port open error!");
                //                GetHSDeviceByAddress(0x1234abcd);
                return(false);
            }
            var p = EnOceanPacket.MakePacket_CO_RD_VERSION();

            controller.Send(p, (EnOceanPacket recvPacket) =>
            {
                if (recvPacket.getType() != PacketType.RESPONSE)
                {
                    return(false);
                }
                var br = new BinaryReader(new MemoryStream(recvPacket.GetData()));
                Console.WriteLine("Ret Code = {0}", br.ReadByte());
                Console.WriteLine("App Version {0}", br.ReadUInt32());
                Console.WriteLine("API Version {0}", br.ReadUInt32());
                var uniqueControllerId  = br.ReadUInt32().ToString("x8");
                this.UniqueControllerId = uniqueControllerId;
                Console.WriteLine("Chip ID {0}", uniqueControllerId);
                Console.WriteLine("Chip Version {0}", br.ReadUInt32());
                var d = Encoding.UTF8.GetString(br.ReadBytes(16), 0, 16);
                Console.WriteLine("APP NAME: {0}", d);
                //TODO: Parse app description
                return(true);
            });
            int timeout = 30;

            while (timeout-- > 0 && this.UniqueControllerId == "Unknown")
            {
                Console.WriteLine("Waiting for controller id!");
                Thread.Sleep(100);
            }
            //            GetHSDeviceByAddress(0x1234abcd);
            if (UniqueControllerId == "Unknown")
            {
                Console.WriteLine("USB Device did not respond!");
                setControllerStatus("Initialization error!");
                return(false);
            }
            LoadChildDevices();
            return(true);
        }
        public Boolean Initialize()
        {
            Dictionary<int, Scheduler.Classes.DeviceClass> device_map = new Dictionary<int, Scheduler.Classes.DeviceClass>();
            //            Dictionary<int, JObject> json_map = new Dictionary<int, JObject>();
            Dictionary<int, bool> processed = new Dictionary<int, bool>();

            //config = new JObject();
            rootDev = getHSRootDevice();
            var extraData = rootDev.get_PlugExtraData_Get(HS);
            if (extraData == null)
                extraData = new PlugExtraData.clsPlugExtraData();
            var typeStr = (string)extraData.GetNamed("EnOcean Type");
            if (typeStr == null)
            {
                Console.WriteLine("No type on device - adding");
                extraData.AddNamed("EnOcean Type", "Controller");
                rootDev.set_PlugExtraData_Set(HS, extraData);
            }
            if (config["nodes"] == null)
            {
                config.Add("nodes", new JObject());
            }

            setControllerStatus("Initializing ctrl instance on port " + getPortName());
            controller = new EnOceanFrameLayer();
            if (controller.Open(getPortName()))
            {
                controller.PacketEventHandler += controller_PacketEvent;
                setControllerStatus("Active");
            }
            else
            {
                setControllerStatus("Port open error!");
                //                GetHSDeviceByAddress(0x1234abcd);
                return false;
            }
            var p = EnOceanPacket.MakePacket_CO_RD_VERSION();
            controller.Send(p, (EnOceanPacket recvPacket) =>
            {
                if (recvPacket.getType() != PacketType.RESPONSE)
                    return false;
                var br = new BinaryReader(new MemoryStream(recvPacket.GetData()));
                Console.WriteLine("Ret Code = {0}", br.ReadByte());
                Console.WriteLine("App Version {0}", br.ReadUInt32());
                Console.WriteLine("API Version {0}", br.ReadUInt32());
                var uniqueControllerId = br.ReadUInt32().ToString("x8");
                this.UniqueControllerId = uniqueControllerId;
                Console.WriteLine("Chip ID {0}", uniqueControllerId);
                Console.WriteLine("Chip Version {0}", br.ReadUInt32());
                var d = Encoding.UTF8.GetString(br.ReadBytes(16), 0, 16);
                Console.WriteLine("APP NAME: {0}", d);
                //TODO: Parse app description
                return true;
            });
            int timeout = 30;
            while (timeout-- > 0 && this.UniqueControllerId == "Unknown")
            {
                Console.WriteLine("Waiting for controller id!");
                Thread.Sleep(100);
            }
            //            GetHSDeviceByAddress(0x1234abcd);
            if (UniqueControllerId == "Unknown")
            {
                Console.WriteLine("USB Device did not respond!");
                setControllerStatus("Initialization error!");
                return false;
            }
            LoadChildDevices();
            return true;
        }