Implemented epics read/write
This commit is contained in:
@@ -163,6 +163,12 @@ func (c *wsClient) dispatchLoop(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case u := <-c.updateCh:
|
||||
if u.Value.MetaUpdate {
|
||||
// Datasource refreshed metadata (e.g. after channel connected).
|
||||
// Re-send the meta message so the client gets accurate type/unit info.
|
||||
c.sendMeta(ctx, u.Ref)
|
||||
continue
|
||||
}
|
||||
msg := outMsg{
|
||||
Type: "update",
|
||||
DS: u.Ref.DS,
|
||||
@@ -265,6 +271,7 @@ func (c *wsClient) handleUnsubscribe(refs []sigRef) {
|
||||
func (c *wsClient) handleWrite(ctx context.Context, msg inMsg) {
|
||||
ds, ok := c.broker.Source(msg.DS)
|
||||
if !ok {
|
||||
c.log.Warn("write: unknown data source", "ds", msg.DS, "name", msg.Name)
|
||||
c.sendError(ctx, "NOT_FOUND", "unknown data source: "+msg.DS)
|
||||
return
|
||||
}
|
||||
@@ -272,10 +279,12 @@ func (c *wsClient) handleWrite(ctx context.Context, msg inMsg) {
|
||||
// Enforce write permission before touching the value payload.
|
||||
meta, err := ds.GetMetadata(ctx, msg.Name)
|
||||
if err != nil {
|
||||
c.log.Warn("write: GetMetadata failed", "ds", msg.DS, "name", msg.Name, "err", err)
|
||||
c.sendError(ctx, "NOT_FOUND", "unknown signal: "+msg.Name)
|
||||
return
|
||||
}
|
||||
if !meta.Writable {
|
||||
c.log.Warn("write: signal not writable", "ds", msg.DS, "name", msg.Name)
|
||||
c.sendError(ctx, "NOT_WRITABLE", "signal is read-only: "+msg.Name)
|
||||
return
|
||||
}
|
||||
@@ -283,12 +292,15 @@ func (c *wsClient) handleWrite(ctx context.Context, msg inMsg) {
|
||||
// Decode the JSON value as the appropriate Go type.
|
||||
var value any
|
||||
if err := json.Unmarshal(msg.Value, &value); err != nil {
|
||||
c.log.Warn("write: parse error", "ds", msg.DS, "name", msg.Name, "err", err)
|
||||
c.sendError(ctx, "PARSE_ERROR", "invalid value: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.log.Info("write", "ds", msg.DS, "name", msg.Name, "value", value)
|
||||
metrics.IncWrites()
|
||||
if err := ds.Write(ctx, msg.Name, value); err != nil {
|
||||
c.log.Warn("write: ds.Write failed", "ds", msg.DS, "name", msg.Name, "err", err)
|
||||
c.sendError(ctx, "WRITE_ERROR", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user