Beispiel #1
0
        /// <summary>
        /// Returns the new Address field for the NodeDevice
        /// </summary>
        public FieldString ShowDialog()
        {
            Window dlg = new AuthenticateDialogView();

            dlg.Owner       = mainWindowExport.Value;
            dlg.DataContext = this;
            dlg.ShowDialog();

            FieldString retVal = null;

            if (Username != string.Empty)
            {
                var twitter = FluentTwitter.CreateRequest()
                              .Configuration.UseHttps()
                              .Authentication.GetClientAuthAccessToken(TwitterConsumer.ConsumerKey,
                                                                       TwitterConsumer.ConsumerSecret,
                                                                       Username,
                                                                       Password.ConvertToUnsecureString());

                var response = twitter.Request();
                try
                {
                    var token = response.AsToken();
                    // the token and token secret seem to be base-64 encoded already, but there's no guarantee, so let's encode them again
                    var item1 = FieldBase64.Encode(token.Token).ToString();
                    var item2 = FieldBase64.Encode(token.TokenSecret).ToString();
                    retVal = new FieldString(item1 + AbstractTwitterDevice.ADDRESS_SEPARATOR + item2);
                    messagingService.ShowMessage(Resources.Strings.ContextMenu_Authenticate_Success, Resources.Strings.ContextMenu_Authenticate_Success_Title);
                }
                catch (Exception ex)
                {
                    logger.Error("Error authenticating Twitter", ex);
                    messagingService.ShowMessage(Resources.Strings.ContextMenu_Authenticate_Fail, Resources.Strings.ContextMenu_Authenticate_Fail_Title);
                }
            }

            return(retVal);
        }
Beispiel #2
0
        private NodeTelegram readSignals(NodeTelegram request)
        {
            // incoming request should just be a bunch of Guids that represent signalIds
            var inPayload  = request.Payload.Decode();
            var outPayload = new StringBuilder();

            if (runtimeApplication != null)
            {
                for (int index = 0; index < inPayload.Length; index += m_guidLength)
                {
                    string oneGuidString = inPayload.Substring(index, m_guidLength);
                    if (FieldGuid.CheckSyntax(oneGuidString))
                    {
                        var signalId = new FieldGuid(oneGuidString);
                        var signal   = runtimeApplication.FindSignal(signalId);
                        if (signal != null)
                        {
                            outPayload.Append(EncodedSignalValue.EncodeSignalValue(signal));
                        }
                    }
                }
            }
            return(request.SetPayload(FieldBase64.Encode(outPayload.ToString())));
        }
        internal static NodeDeviceConfiguration MergeDeviceConfiguration(
            DeviceConfigurationResponse deviceConfigurationResponse,
            NodeDeviceConfiguration existingDeviceConfiguration = null)
        {
            var driversMutable = new Collection <NodeDriver>();
            var nodeDriver     = NodeDriver.BuildWith(
                new FieldGuid(SnapDriver.ARDUINO_LOCAL_IO_DRIVER_TYPE_ID), // TypeId
                new FieldString(string.Empty),                             // Address
                FieldBase64.Encode(string.Empty),                          // Configuration
                new FieldString(deviceConfigurationResponse.DeviceName));

            var devicesMutable = new Collection <NodeDevice>();

            // Just creating 4 "virtual" devices under the Arduino Local I/O driver
            // so that it looks organized in the Solution Explorer
            var discreteInputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("DiscreteInputs"),    // Code
                new FieldGuid(DISCRETE_INPUTS_DEVICE_ID), // TypeId
                new FieldString(),                        // Address
                new FieldBase64(string.Empty),            // Configuration
                new FieldDeviceName("Discrete Inputs"));
            var discreteOutputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("DiscreteOutputs"),    // Code
                new FieldGuid(DISCRETE_OUTPUTS_DEVICE_ID), // TypeId
                new FieldString(),                         // Address
                new FieldBase64(string.Empty),             // Configuration
                new FieldDeviceName("Discrete Outputs"));
            var analogInputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("AnalogInputs"),    // Code
                new FieldGuid(ANALOG_INPUTS_DEVICE_ID), // TypeId
                new FieldString(),                      // Address
                new FieldBase64(string.Empty),          // Configuration
                new FieldDeviceName("Analog Inputs"));
            var analogOutputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("AnalogOutputs"),    // Code
                new FieldGuid(ANALOG_OUTPUTS_DEVICE_ID), // TypeId
                new FieldString(),                       // Address
                new FieldBase64(string.Empty),           // Configuration
                new FieldDeviceName("Analog Outputs"));

            var discreteInputsMutable  = new Collection <NodeDiscreteInput>();
            var discreteOutputsMutable = new Collection <NodeDiscreteOutput>();
            var analogInputsMutable    = new Collection <NodeAnalogInput>();
            var analogOutputsMutable   = new Collection <NodeAnalogOutput>();

            foreach (var ioSignal in deviceConfigurationResponse.IOSignals)
            {
                switch (ioSignal.Type)
                {
                case DeviceConfigurationResponse.IOSignalType.DiscreteInput:
                    var newDiscreteInput = NodeDiscreteInput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingDiscreteInput = existingDeviceConfiguration.GetChildrenRecursive()
                                                    .Select(x => x.Value as NodeDiscreteInput)
                                                    .Where(x => x != null)
                                                    .Where(x => x.Code.ToString() == ioSignal.Name)
                                                    .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingDiscreteInput != null)
                        {
                            newDiscreteInput = newDiscreteInput
                                               .SetSignal(existingDiscreteInput.Signal);
                        }
                    }
                    discreteInputsMutable.Add(newDiscreteInput);
                    break;

                case DeviceConfigurationResponse.IOSignalType.DiscreteOutput:
                    var newDiscreteOutput = NodeDiscreteOutput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingDiscreteOutput = existingDeviceConfiguration.GetChildrenRecursive()
                                                     .Select(x => x.Value as NodeDiscreteOutput)
                                                     .Where(x => x != null)
                                                     .Where(x => x.Code.ToString() == ioSignal.Name)
                                                     .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingDiscreteOutput != null)
                        {
                            newDiscreteOutput = newDiscreteOutput
                                                .SetSignalIn(existingDiscreteOutput.SignalIn);
                        }
                    }
                    discreteOutputsMutable.Add(newDiscreteOutput);
                    break;

                case DeviceConfigurationResponse.IOSignalType.AnalogInput:
                    var newAnalogInput = NodeAnalogInput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingAnalogInput = existingDeviceConfiguration.GetChildrenRecursive()
                                                  .Select(x => x.Value as NodeAnalogInput)
                                                  .Where(x => x != null)
                                                  .Where(x => x.Code.ToString() == ioSignal.Name)
                                                  .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingAnalogInput != null)
                        {
                            newAnalogInput = newAnalogInput
                                             .SetSignal(existingAnalogInput.Signal);
                        }
                    }
                    analogInputsMutable.Add(newAnalogInput);
                    break;

                case DeviceConfigurationResponse.IOSignalType.AnalogOutput:
                    var newAnalogOutput = NodeAnalogOutput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingAnalogOutput = existingDeviceConfiguration.GetChildrenRecursive()
                                                   .Select(x => x.Value as NodeAnalogOutput)
                                                   .Where(x => x != null)
                                                   .Where(x => x.Code.ToString() == ioSignal.Name)
                                                   .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingAnalogOutput != null)
                        {
                            newAnalogOutput = newAnalogOutput
                                              .SetSignalIn(existingAnalogOutput.SignalIn);
                        }
                    }
                    analogOutputsMutable.Add(newAnalogOutput);
                    break;
                }
            }

            discreteInputsDevice = discreteInputsDevice.NodeDiscreteInputChildren.Append(
                new ReadOnlyCollection <NodeDiscreteInput>(discreteInputsMutable));
            discreteOutputsDevice = discreteOutputsDevice.NodeDiscreteOutputChildren.Append(
                new ReadOnlyCollection <NodeDiscreteOutput>(discreteOutputsMutable));
            analogInputsDevice = analogInputsDevice.NodeAnalogInputChildren.Append(
                new ReadOnlyCollection <NodeAnalogInput>(analogInputsMutable));
            analogOutputsDevice = analogOutputsDevice.NodeAnalogOutputChildren.Append(
                new ReadOnlyCollection <NodeAnalogOutput>(analogOutputsMutable));

            devicesMutable.Add(discreteInputsDevice);
            devicesMutable.Add(discreteOutputsDevice);
            devicesMutable.Add(analogInputsDevice);
            devicesMutable.Add(analogOutputsDevice);
            nodeDriver = nodeDriver.NodeDeviceChildren.Append(
                new ReadOnlyCollection <NodeDevice>(devicesMutable));

            driversMutable.Add(nodeDriver);
            var nodeDeviceConfiguration = NodeDeviceConfiguration.BuildWith(
                new ReadOnlyCollection <NodeDriver>(driversMutable));

            return(nodeDeviceConfiguration);
        }
