Skip navigation
All Places > PI Developers Club > Blog > Author: mhalhead

PI Developers Club

2 Posts authored by: mhalhead Champion

LightSwitch with PI

Posted by mhalhead Champion Jul 4, 2013

Microsoft LightSwitch is a Visual Studio add in for rapid application development. If you want to know more google it, but here's a good starting point. IMNHO LS is a really good tool for build quick CRUD style applications (forms over data), you probably could build a nice functional dashboard in LS but this doesn't mean that it is a good idea. The hard core developers reading this are shuddering, but there are times when you need a method of developing a solution without months of plumbing. I've also found that LS solutions are more maintainable as all the plumbing code isn't there. For me the biggest difference between LS and other RAD platforms is that LS is pure .NET. There is no weird and Wonderful runtime engine (other than the CLR) all there is, is a .NET framework that incorporate the best patterns and practises. Because it is pure .NET you can do pretty much anything in LS that you could do in a normal .NET app, yes you might fight the framework but you can do it; again this doesn't mean it is a good idea.


Microsoft's marketing department really hasn't done LS any favours; I think that its confused them. You can use LS for the following:

  • Create a quick well formed CRUD application
  • Create a OData service - you can publish a service only layer with LS providing an OData service that has minor things like really authentication and permissions
  • SharePoint 2013 development - LS is being pushed (badly) as a preferred method of writing SharePoint 2013 applications. We're still on 2010 so I have tried this
  • Mobile clients - the default UI is built in Silverlight but you can also create a companion UI in HTML5. Not you can't create a Win8 RT app directly. You could use the data service layer to create the server side and use the normal VS templates to create the client app.

So where does PI fit in? Well there are numerous examples floating around vCampus of data entry into PI. The example I'm going to use is Event Frames. Yes PI can generate EFs no problem (Abacus will make this work a lot better - Sorry Steve couldn't resist), but there is no easy way out of the box to let users enter additional information into the EFs. I don't know about everyone else but I'm certainly not going to give my users PSE with sufficient rights to edit events.


There are a number of mechanism for connecting to data in LS:

  • Database - Effectively the entity frame providers. I must confess I never tried anything other the SQL Server
  • SharePoint
  • OData
  • WCF RIA Service

Seeing that I'm trying to talk to AF my options are limited. Currently OSIsoft doesn't have OData support and even if they did LS doesn't support OData operation (please add some votes to the feature request on Microsoft's connect site). So WCF RIA Services is it.


The first thing to bear in mind is that LS uses an entity model for data. This means that you will need to project your EFTemplate as an entity; you do this with a plain old C# Object (POCO); don't worry I will show an example.


The first step is to create a new LS project and then add a Class Library Project to the solution; hint user a plain old class library not a WCF Service Library.


Accessing AF via Matlab

Posted by mhalhead Champion May 18, 2013

 My first blog post; finally, only took me 2 months to write this!


Numerous method of accessing PI data from Matlab have been published so a thought I throw another one into the mix. The issue with most of these methods is that support for AF is limited, particularly if you want to write back. The good news is that it turns out that Matlab is very capable of calling the AF SDK (or any .NET assembly for that matter). This means that you can use the very rich AF SDK to interact with AF from Matlab. AF 2.5 also gives you access to all the PI data. So I thought that I would show the basics, the code is not "production" ready seeing that I have not included minor things like exception handling.


Why would you want to do this:


Matlab is an excellent tool for number crunching. Building model and generally manipulating large data sets is really easy in Matlab.  I'm guessing you all know what the PI Systems is (otherwise you probably wouldn't be here). Using the AF SDK has numerous advantageous over other method:

  • I've found the performance to be superior to OLE DB
  • No limitation: you can read/write/... Anything that the AF SDK supports is available to you.

Useful link:

The real stuff

The first step is to create an instance of the AF SDK and import the relevant namespaces (note the .* at the end of the namespaces). Yes you do need the AF SDK on the Matlab computer.  

afSDK = NET.addAssembly('OSIsoft.AFSDK');
import OSIsoft.AF.*
import OSIsoft.AF.Asset.*

 Now connect to an AF server and database:

piSystems = OSIsoft.AF.PISystems;
PIsystem = piSystems.Item('MyAFServer');
AFdatabase = PIsystem.Databases.Item('MyAFDatabase');



 That is it. You now have a fully functional connection to AF via the SDK within Matlab. This means that you can do anything with this connection that the AF SDK allows.


Couple of examples working with the AF:

Find an Element: 

afElements = AFElement.FindElements(AFdatabase, [], 'Amplats', AFSearchField.Name, true, AFSortField.Name,  AFSortOrder.Ascending,intmax('int32'));

You hopefully noticed the one trick, nulls are passed as []. Otherwise it is pretty much the same as working with the AF SDK in C#. Below is a simply example of access the returned elements collection



Get some values: 

powerTransmitters = AFElement.FindElements(AFdatabase, [], '138JI006', AFSearchField.Name, true, AFSortField.Name,  AFSortOrder.Ascending,intmax('int32'));
attrib = powerTransmitters.Item(0).Attributes;
PV = attrib.Item('PV');

myTime = OSIsoft.AF.Time.AFTime.Now;
myValues = PV.Data.RecordedValue(myTime, Data.AFRetrievalMode.AtOrBefore, PV.DefaultUOM);



The more astute reader will have noticed that I went and got another element; I need one that had actual values. You will also notice that I constructed an AFTime object; you must pass a time object to the RecordValue method and a Matlab type doesn't cut it.


I think I've demonstrate just how easy it is to work with the AF SDK directly within Matlab. It very powerful option for access AF (and now PI data thanks to RDA) from Matlab.


All the code was tested on Matlab 2013a. You will need to check the MathWorks documentation if you are using an older version.


So that all folks





Filter Blog

By date: By tag: