Beispiel #1
0
        public List <int> DeleteMessages(List <int> listID)
        {
            List <int> listMsgDelete = new List <int>();

            using (var db = DatabaseFactory.OpenDbConnection())
            {
                foreach (int item in listID)
                {
                    ReceiverMessage receiver = db.Where <ReceiverMessage>(m => m.MessageId == item && m.UserId == NimbusUser.UserId && m.Visible == true).FirstOrDefault();
                    if (receiver != null)
                    {
                        receiver.Visible = false;
                        db.Update <ReceiverMessage>(receiver);
                        listMsgDelete.Add(item);
                    }
                }
            }
            return(listMsgDelete);
        }
Beispiel #2
0
        private static void Parser_MessageReceived(object sender, ProtocolEventArgs e)
        {
            ReceiverMessage rcvMessage  = null;
            RFMMessage      meshMessage = null;

            // Message was successfully parsed
            GPRSMessage receivedGPRSMessage = (e.Message as GPRSMessage);
            // Because GPRS protocol requires acknowledgments. Send acknowledge message.
            GPRSMessage ack = GPRSMessage.GenerateGPRSAcknowledgeMessage(receivedGPRSMessage.MessageNumber);

            ((sender as GPRSParser).Info as IScsServerClient).SendMessage(new ScsRawDataMessage(ack.Bytes));

            // Handle received message. It can be mesh protocol message or old protocol receiver message
            try
            {
                meshMessage = MessageFactory.ParseMeshMessage(receivedGPRSMessage.Body) as RFMMessage;
            }
            catch
            {
                try
                {
                    meshMessage = null;
                    rcvMessage  = MessageFactory.ParseReceiverMessage(receivedGPRSMessage.Body) as ReceiverMessage;
                }
                catch
                {
                    rcvMessage = null;
                }
            }

            if (meshMessage != null)
            {
                // Mesh protocol

                // Print receiver details such as IMEI and Address
                Console.WriteLine(string.Format("GPRS Message # {0}, IMEI: {1}, Address: {2}", receivedGPRSMessage.MessageNumber, receivedGPRSMessage.MainReceiverID, receivedGPRSMessage.ReceiverID));

                // !!! Here you can implement your custom logics according to message type.
                Console.WriteLine(meshMessage.ToString()); // Use ToString method to print inner fields

                // Or handle message according its type as follows
                if (meshMessage.GetType() == typeof(DiagnosticGetShortAddressMessage))
                {
                    DiagnosticGetShortAddressMessage diagnosticGetShorAddressMessage = meshMessage as DiagnosticGetShortAddressMessage;
                    Console.WriteLine(string.Format("Tag {0} DiagnosticGetShortAddressMessage", diagnosticGetShorAddressMessage.DeviceID));
                }

                //// Additional messages. Use as shown before:
                // GetRegisterRequestMessage
                // GetRegisterResponseMessage
                // SetRegisterRequestMessage
                // SetRegisterResponseMessage
                // GetTimeRequestMessage
                // GetTimeResponseMessage
                // TesterCommandMessage
                // AssociationRequestEventMessage
                // CheckInEventMessage
                // DataInMessage
                // DataRawInMessage
                // DataOutMessage
                // DataSensorMessage
                // ErrorEventMessage

                //// Sensor messages:
                // GPIOSensorMessage
                // AccelerometerSensorMessage
                // AnalogSensorMessage
                // BME280SensorMessage
                // DS18B20SensorMessage
                // SI7021SensorMessage
            }
            else if (rcvMessage != null)
            {
                // Old protocol receiver message

                // Print receiver details such as IMEI and Address
                Console.WriteLine(string.Format("GPRS Message # {0}, IMEI: {1}, Address: {2}, Inner message type: {3}", receivedGPRSMessage.MessageNumber, receivedGPRSMessage.MainReceiverID, receivedGPRSMessage.ReceiverID, rcvMessage.GetType().ToString()));

                if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.PowerAlert)
                {
                    // Receiver power alert. Is sent from the gateway when external power is connected or disconnected
                    // Note: Available only on gateways with the inner battery
                    ReceiverPowerAlertMessage pam = rcvMessage as ReceiverPowerAlertMessage;
                    Console.WriteLine(string.Format("Gateway (IMEI: {0}) External power {1}", receivedGPRSMessage.MainReceiverID, pam.ExternalPowerConnected ? "connected" : "disconnected"));
                }
                else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ModemVoltage)
                {
                    // Modem voltage. Actually sends voltage mesured inside GPRS modem module
                    ReceiverModemVoltageMessage mv = rcvMessage as ReceiverModemVoltageMessage;
                    Console.WriteLine(string.Format("Gateway (IMEI: {0}) Modem voltage message: {1}", receivedGPRSMessage.MainReceiverID, mv.ToString()));
                }
                else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ReceiverMessage)
                {
                    // Receiver configuration message.
                    ReceiverConfigurationMessage rcm = rcvMessage as ReceiverConfigurationMessage;

                    if (rcvMessage.GetType() == typeof(ReceiverConfigurationVoltageMessage))
                    {
                        ReceiverConfigurationVoltageMessage cvm = rcvMessage as ReceiverConfigurationVoltageMessage;
                        Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module voltage message: {1}", receivedGPRSMessage.MainReceiverID, cvm.ToString()));
                    }
                    else if (rcvMessage.GetType() == typeof(ReceiverConfigurationExternalVoltageMessage))
                    {
                        ReceiverConfigurationExternalVoltageMessage cevm = rcvMessage as ReceiverConfigurationExternalVoltageMessage;
                        Console.WriteLine(string.Format("Gateway (IMEI: {0}) External voltage message: {1}", receivedGPRSMessage.MainReceiverID, cevm.ToString()));
                    }
                    else if (rcvMessage.GetType() == typeof(ReceiverConfigurationTemperatureMessage))
                    {
                        ReceiverConfigurationTemperatureMessage ctm = rcvMessage as ReceiverConfigurationTemperatureMessage;
                        Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module temperature message: {1}", receivedGPRSMessage.MainReceiverID, ctm.ToString()));
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module configuration message: {1}", receivedGPRSMessage.MainReceiverID, rcm.ToString()));
                    }
                }
                else
                {
                    Console.WriteLine(rcvMessage.ToString());
                }
            }
        }
Beispiel #3
0
        private static void Parser_MessageReceived(object sender, ProtocolEventArgs e)
        {
            // Message was successfully parsed
            GPRSMessage receivedGPRSMessage = (e.Message as GPRSMessage);
            // Because GPRS protocol requires acknowledgments. Send acknowledge message.
            GPRSMessage ack = GPRSMessage.GenerateGPRSAcknowledgeMessage(receivedGPRSMessage.MessageNumber);

            ((sender as GPRSParser).Info as IScsServerClient).SendMessage(new ScsRawDataMessage(ack.Bytes));

            // Handle received message
            ReceiverMessage rcvMessage = MessageFactory.ParseReceiverMessage(receivedGPRSMessage.Body) as ReceiverMessage;

            // Print receiver details such as IMEI and Address
            Console.WriteLine(string.Format("GPRS Message # {0}, IMEI: {1}, Address: {2}, Inner message type: {3}", receivedGPRSMessage.MessageNumber, receivedGPRSMessage.MainReceiverID, receivedGPRSMessage.ReceiverID, rcvMessage.GetType().ToString()));

            if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.TagDataMessages)
            {
                foreach (TagMessage tm in (rcvMessage as TagDataMessage).TagMessages)
                {
                    // Handle tag messages according its type
                    //if (tm.GetType() == typeof(TagTemperatureMessage))
                    //{
                    //    // Temperature message
                    //    TagTemperatureMessage tempMsg = tm as TagTemperatureMessage;
                    //    Console.WriteLine(string.Format("Tag ID {0}, Temperature: {1:0.00}", tempMsg.TagID, tempMsg.Temperature));
                    //}
                    //else if (tm.GetType() == typeof(TagPeriodicMessage))
                    //{
                    //    // Periodic message
                    //    TagPeriodicMessage periodicMsg = tm as TagPeriodicMessage;

                    //    Console.WriteLine(string.Format("Tag ID {0}, Voltage: {1:0.00}", periodicMsg.TagID, periodicMsg.Voltage));
                    //}
                    //else if (tm.GetType() == typeof(TagCounterMessage))
                    //{
                    //    // Counter
                    //    TagCounterMessage counterMsg = tm as TagCounterMessage;
                    //    Console.WriteLine(string.Format("Tag ID {0}, Counter: {1}, Voltage: {2:0.00}", counterMsg.TagID, counterMsg.Counter, counterMsg.Voltage));
                    //}
                    //else if (tm.GetType() == typeof(TagActivateMessage))
                    //{
                    //    // Activate message
                    //    TagActivateMessage activateMsg = tm as TagActivateMessage;

                    //    Console.WriteLine(string.Format("Tag ID {0}, Activator: {1}", activateMsg.TagID, activateMsg.Activator));
                    //}
                    //else if (tm.GetType() == typeof(TagPressureMessage))
                    //{
                    //    // Pressure message
                    //    TagPressureMessage pressureMsg = tm as TagPressureMessage;

                    //    Console.WriteLine(string.Format("Tag ID {0}, Pressure: {1:0.00}, Humidity: {2:0.00}, Temperature: {3:0.00}", pressureMsg.TagID, pressureMsg.Pressure, pressureMsg.Humidity, pressureMsg.Temperature));
                    //}
                    //else if (tm.GetType() == typeof(TagTemperatureRHMessage))
                    //{
                    //    // Temperature RH message
                    //    TagTemperatureRHMessage temperatureRHMsg = tm as TagTemperatureRHMessage;

                    //    Console.WriteLine(string.Format("Tag ID {0}, Temperature: {1:0.00}, Humidity: {2:0.00}, Voltage: {3:0.00}", temperatureRHMsg.TagID, temperatureRHMsg.Temperature, temperatureRHMsg.Humidity, temperatureRHMsg.Voltage));
                    //}

                    //// Additional messages. Use as shown before
                    //// TagPushButtonGPIO9Message
                    //// TagAngleCalibrationMessage
                    //// TagAgroMessage
                    //// TagMovementMessage
                    //// TagAngleCalibrationRecalibrationMessage
                    //// TagMovementAverageMessage
                    //// TagPushButtonGPIO8Message
                    //// TagDistanceMessage
                    //// TagAnalogMessage
                    //// TagNoMovementMessage
                    //// TagAnalogCounterMessage
                    //// TagAngleMessage
                    //// TagIButtonMessage
                    //// TagMagnetDetectionGPIO8Message
                    //// TagProductionMessage
                    //// TagAveragePulsesPerSecondMessage

                    // Print message details by using ToString() method
                    Console.WriteLine(tm.ToString());
                }
            }
            else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.Transient)
            {
                byte[] transientData = (rcvMessage as ReceiverTransientMessage).TransientData;

                if ((transientData != null) && (transientData.Length > 0) && (transientData[0] == 0x96))
                {
                    // New protocol message
                    ClientInfo info = infos[((sender as GPRSParser).Info as IScsServerClient).ClientId];
                    if (info != null && info.new_parser != null)
                    {
                        info.new_parser.AppendBytes(RFPPFHelper.BitStuffEncode(transientData));
                    }
                }
                else
                {
                    Console.WriteLine(RFPPFHelper.ByteArrayToHexString((rcvMessage as ReceiverTransientMessage).TransientData));
                }
            }
            else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.PowerAlert)
            {
                // Receiver power alert. Is sent from the gateway when external power is connected or disconnected
                // Note: Available only on gateways with the inner battery
                ReceiverPowerAlertMessage pam = rcvMessage as ReceiverPowerAlertMessage;
                Console.WriteLine(string.Format("Gateway (IMEI: {0}) External power {1}", receivedGPRSMessage.MainReceiverID, pam.ExternalPowerConnected? "connected" : "disconnected"));
            }
            else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ModemVoltage)
            {
                // Modem voltage. Actually sends voltage mesured inside GPRS modem module
                ReceiverModemVoltageMessage mv = rcvMessage as ReceiverModemVoltageMessage;
                Console.WriteLine(string.Format("Gateway (IMEI: {0}) Modem voltage message: {1}", receivedGPRSMessage.MainReceiverID, mv.ToString()));
            }
            else if (rcvMessage.MessageType == ReceiverMessage.MessageTypes.ReceiverMessage)
            {
                // Receiver configuration message.
                ReceiverConfigurationMessage rcm = rcvMessage as ReceiverConfigurationMessage;

                if (rcvMessage.GetType() == typeof(ReceiverConfigurationVoltageMessage))
                {
                    ReceiverConfigurationVoltageMessage cvm = rcvMessage as ReceiverConfigurationVoltageMessage;
                    Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module voltage message: {1}", receivedGPRSMessage.MainReceiverID, cvm.ToString()));
                }
                else if (rcvMessage.GetType() == typeof(ReceiverConfigurationExternalVoltageMessage))
                {
                    ReceiverConfigurationExternalVoltageMessage cevm = rcvMessage as ReceiverConfigurationExternalVoltageMessage;
                    Console.WriteLine(string.Format("Gateway (IMEI: {0}) External voltage message: {1}", receivedGPRSMessage.MainReceiverID, cevm.ToString()));
                }
                else if (rcvMessage.GetType() == typeof(ReceiverConfigurationTemperatureMessage))
                {
                    ReceiverConfigurationTemperatureMessage ctm = rcvMessage as ReceiverConfigurationTemperatureMessage;
                    Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module temperature message: {1}", receivedGPRSMessage.MainReceiverID, ctm.ToString()));
                }
                else
                {
                    Console.WriteLine(string.Format("Gateway (IMEI: {0}) Radio module configuration message: {1}", receivedGPRSMessage.MainReceiverID, rcm.ToString()));
                }
            }
            else
            {
                Console.WriteLine(rcvMessage.ToString());
            }

            // To send SMS via GPRS gateway use following code
            // Change <Phone Number> to phone number string
            // Change <Message> to message string
            // client is an instance of IScsServerClient (in this case - client = ((sender as GPRSParser).Info as IScsServerClient) )
            //
            //GPRSMessage smsMsg = GPRSMessage.GenerateSendSMSMessage(<Phone Number>, <Message>);
            //client.SendMessage(new ScsRawDataMessage(smsMsg.Bytes));

            // To set register on mesh tag use following code
            // Change <DeviceID> to required tag ID
            // Change <RegisterNumber> to required register number
            // Change <RegisterValue> to required register value
            // client is an instance of IScsServerClient (in this case - client = ((sender as GPRSParser).Info as IScsServerClient) )
            //RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessage getRegMsg = RFPPF.Messages.MessageFactory.CreateMessage(typeof(RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessage), new RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessageBuilder()
            //{
            //    Version = 1,
            //    DeviceID = <DeviceID>,
            //    MessageNumber = 0,
            //    RegisterNumber = <RegisterNumber>,
            //    RegisterValue = <RegisterValue>
            //}) as RFPPF.Messages.RFM.Configuration.SetRegisterRequestMessage;
            //GPRSMessage transientMsg = GPRSMessage.GenerateTransientMessage(getRegMsg.Bytes);
            //client.SendMessage(new ScsRawDataMessage(transientMsg.Bytes));
        }
