Something went wrong. Try again.
A Transparent and Verifiable Way to Sync the AT Protocol's PLC Directory
Something went wrong. Try again.
plcbundle plcbundle.go
4.3 kB · 148 lines
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149package plcbundle
import ( "context" "io" "time"
"tangled.org/atscan.net/plcbundle/bundle" "tangled.org/atscan.net/plcbundle/plc")
// Re-export commonly used types for conveniencetype ( Bundle = bundle.Bundle BundleMetadata = bundle.BundleMetadata Index = bundle.Index Manager = bundle.Manager Config = bundle.Config VerificationResult = bundle.VerificationResult ChainVerificationResult = bundle.ChainVerificationResult DirectoryScanResult = bundle.DirectoryScanResult Logger = bundle.Logger
PLCOperation = plc.PLCOperation PLCClient = plc.Client ExportOptions = plc.ExportOptions)
// Re-export constantsconst ( BUNDLE_SIZE = bundle.BUNDLE_SIZE INDEX_FILE = bundle.INDEX_FILE)
// NewManager creates a new bundle manager (convenience wrapper)func NewManager(config *Config, plcClient *PLCClient) (*Manager, error) { return bundle.NewManager(config, plcClient)}
// NewPLCClient creates a new PLC client (convenience wrapper)func NewPLCClient(baseURL string, opts ...plc.ClientOption) *PLCClient { return plc.NewClient(baseURL, opts...)}
// DefaultConfig returns default configuration (convenience wrapper)func DefaultConfig(bundleDir string) *Config { return bundle.DefaultConfig(bundleDir)}
// NewIndex creates a new empty index (convenience wrapper)func NewIndex() *Index { return bundle.NewIndex()}
// LoadIndex loads an index from a file (convenience wrapper)func LoadIndex(path string) (*Index, error) { return bundle.LoadIndex(path)}
// BundleManager provides a high-level API for bundle operationstype BundleManager struct { mgr *Manager}
// New creates a new BundleManager with default settingsfunc New(bundleDir string, plcURL string) (*BundleManager, error) { config := DefaultConfig(bundleDir) var plcClient *PLCClient if plcURL != "" { plcClient = NewPLCClient(plcURL) }
mgr, err := NewManager(config, plcClient) if err != nil { return nil, err }
return &BundleManager{mgr: mgr}, nil}
// Close closes the managerfunc (bm *BundleManager) Close() { bm.mgr.Close()}
// FetchNext fetches the next bundle from PLCfunc (bm *BundleManager) FetchNext(ctx context.Context) (*Bundle, error) { b, err := bm.mgr.FetchNextBundle(ctx) if err != nil { return nil, err } return b, bm.mgr.SaveBundle(ctx, b)}
// Load loads a bundle by numberfunc (bm *BundleManager) Load(ctx context.Context, bundleNumber int) (*Bundle, error) { return bm.mgr.LoadBundle(ctx, bundleNumber)}
// Verify verifies a bundlefunc (bm *BundleManager) Verify(ctx context.Context, bundleNumber int) (*VerificationResult, error) { return bm.mgr.VerifyBundle(ctx, bundleNumber)}
// VerifyChain verifies the entire chainfunc (bm *BundleManager) VerifyChain(ctx context.Context) (*ChainVerificationResult, error) { return bm.mgr.VerifyChain(ctx)}
// GetIndex returns the indexfunc (bm *BundleManager) GetIndex() *Index { return bm.mgr.GetIndex()}
// GetInfo returns manager infofunc (bm *BundleManager) GetInfo() map[string]interface{} { return bm.mgr.GetInfo()}
// Export exports operations from bundlesfunc (bm *BundleManager) Export(ctx context.Context, afterTime time.Time, count int) ([]PLCOperation, error) { return bm.mgr.ExportOperations(ctx, afterTime, count)}
// Scan scans the directory and rebuilds the indexfunc (bm *BundleManager) Scan() (*DirectoryScanResult, error) { return bm.mgr.ScanDirectory()}
// ScanBundle scans a single bundle filefunc (bm *BundleManager) ScanBundle(path string, bundleNumber int) (*BundleMetadata, error) { return bm.mgr.ScanBundle(path, bundleNumber)}
// IsBundleIndexed checks if a bundle is in the indexfunc (bm *BundleManager) IsBundleIndexed(bundleNumber int) bool { return bm.mgr.IsBundleIndexed(bundleNumber)}
// StreamRaw streams raw compressed bundle datafunc (bm *BundleManager) StreamRaw(ctx context.Context, bundleNumber int) (io.ReadCloser, error) { return bm.mgr.StreamBundleRaw(ctx, bundleNumber)}
// StreamDecompressed streams decompressed bundle datafunc (bm *BundleManager) StreamDecompressed(ctx context.Context, bundleNumber int) (io.ReadCloser, error) { return bm.mgr.StreamBundleDecompressed(ctx, bundleNumber)}