@@ -69,6 +69,9 @@ type Config struct {
6969 EnableEngineEvents bool
7070 DeleteFromCloud bool
7171
72+ HTTPHost string
73+ HTTPPort uint16
74+
7275 MQTTHost string
7376 MQTTPort uint16
7477
@@ -79,6 +82,8 @@ type Config struct {
7982func NewApp (device tedge.Target , config Config ) (* App , error ) {
8083 serviceTarget := device .Service (config .ServiceName )
8184 tedgeOpts := & tedge.ClientConfig {
85+ HTTPHost : config .HTTPHost ,
86+ HTTPPort : config .HTTPPort ,
8287 MqttHost : config .MQTTHost ,
8388 MqttPort : config .MQTTPort ,
8489 C8yHost : config .CumulocityHost ,
@@ -94,6 +99,18 @@ func NewApp(device tedge.Target, config Config) (*App, error) {
9499 return nil , err
95100 }
96101
102+ // Register via http interface
103+ _ , registrationErr := tedgeClient .TedgeAPI .CreateEntity (context .Background (), tedge.Entity {
104+ TedgeType : tedge .EntityTypeService ,
105+ Name : serviceTarget .Name ,
106+ TedgeTopicID : serviceTarget .TopicID ,
107+ })
108+ if registrationErr == nil {
109+ slog .Info ("Registered service" , "topic" , serviceTarget .Topic ())
110+ } else {
111+ slog .Error ("Could not register tedge entity." , "err" , registrationErr )
112+ }
113+
97114 if err := tedgeClient .Connect (); err != nil {
98115 return nil , err
99116 }
@@ -137,13 +154,10 @@ func (a *App) DeleteLegacyService(deleteFromCloud bool) {
137154 target := a .client .Target .Service ("tedge-container-monitor" )
138155 slog .Info ("Removing legacy service from the cloud" , "topic" , target .Topic ())
139156
140- if err := a .client .Publish (tedge .GetHealthTopic (* target ), 1 , true , "" ); err != nil {
141- slog .Warn ("Failed to clear health status." , "topic" , tedge .GetHealthTopic (* target ))
142- }
143- time .Sleep (500 * time .Millisecond )
144- if err := a .client .Publish (tedge .GetTopic (* target ), 1 , true , "" ); err != nil {
145- slog .Warn ("Failed to clear registration message." , "topic" , tedge .GetHealthTopic (* target ))
157+ if _ , err := a .client .TedgeAPI .DeleteEntity (context .Background (), * target ); err != nil {
158+ slog .Warn ("Failed to clear registration message." , "topic-id" , target .TopicID )
146159 }
160+
147161 time .Sleep (500 * time .Millisecond )
148162
149163 if target .CloudIdentity != "" && deleteFromCloud {
@@ -373,10 +387,16 @@ func (a *App) updateMetrics(items []container.TedgeContainer) error {
373387 doWork := func (jobs <- chan container.TedgeContainer , results chan <- error ) {
374388 for j := range jobs {
375389 var jobErr error
376- stats , jobErr := a .ContainerClient .GetStats (context .Background (), j .Container .Id )
390+ // TODO: Check if container exists, if not then do nothing
391+ target := a .Device .Service (j .Name )
392+ if _ , entityErr := a .client .TedgeAPI .GetEntity (context .Background (), * target ); entityErr != nil {
393+ slog .Info ("Entity has not be registered yet, skipping metric for it." , "topic-id" , target .TopicID )
394+ results <- jobErr
395+ continue
396+ }
377397
398+ stats , jobErr := a .ContainerClient .GetStats (context .Background (), j .Container .Id )
378399 if jobErr == nil {
379- target := a .Device .Service (j .Name )
380400 topic := tedge .GetTopic (* target , "m" , "resource_usage" )
381401 payload , err := json .Marshal (stats )
382402 if err == nil {
@@ -443,25 +463,26 @@ func (a *App) doUpdate(filterOptions container.FilterOptions) error {
443463 target := a .Device .Service (item .Name )
444464
445465 // Skip registration message if it already exists
446- if _ , ok := existingServices [target .Topic ()]; ok {
447- slog .Debug ("Container is already registered" , "topic" , target .Topic ())
448- delete (existingServices , target .Topic ())
449- continue
450- }
466+ // if _, ok := existingServices[target.Topic()]; ok {
467+ // slog.Info ("Container is already registered", "topic", target.Topic())
468+ // delete(existingServices, target.Topic())
469+ // continue
470+ // }
451471 delete (existingServices , target .Topic ())
452472
453- payload := map [string ]any {
454- "@type" : "service" ,
455- "name" : item .Name ,
456- "type" : item .ServiceType ,
457- }
458- b , err := json .Marshal (payload )
459- if err != nil {
460- slog .Warn ("Could not marshal registration message" , "err" , err )
461- continue
462- }
463- if err := tedgeClient .Publish (target .Topic (), 1 , true , b ); err != nil {
464- slog .Error ("Failed to register container" , "target" , target .Topic (), "err" , err )
473+ // Register using HTTP API
474+ resp , err := tedgeClient .TedgeAPI .CreateEntity (context .Background (), tedge.Entity {
475+ TedgeType : tedge .EntityTypeService ,
476+ TedgeTopicID : target .TopicID ,
477+ Name : item .Name ,
478+ Type : item .ServiceType ,
479+ TedgeParentID : tedgeClient .Parent .TopicID ,
480+ })
481+
482+ if err == nil {
483+ slog .Info ("Registered container." , "topic" , target .Topic (), "url" , resp .RawResponse .Request .URL .String (), "status_code" , resp .RawResponse .Status )
484+ } else {
485+ slog .Error ("Failed to register container." , "topic" , target .Topic (), "err" , err )
465486 }
466487 }
467488
@@ -490,18 +511,16 @@ func (a *App) doUpdate(filterOptions container.FilterOptions) error {
490511 for _ , item := range items {
491512 target := a .Device .Service (item .Name )
492513
493- topic := tedge .GetTopic (* target , "twin" , "container" )
494-
495514 // Create status
496- payload , err := json .Marshal (item .Container )
497-
515+ _ , err := tedgeClient .TedgeAPI .UpdateTwin (
516+ context .Background (),
517+ tedge.Entity {
518+ TedgeTopicID : target .TopicID ,
519+ },
520+ "container" ,
521+ item .Container ,
522+ )
498523 if err != nil {
499- slog .Error ("Failed to convert payload to json" , "err" , err )
500- continue
501- }
502-
503- slog .Info ("Publishing container status" , "topic" , topic , "payload" , payload )
504- if err := tedgeClient .Publish (topic , 1 , true , payload ); err != nil {
505524 slog .Error ("Could not publish container status" , "err" , err )
506525 }
507526 }
@@ -518,7 +537,7 @@ func (a *App) doUpdate(filterOptions container.FilterOptions) error {
518537 continue
519538 }
520539
521- if err := tedgeClient .DeregisterEntity (* target , "twin/container" ); err != nil {
540+ if err := tedgeClient .DeregisterEntity (* target ); err != nil {
522541 slog .Warn ("Failed to deregister entity." , "err" , err )
523542 }
524543
0 commit comments