Beispiel #4
0
 public void ReadSignalValues(IEnumerable <NodeSignal> signals)
 {
     if (Properties.Settings.Default.RunAsService)
     {
         var outPayload = new StringBuilder();
         foreach (var signal in signals)
         {
             outPayload.Append(signal.SignalId.ToString());
         }
         var response = sendReceiveTelegram <NodeTelegram>(m_TelegramReadSignals.SetPayload(FieldBase64.Encode(outPayload.ToString())));
         if (response != null)
         {
             foreach (var tpl in EncodedSignalValue.ParseEncodedSignals(response.Payload.Decode(), signals))
             {
                 var signal = tpl.Item1;
                 var value  = tpl.Item2;
                 m_runtimeService.Value.NotifyValueChanged(signal, value);
             }
         }
     }
     else
     {
         // uploading is instant
         var nRuntimeApplication = RuntimeApplicationUpload();
         if (nRuntimeApplication != null)
         {
             foreach (var sig in signals)
             {
                 var foundSig = nRuntimeApplication.FindSignal(sig.SignalId);
                 if (foundSig != null)
                 {
                     m_runtimeService.Value.NotifyValueChanged(foundSig, foundSig.Value);
                 }
             }
         }
     }
 }
Beispiel #5
0
        public Engine()
        {
            peer.OnReceiveDelta += new NodePeer.ReceiveDeltaHandler(
                delegate(NodePeer fromPeer, FieldGuid basedOnMessageID)
            {
                // This is responsible for returning the message
                // the delta is based on so that the communication
                // manager can reconstruct the entire message.
                // WATCH OUT - RUNS ON A SEPARATE THREAD
                if (basedOnMessageID == runtimeApplication.ID)
                {
                    return(runtimeApplication);
                }
                else
                {
                    return(lastMessage);
                }
            });

            peer.OnReceive += new NodePeer.ReceiveHandler(
                delegate(NodePeer fromPeer, NodeBase message)
            {
                // New message has arrived.
                // WATCH OUT - RUNS ON A SEPARATE THREAD
                lastMessage    = message;
                var runtimeApp = message as NodeRuntimeApplication;
                var telegram   = message as NodeTelegram;
                if (runtimeApp != null)
                {
                    // peer is sending a new version of the
                    // runtime application - store it and we'll
                    // "see" it on the next logic scan.
                    runtimeApplication = runtimeApp;
                }
                else if (telegram != null)
                {
                    try
                    {
                        var toPeer       = fromPeer;
                        var telegramType = (TelegramType)Enum.Parse(typeof(TelegramType), telegram.MessageType.ToString());
                        switch (telegramType)
                        {
                        case TelegramType.READCONFIGURATION:
                            // peer is requesting an I/O configuration
                            // so do a scan and send one back
                            peer.SendToPeer(toPeer, getDeviceConfiguration());
                            break;

                        case TelegramType.START:
                            Start();
                            peer.SendToPeer(toPeer, telegram.SetPayload(m_TrueResponse));
                            break;

                        case TelegramType.STOP:
                            Stop();
                            peer.SendToPeer(toPeer, telegram.SetPayload(m_TrueResponse));
                            break;

                        case TelegramType.RUNNING:
                            var response = m_FalseResponse;
                            if (run)
                            {
                                response = m_TrueResponse;
                            }
                            peer.SendToPeer(toPeer, telegram.SetPayload(response));
                            break;

                        case TelegramType.RUNTIMEID:
                            FieldBase64 runtimeIdResponse = m_EmptyResponse;
                            if (runtimeApplication != null)
                            {
                                runtimeIdResponse = FieldBase64.Encode(runtimeApplication.RuntimeId.ToString());
                            }
                            peer.SendToPeer(toPeer, telegram.SetPayload(runtimeIdResponse));
                            break;

                        case TelegramType.RUNTIMEVERSIONID:
                            FieldBase64 runtimeVersionIdResponse = m_EmptyResponse;
                            if (runtimeApplication != null)
                            {
                                runtimeVersionIdResponse = FieldBase64.Encode(runtimeApplication.ID.ToString());
                            }
                            peer.SendToPeer(toPeer, telegram.SetPayload(runtimeVersionIdResponse));
                            break;

                        case TelegramType.UPLOAD:
                            peer.SendToPeer(toPeer, runtimeApplication);
                            break;

                        case TelegramType.READSIGNALS:
                            peer.SendToPeer(toPeer, readSignals(telegram));
                            break;
                        }
                    }
                    catch (ArgumentException)
                    {
                        // means message type wasn't parsed to an enum - shouldn't happen, but whatever
                    }
                }
            });

            Load();
        }