scada

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package scada is the headless runtime service container for a SCADA / 组态 (HMI) application: it owns ONE of each backend resource — the real-time tag registry, the alarm engine, the historian, the event log, the recipe book and the live-statistics collector — and wires them together so a screen built from the backend-free gui panels has a single, consistent runtime to talk to.

Import direction. scada sits ABOVE gui in the import graph: it may import core, gui, device, driver, historian, eventlog, recipe, stats, alarmbridge, notify, report and playback, and it feeds the gui panels through their plain SetX / SigY seams. gui MUST NOT import scada — the panels stay decoupled and unit-testable, driven by data and callbacks rather than by a Services handle. The tree-walking wiring that connects a live screen to a Services lives in wire.go (BindScreen); this file is the container and its lifecycle.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindScreen

func BindScreen(s *Services, root gui.IWidget, opt ScreenOptions) (stop func(), err error)

BindScreen wires a live screen (rooted at root) to the container's shared stores. It walks the widget tree once and, per widget:

  • a tag-bound industrial widget (the TagName() seam) is bound to its tag — eased float sinks via gui.BindTagAnimated, boolean sinks via a posted gui.BindTag;
  • a *gui.AlarmPanel is bound live to the AlarmDB and its ack intent routed back to AlarmDB.Ack;
  • each of the five operator panels (Recipe / Report / EventLog / Trend / Stats) is wired through a helper that speaks only the panel's Set*/Sig* API and marshals async backend results back with gui.Post;
  • a *device.DeviceComponent has its point tag names registered now and is started LAST (only when opt.EnableDrivers), so binding never races the first poll.

The returned stop func is idempotent and aggregates every unsubscribe / stop in reverse. A device start failure rolls back everything already wired and returns the error.

Types

type Config

type Config struct {
	HistorianPath string                         // SQLite file for the sample historian
	EventLogPath  string                         // SQLite file for the event/audit log
	RecipePath    string                         // JSON file for the recipe book (loaded if present)
	TagMeta       map[string]core.Meta           // tags registered up front, with engineering + alarm limits
	Notifier      func(title, body string) error // user-facing notification sink (nil = silent)
	OnError       func(error)                    // best-effort error sink for background failures (nil = drop)
}

Config parameterises New. The three paths locate the on-disk stores; TagMeta seeds the tag registry (and its alarm limits) before any store is touched; Notifier and OnError are the two side-effect sinks the container routes through so the host, not scada, owns notification delivery and error policy.

func DefaultConfig

func DefaultConfig(dir string) Config

DefaultConfig returns a Config whose stores live under dir and whose Notifier is the native desktop notifier. A headless caller (tests) overrides Notifier with its own sink; notify.Notify reaches the OS and needs a desktop session.

type ScreenOptions

type ScreenOptions struct {
	TrendTag    string   // live/history tag feeding a TrendPanel's chart
	TrendSeries string   // chart series name (default "trend")
	RecipeTags  []string // tags a RecipePanel captures into a new recipe
	StatsTags   []string // tags a StatsPanel tracks and displays

	EventWindow time.Duration // how far back an EventLogPanel queries (default 1h)

	ReportTags      []string         // columns a ReportView builds
	ReportWindow    time.Duration    // report time span (default 1h)
	ReportInterval  time.Duration    // report bucket width (default 5m)
	ReportAggregate report.Aggregate // report bucket reducer (default Avg)
	ReportDir       string           // dir for exported CSV/HTML (no file written when empty)

	MaxPlaybackPoints int // Downsample budget for history playback (default 500)

	AnimateDur time.Duration // ease duration for float industrial widgets (default 300ms)

	EnableDrivers bool // start DeviceComponents (opens device sockets) when true
}

ScreenOptions carries the per-screen configuration the backend-free gui panels deliberately do NOT hold: which tag feeds the trend chart, which tags a recipe captures, which tags the statistics table tracks, how far back the event log and reports reach, and whether the field-device drivers (which open sockets) are started. Zero-valued fields fall back to the defaults applied by normalize; only EnableDrivers has no default (it must be opt-in).

type Services

type Services struct {
	Tags    *core.TagDB          // the one real-time tag registry
	Alarms  *core.AlarmDB        // the one limit-alarm engine
	Hist    *historian.Historian // the one sample historian
	Events  *eventlog.Log        // the one event/audit log
	Recipes *recipe.Book         // the one recipe book (in memory; Save persists it)
	Stats   *stats.Collector     // the one live-statistics collector
	// contains filtered or unexported fields
}

Services owns one of each backend resource and the teardown list that unwinds everything Start installed. The five public store fields are the single shared instances the wiring (wire.go) and the host bind against.

func New

func New(cfg Config) (*Services, error)

New allocates the container: it creates the single TagDB + AlarmDB, registers cfg.TagMeta into the registry BEFORE opening any store (so alarm limits and engineering ranges are in place first), then opens the historian, event log and recipe book. If a later open fails, the resources already opened are rolled back (closed) before New returns the error, so a failed New leaks nothing. Start is NOT called here — the container is inert until Start.

func (*Services) AddStartupHook

func (s *Services) AddStartupHook(h StartupHook)

AddStartupHook registers h to run during Start. Call before Start; hooks registered after Start are ignored until a (currently unsupported) restart.

func (*Services) RecordEvent

func (s *Services) RecordEvent(kind eventlog.Kind, source, msg string)

RecordEvent appends one event to the event log, routing any write error to cfg.OnError. It is the container's single event-emission point and is safe to call from any goroutine (the event log serialises writes internally).

func (*Services) Start

func (s *Services) Start() error

Start installs the live runtime wiring and records every teardown in the cleanup list: the alarm bridge (alarm transition -> event + notification), an AlarmDB watch on each registered tag (so a limit violation raises), historian recording and stat tracking for the registered tags, then any registered startup hooks. It is idempotent — a second call is a no-op. A hook error rolls back everything installed so far and returns the error.

func (*Services) Stop

func (s *Services) Stop()

Stop tears the container down: it runs the cleanup list in reverse, then closes the event log and the historian last (in that order). It is idempotent — a second call is a no-op. Close errors are routed to cfg.OnError.

type StartupHook

type StartupHook func(s *Services) (cancel func(), err error)

StartupHook is a user-registered wiring step run by Start after the built-in wiring (alarm bridge, historian recording, stat tracking). It returns a cancel func kept in the container's cleanup list, or an error that aborts and rolls back Start. The Services is passed so a hook can reach the shared stores.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL