Eugene Lee

Search for PI Point by Point ID with PI Web API

Discussion created by Eugene Lee Employee on May 15, 2019

This block of code is for this blog.

 

It has nicer indents than the code published directly in the blog.

 

Disclaimer:

This code could contain bugs and shouldn’t be used in production without extensive testing.

You agree that if you use any of the provided code in your own production code that you accept all ownership, risks, liabilities, and responsibilities associated with the performance, support, and maintenance of the code.

function NewIDOnlyWebID(dataType, guid, oid, ownerType, ownerguid) {

    //get the marker for the datatype
    var marker = getOwnerMarker(dataType, ownerType)

    //encode the server id to a base64 string
    var serverwebid = encodeguid(guid)

    //encode the owner id if datatype has a owner
    var typeswithowner = ["AFAnalysisRule", "AFAttribute", 'AFAttributeTemplate', "AFEnumerationValue", "AFTimeRule"];
    if (typeswithowner.includes(dataType)) {
        if (ownerguid) {
            serverwebid += encodeguid(ownerguid)
        }
        else {
            throw 'please provide a valid owner guid'
        }
    }

    //return webid if datatype is a server
    if (dataType == "PIServer" || dataType == "PISystem") {
        return 'I1' + marker + (serverwebid).replace(/=/g, '').replace(/\//g, '_').replace(/\+/g, '-')
    }

    //return webid if datatype is a pi point
    if (dataType == "PIPoint") {
        if (!oid) throw 'provide a valid PI Point ID'
        var arr = new Uint8Array(new Uint32Array([oid]).buffer);
        var pointwebid = btoa(String.fromCharCode.apply(null, arr))
        return 'I1' + marker + (serverwebid + pointwebid).replace(/=/g, '').replace(/\//g, '_').replace(/\+/g, '-')
    }

    //return webid for af objects
    var afwebid = encodeguid(oid)
    return 'I1' + marker + (serverwebid + afwebid).replace(/=/g, '').replace(/\//g, '_').replace(/\+/g, '-')
}

function encodeguid(guid) {

    var s = guid.replace(/[^0-9a-f]/ig, '').toLowerCase();

    //check for invalid guid
    if (s.length != 32) throw 'invalid guid';

    //arrange bytes as PI Web API uses Microsoft style GUID
    s = s.slice(6, 8) + s.slice(4, 6) + s.slice(2, 4) + s.slice(0, 2) +
        s.slice(10, 12) + s.slice(8, 10) +
        s.slice(14, 16) + s.slice(12, 14) +
        s.slice(16);

    //base64 encode the byte array
    var t = '';
    for (var n = 0; n < s.length; n += 2) {
        t += String.fromCharCode(parseInt(s.substr(n, 2), 16));
    }
    return btoa(t)
}

function getOwnerMarker(dataType, ownerType) {
    var marker
    switch (dataType) {
        case 'AFAnalysis':
            marker = 'XS'
            break;
        case 'AFAnalysisCategory':
            marker = 'XC'
            break;
        case 'AFAnalysisTemplate':
            marker = 'XT'
            break;
        case 'AFAnalysisRule':
            marker = 'XR'
            switch (ownerType) {
                case 'AFAnalysis':
                    marker += "X"
                    break
                case 'AFAnalysisTemplate':
                    marker += "T"
                    break
                default:
                    throw "please provide owner type"
            }
            break;
        case 'AFAnalysisRulePlugin':
            marker = 'XP'
            break;
        case 'AFAttribute':
            marker = 'Ab'
            switch (ownerType) {
                case 'AFElement':
                    marker += "E"
                    break
                case 'AFEventFrame':
                    marker += "F"
                    break
                case 'AFNotification':
                    marker += "N"
                    break
                default:
                    throw "please provide owner type"
            }
            break;
        case 'AFAttributeCategory':
            marker = 'AC'
            break;
        case 'AFAttributeTemplate':
            marker = 'ATE'
            break;
        case 'AFDatabase':
            marker = 'RD'
            break;
        case 'AFElement':
            marker = 'Em'
            break;
        case 'AFElementCategory':
            marker = 'EC'
            break;
        case 'AFElementTemplate':
            marker = 'ET'
            break;
        case 'AFEnumerationSet':
            marker = 'MS'
            switch (ownerType) {
                case 'PISystem':
                    marker += "R"
                    break
                case 'PIServer':
                    marker += "D"
                    break
                default:
                    throw "please provide owner type"
            }
            break;
        case 'AFEnumerationValue':
            marker = 'MV'
            switch (ownerType) {
                case 'PISystem':
                    marker += "R"
                    break
                case 'PIServer':
                    marker += "D"
                    break
                default:
                    throw "please provide owner type"
            }
            break;
        case 'AFEventFrame':
            marker = 'Fm'
            break;
        case 'AFNotification':
            marker = 'Nf'
            break;
        case 'AFNotificationTemplate':
            marker = 'NT'
            break;
        case 'AFNotificationContactTemplate':
            marker = 'NC'
            break;
        case 'AFTimeRule':
            marker = 'TR'
            switch (ownerType) {
                case 'AFAnalysis':
                    marker += "X"
                    break
                case 'AFAnalysisTemplate':
                    marker += "T"
                    break
                default:
                    throw "please provide owner type"
            }
            break;
        case 'AFTimeRulePlugin':
            marker = 'TP'
            break;
        case 'AFSecurityIdentity':
            marker = 'SI'
            break;
        case 'AFSecurityMapping':
            marker = 'SM'
            break;
        case 'AFTable':
            marker = 'Bl'
            break;
        case 'AFTableCategory':
            marker = 'BC'
            break;
        case 'PIPoint':
            marker = 'DP'
            break;
        case 'PIServer':
            marker = 'DS'
            break;
        case 'PISystem':
            marker = 'RS'
            break;
        case 'UOM':
            marker = 'Ut'
            break;
        case 'UOMClass':
            marker = 'UC'
            break;
        default:
            throw "please provide a suitable datatype"
    }
    return marker
}

 

Licensing

Copyright 2019 OSIsoft, LLC.

 

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 

http://www.apache.org/licenses/LICENSE-2.0

 

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Outcomes