private void customPacker_SensorRecordGet(object sender, WATR_SensorRecordGetArgs e) { //Log the update database.LogSensorRecord(e.SensorRecord); //Update the device to point to the most current record; deviceList[e.SensorRecord.SourceAddress].SetRecord(e.SensorRecord); }
//Parse an incoming temperature record private void parseRFDataTemperatureRecord(ulong source, byte[] rf) { string rfData = rf.ConvertToString(customPacketDataStart); //Simple error check - the temp record should -NEVER- be less than 5 bytes. EVER. if (rfData.Length < 5) return; //Get the temperature //Math: The index of the first "T", add one so we don't include the T //Then, the length is the lower 't' position subtracting the upper-case T's location //So if we have "T".loc = 3 and "t".loc = 8 //We start at position 3 + 1 = 4 for the first part of the temperature //Then copy until 8 - 3 = 5 - 1 = 4 (because of the fact that "T" is actually one more letter than we need) int temperature = Convert.ToInt32(rfData.Substring(rfData.IndexOf('T') + 1, rfData.IndexOf('t') - rfData.IndexOf('T') - 1)); //The rest of the records follow the same logic int humidity = Convert.ToInt32(rfData.Substring(rfData.IndexOf('H') + 1, rfData.IndexOf('h') - rfData.IndexOf('H') - 1)); int moisture = Convert.ToInt32(rfData.Substring(rfData.IndexOf('M') + 1, rfData.IndexOf('m') - rfData.IndexOf('M') - 1)); int battery = Convert.ToInt32(rfData.Substring(rfData.IndexOf('B') + 1, rfData.IndexOf('b') - rfData.IndexOf('B') - 1)); //Create a record WATR_SensorRecord record = new WATR_SensorRecord(source, temperature, humidity, moisture, battery); //Raise an event WATR_SensorRecordGetArgs getRecord = new WATR_SensorRecordGetArgs(record); if (getRecord != null) SensorRecordGet(this, getRecord); }