/// <summary> /// Updates a device /// </summary> /// <param name="thisDevice"></param> public static void updateLastDeviceState(Device thisDevice) { lock (LastDeviceState) { if (LastDeviceState.ContainsKey(thisDevice.deviceIdentifier)) LastDeviceState.Remove(thisDevice.deviceIdentifier); LastDeviceState.Add(thisDevice.deviceIdentifier, thisDevice); var device = (from x in Devices where x.room.RoomNum == thisDevice.room.RoomNum && x.devicenum == thisDevice.devicenum select x); if (device == null) { Devices.Add(thisDevice); } else { Devices.Remove(device.FirstOrDefault()); Devices.Add(thisDevice); } } }
/// <summary> /// /// </summary> private static void listenThreadWorker() { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9760); sock.Bind(iep); EndPoint ep = (EndPoint)iep; Console.WriteLine("Ready to receive..."); try { while (true) { byte[] data = new byte[1024]; int recv = sock.ReceiveFrom(data, ref ep); string stringData = Encoding.ASCII.GetString(data, 0, recv); if(Raw !=null) Raw(null,stringData); Match OnOffMatch = new Regex(OnOffRegEx).Match(stringData); Match AllOffMatch = new Regex(allOffRegEx).Match(stringData); Match MoodMatch = new Regex(moodRegEx).Match(stringData); Match DimMatch = new Regex(dimRegEx).Match(stringData); Match HeatMatch = new Regex(heatRegEx).Match(stringData); if (OnOffMatch.Success) { var thisDevice = new Device(new Room(int.Parse(OnOffMatch.Groups["Room"].Value)), int.Parse(OnOffMatch.Groups["Device"].Value),"Dev " + int.Parse(OnOffMatch.Groups["Device"].Value),DeviceType.OnOff, StateStrings.GetStateFromString(OnOffMatch.Groups["State"].Value)); updateLastDeviceState(thisDevice); if (OnOff != null) { OnOff(null, thisDevice.room.RoomNum, thisDevice.devicenum, thisDevice.state); } } if (AllOffMatch.Success && OnAllOff!=null) { OnAllOff(null, int.Parse(AllOffMatch.Groups["Room"].Value)); } if (MoodMatch.Success && OnMood!=null) { OnMood(null, int.Parse(MoodMatch.Groups["Room"].Value), int.Parse(MoodMatch.Groups["Mood"].Value)); } if (DimMatch.Success) { State deviceState = new State(); if (DimMatch.Groups["State"].Value == "0") deviceState = State.Off; else deviceState = State.On; var thisDevice = new Device(new Room(int.Parse(DimMatch.Groups["Room"].Value)), int.Parse(DimMatch.Groups["Device"].Value), "Dev " + int.Parse(DimMatch.Groups["Device"].Value), DeviceType.OnOff, deviceState); thisDevice.DimLevel = (int)Math.Round(double.Parse(DimMatch.Groups["State"].Value)/32*100); updateLastDeviceState(thisDevice); if (OnDim != null) { OnDim(null, thisDevice.room.RoomNum, thisDevice.devicenum, thisDevice.DimLevel); } } if (HeatMatch.Success) { var thisDevice = new Device(new Room(int.Parse(HeatMatch.Groups["Room"].Value)), 0, "Rad " + int.Parse(HeatMatch.Groups["Room"].Value), DeviceType.Radiator, StateStrings.GetStateFromString(HeatMatch.Groups["State"].Value)); updateLastDeviceState(thisDevice); if (OnHeat != null) { OnHeat(null, thisDevice.room.RoomNum, thisDevice.state); } } } } finally { sock.Close(); } }
/// <summary> /// Start monitoring all valves /// where any valve requires heat - switch on the device which is passed. /// </summary> /// <param name="newHeatingDevice"></param> public static void HeatingControlFromValveTemp(Device newHeatingDevice = null) { //set to old boiler control if (newHeatingDevice == null) newHeatingDevice = new Device(new Room(16), 1, "Boiler", DeviceType.OnOff, State.Open); heatingDevice = newHeatingDevice; if(HeatingDemand == null) { HeatingDemand = new Thread(new ThreadStart(heatingDemandWorker)); HeatingDemand.Start(); } }