Esempio n. 1
0
		public IMAXMessage ProcessMessage(String Message, House _House)
		{
			if (Message.Length < 2)
				throw new MAXException("Unable to process message: "+Message);

			// check what type of message we got and return the processed values

			if (Message.StartsWith("M:"))
				return new M_Message(Message, _House);

			if (Message.StartsWith("H:"))
				return new H_Message(Message, _House);

//			if (Message.StartsWith("C:"))
//				return new C_Message(Message, _House);

			if (Message.StartsWith("L:"))
				return new L_Message(Message, _House);

			//throw new NotImplementedException();
			return null;
		}
Esempio n. 2
0
File: Main.cs Progetto: aheil/hacs
        public static void Main(string[] args)
        {
            House thisHouse = new House();

            ConsoleOutputLogger.verbose = true;
            ConsoleOutputLogger.writeLogfile = true;

            Console.WriteLine ("ELV MAX! Debug Tool version 1 (C) Daniel Kirstenpfad 2012");
            Console.WriteLine();

            // not enough paramteres given, display help
            if (args.Length < 2)
            {
                Console.WriteLine("Syntax:");
                Console.WriteLine();
                Console.WriteLine("\tmaxdebug <hostname/ip> <port (e.g. 62910)> [commands]");
                Console.WriteLine();
                return;
            }
            ConsoleOutputLogger.LogToFile("--------------------------------------");
            // we obviously have enough paramteres, go on and try to connect
            TcpClient client = new TcpClient();
            client.Connect(args[0], Convert.ToInt32 (args[1]));
            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();

            // 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 e)
                {
                    //Console.WriteLine("Exception: "+e.Message);
                    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(), thisHouse);
                if (Message != null)
                {
                    //ConsoleOutputLogger.WriteLine(_Message.ToString());
                    ConsoleOutputLogger.WriteLine(Message.ToString());
                    //ConsoleOutputLogger.WriteLine("");
                }
            }
            // some writing
            if (args.Length > 2)
            {
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                byte[] args_data_buffer = enc.GetBytes(args[2]+"\r\n");

                ConsoleOutputLogger.WriteLine("Sending Command: "+args[2]);

                stream.Write(args_data_buffer,0,args_data_buffer.Length);
                keepRunning = true;
                Messages = new List<string>();

                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 e)
                    {
                        //jConsole.WriteLine("Exception: "+e.Message);
                        keepRunning = false;
                    }
                }
                while(keepRunning);

                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);
                }

                foreach(String _Message in PreProcessedMessages)
                {
                    IMAXMessage Message = DecoderEncoder.ProcessMessage(_Message,thisHouse);
                    if (Message != null)
                    {
                        ConsoleOutputLogger.WriteLine(Message.ToString());
                        ConsoleOutputLogger.LogToFile("");
                    }
                }
            }

            stream.Close();
            client.Close();
        }
Esempio n. 3
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);
			}
		}
Esempio n. 4
0
		// initializes this class and processes the given Input Message and fills the Message Fields
		public M_Message (String RAW_Message, House _House)
		{
			thisHouse = _House;
			if (RAW_Message.Length < 2)
				throw new MAXException("Unable to process the RAW Message.");

			if (!RAW_Message.StartsWith("M:"))
				throw new MAXException("Unable to process the RAW Message. Not a M Message.");

			String[] SplittedRAWMessage = RAW_Message.Remove(0,2).Split(new char[1] { ',' });

			if (SplittedRAWMessage.Length >= 3)
			{
				Index = Int32.Parse(SplittedRAWMessage[0],System.Globalization.NumberStyles.HexNumber);
				Count = Int32.Parse(SplittedRAWMessage[1],System.Globalization.NumberStyles.HexNumber);
				RawMessageDecoded = Base64.Decode(SplittedRAWMessage[2]);

//				System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
//				Console.WriteLine(enc.GetString (RawMessageDecoded));
//				StringBuilder hexlist = new StringBuilder();
//
//				foreach(Byte _byte in RawMessageDecoded)
//				{
//					hexlist.Append(_byte.ToString()+" ");
//				}

				Int32 Cursor = 2;

				// now go deeper
				Byte RoomCount = RawMessageDecoded[Cursor];
				Cursor++;

				#region Rooms
				// go through every room
				for(byte roomnumber=1;roomnumber<=RoomCount;roomnumber++)
				{
					Room newRoom = new Room(thisHouse);

					newRoom.RoomID = RawMessageDecoded[Cursor];
					Cursor++;

					Byte RoomNameLength = RawMessageDecoded[Cursor];
					Cursor++;

					byte[] RoomName = new byte[RoomNameLength];

					for(Byte j=0;j<=RoomNameLength-1;j++)
					{
						//RoomName.Append((char)RawMessageDecoded[Cursor]);
						RoomName[j] = RawMessageDecoded[Cursor];
						Cursor++;
					}
					newRoom.RoomName = System.Text.Encoding.UTF8.GetString(RoomName);

					StringBuilder RFAddress_Buffer = new StringBuilder();
					for(Byte j=0;j<=3-1;j++)
					{
						RFAddress_Buffer.Append(RawMessageDecoded[Cursor]);
						Cursor++;
					}
					newRoom.RFAddress = RFAddress_Buffer.ToString();//Int32.Parse(RFAddress_Buffer.ToString(),System.Globalization.NumberStyles.HexNumber);

					_House.Rooms.Add(newRoom);
				}

				#region Devices
				//newRoom.RFAddress = Int32.Parse(RFAddress.ToString(),System.Globalization.NumberStyles.HexNumber);
				Byte DeviceCount = RawMessageDecoded[Cursor];
				Cursor++;
				// go through all the devices in here
				for(byte devicenumber=1;devicenumber<=DeviceCount;devicenumber++)
				{
					// read in the device
					IMAXDevice newDevice = new UnknownDevice();

					#region Determine DeviceType
					Byte DevType = RawMessageDecoded[Cursor];
					Cursor++;
					
					switch(DevType)
					{
						case 1: 
							newDevice = new HeatingThermostat();
					        break;
					    case 2:
					        newDevice = new HeatingThermostatPlus();
					        break;
					    case 3:
					        newDevice = new WallMountedThermostat();
					        break;
					    case 4:
					        newDevice = new ShutterContact();
					        break;
					    case 5:
					        newDevice = new PushButton();
					        break;
					    default:
					        break;
					}
					#endregion

					StringBuilder DeviceRFAddress = new StringBuilder();
					for(Byte j=0;j<=3-1;j++)
					{
						DeviceRFAddress.Append(RawMessageDecoded[Cursor]);
						Cursor++;
					}
					newDevice.RFAddress = DeviceRFAddress.ToString();//Int32.Parse(DeviceRFAddress.ToString(),System.Globalization.NumberStyles.HexNumber);

					StringBuilder DeviceSerialNumber = new StringBuilder();
					for(Byte j=0;j<=10-1;j++)
					{
						DeviceSerialNumber.Append((char)RawMessageDecoded[Cursor]);
						Cursor++;
					}
					newDevice.SerialNumber = DeviceSerialNumber.ToString();

					Byte DeviceNameLength = RawMessageDecoded[Cursor];
					Cursor++;

					byte[] DeviceName = new byte[DeviceNameLength];

					for(Byte j=0;j<=DeviceNameLength-1;j++)
					{
						DeviceName[j] = RawMessageDecoded[Cursor];
						Cursor++;
					}
					newDevice.Name = System.Text.Encoding.UTF8.GetString(DeviceName);

					Byte RoomID = RawMessageDecoded[Cursor];
					Cursor++;

					// add the device to the room
					foreach(Room newRoom in _House.Rooms)
					{
						if (newRoom.RoomID == RoomID)
						{
							newDevice.AssociatedRoom = newRoom;
							newRoom.Devices.Add(newDevice);
							break;
						}
					}
				}
				#endregion
				// add this Room to the M_Message-Structure
				#endregion
			}
			else
				throw new MAXException("Unable to process M Message. Not enough content.");

		}
Esempio n. 5
0
		// initializes this class and processes the given Input Message and fills the Message Fields
		public L_Message (String RAW_Message, House _House)
		{
			DevicesInThisMessage = new List<IMAXDevice>();

			if (RAW_Message.Length < 2)
				throw new MAXException("Unable to process the RAW Message.");

			if (!RAW_Message.StartsWith("L:"))
				throw new MAXException("Unable to process the RAW Message. Not a L Message.");

			RawMessageDecoded = Base64.Decode(RAW_Message.Remove(0,2));

			// Tokenize RAW Message
			List<byte[]> Tokenized = TokenizeMessage.Tokenize(RawMessageDecoded);

			foreach(byte[] array in Tokenized)
			{
				StringBuilder sb = new StringBuilder();

				for(int i=0;i<=2;i++)
				{
					sb.Append(array[i]);
				}
				// get data 1 and data 2 out
				// on position 5,6
				byte Data1 = array[4];
				byte Data2 = array[5];

				String binValueData1 = Convert.ToString(Data1,2);
				binValueData1 = binValueData1.PadLeft(8, '0');
				String binValueData2 = Convert.ToString(Data2,2);
				binValueData2 = binValueData2.PadLeft(8, '0');

				Int32 Cursor = 7;	// the current position, skipping ?1,

				String RFAddress = sb.ToString();

				#region look for this RF Adress in the House's device list
				List<IMAXDevice> AllDevices = _House.GetAllDevices();
				IMAXDevice foundDevice = null;
				foreach(IMAXDevice _device in AllDevices)
				{
					if (_device.RFAddress == RFAddress)
					{
						foundDevice = _device;
						break;
					}
				}
				#endregion

				if (foundDevice != null)
				{
					DevicesInThisMessage.Add(foundDevice);

					#region HeatingThermostat
					if (foundDevice.Type == DeviceTypes.HeatingThermostat)
					{
						HeatingThermostat KnownDevice = (HeatingThermostat)foundDevice;
		
						#region get all those flags out of Data1 and Data2

						#region Valid
						if (binValueData1[3] == '1')
							KnownDevice.Valid = true;
						else
							KnownDevice.Valid = false;
						#endregion

						#region Error
						if (binValueData1[4] == '1')
							KnownDevice.Error = true;
						else
							KnownDevice.Error = false;
						#endregion

						#region IsAnswer
						if (binValueData1[5] == '1')
							KnownDevice.IsAnswer = true;
						else
							KnownDevice.IsAnswer = false;
						#endregion

						#region LowBattery
						if (binValueData2[0] == '1')
							KnownDevice.LowBattery = true;
						else
							KnownDevice.LowBattery = false;
						#endregion

						#region LinkError
						if (binValueData2[1] == '1')
							KnownDevice.LinkError = true;
						else
							KnownDevice.LinkError = false;
						#endregion

						#region PanelLock
						if (binValueData2[2] == '1')
							KnownDevice.PanelLock = true;
						else
							KnownDevice.PanelLock = false;
						#endregion

						#region GatewayOK
						if (binValueData2[3] == '1')
							KnownDevice.GatewayOK = true;
						else
							KnownDevice.GatewayOK = false;
						#endregion

						#region Mode
						String ModeValue = binValueData2[6]+""+binValueData2[7];

						switch(ModeValue)
						{
							case "00":
								KnownDevice.Mode = ThermostatModes.automatic;
							break;
							case "01":
								KnownDevice.Mode = ThermostatModes.manual;
							break;
							case "10":
								KnownDevice.Mode = ThermostatModes.vacation;
							break;
							case "11":
								KnownDevice.Mode = ThermostatModes.boost;
							break;	
							default:
							break;
						}
						#endregion

						#endregion

						// hurray, we've got a device we know how to handle B-)
						((HeatingThermostat)foundDevice).Temperature = array[Cursor]/2;
						Cursor++;
					}
					#endregion

					#region ShutterContact
					if (foundDevice.Type == DeviceTypes.ShutterContact)
					{
						ShutterContact KnownDevice = (ShutterContact)foundDevice;
		
						#region get all those flags out of Data1 and Data2

						#region Valid
						if (binValueData1[3] == '1')
							KnownDevice.Valid = true;
						else
							KnownDevice.Valid = false;
						#endregion

						#region Error
						if (binValueData1[4] == '1')
							KnownDevice.Error = true;
						else
							KnownDevice.Error = false;
						#endregion

						#region IsAnswer
						if (binValueData1[5] == '1')
							KnownDevice.IsAnswer = true;
						else
							KnownDevice.IsAnswer = false;
						#endregion

						#region LowBattery
						if (binValueData2[0] == '1')
							KnownDevice.LowBattery = true;
						else
							KnownDevice.LowBattery = false;
						#endregion

						#region LinkError
						if (binValueData2[1] == '1')
							KnownDevice.LinkError = true;
						else
							KnownDevice.LinkError = false;
						#endregion

						#region PanelLock
						if (binValueData2[2] == '1')
							KnownDevice.PanelLock = true;
						else
							KnownDevice.PanelLock = false;
						#endregion

						#region GatewayOK
						if (binValueData2[3] == '1')
							KnownDevice.GatewayOK = true;
						else
							KnownDevice.GatewayOK = false;
						#endregion

						#region Mode
						String ModeValue = binValueData2[6]+""+binValueData2[7];

						switch(ModeValue)
						{
							case "00":
								KnownDevice.ShutterState = ShutterContactModes.closed;
							break;
							case "10":
								KnownDevice.ShutterState = ShutterContactModes.open;
							break;
							default:
							break;
						}
						#endregion

						#endregion

					}
					#endregion
				}
			}
		}
Esempio n. 6
0
 public Room(House _House)
 {
     AssociatedHouse = _House;
     Devices         = new List <IMAXDevice>();
 }