void controller_PacketEvent(EnOceanPacket pkt) { // Console.WriteLine("Got packet: {0}, opt type {1}", pkt, pkt.Get_OptionalData().getType()); Console.WriteLine("Got telegram of type: {0}", pkt.getTelegramType()); if (pkt.Get_OptionalData() != null && pkt.Get_OptionalData().getSize() > 0) { var odata = pkt.Get_OptionalData(); // Console.WriteLine(" - destination was {0:X8}", odata.getDestination()); String childDevId = ControllerId + ":" + pkt.getSource().ToString("x8"); // var devInst = RegisteredDevices[ControllerId + ":" + odata.getDestination().ToString("x8")]; if (RegisteredDevices.ContainsKey(childDevId)) { var devInst = RegisteredDevices[childDevId]; // Console.WriteLine("Located hsDev : {0}", devInst.DeviceId); devInst.ProcessPacket(pkt); } else { if (pkt.getTelegramType() == TelegramType.TT_4BS && ((pkt.GetData()[4]) & 0x08) == 0x08) { Console.WriteLine("4BS - but no teach in bit, ignoring"); } else { Console.WriteLine("Add to AddSeenDevices"); AddSeenDevice(childDevId); } Console.WriteLine("Did not locate {0:x8}", childDevId); } } }
static public EnOceanPacket MakePacket_CO_RD_VERSION() { var pkt = new EnOceanPacket(PacketType.COMMON_COMMAND, new byte[] { 0x03 }, null); pkt.BuildPacket(); return(pkt); }
public bool Send(EnOceanPacket packet, IReceiveHandler handler, int retries = 3, int timeout = 1000) { var rawPacket = packet.BuildPacket(); var pl = new PacketListener(handler); PacketListeners.Add(pl); AGAIN: // TESTING SendFrame(rawPacket); pl.waitHandle.WaitOne(timeout); if (pl.succeeded) { PacketListeners.Remove(pl); return(true); } else { if (retries-- > 0) { goto AGAIN; } PacketListeners.Remove(pl); return(false); // Packet not what he wanted.. wait for next! } }
public override bool ProcessPacket(EnOceanPacket packet) { Console.WriteLine("Specific handler for packet, dta1={0}", packet.GetData()[1]); int button = 0xff; byte cmd = packet.GetData()[1]; // FIXME: 0x10 is spring up/down and rest is button down map // Adjust button detection to handle correctly. // This will allow us to register that 2 buttons are down at the same time // Above is probably incorrect still .. check docs!!! switch (cmd) { case 0x10: button = 2; break; case 0x30: button = 1; break; case 0x50: button = 4; break; case 0x70: button = 3; break; // default: Console.WriteLine("Unknown button, releasing: {0}", cmd); button = 0; // Released for (int bc = 1; bc < 5; bc++) { if (buttonState.ContainsKey(bc) && buttonState[bc]) { SetButtonState(bc, false); } } return(true); case 0x0: Console.WriteLine("Buttons released!"); button = 0; // Released for (int bc = 1; bc < 5; bc++) { if (buttonState.ContainsKey(bc) && buttonState[bc]) { SetButtonState(bc, false); } } return(true); } Console.WriteLine("Button {0} pressed", button); SetButtonState(button, true); return(true); }
void processReceiveBuffer() { AGAIN: while (receiveBuffer.Count > 0 && receiveBuffer[0] != 0x55) { receiveBuffer.RemoveAt(0); } if (receiveBuffer.Count < 6) { return; } receiveBuffer.RemoveAt(0); // Remove SYNC byte 0x55 byte hdrCrc8 = EnOceanChecksum.CalcCRC8(receiveBuffer, 4); if (hdrCrc8 != receiveBuffer[4]) { Console.WriteLine("CRC ERROR FOR PACKET HDR - or not a sync start\n"); goto AGAIN; } UInt16 pktLen = receiveBuffer[0]; pktLen *= 256; pktLen += receiveBuffer[1]; Byte optLen = receiveBuffer[2]; Byte pktType = receiveBuffer[3]; if ((pktLen + optLen + 6) > receiveBuffer.Count) { // Not enough data yet.. push back header.. Console.WriteLine(" ABANDON FOR LATER - NOT ENOUGH DATA"); receiveBuffer.Insert(0, 0x55); return; } List <byte> pktHdr = receiveBuffer.GetRange(0, 5); receiveBuffer.RemoveRange(0, 5); // Remove hdr Byte dtaCrc = EnOceanChecksum.CalcCRC8(receiveBuffer, pktLen + optLen); if (dtaCrc == receiveBuffer[optLen + pktLen]) { // Console.WriteLine(" ----- MATCH DATA CRC OK"); receiveBuffer.RemoveAt(receiveBuffer.Count - 1); // Remove checksum - we have checked it already List <byte> payload = receiveBuffer.GetRange(0, pktLen); List <byte> optPayload = receiveBuffer.GetRange(pktLen, optLen); Console.WriteLine("Dispatching validated packet of {0} bytes and {1} bytes", payload.Count, optPayload.Count); EnOceanPacket parsedPkt = EnOceanPacket.Parse(pktHdr[3], payload, optPayload); rxPacketQueue.Enqueue(parsedPkt); rxCommThreadWaitHandle.Set(); // Notify rx thread receiveBuffer.RemoveRange(0, optLen + pktLen); goto AGAIN; } }
public override bool ProcessPacket(EnOceanPacket packet) { Double dta = packet.GetData()[3]; Console.WriteLine("Specific handler for packet, dta1={0}", dta); Double tempBase = 0; Double tempRange = 40; Double tempResolution = 256; Double temperature = tempRange - (tempBase + ((tempRange / tempResolution) * dta)); HS.SetDeviceValueByRef(hsDevice.get_Ref(null), temperature, true); return(true); }
public override bool ProcessPacket(EnOceanPacket packet) { Console.WriteLine("Specific handler for packet, dta1={0}", packet.GetData()[1]); // Check correct type // if (packet.getType() == PacketType.) byte cmd = packet.GetData()[1]; switch (cmd) { case 0x08: HS.SetDeviceValueByRef(hsDevice.get_Ref(null), 0x1, true); break; case 0x09: HS.SetDeviceValueByRef(hsDevice.get_Ref(null), 0x0, true); break; // default: Console.WriteLine("Unknown state, fixme: {0}", cmd); return(true); } return(true); }
public override bool ProcessPacket(EnOceanPacket packet) { Console.WriteLine("Specific handler for packet, dta1={0}", packet.GetData()[1]); // Check correct type // if (packet.getType() == PacketType.) byte cmd = packet.GetData()[1]; switch (cmd) { case 0x08: HS.SetDeviceValueByRef(hsDevice.get_Ref(null), 0x1, true); break; case 0x09: HS.SetDeviceValueByRef(hsDevice.get_Ref(null), 0x0, true); break; // default: Console.WriteLine("Unknown state, fixme: {0}", cmd); return true; } return true; }
public PacketListener(IReceiveHandler pHandler) { this.handler = pHandler; succeeded = false; packet = null; waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset); }
public bool Send(EnOceanPacket packet, IReceiveHandler handler, int retries = 3, int timeout = 1000) { var rawPacket = packet.BuildPacket(); var pl = new PacketListener(handler); PacketListeners.Add(pl); AGAIN: // TESTING SendFrame(rawPacket); pl.waitHandle.WaitOne(timeout); if (pl.succeeded) { PacketListeners.Remove(pl); return true; } else { if (retries-- > 0) goto AGAIN; PacketListeners.Remove(pl); return false; // Packet not what he wanted.. wait for next! } }
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 bool ProcessCommand(EnOceanPacket packet, Scheduler.Classes.DeviceClass hsDev) { Console.WriteLine("Processing command : {0}", packet.getType().ToString()); return true; }
public static EnOceanPacket MakePacket_CO_RD_VERSION() { var pkt = new EnOceanPacket(PacketType.COMMON_COMMAND, new byte[] { 0x03 }, null); pkt.BuildPacket(); return pkt; }
public bool ProcessCommand(EnOceanPacket packet, Scheduler.Classes.DeviceClass hsDev) { Console.WriteLine("Processing command : {0}", packet.getType().ToString()); return(true); }
public abstract bool ProcessPacket(EnOceanPacket packet);
public override bool ProcessPacket(EnOceanPacket packet) { Console.WriteLine("Specific handler for packet, dta1={0}", packet.GetData()[1]); int button = 0xff; byte cmd = packet.GetData()[1]; // FIXME: 0x10 is spring up/down and rest is button down map // Adjust button detection to handle correctly. // This will allow us to register that 2 buttons are down at the same time // Above is probably incorrect still .. check docs!!! switch (cmd) { case 0x10: button = 2; break; case 0x30: button = 1; break; case 0x50: button = 4; break; case 0x70: button = 3; break; // default: Console.WriteLine("Unknown button, releasing: {0}", cmd); button = 0; // Released for (int bc = 1; bc < 5; bc++) { if (buttonState.ContainsKey(bc) && buttonState[bc]) SetButtonState(bc, false); } return true; case 0x0: Console.WriteLine("Buttons released!"); button = 0; // Released for (int bc = 1; bc < 5; bc++) { if (buttonState.ContainsKey(bc) && buttonState[bc]) SetButtonState(bc, false); } return true; } Console.WriteLine("Button {0} pressed", button); SetButtonState(button, true); return true; }
public override bool ProcessPacket(EnOceanPacket packet) { Double dta = packet.GetData()[3]; Console.WriteLine("Specific handler for packet, dta1={0}", dta); Double tempBase = 0; Double tempRange = 40; Double tempResolution = 256; Double temperature = tempRange - (tempBase + ( (tempRange / tempResolution) * dta)); HS.SetDeviceValueByRef(hsDevice.get_Ref(null), temperature, true); return true; }