Exemplo n.º 1
0
        // this is the ELV MAX! Cube monitoring script
        public void Run()
        {
            while (running)
            {
                #region Update House
                try
                {
                    if (theHouse != null)
                    {
                        previousHouse = theHouse.GetAllDevicesInADictionary();
                    }

                    theHouse = new House();

                    // we obviously have enough paramteres, go on and try to connect
                    TcpClient client = new TcpClient();
                    client.Connect(Hostname, Port);
                    NetworkStream stream = client.GetStream();

                    // the read buffer (chosen quite big)
                    byte[]        myReadBuffer = new byte[4096 * 8];
                    List <String> Messages     = new List <string>();

                    // to build the complete message
                    StringBuilder myCompleteMessage = new StringBuilder();
                    int           numberOfBytesRead = 0;

                    MAXEncodeDecode DecoderEncoder = new MAXEncodeDecode();
                    keepRunning = true;
                    // Incoming message may be larger than the buffer size.
                    do
                    {
                        myCompleteMessage  = new StringBuilder();
                        stream.ReadTimeout = 1000;
                        try
                        {
                            numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
                            myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

                            Messages.Add(myCompleteMessage.ToString());
                        }
                        catch (Exception)
                        {
                            keepRunning = false;
                        }
                    }while(keepRunning);

                    List <String> PreProcessedMessages = new List <string>();
                    // preprocess
                    foreach (String _Message in Messages)
                    {
                        if (_Message.Remove(_Message.Length - 2).Contains("\r\n"))
                        {
                            String[] PMessages = _Message.Remove(_Message.Length - 2).Split(new char[1] {
                                '\n'
                            }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (String pmessage in PMessages)
                            {
                                PreProcessedMessages.Add(pmessage.Replace("\r", "") + "\r\n");
                            }
                        }
                        else
                        {
                            PreProcessedMessages.Add(_Message);
                        }
                    }
                    // Analyze and Output Messages
                    foreach (String _Message in PreProcessedMessages)
                    {
                        IMAXMessage Message = DecoderEncoder.ProcessMessage(_Message.ToString(), theHouse);

                        /*					if (Message != null)
                         *                                      {
                         *                                              ConsoleOutputLogger.WriteLine(_Message.ToString());
                         *                                              ConsoleOutputLogger.WriteLine(Message.ToString());
                         *                                              ConsoleOutputLogger.WriteLine("");
                         *                                      }*/
                    }
                    stream.Close();
                    client.Close();
                }
                catch (Exception)
                {
                }
                #endregion

                #region Diff the house
                if (previousHouse != null)
                {
                    // only if we already got two houses in here...
                    List <IDeviceDiffSet> differences = DiffHouse.CalculateDifferences(previousHouse, theHouse.GetAllDevicesInADictionary());
                    if (differences.Count != 0)
                    {
                        foreach (IDeviceDiffSet _difference in differences)
                        {
                            StringBuilder sb = new StringBuilder();

                            sb.Append("S\t" + _difference.DeviceName + "\t" + _difference.DeviceType);

                            if (_difference.DeviceType == DeviceTypes.HeatingThermostat)
                            {
                                HeatingThermostatDiff _heating = (HeatingThermostatDiff)_difference;

                                ConsoleOutputLogger.WriteLine(_heating.ToString());
                            }

                            if (_difference.DeviceType == DeviceTypes.ShutterContact)
                            {
                                ShutterContactDiff _shutter = (ShutterContactDiff)_difference;

                                ConsoleOutputLogger.WriteLine(_shutter.ToString());
                            }
                        }
                    }
                }
                #endregion
                Thread.Sleep(MAXUpdateTime);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method calculates the difference between house1 and house2 - whereas house1 is the old house2 the new one
        /// </summary>
        public static List <IDeviceDiffSet> CalculateDifferences(Dictionary <String, IMAXDevice> DevicesHouse1, Dictionary <String, IMAXDevice> DevicesHouse2)
        {
            List <IDeviceDiffSet> Differences = new List <IDeviceDiffSet>();

            foreach (KeyValuePair <String, IMAXDevice> House1KVPair in DevicesHouse1)
            {
                // now we have a device from house 1 - we need to get that same device in house 2
                if (DevicesHouse2.ContainsKey(House1KVPair.Key))
                {
                    // we got it
                    IMAXDevice House2Device = DevicesHouse2[House1KVPair.Key];

                    if (House1KVPair.Value.Type == DeviceTypes.HeatingThermostat)
                    {
                        // HeatingThermostat
                        HeatingThermostatDiff Diff = null;

                        HeatingThermostat Device1 = (HeatingThermostat)House1KVPair.Value;
                        HeatingThermostat Device2 = (HeatingThermostat)House2Device;

                        if (Device1.LowBattery != Device2.LowBattery)
                        {
                            if (Diff == null)
                            {
                                Diff = new HeatingThermostatDiff(Device2.Name, Device2.AssociatedRoom.RoomID, Device2.AssociatedRoom.RoomName);
                            }

                            if (Device2.LowBattery)
                            {
                                Diff.LowBattery = BatteryStatus.lowbattery;
                            }
                            else
                            {
                                Diff.LowBattery = BatteryStatus.ok;
                            }
                        }

                        if (Device1.Mode != Device2.Mode)
                        {
                            if (Diff == null)
                            {
                                Diff = new HeatingThermostatDiff(Device2.Name, Device2.AssociatedRoom.RoomID, Device2.AssociatedRoom.RoomName);
                            }

                            Diff.Mode = Device2.Mode;
                        }

                        if (Device1.Temperature != Device2.Temperature)
                        {
                            if (Diff == null)
                            {
                                Diff = new HeatingThermostatDiff(Device2.Name, Device2.AssociatedRoom.RoomID, Device2.AssociatedRoom.RoomName);
                            }

                            Diff.Temperature = Device2.Temperature;
                        }

                        if (Diff != null)
                        {
                            Differences.Add(Diff);
                        }
                    }
                    else
                    if (House1KVPair.Value.Type == DeviceTypes.ShutterContact)
                    {
                        // ShutterContact
                        ShutterContactDiff Diff = null;

                        ShutterContact Device1 = (ShutterContact)House1KVPair.Value;
                        ShutterContact Device2 = (ShutterContact)House2Device;

                        if (Device1.LowBattery != Device2.LowBattery)
                        {
                            if (Diff == null)
                            {
                                Diff = new ShutterContactDiff(Device2.Name, Device2.AssociatedRoom.RoomID, Device2.AssociatedRoom.RoomName);
                            }

                            if (Device2.LowBattery)
                            {
                                Diff.LowBattery = BatteryStatus.lowbattery;
                            }
                            else
                            {
                                Diff.LowBattery = BatteryStatus.ok;
                            }
                        }

                        if (Device1.ShutterState != Device2.ShutterState)
                        {
                            if (Diff == null)
                            {
                                Diff = new ShutterContactDiff(Device2.Name, Device2.AssociatedRoom.RoomID, Device2.AssociatedRoom.RoomName);
                            }

                            Diff.ShutterState = Device2.ShutterState;
                        }

                        if (Diff != null)
                        {
                            Differences.Add(Diff);
                        }
                    }
                }
            }

            return(Differences);
        }