Exemplo n.º 1
0
            public void PrepareForBroadcast(ArduinoDeviceManager adm)
            {
                ADMMessage message = (ADMMessage)Message;

                message.AddValue("BoardID", adm.BoardID);
                ArduinoDevice dev = null;

                if (message.TargetID > 0)
                {
                    dev = adm.GetDeviceByBoardID(message.TargetID);
                }
                else if (message.Sender != null && message.Sender != String.Empty)
                {
                    dev = adm.GetDevice(message.Sender);
                }
                message.AddValue(DEVICE_ID, dev != null ? dev.ID : "");
                message.AddValue(DEVICE_NAME, dev != null ? dev.Name : "");
            }
Exemplo n.º 2
0
        private void HandleReceivedADMMessage(ADMMessage message)
        {
            try
            {
                //do some general checking
                //TODO: check BoardID matches to SenderID
                lock (MessageStatsLock)
                {
                    LastMessageReceived    = message;
                    LastMessageReceivedOn  = DateTime.Now;
                    MessageReceivedSuccess = message.Tag == LastMessageSent.Tag;
                    MessagesReceived++;
                }
                //Console.WriteLine("<--------- {0}: Received message {1} tag {2} target {3} from sender {4}", PortAndNodeID, message.Type, message.Tag, message.TargetID, message.SenderID);

                switch (message.Type)
                {
                case Messaging.MessageType.INITIALISE_RESPONSE:
                    message.AddValue("LittleEndian", message.ArgumentAsBool(0));
                    LittleEndian = message.GetBool("LittleEndian");
                    message.AddValue("FreeMemory", message.ArgumentAsInt(1));
                    BoardID = message.SenderID;
                    message.AddValue("MaxDevices", message.ArgumentAsInt(2));
                    MaxDevices = message.GetInt("MaxDevices");
                    message.AddValue("LEDBI", message.ArgumentAsInt(3));
                    LEDBIPin = message.GetInt("LEDBI");
                    break;

                case Messaging.MessageType.STATUS_RESPONSE:
                    try
                    {
                        if (message.TargetID == 0)
                        {
                            message.AddValue("FreeMemory", message.ArgumentAsInt(0));
                            message.AddValue("BoardType", message.ArgumentAsString(1));
                            message.AddValue("Initialised", message.ArgumentAsBool(2));
                            message.AddValue("DeviceCount", message.ArgumentAsInt(3));
                        }
                        if (State == ADMState.CONNECTED)
                        {
                            State = ADMState.DEVICE_READY;
                        }
                    }
                    catch (Exception e)
                    {
                        Tracing?.TraceEvent(TraceEventType.Error, 4000, "STATUS_RESPONSE error: {0}, {1}", e.GetType(), e.Message);
                        throw new ArduinoException(PortAndNodeID, e.Message, e);
                    }

                    //record this
                    LastStatusResponseMessage = message;
                    LastStatusResponseOn      = DateTime.Now;
                    break;

                case Messaging.MessageType.PING_RESPONSE:
                    //record this
                    LastPingResponseMessage = message;
                    LastPingResponseOn      = DateTime.Now;
                    break;

                case Messaging.MessageType.CONFIGURE_RESPONSE:
                    break;

                case Messaging.MessageType.ERROR:
                    //record last error message
                    if (message.Arguments.Count > 0)
                    {
                        message.AddValue("ErrorCode", (ErrorCode)message.ArgumentAsByte(0));
                    }
                    else
                    {
                        message.AddValue("ErrorCode", ErrorCode.ERROR_HOST_GENERATED);
                    }
                    LastErrorMessage = message;
                    LastErrorOn      = DateTime.Now;
                    break;
                }

                if (State == ADMState.DEVICE_READY || State == ADMState.DEVICE_CONNECTED)
                {
                    //direct messages to devices
                    var dev = GetTargetedDevice(message);
                    if (dev != null)
                    {
#if DEBUG
                        Debug.Print(String.Format("Handling message {0} for device {1} ... connected: {2}, memory: {3}", message.Type, dev.ID, dev.IsConnected, message.HasValue("FM") ? message.GetValue("FM") : "N/A"));
#endif
                        try
                        {
                            dev.HandleMessage(message);
                        }
                        catch (Exception e)
                        {
                            Tracing?.TraceEvent(TraceEventType.Error, 4000, "Handling message for device {0} produced exception {1}: {2}", dev.ID, e.GetType(), e.Message);
                            throw new ArduinoException(dev.ID, e.Message, e);
                        }
                    }

                    //we do this test after handling message because the message maybe a CONFIGURE_RESPONSE message which will then set the 'connected' status of the device
                    if (message.Type == Messaging.MessageType.CONFIGURE_RESPONSE && DevicesConnected)
                    {
                        State = ADMState.DEVICE_CONNECTED;
                    }
                }
            } catch (Exception e)
            {
                byte tag = message != null ? message.Tag : (byte)0;
                message       = new ADMMessage();
                message.Type  = Messaging.MessageType.ERROR;
                message.Value = e.Message;
                message.AddValue("ErrorCode", ErrorCode.ERROR_HOST_GENERATED);
                if (e is ArduinoException)
                {
                    ArduinoException ae = (ArduinoException)e;
                    message.AddValue("ErrorSource", ae.Source);
                }

                message.Tag = tag;
            }

            Broadcast(message);
        }