@@ -136,101 +136,102 @@ func (s *DeviceService) Stop(force bool) {
136
136
137
137
// selfRegister register device service itself onto metadata.
138
138
func (s * DeviceService ) selfRegister () error {
139
- s .LoggingClient .Debug ("Trying to find Device Service: " + s .ServiceName )
139
+ addr , err := s .createAndUpdateAddressable ()
140
+ if err != nil {
141
+ s .LoggingClient .Error (fmt .Sprintf ("createAndUpdateAddressable failed: %v" , err ))
142
+ return err
143
+ }
144
+
145
+ newDeviceService := contract.DeviceService {
146
+ Name : s .ServiceName ,
147
+ Labels : s .config .Service .Labels ,
148
+ OperatingState : contract .Enabled ,
149
+ Addressable : * addr ,
150
+ AdminState : contract .Unlocked ,
151
+ }
152
+ newDeviceService .Origin = time .Now ().UnixNano () / int64 (time .Millisecond )
140
153
141
154
ctx := context .WithValue (context .Background (), common .CorrelationHeader , uuid .New ().String ())
155
+ s .LoggingClient .Debug ("Trying to find DeviceService: " + s .ServiceName )
142
156
ds , err := s .edgexClients .DeviceServiceClient .DeviceServiceForName (ctx , s .ServiceName )
143
-
144
157
if err != nil {
145
158
if errsc , ok := err .(types.ErrServiceClient ); ok && (errsc .StatusCode == http .StatusNotFound ) {
146
- s .LoggingClient .Info (fmt .Sprintf ("Device Service %s doesn't exist, creating a new one" , s .ServiceName ))
147
- ds , err = s .createNewDeviceService ()
159
+ s .LoggingClient .Info (fmt .Sprintf ("DeviceService %s doesn't exist, creating a new one" , s .ServiceName ))
160
+ id , err := s .edgexClients .DeviceServiceClient .Add (ctx , & newDeviceService )
161
+ if err != nil {
162
+ s .LoggingClient .Error (fmt .Sprintf ("Failed to add Deviceservice %s: %v" , s .ServiceName , err ))
163
+ return err
164
+ }
165
+ if err = common .VerifyIdFormat (id , "Device Service" ); err != nil {
166
+ return err
167
+ }
168
+ // NOTE - this differs from Addressable and Device Resources,
169
+ // neither of which require the '.Service'prefix
170
+ newDeviceService .Id = id
171
+ s .LoggingClient .Debug ("New DeviceService Id: " + newDeviceService .Id )
148
172
} else {
149
173
s .LoggingClient .Error (fmt .Sprintf ("DeviceServicForName failed: %v" , err ))
150
174
return err
151
175
}
152
176
} else {
153
- s .LoggingClient .Info (fmt .Sprintf ("Device Service %s exists" , ds .Name ))
177
+ s .LoggingClient .Info (fmt .Sprintf ("DeviceService %s exists, updating it" , ds .Name ))
178
+ err = s .edgexClients .DeviceServiceClient .Update (ctx , newDeviceService )
179
+ if err != nil {
180
+ s .LoggingClient .Error (fmt .Sprintf ("Failed to update DeviceService %s: %v" , newDeviceService .Name , err ))
181
+ // use the existed one to at least make sure config is in sync with metadata.
182
+ newDeviceService = ds
183
+ }
184
+ newDeviceService .Id = ds .Id
154
185
}
155
186
156
- s .LoggingClient .Debug (fmt .Sprintf ("Device Service in Core MetaData: %s" , s .ServiceName ))
157
- s .deviceService = ds
158
-
187
+ s .deviceService = newDeviceService
159
188
return nil
160
189
}
161
190
162
- func (s * DeviceService ) createNewDeviceService () (contract.DeviceService , error ) {
163
- addr , err := s .makeNewAddressable ()
164
- if err != nil {
165
- s .LoggingClient .Error (fmt .Sprintf ("makeNewAddressable failed: %v" , err ))
166
- return contract.DeviceService {}, err
167
- }
168
-
169
- millis := time .Now ().UnixNano () / int64 (time .Millisecond )
170
- ds := contract.DeviceService {
171
- Name : s .ServiceName ,
172
- Labels : s .config .Service .Labels ,
173
- OperatingState : contract .Enabled ,
174
- Addressable : * addr ,
175
- AdminState : contract .Unlocked ,
176
- }
177
- ds .Origin = millis
178
-
191
+ // TODO: Addressable will be removed in v2.
192
+ func (s * DeviceService ) createAndUpdateAddressable () (* contract.Addressable , error ) {
179
193
ctx := context .WithValue (context .Background (), common .CorrelationHeader , uuid .New ().String ())
180
- id , err := s .edgexClients .DeviceServiceClient .Add (ctx , & ds )
181
- if err != nil {
182
- s .LoggingClient .Error (fmt .Sprintf ("Add Deviceservice: %s; failed: %v" , s .ServiceName , err ))
183
- return contract.DeviceService {}, err
184
- }
185
- if err = common .VerifyIdFormat (id , "Device Service" ); err != nil {
186
- return contract.DeviceService {}, err
194
+ newAddr := contract.Addressable {
195
+ Timestamps : contract.Timestamps {
196
+ Origin : time .Now ().UnixNano () / int64 (time .Millisecond ),
197
+ },
198
+ Name : s .ServiceName ,
199
+ HTTPMethod : http .MethodPost ,
200
+ Protocol : common .HttpProto ,
201
+ Address : s .config .Service .Host ,
202
+ Port : s .config .Service .Port ,
203
+ Path : common .APICallbackRoute ,
187
204
}
188
205
189
- // NOTE - this differs from Addressable and Device Resources,
190
- // neither of which require the '.Service'prefix
191
- ds .Id = id
192
- s .LoggingClient .Debug ("New device service Id: " + ds .Id )
193
-
194
- return ds , nil
195
- }
196
-
197
- func (s * DeviceService ) makeNewAddressable () (* contract.Addressable , error ) {
198
- // check whether there has been an existing addressable
199
- ctx := context .WithValue (context .Background (), common .CorrelationHeader , uuid .New ().String ())
200
206
addr , err := s .edgexClients .AddressableClient .AddressableForName (ctx , s .ServiceName )
201
207
if err != nil {
202
208
if errsc , ok := err .(types.ErrServiceClient ); ok && (errsc .StatusCode == http .StatusNotFound ) {
203
209
s .LoggingClient .Info (fmt .Sprintf ("Addressable %s doesn't exist, creating a new one" , s .ServiceName ))
204
- millis := time .Now ().UnixNano () / int64 (time .Millisecond )
205
- addr = contract.Addressable {
206
- Timestamps : contract.Timestamps {
207
- Origin : millis ,
208
- },
209
- Name : s .ServiceName ,
210
- HTTPMethod : http .MethodPost ,
211
- Protocol : common .HttpProto ,
212
- Address : s .config .Service .Host ,
213
- Port : s .config .Service .Port ,
214
- Path : common .APICallbackRoute ,
215
- }
216
- id , err := s .edgexClients .AddressableClient .Add (ctx , & addr )
210
+ id , err := s .edgexClients .AddressableClient .Add (ctx , & newAddr )
217
211
if err != nil {
218
- s .LoggingClient .Error (fmt .Sprintf ("Add addressable failed %s, error : %v" , addr .Name , err ))
212
+ s .LoggingClient .Error (fmt .Sprintf ("Failed to add Addressable %s : %v" , newAddr .Name , err ))
219
213
return nil , err
220
214
}
221
215
if err = common .VerifyIdFormat (id , "Addressable" ); err != nil {
222
216
return nil , err
223
217
}
224
- addr .Id = id
218
+ newAddr .Id = id
225
219
} else {
226
220
s .LoggingClient .Error (fmt .Sprintf ("AddressableForName failed: %v" , err ))
227
221
return nil , err
228
222
}
229
223
} else {
230
- s .LoggingClient .Info (fmt .Sprintf ("Addressable %s exists" , s .ServiceName ))
224
+ s .LoggingClient .Info (fmt .Sprintf ("Addressable %s exists, updating it" , s .ServiceName ))
225
+ err = s .edgexClients .AddressableClient .Update (ctx , newAddr )
226
+ if err != nil {
227
+ s .LoggingClient .Error (fmt .Sprintf ("Failed to update Addressable %s: %v" , s .ServiceName , err ))
228
+ // use the existed one to at least make sure config is in sync with metadata.
229
+ newAddr = addr
230
+ }
231
+ newAddr .Id = addr .Id
231
232
}
232
233
233
- return & addr , nil
234
+ return & newAddr , nil
234
235
}
235
236
236
237
// RunningService returns the Service instance which is running
0 commit comments