public static NodeDevice BuildWith( FieldIdentifier Code, FieldGuid TypeId, FieldString Address, FieldBase64 Configuration, FieldDeviceName DeviceName) { //build fields Dictionary <FieldIdentifier, FieldBase> mutableFields = new Dictionary <FieldIdentifier, FieldBase>(); mutableFields.Add(new FieldIdentifier(m_CodeName), Code); mutableFields.Add(new FieldIdentifier(m_TypeIdName), TypeId); mutableFields.Add(new FieldIdentifier(m_AddressName), Address); mutableFields.Add(new FieldIdentifier(m_ConfigurationName), Configuration); mutableFields.Add(new FieldIdentifier(m_DeviceNameName), DeviceName); //Add Fields here: mutableFields.Add(new FieldIdentifier(m_CodeName), Code); //build children KeyedNodeCollection <NodeBase> mutableChildren = new KeyedNodeCollection <NodeBase>(); //Add Children here: mutableChildren.Add(SomeChild); //build node NodeDevice Builder = new NodeDevice( new ReadOnlyDictionary <FieldIdentifier, FieldBase>(mutableFields), new ReadOnlyCollection <NodeBase>(mutableChildren)); return(Builder); }
public NodeDevice SetConfiguration(FieldBase64 Configuration) { if (Configuration == null) { throw new ArgumentNullException(m_ConfigurationName); } return(new NodeDevice(this.SetField(new FieldIdentifier(m_ConfigurationName), Configuration), ChildCollection)); }
public static NodeRuntimeApplication BuildWith( FieldIdentifier Code, FieldGuid TypeId, FieldGuid RuntimeId, FieldString Address, FieldBase64 Configuration, FieldBool ExecuteOnStartup, NodePageCollection Logic, NodeDeviceConfiguration DeviceConfiguration) { var rta = NodeRuntimeApplication.BuildWith( Code, TypeId, RuntimeId, Address, Configuration, ExecuteOnStartup); rta = rta.SetLogic(Logic); return(rta.SetDeviceConfiguration(DeviceConfiguration)); }
public static NodeDevice StaticBuild() { FieldIdentifier code; FieldGuid typeId; FieldString address; FieldBase64 configuration; FieldDeviceName deviceName; code = new FieldIdentifier(CODE); typeId = new FieldGuid(TYPE_ID); address = new FieldString(string.Empty); configuration = new FieldBase64(string.Empty); deviceName = new FieldDeviceName(Resources.Strings.Unknown_Phidget_Name); NodeDevice device = NodeDevice.BuildWith(code, typeId, address, configuration, deviceName); return(device); }
private void kickOffBackgroundRequestIfNecessary(NodeDevice device) { if (device == null) { throw new ArgumentNullException("device"); } if (deviceShouldBeRunning(device) && !deviceState.IsRunning(device)) { // start it deviceState.SetIsRunning(device, true); string[] addressParts = device.Address.ToString().Split(new string[] { AbstractTwitterDevice.ADDRESS_SEPARATOR }, StringSplitOptions.None); string token = new FieldBase64(addressParts[0]).Decode(); string tokenSecret = new FieldBase64(addressParts[1]).Decode(); var twitter = FluentTwitter.CreateRequest() .AuthenticateWith(TwitterConsumer.ConsumerKey, TwitterConsumer.ConsumerSecret, token, tokenSecret) .Statuses().OnUserTimeline(); twitter.CallbackTo((sender, result, userstate) => { deviceState.SetUserStatusMonitor(device, result.AsStatuses()); // Implement some rate limiting long waitSeconds100Percent = 20; if (result.RateLimitStatus.RemainingHits > 0) { long secondsBeforeReset = (long)result.RateLimitStatus.ResetTime.Subtract(DateTime.Now).TotalSeconds; waitSeconds100Percent = secondsBeforeReset / result.RateLimitStatus.RemainingHits; } long waitSecondsMinimum = 20; if (result.RateLimitStatus.HourlyLimit > 0) { waitSecondsMinimum = 3600 / result.RateLimitStatus.HourlyLimit; } long waitSeconds = Math.Max((long)((1 / 50.Percent()) * waitSeconds100Percent), waitSecondsMinimum); // limits to a certain percentage, with a floor System.Threading.Thread.Sleep((int)(waitSeconds * 1000)); deviceState.SetIsRunning(device, false); }); twitter.BeginRequest(); } }
public static NodeRuntimeApplication BuildWith( FieldIdentifier Code, FieldGuid TypeId, FieldGuid RuntimeId, FieldString Address, FieldBase64 Configuration, FieldBool ExecuteOnStartup) { //build fields Dictionary <FieldIdentifier, FieldBase> mutableFields = new Dictionary <FieldIdentifier, FieldBase>(); mutableFields.Add(new FieldIdentifier(m_CodeName), Code); mutableFields.Add(new FieldIdentifier(m_TypeIdName), TypeId); mutableFields.Add(new FieldIdentifier(m_RuntimeIdName), RuntimeId); mutableFields.Add(new FieldIdentifier(m_AddressName), Address); mutableFields.Add(new FieldIdentifier(m_ConfigurationName), Configuration); mutableFields.Add(new FieldIdentifier(m_ExecuteOnStartupName), ExecuteOnStartup); mutableFields.Add(new FieldIdentifier(m_TryModeName), new FieldBool(false)); //Add Fields here: mutableFields.Add(new FieldIdentifier("Code"), Code); //build children KeyedNodeCollection <NodeBase> mutableChildren = new KeyedNodeCollection <NodeBase>(); var pc = NodePageCollection.BuildWith( new FieldPageCollectionName(m_LogicName) ); pc = pc.SetLogicRoot(new FieldBool(true)); mutableChildren.Add(pc); mutableChildren.Add( NodeDeviceConfiguration.BuildWith( new ReadOnlyCollection <NodeDriver>(new Collection <NodeDriver>()) )); //Add Children here: mutableChildren.Add(SomeChild); //build node NodeRuntimeApplication Builder = new NodeRuntimeApplication( new ReadOnlyDictionary <FieldIdentifier, FieldBase>(mutableFields), new ReadOnlyCollection <NodeBase>(mutableChildren)); return(Builder); }
/// <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); }
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); }
public static NodeDevice StaticBuildHelper(string deviceName, string typeId, Guid instanceId, string code, int buttons, int axes, int povhats) { FieldIdentifier c; FieldGuid typ; FieldString address; FieldBase64 configuration; FieldDeviceName dName; c = new FieldIdentifier(code); typ = new FieldGuid(typeId); address = new FieldString(instanceId.ToString()); configuration = new FieldBase64(string.Empty); dName = new FieldDeviceName(deviceName); NodeDevice device = NodeDevice.BuildWith(c, typ, address, configuration, dName); // Add the inputs var inputsMutable = new Collection <NodeDiscreteInput>(); for (int i = 0; i < buttons; i++) { int buttonNumber = i + 1; inputsMutable.Add(NodeDiscreteInput.BuildWith( new FieldIdentifier(Resources.Strings.Button + buttonNumber), new FieldString(i.ToString()), new FieldSignalName(Resources.Strings.Button + " " + buttonNumber))); } var inputs = new ReadOnlyCollection <NodeDiscreteInput>(inputsMutable); device = device.NodeDiscreteInputChildren.Append(inputs); var analogInputsMutable = new Collection <NodeAnalogInput>(); for (int i = 0; i < axes; i++) { if (i == 3) { break; // only supports up to 3 axes } int axisNumber = i + 1; string axisName = axisNumber == 1 ? "X" : axisNumber == 2 ? "Y" : axisNumber == 3 ? "Z" : null; analogInputsMutable.Add(NodeAnalogInput.BuildWith( new FieldIdentifier(axisName), new FieldString(axisName), new FieldSignalName(axisName))); string rotationName = "Rotation" + axisName; analogInputsMutable.Add(NodeAnalogInput.BuildWith( new FieldIdentifier(rotationName), new FieldString(rotationName), new FieldSignalName(rotationName))); } for (int i = 0; i < povhats; i++) { int povNumber = i + 1; analogInputsMutable.Add(NodeAnalogInput.BuildWith( new FieldIdentifier(Resources.Strings.PoVHat + povNumber.ToString()), new FieldString(i.ToString()), new FieldSignalName(Resources.Strings.PoVHat + " " + povNumber.ToString()))); } device = device.NodeAnalogInputChildren.Append(new ReadOnlyCollection <NodeAnalogInput>(analogInputsMutable)); return(device); }
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); } } } } }
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(); }
public static NodeDevice StaticBuildHelper(string deviceName, int serialNumber, string code, string typeId, int discreteInputs, int discreteOutputs, int analogInputs, int analogOutputs, int stringInputs, int stringOutputs, string analogOutputNameOverride, string discreteOutputNameOverride) { FieldIdentifier c; FieldGuid typ; FieldString address; FieldBase64 configuration; FieldDeviceName dName; c = new FieldIdentifier(code); typ = new FieldGuid(typeId); address = new FieldString(serialNumber.ToString()); configuration = new FieldBase64(string.Empty); dName = new FieldDeviceName(deviceName); NodeDevice device = NodeDevice.BuildWith(c, typ, address, configuration, dName); // Add the inputs var inputsMutable = new Collection <NodeDiscreteInput>(); for (int i = 0; i < discreteInputs; i++) { inputsMutable.Add(NodeDiscreteInput.BuildWith( new FieldIdentifier(Resources.Strings.Input + i), new FieldString(i.ToString()), new FieldSignalName(Resources.Strings.Input + " " + i))); } var inputs = new ReadOnlyCollection <NodeDiscreteInput>(inputsMutable); device = device.NodeDiscreteInputChildren.Append(inputs); var analogInputsMutable = new Collection <NodeAnalogInput>(); for (int i = 0; i < analogInputs; i++) { analogInputsMutable.Add(NodeAnalogInput.BuildWith( new FieldIdentifier(Resources.Strings.AnalogInput + i), new FieldString(i.ToString()), new FieldSignalName(Resources.Strings.AnalogInput + " " + i))); } device = device.NodeAnalogInputChildren.Append(new ReadOnlyCollection <NodeAnalogInput>(analogInputsMutable)); var stringInputsMutable = new Collection <NodeStringInput>(); for (int i = 0; i < stringInputs; i++) { stringInputsMutable.Add(NodeStringInput.BuildWith( new FieldIdentifier(Resources.Strings.StringInput + i), new FieldString(i.ToString()), new FieldSignalName(Resources.Strings.StringInput + " " + i))); } device = device.NodeStringInputChildren.Append(new ReadOnlyCollection <NodeStringInput>(stringInputsMutable)); // Add the outputs var outputsMutable = new Collection <NodeDiscreteOutput>(); for (int i = 0; i < discreteOutputs; i++) { outputsMutable.Add(NodeDiscreteOutput.BuildWith( new FieldIdentifier(Resources.Strings.Output + i), new FieldString(i.ToString()), new FieldSignalName(discreteOutputNameOverride + " " + i))); } var outputs = new ReadOnlyCollection <NodeDiscreteOutput>(outputsMutable); device = device.NodeDiscreteOutputChildren.Append(outputs); var analogOutputsMutable = new Collection <NodeAnalogOutput>(); for (int i = 0; i < analogOutputs; i++) { analogOutputsMutable.Add(NodeAnalogOutput.BuildWith( new FieldIdentifier(Resources.Strings.AnalogOutput + i), new FieldString(i.ToString()), new FieldSignalName(analogOutputNameOverride + " " + i))); } device = device.NodeAnalogOutputChildren.Append(new ReadOnlyCollection <NodeAnalogOutput>(analogOutputsMutable)); var stringOutputsMutable = new Collection <NodeStringOutput>(); for (int i = 0; i < stringOutputs; i++) { stringOutputsMutable.Add(NodeStringOutput.BuildWith( new FieldIdentifier(Resources.Strings.StringOutput + i), new FieldString(i.ToString()), new FieldSignalName(Resources.Strings.StringOutput + " " + i))); } device = device.NodeStringOutputChildren.Append(new ReadOnlyCollection <NodeStringOutput>(stringOutputsMutable)); return(device); }