/// <summary> /// Takes in a BlockingCollection and removes data. Sends data to be assessed elsewhere /// </summary> /// <param name="source">Blocking collection used to hold data for producer consumer pattern</param> public void ReceiveGreenhouseData(BlockingCollection <byte[]> source) { // Create a container for the TLH and moisture packets so we can deserialize them easily TLHPacketContainer tlhContainer = new TLHPacketContainer(); MoisturePacketContainer moistureContainer = new MoisturePacketContainer(); // Take the bytes out of the queue and turn them back into a string source.TryTake(out _data); string json = Encoding.ASCII.GetString(_data); // Take the string to a JObject and deserialize according to the appropriate Type value JObject received = JObject.Parse(json); Console.WriteLine(received.ToString()); switch (received["Type"].Value <int>()) { case 0: tlhContainer = JsonConvert.DeserializeObject <TLHPacketContainer>(json); _tlhInformation = tlhContainer.Packets; break; case 1: moistureContainer = JsonConvert.DeserializeObject <MoisturePacketContainer>(json); _moistureInformation = moistureContainer.Packets; break; case 2: _limits = JsonConvert.DeserializeObject <LimitPacket>(json); break; case 3: _manual = JsonConvert.DeserializeObject <ManualPacket>(json); break; } // If we have all the TLH information, moisture information, limit and manual information we need... if (_tlhInformation != null && _moistureInformation != null && _limits != null && _manual != null) { Console.WriteLine("Sending to analyzers"); // Put everything into temporary variables and clear their values afterwards TLHPacket[] tlhToSend = new TLHPacket[_tlhInformation.Count]; _tlhInformation.CopyTo(tlhToSend); _tlhInformation.Clear(); MoisturePacket[] moistureToSend = new MoisturePacket[_moistureInformation.Count]; _moistureInformation.CopyTo(moistureToSend); _moistureInformation.Clear(); ManualPacket tempManual = _manual; _manual = null; LimitPacket tempLimits = _limits; _limits = null; // Send the temporary variables off to be analyzed DataAnalyzer data = new DataAnalyzer(); data.ExecuteActions(tlhToSend, moistureToSend, tempManual, tempLimits); } }
static void Main(string[] args) { Thread.Sleep(1000); TcpListener serverListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8888); TcpClient client = default(TcpClient); serverListener.Start(); int[] tlhZones = new int[] { 1, 2, 3, 4, 5 }; int[] mZones = new int[] { 1, 2, 3, 4, 5, 6 }; List <ZoneSchedule> Light = new List <ZoneSchedule>(); List <ZoneSchedule> Water = new List <ZoneSchedule>(); foreach (int zone in tlhZones) { Light.Add(new ZoneSchedule() { zone = zone, start = new DateTime(2017, 4, 5, 18, 0, 0), end = new DateTime(2017, 4, 5, 18, 0, 0) }); } foreach (int zone in mZones) { Water.Add(new ZoneSchedule() { zone = zone, start = new DateTime(2017, 4, 5, 18, 0, 0), end = new DateTime(2017, 4, 5, 18, 0, 0) }); } string limits = JsonConvert.SerializeObject(new LimitPacket() { TempHi = 80, TempLo = 65, Water = Water, Light = Light, ShadeLim = 50000 }).Normalize(); Console.WriteLine(limits); byte[] limitsToSend = Encoding.ASCII.GetBytes(limits); // TODO: add ability to change greenhouse limits Console.WriteLine("Would you like to use manual or random mode? Press M for manual, R for random."); //var key = Console.ReadLine(); var key = "r"; Console.WriteLine(); if (key == "m" || key == "M") { } else if (key == "R" || key == "r") { #region Random Data byte[] buffer = new byte[10024]; while (true) { Console.WriteLine("Accepting connection..."); client = serverListener.AcceptTcpClient(); Console.WriteLine("Connection accepted..."); NetworkStream networkStream = client.GetStream(); networkStream.Read(buffer, 0, buffer.Length); string received = JsonConvert.DeserializeObject <string>(Encoding.ASCII.GetString(buffer)); Array.Clear(buffer, 0, buffer.Length); if (received == "TLH") { Console.WriteLine("Request for data received!"); try { List <TLHPacket> jspoofs = new List <TLHPacket>(); JsonSpoof jSpoof = new JsonSpoof(); foreach (int zone in tlhZones) { TLHPacket packet = jSpoof.TLHData(zone); jspoofs.Add(packet); Console.WriteLine($"{packet}"); } TLHPacketContainer container = new TLHPacketContainer() { Packets = jspoofs }; string json = JsonConvert.SerializeObject(container); byte[] sendBytes = Encoding.ASCII.GetBytes(json); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); Console.WriteLine($"{json}"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } else if (received == "MOISTURE") { Console.WriteLine("Request for data received!"); try { List <MoisturePacket> jspoofs = new List <MoisturePacket>(); JsonSpoof jSpoof = new JsonSpoof(); foreach (int zone in mZones) { MoisturePacket packet = jSpoof.MoistureData(zone); jspoofs.Add(packet); Console.WriteLine($"{packet}"); } MoisturePacketContainer container = new MoisturePacketContainer() { Packets = jspoofs }; string json = JsonConvert.SerializeObject(container); byte[] sendBytes = Encoding.ASCII.GetBytes(json); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); Console.WriteLine($"{json}"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } else if (received == "MANUAL") { ManualPacket packet = new ManualPacket() { ManualCool = true, ManualHeat = null, ManualLight = true, ManualWater = true, ManualShade = null }; string manual = JsonConvert.SerializeObject(packet); byte[] manualBytes = Encoding.ASCII.GetBytes(manual); networkStream.Write(manualBytes, 0, manualBytes.Length); networkStream.Flush(); Console.WriteLine($"{manual}"); } else if (received == "LIMITS") { networkStream.Write(limitsToSend, 0, limitsToSend.Length); networkStream.Flush(); } } #endregion } else { Console.WriteLine("Invalid character, exiting!"); } client.Close(); serverListener.Stop(); Console.WriteLine("Exiting..."); }