Tutorial: Creating an RFID Device...

22
Contents Tutorial: Creating an RFID Device Provider............................3 In This Section......................................................3 See Also.............................................................3 Step 1: Create a Skeleton Provider by Using the RFID Provider Template 3 Procedures...........................................................4 Next Steps...........................................................4 Step 2: Start the RFID Provider.......................................5 Procedures...........................................................5 Next Steps...........................................................6 Step 3: Add an RFID Device and View Its Properties....................6 Procedures...........................................................6 Next Steps..........................................................10 Step 4: Create a Test Process to Test the RFID Provider..............10 Procedures..........................................................10 Next Steps..........................................................12 Step 5: Create a Vendor-Defined Command..............................13 Prerequisites.......................................................13 Overview of This Step...............................................13 Next Steps..........................................................20 Copyright............................................................ 21

Transcript of Tutorial: Creating an RFID Device...

Page 1: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

ContentsTutorial: Creating an RFID Device Provider....................................................................................3

In This Section............................................................................................................................. 3See Also...................................................................................................................................... 3

Step 1: Create a Skeleton Provider by Using the RFID Provider Template.....................................3Procedures.................................................................................................................................. 4Next Steps................................................................................................................................... 4

Step 2: Start the RFID Provider......................................................................................................5Procedures.................................................................................................................................. 5Next Steps................................................................................................................................... 6

Step 3: Add an RFID Device and View Its Properties.....................................................................6Procedures.................................................................................................................................. 6Next Steps................................................................................................................................. 10

Step 4: Create a Test Process to Test the RFID Provider.............................................................10Procedures................................................................................................................................ 10Next Steps................................................................................................................................. 12

Step 5: Create a Vendor-Defined Command................................................................................13Prerequisites............................................................................................................................. 13Overview of This Step...............................................................................................................13Next Steps................................................................................................................................. 20

Copyright...................................................................................................................................... 21

Page 2: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