Beispiel #4
0
        public List <Message> Get_Message_Apartment(ReceiverMessage receiver, string type)
        {
            var data = new List <Message>();

            try
            {
                var db    = new DB();
                var Query = "";
                if (type == "1")
                {
                    Query = @"select m.id,r.usrname,m.message_body,m.from_user_id, r.images, m.seen from messages m, register r where m.from_user_id = r.user_id and apartment_id = @m_apartment_id and to_user_id = @m_user_id and seen is null";
                }
                else
                {
                    Query = @"select m.id,r.usrname,m.message_body,m.from_user_id, r.images, m.seen from messages m, register r where m.from_user_id = r.user_id and apartment_id = @m_apartment_id and to_user_id = @m_user_id and seen is not null";
                }

                var cmd = new NpgsqlCommand();
                cmd.CommandText = Query;
                cmd.Parameters.AddWithValue("@m_user_id", receiver.to_user_id);
                cmd.Parameters.AddWithValue("@m_apartment_id", receiver.apartment_id);
                cmd.Connection = db.npgsqlConnection;
                NpgsqlDataReader DataReader;
                DataReader = cmd.ExecuteReader();

                cmd.Dispose();

                if (DataReader.HasRows)
                {
                    while (DataReader.Read())
                    {
                        data.Add(new Message
                        {
                            id            = Convert.ToInt32(DataReader.GetValue(0)),
                            from_username = DataReader.GetValue(1).ToString(),
                            message_body  = DataReader.GetValue(2).ToString(),
                            from_user_id  = Convert.ToInt32(DataReader.GetValue(3)),
                            images        = DataReader.GetValue(4).ToString(),
                            seen          = DataReader.GetValue(5).ToString(),
                        });
                    }
                }
                DataReader.Close();
                DataReader.Dispose();
                db.close();
                if (type == "1")
                {
                    for (int i = 0; i < data.Count(); i++)
                    {
                        data[i].images = "<img src=' " + data[i].images + " ' style='height:80px; width:80px'>";
                        data[i].seen   = "<img src=' " + "https://cdn4.iconfinder.com/data/icons/starup-business-2/120/hide_unseen_seen_eye_blind-512.png" + " ' style='height:20px; width:20px'>";
                    }
                }
                else
                {
                    for (int i = 0; i < data.Count(); i++)
                    {
                        data[i].images = "<img src=' " + data[i].images + " ' style='height:80px; width:80px'>";
                        data[i].seen   = "<img src=' " + data[i].seen + " ' style='height:30px; width:30px'>";
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(data);
        }
Beispiel #5
0
 public ReceiverMessage GetMessagesApartment([FromBody] ReceiverMessage values)
 {
     receiver.apartment_id = values.apartment_id;
     receiver.to_user_id   = values.to_user_id;
     return(receiver);
 }