private void xL_XplMessageReceived(object sender, xpllib.XplListener.XplEventArgs e) { //do not log our own messages... if ((e.XplMsg.SourceVendor.ToLower() == VENDORID) && (e.XplMsg.SourceDevice.ToLower() == DEVICEID) && (e.XplMsg.SourceInstance.ToLower() == xL.InstanceName.ToLower())) { return; } //...neither message for ourselves unless the schema is LOG.BASIC if ((e.XplMsg.TargetVendor.ToLower() == VENDORID) && (e.XplMsg.TargetDevice.ToLower() == DEVICEID) && (e.XplMsg.TargetInstance.ToLower() == xL.InstanceName.ToLower()) && (e.XplMsg.Class.ToLower() != "log") && (e.XplMsg.Type.ToLower() != "basic")) { return; } //maybe it should be ignored if ((mFilterApps.Count > 0) && (mFilterApps.ContainsKey(e.XplMsg.SourceVendor + "." + e.XplMsg.SourceDevice))) { string lvls = (string)mFilterApps[e.XplMsg.SourceVendor + "." + e.XplMsg.SourceDevice]; bool foundKey = false; bool foundMatch = false; foreach (XplMsg.KeyValuePair kv in e.XplMsg.KeyValues) { if (lvls.IndexOf(kv.Key + "=") >= 0) { foundKey = true; if (lvls.IndexOf(kv.Key + "=" + kv.Value.ToLower()) >= 0) { foundMatch = true; break; } } } if (foundKey && !foundMatch) { return; } } MsgCache[MsgCacheIdx] = e.XplMsg; MsgCacheIn[MsgCacheIdx] = DateTime.Now; MsgCacheIdx++; //flush if the cache is full or if we received an explicit log message if ((MsgCacheIdx == MSGCACHESIZE) || (e.XplMsg.Class.ToLower() == "log")) { WriteXPLasXML(); MsgCacheIdx = 0; } }
private void xL_XplMessageReceived(object sender, xpllib.XplListener.XplEventArgs e) { try { X10_COMMANDS command = 0; string scmd = e.XplMsg.GetKeyValue("command").ToUpper(); switch (scmd) { case "ALL_LIGHTS_OFF": command = X10_COMMANDS.ALL_LIGHTS_OFF; break; case "ALL_LIGHTS_ON": command = X10_COMMANDS.ALL_LIGHTS_ON; break; case "ON": command = X10_COMMANDS.ON; break; case "OFF": command = X10_COMMANDS.OFF; break; case "DIM": command = X10_COMMANDS.DIM; break; case "BRIGHT": command = X10_COMMANDS.BRIGHT; break; case "ALL_UNITS_OFF": command = X10_COMMANDS.ALL_UNITS_OFF; break; case "EXTENDED": command = X10_COMMANDS.EXTENDED; break; case "HAIL_REQ": command = X10_COMMANDS.HAIL_REQ; break; case "HAIL_ACK": command = X10_COMMANDS.HAIL_ACK; break; case "PREDIM1": command = X10_COMMANDS.PRESET_DIM1; break; case "PREDIM2": command = X10_COMMANDS.PRESET_DIM2; break; case "EXTENDED_DATA": command = X10_COMMANDS.EXTENDED_DATA; break; case "STATUS_ON": command = X10_COMMANDS.STATUS_ON; break; case "STATUS_OFF": command = X10_COMMANDS.STATUS_OFF; break; case "STATUS": command = X10_COMMANDS.STATUS_REQUEST; break; default: throw new Exception("Unknown x10 command received: " + scmd); } string alldevices = e.XplMsg.GetKeyValue("device"); if (alldevices.Length < 2) { throw new Exception("Invalid device(s): " + alldevices); } string[] ardev = alldevices.ToUpper().Split(','); int i = 0; //verify device addresses while (i < ardev.Length) { if (ardev[i].Length < 2 || ardev[i].Length > 3) { throw new Exception("Invalid device: " + ardev[i]); } if (Convert.ToChar(ardev[i].Substring(0, 1)) < 'A' || Convert.ToChar(ardev[i].Substring(0, 1)) > 'P') { throw new Exception("Housecode out of range: " + ardev[i]); } try { int devicenr = Int32.Parse(ardev[i].Substring(1)); if (devicenr < 1 || devicenr > 16) { throw new Exception(); } } catch { throw new Exception("Devicecode out of range: " + ardev[i]); } i++; } //verify other attributes if applicable byte level = 0; byte data1 = 0; byte data2 = 0; if (command == X10_COMMANDS.BRIGHT || command == X10_COMMANDS.DIM || command == X10_COMMANDS.PRESET_DIM1 || command == X10_COMMANDS.PRESET_DIM2) { try { level = Byte.Parse(e.XplMsg.GetKeyValue("level")); if (level < 0 || level > 100) { throw new Exception(); } } catch { throw new Exception("Level out of range: " + e.XplMsg.GetKeyValue("level")); } } if (command == X10_COMMANDS.EXTENDED) { try { data1 = Byte.Parse(e.XplMsg.GetKeyValue("data1")); data2 = Byte.Parse(e.XplMsg.GetKeyValue("data2")); if (data1 < 0 || data1 > 255 || data2 < 0 || data2 > 255) { throw new Exception(); } } catch { throw new Exception("Extended data out of range: data1=" + e.XplMsg.GetKeyValue("data1") + " data2=" + e.XplMsg.GetKeyValue("data2")); } } i = 0; while (i < ardev.Length) { //group devices of the same housecode string devices = ardev[i]; int j = i + 1; while (j < ardev.Length && ardev[j].Substring(0, 1) == devices.Substring(0, 1)) { devices += "," + ardev[j].Substring(1); j++; } i = j; if (command == X10_COMMANDS.BRIGHT || command == X10_COMMANDS.DIM || command == X10_COMMANDS.PRESET_DIM1 || command == X10_COMMANDS.PRESET_DIM2) { x10.AsyncSendX10(devices, (byte)command, level); } else if (command == X10_COMMANDS.EXTENDED) { x10.AsyncSendX10(devices, (byte)command, data1, data2); } else { x10.AsyncSendX10(devices, (byte)command); } } } catch (Exception Ex) { EventLog.WriteEntry("Error parsing xpl command: " + Ex.Message, EventLogEntryType.Error); } }