Tutorial: Creating an RFID Device ProviderThis tutorial shows how to create a provider by using the Provider Template sample that you can download from the Microsoft® BizTalk® Server R2 Code Samples Web site (http://go.microsoft.com/fwlink/?LinkId=83460). The tutorial also shows how to use RFID Manager to test a provider at each step of development instead of implementing the entire provider before starting to test it.

Using the development and testing methods shown in this tutorial can make it easier to discover and correct problems in the providers that you create.

In This Section Step 1: Create a Skeleton Provider by Using the RFID Provider Template

Step 2: Start the RFID Provider

Step 3: Add an RFID Device and View Its Properties

Step 4: Create a Test Process to Test the RFID Provider

Step 5: Create a Vendor-Defined Command

See Also

Step 1: Create a Skeleton Provider by Using the RFID Provider TemplateProvider Template is a Microsoft Visual C#® sample that ships with a Microsoft Visual Studio® template that can be used to write Device Service Provider Interface (DSPI) device providers. You can download the sample from the BizTalk Server R2 Code Samples Web site (http://go.microsoft.com/fwlink/?LinkId=83460)

To write a DSPI device provider by using the provider template, you need to create a new Visual Studio project using the template and implement the methods in the classes that derive from the T:Microsoft.SensorServices.Rfid.Dspi.DeviceProvider and T:Microsoft.SensorServices.Rfid.Dspi.PhysicalDeviceProxy classes.

In this walkthrough, you will create a Visual Studio project using the provider template, but you will not add any code. You will add code to implement a few methods in the T:Microsoft.SensorServices.Rfid.Dspi.DeviceProvider derived class in Step 2: Start the RFID Provider. In Step 3: Add an RFID Device and View Its Properties you will add code to implement a few methods in the T:Microsoft.SensorServices.Rfid.Dspi.PhysicalDeviceProxy derived class.

3

Page 3: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

ProceduresTo create a skeleton provider by using the provider template

1. Copy the DSPI Provider Template.zip file that ships with the Provider Template sample to the Visual Studio templates folder.

The default location of the Visual Studio templates folder is C:\Documents and Settings\<User Name>\My Documents\Visual Studio <version number>\Templates\ProjectTemplates\Visual C#. You need to place the .zip file in the Visual Studio templates folder exactly as it is (that is, without decompressing it). Visual Studio expects the templates to be in the .zip format.

2. Click Start, point to All Programs, point to Visual Studio, and then click Visual Studio.

3. On the menu bar, click File, point to New, and then click Project. 4. Click Visual C# in the Project types list.

5. Click DSPI Provider Template in the My Templates in the Templates list.

6. Type MyProvider for Name, and then click OK.

7. In Solution Explorer, right-click MyProvider, and then click Build.

The project template is set up so that after the build, the provider DLL is automatically copied to the provider folder and the provider is registered with the server. Thus simply building the empty project by using the supplied template results in your provider being registered with BizTalk RFID. See the pre-build and post-build commands for the project to understand how this works.

To verify that the MyProvider provider is successfully registered

1. Click Start, point to All Programs, click Microsoft BizTalk RFID, and then click RFID Manager. If you had RFID Manager open before building the Visual Studio project, press F5 to refresh the list of providers.

2. Expand RFID Services Administration, expand <Machine Name>, and then click Device Providers.

3. Verify that you see MyProvider in the list of Device Providers, and that the status of the provider is Registered.

Next StepsNow that you have completed step 1, perform Step 2: Start the RFID Provider, which gives you step-by-step instructions for adding code to the MyDeviceProvider class that derives from the T:Microsoft.SensorServices.Rfid.Dspi.DeviceProvider class.

4

Page 4: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

Step 2: Start the RFID ProviderIn the previous walkthrough, you created a Visual Studio project using the provider template. In this walkthrough you will add code to implement the Init method of the MyDeviceProvider class. RFID Manager invokes the Init method when you start the MyProvider provider. Note that the GetProviderMetadata method contains the default implementation, and you may want to change the implementation based on your requirements.

ProceduresTo test the MyProvider provider before implementing the Init method

1. In RFID Manager, in the list of Device Providers, right-click TestProvider, and then click Start. You should see a message box that says "Provider MyProvider encountered error. The method or operation is not implemented. Resolve the issue that is causing the error, and then retry the operation". You get this error message because the MyDeviceProvider class does not implement the Init method.

2. Click OK in the message box

To implement the Init method of the MyDeviceProvider class

1. In Visual Studio, in Solution Explorer, double-click MyDeviceProvider.cs.

2. Add the following statement at the beginning of the file after the existing using statements:

using Microsoft.SensorServices.Rfid.Utilities;

3. Add a private member variable logger of type ILogger to the MyDeviceProvider class:

private ILogger logger;

4. Expand the region titled provider lifecycle and factory method in the source editor.

5. Delete the throw statement in the Init method.

6. Add the following code to the Init method:

//use the logger object passed as a parameter to log information.

this.logger = providerContext.Logger;

logger.Info("In the Init function of the provider");

Note The default location for the provider log file is %RFIDDATADIR%\Providers\MyProvider, and the name of the log file is MyProvider.Log.

7. Delete the throw statement from the Shutdown method.

8. Expand the region titled metadata support.

5

Page 5: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

9. Review the code in the GetProviderMetadata method.

10. Update the code in the GetProviderMetadata method with information relevant to your organization. This step is optional.

11. Right-click MyProvider, and then click Build.

To test the MyProvider provider after implementing the Init method

1. In RFID Manager, click DeviceProviders in the left pane if any other node is selected.

2. Press F5 to refresh the view.

3. Verify that the value of the status field is set to Started - OK for MyProvider.

Note The post-build command of the project starts the provider automatically when you build the project. It failed the previous time because the Init method was not implemented.

4. Navigate to the %RFIDDATADIR%\Providers\MyProvider directory, and open the MyProvider.log file in Notepad.

5. Verify that you see the message that the Init method logs in the log file.

Next StepsNow that you have completed this walkthrough, perform Step 3: Add an RFID Device and View Its Properties, which gives you step-by-step instructions for adding code to implement a few methods of the MyPhysicalDeviceProxy class that derives from the T:Microsoft.SensorServices.Rfid.Dspi.PhysicalDeviceProxy class.

Step 3: Add an RFID Device and View Its PropertiesIn the previous walkthrough, you added code for the Init method of the MyDeviceProvider class. In this walkthrough, you will add code to implement a few methods in the MyPhysicalDeviceProxy class. These methods are called when you add a device and view properties of the device that uses the MyProvider provider.

ProceduresTo add a device that uses the MyProvider provider

1. In RFID Manager, right-click Devices, and then click New Device.

2. On the Introduction page, click Next.

6

Page 6: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

3. On the Provider page, select MyProvider from the Available Providers, and then click Next.

4. On the Connection page, verify that you see only TCP in the drop-down list of protocols.

You will see only TCP in the drop-down list of protocols because TCP is the only protocol supported by the MyProvider provider. See the GetProviderMetadata implementation of the MyDeviceProvider class to confirm this.

5. On the Connection page, type MyDevice for the name.

6. Type 7777 for the port, and then click Next. 7. On the Add Devices to a Group page, click Next. 8. On the Authentication page, click Next. 9. On the Unsuccessful page, verify that the connection to the device failed. The

connection to the device failed because the MyPhysicalDeviceProxy class does not implement the methods that RFID Manager invokes.

10. Type MyDevice for Name, and then click Next. 11. On the Completion page, click Finish.

To view properties of the MyDevice device

1. In RFID Manager, right-click MyDevice, and then click Properties.

You should see a message box that says "A connection could not be established to the device. To view the last known properties of the device or to modify the connection information, click OK". Viewing the properties of the device failed because the MyPhysicalDeviceProxy class does not implement the methods that RFID Manager invokes.

2. Click OK in the message box.

3. Click Cancel in the MyDevice Device Properties dialog box. Now you will implement the methods required to establish a connection to MyDevice.

To implement methods of the PhysicalDeviceProxy class

1. In Visual Studio, in Solution Explorer, double-click MyDeviceProvider.cs if it is not already open in the source editor.

2. Expand the region titled provider lifecycle and factory methods if it is not already expanded.

3. Verify that the GetDevice method creates and returns an object of the MyProviderDeviceProxy class.

4. In Solution Explorer, double-click MyPhysicalDeviceProxy.cs.

5. Expand #region internal implementation.

6. Add a private member variable named connectionInformation of type ConnectionInformation as shown in the following statement:

7

Page 7: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

private ConnectionInformation connectionInformation;

7. Remove the existing throw statement from the constructor.

8. Add the following statement to the MyPhysicalDeviceProxy constructor:

this.connectionInformation = connectionInformation;

9. Expand the region titled connection management. 10. Remove throw statements from all the methods in this region (SetupConnection,

IsConnectionAlive, and Close).

11. Add the following statement to the IsConnectionAlive method:

return true;

Note You can add code to the SetupConnection method to set up a connection to your device, to the Close method to close the connection to your device, and to the IsConnectionAlive method to ping the device to see if the connection is still alive.

12. Expand the region titled device metadata.

13. Delete the throw statement from the Get method of the DeviceInformation property.

14. Add the following code to the Get method of the DeviceInformation property:

//use the connectionInformation that was saved off in the constructor

DeviceInformation info = new DeviceInformation("1",

this.connectionInformation, null);

return info;

15. In Solution Explorer, right-click MyProvider, and then click Build.

16. In RFID Manager, right-click MyDevice, and then click Properties.

You should see a message box that says "The properties of the device could not be retrieved”.

Note The methods that you have implemented so far allow a connection to the device to succeed. Now you will implement the methods to allow clients to get information about properties.

17. Delete the throw statement from the GetPropertyMetadata method.

18. Add the following code to the GetPropertyMetadata method:

//todo: Return metadata for the properties supported by your device instead

//of returning an empty dictionary

Dictionary<PropertyKey, RfidDevicePropertyMetadata> result = new

Dictionary<PropertyKey, RfidDevicePropertyMetadata>();

return result;

8

Page 8: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

19. Expand the region titled commands and notifications.

20. Add the following code to the SendMessage method that takes two parameters (sourceName and command):

ResponseEventArgs response = new ResponseEventArgs(command);

switch (command.GetType().Name)

{

case "GetCurrentPropertyProfileCommand":

{

GetCurrentPropertyProfileCommand cmd = command as

GetCurrentPropertyProfileCommand;

PropertyProfile pp;

if (sourceName == null)

{

//todo: get the actual properties from the device

pp = new PropertyProfile();

}

else

{

//todo: get the actual properties from the source specified

pp = new PropertyProfile();

}

cmd.Response = new GetCurrentPropertyProfileResponse(pp);

break;

}

default:

{

response = new ResponseEventArgs(command, new

CommandError(ErrorCode.FunctionUnsupported, "not supported", "notsup"));

break;

}

}

CmdResponseEvent(this, response);

21. Add the following code to the SendMessage method that takes only one parameter:

SendMessage(null, command);

9

Page 9: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

22. Expand the region titled device metadata.

23. Delete the throw statement from the GetSources method.

24. Add the following code to the GetSources method:

//todo: return the actual sources / antennas present in the device

Dictionary<string, PropertyProfile> result = new Dictionary<string,

PropertyProfile>();

return result;

25. In Solution Explorer, right-click MyProvider, and then click Build.

26. In RFID Manager, right-click MyDevice, and then click Properties. You should be able to see the MyDevice Device Properties dialog box without any error messages.

Next StepsNow that you have completed this walkthrough, perform Step 4: Create a Test Process to Test the RFID Provider, which gives you step-by-step instructions for testing the MyProvider provider by using an RFID process.

Step 4: Create a Test Process to Test the RFID ProviderIn this walkthrough, you will add code to handle the TagReadEvent event, create a test RFID process, and use RFID Manager to send tags to the process.

ProceduresTo add code to support handling the TagReadEvent event

1. In Visual Studio, in Solution Explorer, double-click MyPhysicalDeviceProxy.cs if the file is not already open in the source editor.

2. Add the following statement at the beginning of the file after the existing using statements:

using Microsoft.SensorServices.Rfid;

3. Add the following code at the end of the file:

#region handle events

void handleTagReadEvent(object sender, byte[] data, string source, DateTime

timestamp)

{

10

Page 10: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

TagReadEvent ev = new TagReadEvent(data,

TagType.EpcClass1Gen2,

null, source, timestamp,

null, TagDataSelector.All);

Notification n = new Notification(ev);

NotificationEventArgs args = new NotificationEventArgs(n);

if (DeviceNotificationEvent != null)

{

DeviceNotificationEvent(this, args);

}

}

#endregion //handle events

Note This method is invoked when the provider receives a tag-read event asynchronously.

4. To test the HandleTagReadEvent method and to see tags in the RFID Manager before actually hooking up your provider to get asynchronous tags from the physical device, you can temporarily add the following code to the SetupConnection method MyPhysicalDeviceProxy class to simulate some asynchronous tag-read events. Double-click MyphysicalDeviceProxy.cs in Solution Explorer, and copy the following code to the SetupConnection method.

//todo: Remove this temporary code after hooking up async tag

//notifications from the device to the provider.

WaitCallback cb =

delegate

{

//simulate some async tag notifications.

//Give the connection some time to get established

//before sending these mock tags over to the server.

Thread.Sleep(2000);

for (int i = 0; i < 5; i++)

{

handleTagReadEvent(null, new byte[12], "some location",

DateTime.Now);

11

Page 11: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

}

};

ThreadPool.QueueUserWorkItem(cb);

5. Add the following statement at the beginning of the file after the existing using statements:

using System.Threading;

6. Right-click MyProvider, and then click Build.

To create an RFID process

1. In RFID Manager, right-click Processes, and then click New Process.

2. Type TestProcess for the Process Name. The Bind Wizard should open.

3. On the Introduction page, click Next. 4. On the Logical Devices page, click New. You should see the Logical Device Name

window.

5. Type mylogicaldevice for the Name, and then click OK.

6. Select the check box next to mylogicaldevice in the list of Logical Devices.

7. On the Logical Devices page, click Next. 8. On Configure logical device mylogicaldevice page, select the check box next to

MyDevice in the list of Devices and Groups, and then click Next. 9. On the Components page, click New Component. 10. Select SqlServerSink from the List of available Components, and then click Add.

11. Type MySqlSink for the Instance Name, and then click OK.

12. In the Add Component dialog box, click Close.

13. On the Components page, click Next. 14. Select the option Start the process when I click Finish.

15. Click Finish.

16. Verify that the status of the process is Started.

17. Right-click TestProcess, and then click View Tags to see the tags received by the TestProcess from the MyProvider provider.

Next StepsYou now have a simple provider. You can expand it by adding support for management events, firmware compatibility checks and upgrades, provider and device properties, and commands based on the functionality of your devices. For example, you may wish to add a vendor-defined command to your provider, which you can perform in Step 5: Create a Vendor-Defined Command.

12

Page 12: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

Step 5: Create a Vendor-Defined CommandBizTalk RFID enables vendors to extend the RFID interface to incorporate their own hardware or software features. Such extensions can include vendor-defined commands, responses, transport settings, general event messages, or printer functionality. This walkthrough shows how to add a vendor-defined command to a provider, and how to create a simple application that uses the command to access the provider.

After you have completed this tutorial, you should be able to do the following:

Describe a command to a provider by using the BizTalk RFID VendorDefined interfaces.This command takes three parameters:

fictionalPort - A string containing the number of the fictional port whose state change causes an action to occur. Valid values are "1", "2", "3", or "4".

newValue - An integer containing the value that gpIOPort is to change to: either 1 or 0.

Action - The command to execute when gpIOPort changes. Permissible values are "turnOffThing", "turnOnThing", and "setDenseMode".

Implement the command in the provider. The provider has a hook in SendMessage for all vendor-defined commands, and implements the response to the vendor-defined commands in the private VendorCommandsImplementation method.

Call the command from a client application. The client application opens a connection to the device, sends the vendor-defined command, and closes the connection.

PrerequisitesYou must complete Step 4: Create a Test Process to Test the RFID Provider before performing this step.

Overview of This StepYou perform the following procedures in this step.

Procedure Description

To create a vendor-defined command This procedure has instructions for how to create a vendor-defined command called SetProgramOnDevice.

To add a vendor-defined command hook to SendMessage

This procedure has instructions for implementing the command in the device provider. To do so, you must add a hook that catches a vendor-defined command in the SendMessage method of the physical device

13

Page 13: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

Procedure Description

proxy.

To implement a response to the vendor-defined command

This procedure has instructions for implementing the specific details of the response to the vendor-defined command in the physical device proxy.

To create a client test application This procedure has instructions for creating a test application to confirm your additions.

To create a vendor-defined command

1. In Visual Studio, in Solution Explorer, double-click MyDeviceProvider.cs.

2. Add the following code to the beginning of the file:

using Microsoft.SensorServices.Rfid;

using System.Text;

You need to add this code because the vendor extensions exist in the SensorServices.Rfid namespace, and the sample uses several lines of code that perform string manipulation.

3. In your GetProviderMetadata implementation, modify the call to ProviderMetadata as follows:

ProviderMetadata metadata = new ProviderMetadata(information, capabilities,

providerMetadata, GetProviderPropertyMetadata(), null);

Later in this tutorial, you will define the GetProviderPropertyMetadata method to return the relevant information about your vendor-defined command.

4. Add a new region at the end of the file:

#region vendor-defined metadata

#endregion

The vendor-defined metadata is large enough to deserve its own region.

5. In the new region, create a new method called GetProviderPropertyMetadata that returns a VendorEntityMetadata dictionary:

private static Dictionary<VendorEntityKey, VendorEntityMetadata>

GetProviderPropertyMetadata()

{

//ToDo1: Create metadata dictionary and key

//ToDo1: Create parameter definitions.

//ToDo3: Put parameters in dictionary and return.

14

Page 14: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

}

Note All of the remaining steps in this procedure deal with implementing GetProviderPropertyMetadata.

6. Create a VendorEntityMetadata dictionary to hold all the descriptions of the vendor extensions:

Dictionary<VendorEntityKey, VendorEntityMetadata> vendorExtensions = new

Dictionary<VendorEntityKey, VendorEntityMetadata>();

This is the object you will return; in this example, it will contain a single VendorEntityParameter dictionary, which in turn will contain all of the parameters for the vendor-defined command.

7. Create the key to use in the VendorEntityMetadata dictionary to identify your vendor-defined command:

VendorEntityKey commandKey = new

VendorEntityKey(typeof(VendorDefinedCommand), "SetProgramOnDevice",

EntityType.Command);

8. Create a VendorEntityParameterMetadata dictionary to hold the parameter information for the command:

Dictionary<string, VendorEntityParameterMetadata>

parametersForSetProgramOnDevice = new Dictionary<string,

VendorEntityParameterMetadata>();

9. Define the possible values for the first parameter, and add the parameter to the VendorEntityParameterMetadata dictionary:

Collection<object> availableFictionalPorts = new

Collection<object>();

availableFictionalPorts.Add("1");

availableFictionalPorts.Add("2");

availableFictionalPorts.Add("3");

availableFictionalPorts.Add("4");

parametersForSetProgramOnDevice.Add("FictionalPort", new

VendorEntityParameterMetadata(

typeof(string),

"the Fictional port,

for whose state change event, some action should be taken",

null,

true,

15

Page 15: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

availableFictionalPorts

));

10. Define the second parameter and add it to the dictionary:

parametersForSetProgramOnDevice.Add("newValue", new

VendorEntityParameterMetadata(

typeof(int),

"The value the port

should change to, for executing the action",

null,

true,

0,

1

));

11. Define the third parameter and add it to the dictionary:

Collection<object> availableActions = new Collection<object>();

availableActions.Add("turnOffThing");

availableActions.Add("turnOnThing");

availableActions.Add("setModeOnThing");

parametersForSetProgramOnDevice.Add("action", new

VendorEntityParameterMetadata(

typeof(string),

"the action to

execute",

null,

true,

availableActions

));

12. Create a VendorEntityMetadata object to hold the VendorEntityParameterMetadata object:

VendorEntityMetadata commandMetadata = new VendorEntityMetadata(

"Sets programs on the device",

parametersForSetProgramOnDevice);

13. Combine the VendorEntityMetadata object with the command key in the

16

Page 16: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

vendorExtensions dictionary, and return vendorExtensions:

vendorExtensions[commandKey] = commandMetadata;

return vendorExtensions;

14. Compile and run your code.

To add a vendor-defined command hook to SendMessage

1. In Visual Studio, in Solution Explorer, double-click MyPhysicalDeviceProxy.cs.

2. Expand the commands and notifications region, locate the SendMessage method implementation, and add the following case statement beneath the "GetCurrentPropertyProfileCommand" case statement:

case "VendorDefinedCommand":

{

VendorDefinedCommand cmd = command as VendorDefinedCommand;

cmd.Response = new VendorDefinedResponse(cmd.Name, null);

object errorCode = VendorCommandsImplementation(sourceName,

cmd.Name,

cmd.VendorDefinedParameters,

cmd.Response);

if (null != errorCode)

{

response = new ResponseEventArgs(command, new

CommandError((ErrorCode)errorCode,

string.Format("The command {0} failed to execute.", cmd.Name),

"Custom Command Failed"));

}

break;

This implementation catches all vendor-defined commands and redirects them to the private VendorCommandsImplementation method. That method, defined in the next procedure, returns either null or an error code. If it returns an error code, then an error has occurred and the error is bubbled up. Otherwise, null indicates that the message was successfully retrieved.

To implement a response to the vendor-defined command

1. In Solution Explorer, add a reference to System.Windows.Forms.

2. In MyPhysicalDeviceProxy.cs, add the following code at the beginning of the file:

17

Page 17: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

using System.Windows.Forms;

The default implementation of the vendor-defined command displays a message box. Therefore, you need to add the Windows.Forms namespace so that you can access the message box.

3. Beneath the SendMessage implementation, add the following code:

private object VendorCommandsImplementation(string sourceName, string

commandName, VendorSpecificInformation parameters, VendorDefinedResponse

response)

{

switch (commandName)

{

case "SetProgramOnDevice":

{

string DisplayString = commandName + " received: " +

parameters["FictionalPort"] + " " + parameters["newValue"] + " " +

parameters["action"];

MessageBox.Show(DisplayString,

"Test Alert",

MessageBoxButtons.OK,

MessageBoxIcon.Exclamation,

MessageBoxDefaultButton.Button1,

MessageBoxOptions.ServiceNotification);

return null;

}

default:

{

break;

}

}

return null;

}

This code creates a message box to let you know that this part of the application has been reached, and displays the command values passed into the method.

18

Page 18: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

To create a client test application

1. In Visual Studio, create a new console C# solution named VendorExtensionsClient.This is the file that will contain the client test application.

2. In Solution Explorer, add references to the Microsoft.Rfid.SpiSdk.dll and Microsoft.Rfid.Client.dll assemblies.

The default location for these assemblies is <installdir>\Program Files\Microsoft BizTalk RFID\bin.

3. In the Program.cs file of your new solution, add the following code:

using System;

using System.Collections.Generic;

using System.IO.SensorServices.Rfid.Client;

using System.Text;

namespace VendorExtensionApp

{

class VendorExtensionsClient

{

static void Main(string[] args)

{

//ToDo1: Create vendor-defined parameters.

//ToDo2: Call the vendor-defined command.

}

}

}

This code lays out the general structure of the test application: a single namespace, one class, and the Main method. The only thing of note is the addition of the Rfid.Client namespace at the beginning, which enables you to make calls to the RFID service.

4. Build the input parameters for the call by replacing the ToDo1 line above with the following code:

VendorSpecificInformation inputParameters = new

VendorSpecificInformation();

inputParameters.Add("FictionalPort", "2");

inputParameters.Add("newValue", 1);

inputParameters.Add("action", "TurnOnThing");

19

Page 19: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

VendorDefinedParameters vendorParameters = new

VendorDefinedParameters();

vendorParameters.InputParameters = inputParameters;

This code creates a VendorSpecificInformation object that contains three parameters and their associated values. This object is stored inside a VendorDefinedParameter object, so that it can be passed to the provider through a call to a vendor-defined command.

5. Open a connection to the provider and call the vendor-defined command by replacing the ToDo2 line with the following code:

using (DeviceConnection dc = new DeviceConnection("MyDevice"))

{

dc.Open();

dc.ExecuteVendorDefinedCommand(

null,

null,

"SetProgramOnDevice",

"SetProgramOnDevice",

null,

vendorParameters

);

dc.Close();

}

This code creates a connection to MyDevice and calls ExecuteVendorDefinedCommand, using the vendor parameters defined in the previous statement. Note that the instruction and command parameters for ExecuteVendorDefinedCommand are identical. This is because the vendor-defined command does not need any additional information passed through the command parameter; however, the command cannot be null. Therefore, you can simply use the same string as the instruction, so that the code will validate.

6. Build and debug the VendorExtensionsClient sample.

You should see a pop-up message box informing you that your vendor-defined command has been received by the provider.

Next StepsYou have now added a vendor-defined command to your provider. However, the response to the command is rather simple. You may wish to improve your provider by creating a vendor-defined response.

20

Page 20: Tutorial: Creating an RFID Device Providerdownload.microsoft.com/download/C/2/0/C20A979A-FA6C-405B... · Web viewThis tutorial shows how to create a provider by using the Provider

CopyrightInformation in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted in examples herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

© 2009 Microsoft Corporation. All rights reserved.

Microsoft, BizTalk, Visual C#, and Visual Studio are trademarks of the Microsoft group of companies.

All other trademarks are property of their respective owners.

21