/// <summary> /// Sends the Wake Up message to all the selected sensors /// and expects to receive the DoneWakeUp message in return /// as acknowledgement /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnWakeUp_Click(object sender, EventArgs e) { int count = listKnownSensors.SelectedItems.Count; if (count == 0) { MessageBox.Show("No sensor selected" + "\n" + Util.GetMethodNameAndLineNum(), "SENSIT Server", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { ServerMsg msg = new ServerMsg(); for (int i = 0; i < count; i++) { msg.RecipientId = (string)this.listKnownSensors.SelectedItems[i]; msg.Tag = ServerMsg.CommandTag.WakeUp; msg.Length = 0; msg.Value = null; WritePacket(msg); } } }
/// <summary> /// Sends the Set Ping Interval message to all the selected sensors /// and expects to receive the DoneSetPingInterval message in return /// as acknowledgement /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSetPingInterval_Click(object sender, EventArgs e) { int count = listKnownSensors.SelectedItems.Count; if (count == 0) { MessageBox.Show("No sensor selected\n" + "\n" + Util.GetMethodNameAndLineNum(), "SENSIT Server", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { ServerMsg msg = new ServerMsg(); decimal hh = numericHH.Value; decimal mm = numericMM.Value; decimal ss = numericSS.Value; float pingInterval = (float)((hh * 60 * 60) + (mm * 60) + ss); for (int i = 0; i < count; ++i) { msg.RecipientId = (string)this.listKnownSensors.SelectedItems[i]; msg.Tag = ServerMsg.CommandTag.SetPingInterval; msg.Length = 4; msg.Value = Util.FloatToByteArr(pingInterval); WritePacket(msg); } } }
private void btnRefreshSensorState_Click(object sender, EventArgs e) { ServerMsg msg = new ServerMsg(); msg.RecipientId = (string)this.listKnownSensors.SelectedItem; msg.Tag = ServerMsg.CommandTag.GetState; msg.Length = 0; msg.Value = null; WritePacket(msg); }
/// <summary> /// It processes any message received from any /// sensor. This function must be called by the derived classes /// of this class upon receiving a complete message packet from /// a sensor /// </summary> /// <param name="sensorMsg">Message packet from sensor</param> private void ProcessMessage(SensorMsg sensorMsg) { LoggerEnterMethod(); ServerMsg nextServerMsg; string Id = sensorMsg.SenderId; switch (sensorMsg.Tag) { case SensorMsg.CommandTag.Log: { float reading = Util.ByteArrToFloat(sensorMsg.Value) * Conf.SENSOR_READING_TO_RAINFALL_FACTOR; LoggerInfo("RECEIVED Rainfall measurement = " + reading + " from Sensor " + Id + " at " + sensorMsg.Date + ", " + sensorMsg.Time); /* Check if the sensor is already known or not. * If not, add it to the list of known sensors and * ask for its description */ if (KnownSensors.ContainsKey(Id) == false) { KnownSensors.Add(Id, new Sensor(Id)); AddToListKnownSensors(Id); LoggerInfo("ADDED Sensor " + Id); nextServerMsg = new ServerMsg(); nextServerMsg.RecipientId = Id; nextServerMsg.Tag = ServerMsg.CommandTag.SendDescription; nextServerMsg.Length = 0; nextServerMsg.Value = null; WritePacket(nextServerMsg); } // Save the last reading of the sensor KnownSensors[Id].LastReading = reading; KnownSensors[Id].LastReadingTime = sensorMsg.Date + " " + sensorMsg.Time; SaveSensorInfo(Id); // Logging the reading in the appropriate log file string path = frmOptions.RainfallLogDirPath + "/" + Id + "/" + Id + "_" + KnownSensors[Id].LastReadingTime.Split(new char[] { ' ' })[0].Replace('/', '-') + ".csv"; string dir = Path.GetDirectoryName(path); if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir); LoggerLog(path, KnownSensors[Id].LastReadingTime.Split(new char[] { ' ' })[1], String.Format("{0:" + Logging.SENSOR_LOG_READING_FORMAT + "}", KnownSensors[Id].LastReading)); break; } case SensorMsg.CommandTag.Description: { string desc = Util.ByteArrToString(sensorMsg.Value); LoggerInfo("RECEIVED Description = " + desc + " from Sensor " + Id); KnownSensors[Id].Description = desc; SaveSensorInfo(Id); break; } case SensorMsg.CommandTag.Resend: { LoggerInfo("RECEIVED Resend from Sensor " + Id); WritePacket(prevServerMsg[Id]); break; } case SensorMsg.CommandTag.DebugMsg: { string dm = Util.ByteArrToString(sensorMsg.Value); LoggerInfo("RECEIVED Debug Message = " + dm + " from Sensor " + Id); break; } case SensorMsg.CommandTag.PingInterval: { float pingInterval = Util.ByteArrToFloat(sensorMsg.Value); LoggerInfo("RECEIVED PingInterval = " + pingInterval.ToString() + " s from Sensor " + Id); KnownSensors[Id].PingInterval = pingInterval; SaveSensorInfo(Id); break; } case SensorMsg.CommandTag.State: { string state = Util.ByteArrToString(sensorMsg.Value); LoggerInfo("RECEIVED State = " + state + " from Sensor " + Id); KnownSensors[Id].State = (Sensor.StateEnum)Enum.Parse(typeof(Sensor.StateEnum), state); SaveSensorInfo(Id); break; } default: { LoggerError("Unhandled case " + sensorMsg.Tag.ToString()); #if DEBUG MessageBox.Show("Unhandled case " + sensorMsg.Tag.ToString() + "\n" + Util.GetMethodNameAndLineNum(), "SENSIT Server", MessageBoxButtons.OK, MessageBoxIcon.Error); #endif break; } } RefreshGuiSensorInfo(); // Remembering the previous message received from each sensor KnownSensors[Id].LastMsgSent = sensorMsg; LoggerLeaveMethod(); }
/// <summary> /// This function processes any message received from any /// sensor. This function must be called by the derived classes /// of this class upon receiving a complete message packet from /// a sensor /// </summary> /// <param name="serverMsg">Message packet from sensor</param> private void ProcessMessage(ServerMsg serverMsg) { SensorMsg nextSensorMsg = new SensorMsg(); nextSensorMsg.SenderId = sensor.Id; AddToTxtRX("--------------------\r\n"); switch (serverMsg.Tag) { case ServerMsg.CommandTag.SendDescription: AddToTxtRX("SendDescription\r\n"); nextSensorMsg.Tag = SensorMsg.CommandTag.Description; nextSensorMsg.Length = (byte)sensor.Desc.Length; nextSensorMsg.Value = (new UTF8Encoding()).GetBytes(sensor.Desc); WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.DebugMsg: AddToTxtRX("DebugMsg\r\n"); AddToTxtRX(Util.ByteArrToString(serverMsg.Value) + "\r\n"); break; case ServerMsg.CommandTag.Resend: AddToTxtRX("Resend\r\n"); WritePacket(prevSensorMsg); break; case ServerMsg.CommandTag.ResetReading: AddToTxtRX("ResetReading\r\n"); nextSensorMsg.Tag = SensorMsg.CommandTag.DoneResetReading; nextSensorMsg.Length = 0; nextSensorMsg.Value = null; WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.SetPingInterval: AddToTxtRX("SetPingTimePeriod\r\n"); float new_t = Util.ByteArrToFloat(serverMsg.Value); AddToTxtRX(new_t.ToString()+"\r\n"); nextSensorMsg.Tag = SensorMsg.CommandTag.DoneSetPingInterval; nextSensorMsg.Length = 4; nextSensorMsg.Value = Util.FloatToByteArr(new_t); WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.SetServerAddress: AddToTxtRX("SetServerAddress\r\n"); string new_add = Util.ByteArrToString(serverMsg.Value); AddToTxtRX(new_add + "\r\n"); break; case ServerMsg.CommandTag.Sleep: AddToTxtRX("Sleep\r\n"); nextSensorMsg.Tag = SensorMsg.CommandTag.DoneSleep; nextSensorMsg.Length = 0; nextSensorMsg.Value = null; WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.WakeUp: AddToTxtRX("WakeUp\r\n"); nextSensorMsg.Tag = SensorMsg.CommandTag.DoneWakeUp; nextSensorMsg.Length = 0; nextSensorMsg.Value = null; WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.Calibrate: AddToTxtRX("Calibrate\r\n"); calibParam.CalibDone = false; calibParam.NumSamples = 0; nextSensorMsg.Tag = SensorMsg.CommandTag.StartCalibration; nextSensorMsg.Length = 0; nextSensorMsg.Value = null; WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.MinHeight: AddToTxtRX("Min Height = " + Util.ByteArrToFloat(serverMsg.Value).ToString() + "\r\n"); calibParam.MinHeight = Util.ByteArrToFloat(serverMsg.Value); nextSensorMsg.Tag = SensorMsg.CommandTag.DoneMinHeight; nextSensorMsg.Length = 4; nextSensorMsg.Value = serverMsg.Value; WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.MaxHeight: AddToTxtRX("Max Height = " + Util.ByteArrToFloat(serverMsg.Value).ToString() + "\r\n"); calibParam.MaxHeight = Util.ByteArrToFloat(serverMsg.Value); nextSensorMsg.Tag = SensorMsg.CommandTag.DoneMaxHeight; nextSensorMsg.Length = 4; nextSensorMsg.Value = serverMsg.Value; WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.Height: AddToTxtRX("Height = " + Util.ByteArrToFloat(serverMsg.Value).ToString() + "\r\n"); ProcessCalibSample(Util.ByteArrToFloat(serverMsg.Value)); nextSensorMsg.Tag = SensorMsg.CommandTag.DoneHeight; nextSensorMsg.Length = 4; nextSensorMsg.Value = serverMsg.Value; WritePacket(nextSensorMsg); break; case ServerMsg.CommandTag.CalibrationDone: AddToTxtRX("Calibration Done\r\n"); CalibrateSensor(); // Calibrating the sensor with the collected data nextSensorMsg.Tag = SensorMsg.CommandTag.DoneCalibration; nextSensorMsg.Length = 0; nextSensorMsg.Value = null; WritePacket(nextSensorMsg); break; default: MessageBox.Show("Unhandled case " + serverMsg.Tag.ToString(), "Sensor Emulator", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } }
/// <summary> /// Sends a message to a sensor /// Format: /// 1 Byte: CommandTag of the message (Payload) /// 1 Byte: Length of the message /// Length Bytes: Message /// </summary> /// <param name="msg">It holds the message to be sent</param> /// <returns> /// Result.Success if writing is successful /// otherwise Result.Failure /// </returns> private Result WritePacket(ServerMsg msg) { LoggerEnterMethod(); try { byte[] byteArr = new byte[1 + 1 + msg.Length]; int idx = 0; byteArr[idx++] = (byte)msg.Tag; // CommandTag byteArr[idx++] = (byte)(msg.Length * 2); // Length for (int i = 0; i < msg.Length; ++i) byteArr[idx++] = msg.Value[i]; // Payload string hexStr = Util.ByteArrToHex(byteArr); LoggerInfo("Want to send " + hexStr); #region GSM SMS sending try { lock (SPortLock) { SerialPortWrite("AT+CMGS=\"" + msg.RecipientId + "\"\r"); SerialPortReadTo("> "); SerialPortWrite(hexStr); SerialPortWrite(new byte[] { 26 }, 0, 1); // Waiting for "OK" from SIM SerialPortReadTo("OK\r"); } } catch (TimeoutException exc) { LoggerError("Could not send SMS : " + exc.Message); #if DEBUG MessageBox.Show("GSM module not responding" + "\n" + exc.Message + "\n" + Util.GetMethodNameAndLineNum(), "SENSIT Server", MessageBoxButtons.OK, MessageBoxIcon.Error); #endif LoggerLeaveMethod(); return Result.Failure; } #endregion prevServerMsg[msg.RecipientId] = msg; #region Adding activity message to log files string activityMsg = string.Empty; switch (msg.Tag) { case ServerMsg.CommandTag.DebugMsg: { activityMsg = msg.Tag.ToString() + " = " + Util.ByteArrToString(msg.Value); break; } case ServerMsg.CommandTag.Resend: { activityMsg = msg.Tag.ToString(); break; } case ServerMsg.CommandTag.ResetReading: { activityMsg = msg.Tag.ToString(); break; } case ServerMsg.CommandTag.SendDescription: { activityMsg = msg.Tag.ToString(); break; } case ServerMsg.CommandTag.SetPingInterval: { activityMsg = msg.Tag.ToString() + " = " + Util.ByteArrToFloat(msg.Value).ToString() + " s"; break; } case ServerMsg.CommandTag.Sleep: { activityMsg = msg.Tag.ToString(); break; } case ServerMsg.CommandTag.WakeUp: { activityMsg = msg.Tag.ToString(); break; } case ServerMsg.CommandTag.GetPingInterval: { activityMsg = msg.Tag.ToString(); break; } case ServerMsg.CommandTag.GetState: { activityMsg = msg.Tag.ToString(); break; } default: { LoggerError("Unhandled case " + msg.Tag.ToString()); #if DEBUG MessageBox.Show("Unhandled case " + msg.Tag.ToString() + "\n" + Util.GetMethodNameAndLineNum(), "SENSIT Server", MessageBoxButtons.OK, MessageBoxIcon.Error); #endif break; } } LoggerInfo("SENT " + activityMsg + " to Sensor " + msg.RecipientId); #endregion } catch (Exception exc) { LoggerError("Something went wrong : " + exc.Message); #if DEBUG MessageBox.Show("Exception: " + exc.Message + "\n" + Util.GetMethodNameAndLineNum(), "SENSIT Server", MessageBoxButtons.OK, MessageBoxIcon.Error); #endif SetStripMessageText(string.Empty); SetStripGSMForeColor(Color.Red); LoggerLeaveMethod(); return Result.Failure; } LoggerLeaveMethod(); return Result.Success; }