| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package device
- import (
- "context"
- "strings"
- "time"
- hthv1 "hth/internal/gen/hth/api/v1"
- "hth/internal/grpcserver"
- "hth/internal/protocol"
- "hth/internal/safety"
- )
- func terminalDispatch(s hthv1.ActionDispatchState) bool {
- switch s {
- case hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_UNCONFIRMED,
- hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_CONFIRMED,
- hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_OUTCOME_UNKNOWN,
- hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_NOT_DELIVERED,
- hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_REJECTED,
- hthv1.ActionDispatchState_ACTION_DISPATCH_STATE_SUPERSEDED:
- return true
- default:
- return false
- }
- }
- func internalOrigin(reason string) string {
- if strings.HasPrefix(reason, "daemon/internal") {
- return reason
- }
- if reason == "" {
- return "daemon/internal"
- }
- return "daemon/internal:" + reason
- }
- func capturePeer(ctx context.Context, r *req) {
- if r == nil || ctx == nil {
- return
- }
- if cred, ok := grpcserver.PeerFromContext(ctx); ok {
- r.peerUID, r.peerGID, r.peerPID = cred.UID, cred.GID, cred.PID
- }
- }
- func (d *Device) actionContextFromReq(commandID, origin string, mode hthv1.ActionMode, frame protocol.SetAll, r req) ActionExecutionContext {
- ax := d.newActionContext(commandID, origin, mode, frame, r.rpcCtx)
- if r.peerUID != 0 || r.peerGID != 0 || r.peerPID != 0 {
- ax.PeerUID, ax.PeerGID, ax.PeerPID = r.peerUID, r.peerGID, r.peerPID
- }
- ax.LeaseID = r.leaseID
- ax.LeaseEpoch = r.leaseEpoch
- if r.batch != nil {
- if ax.LeaseID == "" {
- ax.LeaseID = r.batch.GetLeaseId()
- ax.LeaseEpoch = r.batch.GetLeaseEpoch()
- }
- ax.Requested = r.batch
- if r.batch.ValidUntil != nil {
- ax.ValidUntil = r.batch.ValidUntil.AsTime()
- }
- }
- return ax
- }
- func recordFromAction(ax ActionExecutionContext, now time.Time, conf hthv1.ConfirmationStatus, dispatch hthv1.ActionDispatchState) *hthv1.ActionRecord {
- client := "hthd"
- if ax.Requested != nil && ax.Requested.ClientId != "" {
- client = ax.Requested.ClientId
- }
- rec := actionRecord(&hthv1.ActionBatch{CommandId: ax.CommandID, ClientId: client}, now, conf, ax.EffectiveTarget)
- if rec == nil {
- return nil
- }
- if ax.Requested != nil {
- rec.FieldAudits = safety.RawFieldAudits(ax.Requested)
- }
- return bindActionRecord(rec, ax, dispatch)
- }
- func stampResultContext(res *hthv1.ActionResult, ax ActionExecutionContext) *hthv1.ActionResult {
- if res == nil {
- return nil
- }
- if res.CommandId == "" {
- res.CommandId = ax.CommandID
- }
- if res.ActionOrigin == "" {
- res.ActionOrigin = ax.Origin
- }
- if res.DeviceEpoch == 0 {
- res.DeviceEpoch = ax.DeviceEpoch
- }
- if res.PortGeneration == 0 {
- res.PortGeneration = ax.PortGeneration
- }
- return res
- }
|