Hugovdberg / PIconnect

A python connector to the OSISoft PI and PI-AF databases
MIT License
91 stars 41 forks source link

Connecting to the PI server #554

Closed fouadsel88 closed 3 years ago

fouadsel88 commented 3 years ago

Description

When I tried to import the PI package using below code

import PIconnect as PI

I got the below error, when i use my personal computer worked fine however when I ran it in my work machine I get this error, is this normal behavior from a protected network? and how I can fix that?


SocketException                           Traceback (most recent call last)
SocketException: No such host is known
   at System.Net.Dns.GetAddrInfo(String name)
   at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
   at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
   at System.ServiceModel.Channels.DnsCache.Resolve(Uri uri)

The above exception was the direct cause of the following exception:

EndpointNotFoundException                 Traceback (most recent call last)
<ipython-input-1-4cbc695e5bee> in <module>
----> 1 import PIconnect as PI

...project_env\lib\site-packages\PIconnect\__init__.py in <module>
     26 from PIconnect.config import PIConfig
     27 from PIconnect.PI import PIServer
---> 28 from PIconnect.PIAF import PIAFDatabase
     29 
     30 # pragma pylint: enable=unused-import

...project_env\lib\site-packages\PIconnect\PIAF.py in <module>
     59 
     60 
---> 61 class PIAFDatabase(object):
     62     """PIAFDatabase
     63 

...project_env\lib\site-packages\PIconnect\PIAF.py in PIAFDatabase()
     69     servers = {
     70         s.Name: {"server": s, "databases": {d.Name: d for d in s.Databases}}
---> 71         for s in AF.PISystems()
     72     }
     73     if AF.PISystems().DefaultPISystem:

...project_env\lib\site-packages\PIconnect\PIAF.py in <dictcomp>(.0)
     69     servers = {
     70         s.Name: {"server": s, "databases": {d.Name: d for d in s.Databases}}
---> 71         for s in AF.PISystems()
     72     }
     73     if AF.PISystems().DefaultPISystem:

...roject_env\lib\site-packages\PIconnect\PIAF.py in <dictcomp>(.0)
     68 
     69     servers = {
---> 70         s.Name: {"server": s, "databases": {d.Name: d for d in s.Databases}}
     71         for s in AF.PISystems()
     72     }

EndpointNotFoundException: No DNS entries exist for host myafserver.

Server stack trace: 
   at System.ServiceModel.Channels.DnsCache.Resolve(Uri uri)
   at System.ServiceModel.Channels.SocketConnectionInitiator.GetIPAddresses(Uri uri)
   at System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri, TimeSpan timeout)
   at System.ServiceModel.Channels.BufferedConnectionInitiator.Connect(Uri uri, TimeSpan timeout)
   at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at System.ServiceModel.ICommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ClientBase`1.Open()
   at OSIsoft.AF.Support.AFProxy.Reconnect(Boolean autoPrompt, AFConnectionProtocol protocol, String host, Int32 port, String accountName, TimeSpan timeout)

System

boardinary commented 3 years ago

I also get this error. I can get my code to work by running it a second time. I've been trying to dig into what exactly is happening but I have had no luck so far. I'm not sure where myafserver is even defined.

boardinary commented 3 years ago

I was able to get it to work by commenting out some code and hardcoding the default server name into PIAF.py:

class PIAFDatabase(object):
    """PIAFDatabase

    Context manager for connections to the PI Asset Framework database.
    """

    version = "0.1.1"

    # servers = {
        # s.Name: {"server": s, "databases": {d.Name: d for d in s.Databases}}
        # for s in AF.PISystems()
    # }
    servers = {}
    # if AF.PISystems().DefaultPISystem:
        # default_server = servers[AF.PISystems().DefaultPISystem.Name]
    # elif len(servers) > 0:
        # default_server = servers[list(servers)[0]]
    # else:
        # default_server = None
    default_server = "your_server_name_here"
somuchung commented 3 years ago

I was able to get it to work by commenting out some code and hardcoding the default server name into PIAF.py:

class PIAFDatabase(object):
    """PIAFDatabase

    Context manager for connections to the PI Asset Framework database.
    """

    version = "0.1.1"

    # servers = {
        # s.Name: {"server": s, "databases": {d.Name: d for d in s.Databases}}
        # for s in AF.PISystems()
    # }
    servers = {}
    # if AF.PISystems().DefaultPISystem:
        # default_server = servers[AF.PISystems().DefaultPISystem.Name]
    # elif len(servers) > 0:
        # default_server = servers[list(servers)[0]]
    # else:
        # default_server = None
    default_server = "your_server_name_here"

Thank you! This worked for me. My issue was when I made my script an executable file and put it on a computer in a different domain, it couldn't connect the server for some reason...even when PISDK had connection with the PI server.

fouadsel88 commented 3 years ago

Thank you all for your answers. I found this link very helpful https://pisquare.osisoft.com/s/question/0D51I00004UHq9c/python-36-and-afsdk-example-pithon If you follow the steps in the link you can use FindPIServer function to find your server

from OSIsoft.AF import *
from OSIsoft.AF.PI import *
from OSIsoft.AF.Asset import *
from OSIsoft.AF.Data import *
from OSIsoft.AF.Time import *
from OSIsoft.AF.UnitsOfMeasure import *
from OSIsoft.AF import UI

Myserver = UI.AFHelper.FindPIServer(yourServerName)
fouadsel88 commented 3 years ago

I am able to connect to the PI server, thank you all for sharing this valuables information