Documentation
¶
Overview ¶
Package sim is an in-process AVEVA PI Web API for tests, built from the public PI Web API reference (no shared code with the client): one Data Archive with points and archived values, and one AF server with databases, elements, attributes (PI point data references or static values) and event frames. It answers the stream and stream set reads (value, end, recorded, interpolated, interpolated at times, plot, summary, recorded at time and times), the value and values updates with every update option and 207 answers for partial failures, stream updates with markers, batch requests with parents and parameters, the object lookups by path, WebId, name and name filter with paging, and basic, bearer and Kerberos (HTTP Negotiate) logins. Kerberos tickets are checked by gokrb5's SPNEGO acceptor with a service keytab, for example one from the KDC in sim/kdcsim. Faults can be injected per path (any status, delays), points made read-only, and requests that change data are refused without X-Requested-With, as the PI Web API's CSRF defence does.
WebIds are path-only WebID 2.0 WebIds, so a client can also make them from paths. Where the reference leaves a behaviour open (what a simulated summary computes, the status of a request whose batch parent failed), the package documentation of the function says what the simulator does.
Example ¶
Serve one Data Archive with a point, and list the Data Archives.
package main
import (
"fmt"
"io"
"net/http"
"time"
"github.com/cB-Sigma/piwebapi-sim/sim"
)
func main() {
s := sim.NewPIServer("PISRV01", "demo", "demo-pw")
defer s.Close()
s.AddPoint("TANK1.LEVEL", "m", "tank level", sim.PIValue{Time: time.Now().Add(-time.Minute), Value: 3.2})
req, _ := http.NewRequest(http.MethodGet, s.Base()+"/dataservers", nil)
req.SetBasicAuth("demo", "demo-pw")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, resp.Body)
fmt.Println(resp.StatusCode)
}
Output: 200
Index ¶
- type AFAttribute
- type Element
- type EventFrame
- type Fault
- type PIServer
- func (s *PIServer) AddDatabase(name, description string)
- func (s *PIServer) AddElement(e Element) error
- func (s *PIServer) AddEventFrame(ef EventFrame) error
- func (s *PIServer) AddFault(f Fault)
- func (s *PIServer) AddPoint(name, units, desc string, values ...PIValue)
- func (s *PIServer) Append(name string, values ...PIValue)
- func (s *PIServer) Base() string
- func (s *PIServer) ClearFaults()
- func (s *PIServer) KerberosLogins() []string
- func (s *PIServer) RequestLog() []string
- func (s *PIServer) SetCSRFDefence(on bool)
- func (s *PIServer) SetKeytab(kt *keytab.Keytab)
- func (s *PIServer) SetMaxItems(n int)
- func (s *PIServer) SetReadOnly(name string, readOnly bool)
- func (s *PIServer) Values(name string) []PIValue
- type PIValue
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AFAttribute ¶
type AFAttribute struct {
Name, Description, Units string
// Type is the value type (Double, Int32, String, Boolean...); empty
// takes it from the point or the static value.
Type string
Point string
Static any
Categories []string
// Hidden and Excluded attributes are left out of lists and stream
// sets unless showHidden or showExcluded asks for them.
Hidden, Excluded bool
// Step marks the values as steps (no interpolation).
Step bool
Children []AFAttribute
}
AFAttribute is an attribute of an element or event frame: a PI point data reference (Point names a point of the Data Archive) or, without Point, a static value (Static: a number, text or a boolean).
type Element ¶
type Element struct {
Path string
Template string
Description string
Categories []string
Attributes []AFAttribute
}
Element is an AF element to add: Path is the database and the element names below it ("Plant\Area1\Boiler1"); missing parents are added as plain elements.
type EventFrame ¶
type EventFrame struct {
Database, Name, Parent string
Template, Description string
Severity string
Start, End time.Time
Elements []string
Categories []string
Attributes []AFAttribute
Acknowledged, CanAcknowledge bool
}
EventFrame is an AF event frame to add, in Database, under the event frame named Parent when set. A zero End is an event frame in progress. Elements are the referenced elements' paths below the database ("Area1\Boiler1").
type Fault ¶
type Fault struct {
Method string
Path string
Status int
Message string
Delay time.Duration
RetryAfter int
Times int
// contains filtered or unexported fields
}
Fault makes matching requests fail or wait. A request matches when its method is Method (empty: any) and its path after /piwebapi/ starts with Path (empty: any). Delay is waited first (without holding up other requests); then a Status other than 0 is answered with Message in the Errors list (and Retry-After when set). Times limits the requests the fault applies to (0: until ClearFaults).
type PIServer ¶
type PIServer struct {
*httptest.Server
Archive string
User string
Password string
Token string
// Keytab, when set, also admits Kerberos: Authorization: Negotiate
// with a SPNEGO token for a service whose key the keytab holds,
// checked by gokrb5's acceptor as an IIS-hosted PI Web API checks it
// against its service account. Challenges then offer Negotiate.
Keytab *keytab.Keytab
// KerberosUsers are the principals that logged in with Kerberos.
KerberosUsers []string
// AssetServerName is the AF server's name (default PIAF01).
AssetServerName string
// AllowNoCSRFHeader admits requests that change data without an
// X-Requested-With header; by default they are refused with 403, as
// the PI Web API's CSRF defence refuses them.
AllowNoCSRFHeader bool
// MaxItems is the most values or objects one read returns (the PI
// Web API's MaxReturnedItemsPerCall; default 150000); a read asking
// for more is refused with 400.
MaxItems int
// Now is the simulator's clock (default time.Now).
Now func() time.Time
// Delegation makes system/userinfo report the impersonation level
// Delegation for Kerberos logins (else Impersonation).
Delegation bool
Requests []string
// contains filtered or unexported fields
}
PIServer is a PI Web API with one Data Archive and one AF server. It follows the PI Web API reference: lists are {"Items": […]}, errors {"Errors": […]} with the status the reference gives (404 for an unknown object, 400 for a bad parameter, 401 for a refused login).
func NewPIServer ¶
NewPIServer starts a PI Web API at <URL>/piwebapi.
func NewUnstartedPIServer ¶
NewUnstartedPIServer is NewPIServer without starting the server, so that its Listener can be replaced (a fixed port) before Start.
func (*PIServer) AddDatabase ¶
AddDatabase adds an AF database.
func (*PIServer) AddElement ¶
AddElement adds an element with its attributes.
func (*PIServer) AddEventFrame ¶
func (s *PIServer) AddEventFrame(ef EventFrame) error
AddEventFrame adds an event frame.
func (*PIServer) AddPoint ¶
AddPoint creates a point with its archived values. Its type is Digital when a value has a State, String when one has Text, else Float32.
func (*PIServer) KerberosLogins ¶
KerberosLogins returns the principals that logged in with Kerberos.
func (*PIServer) RequestLog ¶
RequestLog returns the requests served so far (path?query).
func (*PIServer) SetCSRFDefence ¶
SetCSRFDefence turns the X-Requested-With check on or off while the server runs (AllowNoCSRFHeader is the opposite of on).
func (*PIServer) SetKeytab ¶
SetKeytab replaces the service keytab while the server runs (a service account whose key changed).
func (*PIServer) SetMaxItems ¶
SetMaxItems changes MaxItems while the server runs.
func (*PIServer) SetReadOnly ¶
SetReadOnly makes writes to a point fail with 403, as a point the login may read but not write.
type PIValue ¶
type PIValue struct {
Time time.Time
Value float64
State string
System bool
Bad bool
// Text is the value of a String point.
Text string
// Questionable, Substituted and Annotated are returned as the value's
// flags of the same names.
Questionable, Substituted, Annotated bool
}
PIValue is one archived value of a PI point: a number (Value), text (Text, for a String point) or a digital state (State, with System for system states such as "Shutdown" or "I/O Timeout"), and its quality.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package kdcsim is an in-process Kerberos V5 key distribution centre for tests, built from RFC 4120 (sections 3.1 and 3.3, the AS and TGS exchanges, and 7.2 for the TCP and UDP framing) and RFC 3962 (AES encryption types): one realm with users (password keys, salted with realm and name), services (HTTP/host) and krbtgt.
|
Package kdcsim is an in-process Kerberos V5 key distribution centre for tests, built from RFC 4120 (sections 3.1 and 3.3, the AS and TGS exchanges, and 7.2 for the TCP and UDP framing) and RFC 3962 (AES encryption types): one realm with users (password keys, salted with realm and name), services (HTTP/host) and krbtgt. |