Newer
Older
/*
* Copyright (c) 2024 The AdvantEDGE Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
"sync"
"time"
log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
uuid "github.com/google/uuid"
)
// Sensors-Sharing Service Manager
type SssMgr struct {
name string
namespace string
mutex sync.Mutex
wg sync.WaitGroup
refreshTicker *time.Ticker
sss_discovery_notify func()
sss_status_notify func()
sss_data_notify func()
Address string
Port int
Name string
IotPlatformId string
type Point struct {
Latitude float64
Longitude float64
}
type SensorCharacteristic struct {
CharacteristicName string
CharacteristicValue string
CharacteristicUnitOfMeasure *string
}
type SensorDiscoveryInfo struct {
SensorIdentifier string
SensorType string
SensorPropertyList []string
SensorCharacteristicList []SensorCharacteristic
SensorPosition *Point
IotPlatformId string
}
var registeredIotPlatformsMap = map[string]IotPlatformInfo{} // List of discovered IOT Plateform
var sensorsMap = map[string]SensorDiscoveryInfo{} // Map sensors by sensorIdentifier
var sensorsPerPlatformMap = map[string][]string{} // Map dsensorIdentifiers per platform
// Timer to refresh devices list for all IoT platform
const refreshTickerExpeary = 10 // In seconds
// Enable profiling
const profiling = false
var profilingTimers map[string]time.Time
const (
iot_platform_address = "lab-oai.etsi.org"
iot_platform_port = 31110
iot_platform_name = "laboai-acme-ic-cse"
iot_platform_id = "7feaadbb0400"
// NewSssMgr - Creates and initializes a new SSS Traffic Manager
func NewSssMgr(name string, namespace string, bindingProtocol string, host string, port int, sss_discovery_notify func(), sss_status_notify func(), sss_data_notify func()) (tm *SssMgr, err error) {
if name == "" {
err = errors.New("Missing connector name")
return nil, err
}
// Create new Traffic Manager
tm.name = name
if namespace != "" {
tm.namespace = namespace
} else {
tm.namespace = "default"
}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
tm.bindingProtocol = bindingProtocol
tm.host = host
tm.port = port
if tm.bindingProtocol == "MQTT" {
if tm.host == "" {
err := errors.New("Host not set for MQTTP protocol")
log.Error(err.Error())
return nil, err
}
if tm.port == 0 {
tm.port = 1883
}
protocol = NewSssMgrMqtt()
} else if tm.bindingProtocol == "HTTP" {
protocol = NewSssMgrHttp()
} else {
err := errors.New("Binding protocol not set")
log.Error(err.Error())
return nil, err
}
err = protocol.init(tm)
if err != nil {
log.Error(err.Error())
return nil, err
}
tm.sss_discovery_notify = sss_discovery_notify
tm.sss_status_notify = sss_status_notify
tm.sss_data_notify = sss_data_notify
tm.init()
return tm, nil
}
// Profiling init
if profiling {
profilingTimers = make(map[string]time.Time)
}
registeredIotPlatformsMap = make(map[string]IotPlatformInfo, 1)
registeredIotPlatformsMap[iot_platform_address] = IotPlatformInfo{ // FIXME FSCOM How to register IoT platform to meep-sss (see meep-iot?)
Address: iot_platform_address,
Port: iot_platform_port,
Name: iot_platform_name,
IotPlatformId: iot_platform_id,
}
sensorsMap = make(map[string]SensorDiscoveryInfo, 0)
sensorsPerPlatformMap = make(map[string][]string, 0)
// DeleteSssMgr -
func (tm *SssMgr) DeleteSssMgr() (err error) {
if protocol != nil {
protocol.uninit()
protocol = nil
}
func (tm *SssMgr) startRefreshTicker() {
log.Debug("Starting refresh loop")
tm.refreshTicker = time.NewTicker(refreshTickerExpeary * time.Second)
go func() {
if tm.refreshTicker != nil {
for range tm.refreshTicker.C {
// Refresh the list of devices
tm.wg.Add(1)
err := tm.populateDevicesPerIotPlatforms()
if err != nil {
log.Error(err)
}
tm.wg.Done()
}
}
}()
}
func (tm *SssMgr) stopRefreshTicker() {
if tm.refreshTicker != nil {
// Refresh the list of devices
tm.wg.Add(1)
tm.refreshTicker.Stop()
tm.refreshTicker = nil
registeredIotPlatformsMap = nil
sensorsMap = nil
sensorsPerPlatformMap = nil
tm.wg.Done()
log.Debug("Refresh loop stopped")
}
}
func (tm *SssMgr) SensorDiscoveryInfoAll() (sensors []SensorDiscoveryInfo, err error) {
profilingTimers["GetDevices"] = time.Now()
log.Info(">>> SensorDiscoveryInfoAll")
err = tm.populateDevicesPerIotPlatforms() // FIXME FSCOM User timer. See startRefreshTicker
if err != nil {
return sensors, err
log.Info("SensorDiscoveryInfoAll: After Synchro")
sensors = make([]SensorDiscoveryInfo, 0)
for _, v := range sensorsMap {
log.Info("SensorDiscoveryInfoAll: adding sensor: ", v)
sensors = append(sensors, v)
log.Info("SensorDiscoveryInfoAll: sensors: ", sensors)
log.Debug("SensorDiscoveryInfoAll: ", now.Sub(profilingTimers["GetDevices"]))
func (tm *SssMgr) GetSensor(sensorIdentifier string) (sensor SensorDiscoveryInfo, err error) {
profilingTimers["GetSensor"] = time.Now()
log.Info(">>> GetSensor: sensorIdentifier: ", sensorIdentifier)
log.Info("GetSensor: After Synchro")
if val, ok := sensorsMap[sensorIdentifier]; !ok {
log.Debug("GetSensor: ", now.Sub(profilingTimers["GetSensor"]))
log.Info("GetSensor: sensor: ", sensor)
}
/*
* func populateDevicesPerIotPlatforms IoT devices for all registered Iot platform
* @return {struct} nil on success, error otherwise
*/
func (tm *SssMgr) populateDevicesPerIotPlatforms() error {
tm.mutex.Lock()
defer tm.mutex.Unlock()
if profiling {
profilingTimers["populateDevicesPerIotPlatforms"] = time.Now()
}
if len(registeredIotPlatformsMap) == 0 {
return nil
}
// Refresh the list of devices for all registered Iot platform
for _, iotPlatform := range registeredIotPlatformsMap {
log.Debug("populateDevicesPerIotPlatforms: processing: ", iotPlatform.Address)
err := tm.populateSensors(iotPlatform)
if err != nil {
log.Error("populateDevicesPerIotPlatforms: ", err)
continue
}
} // End of 'for' statement
if profiling {
now := time.Now()
log.Debug("populateDevicesPerIotPlatforms: ", now.Sub(profilingTimers["populateDevicesPerIotPlatforms"]))
}
return nil
}
/*
* func PopulateDevices IoT devices for the specified Iot platform
* @param {string} iotPlatformId contains the IoT platform identifier
* @return {struct} nil on success, error otherwise
*/
func (tm *SssMgr) populateSensors(iotPlatformInfo IotPlatformInfo) error {
profilingTimers["populateSensors"] = time.Now()
log.Info(">>> populateSensors: iotPlatformInfo=", iotPlatformInfo)
// Build the context
var ctx = SssMgrBindingProtocolContext{
host: iotPlatformInfo.Address,
port: iotPlatformInfo.Port,
name: iotPlatformInfo.Name,
to: iotPlatformInfo.Name,
from: "Admin", // FIXME FSCOM How to get it
op: 2, // RETRIEVE
ty: -1,
rqi: uuid.New().String(),
rvi: []string{"4"}, // FIXME FSCOM How to get it
code: 200,
}
// Build the queries
queries := map[string]string{}
queries["fu"] = "1" // Filter usage
queries["ty"] = "3" // Filter on oneM2M CIN for sensors
ctx.queries = queries
err, resp := protocol.send(ctx)
log.Debug("populateSensors: resp: ", resp)
log.Debug("populateSensors: TypeOf(resp): ", reflect.TypeOf(resp))
oneM2M_uril := resp.(map[string]interface{})
log.Debug("populateSensors: oneM2M_uril: ", oneM2M_uril)
log.Debug("populateSensors: TypeOf(oneM2M_uril): ", reflect.TypeOf(oneM2M_uril))
log.Debug("populateSensors: len(oneM2M_uril): ", len(oneM2M_uril))
// Loop for each CIN and build the sensor list
for _, v := range oneM2M_uril["m2m:uril"].([]interface{}) {
log.Debug("populateSensors: Processing key: v: ", v)
log.Debug("populateSensors: Processing key: TypeOf(v): ", reflect.TypeOf(v))
s := v.(string)
if s == "laboai-cse-in/acpCreateACPs" || s == "laboai-cse-in/CAdmin" {
// FIXME FSCOM Bug in MQTT DISCOVERY request which does not provide the same response that HTTP DISCOVERY with the same filter criteria
continue // Discard it
ctx.to = s
ctx.queries["fu"] = "2"
err, resp := protocol.send(ctx)
log.Error("oneM2M_create: ", err.Error())
continue
}
log.Debug("populateSensors: resp: ", resp)
log.Debug("populateSensors: type(resp): ", reflect.TypeOf(resp))
if resp.(map[string]interface{}) == nil || resp.(map[string]interface{})["m2m:cnt"] == nil {
oneM2M_cin := resp.(map[string]interface{})["m2m:cnt"].(map[string]interface{})
log.Debug("populateSensors: type(oneM2M_cin): ", reflect.TypeOf(oneM2M_cin))
log.Debug("populateSensors: len(oneM2M_cin): ", len(oneM2M_cin))
log.Debug("populateSensors: oneM2M_cin: ", oneM2M_cin)
var sensor = SensorDiscoveryInfo{
IotPlatformId: iotPlatformInfo.IotPlatformId,
}
for k, v := range oneM2M_cin {
log.Debug(k, " value is ", v)
log.Debug("populateSensors: type(v): ", reflect.TypeOf(v))
if k == "ri" {
if item, ok := v.(string); ok {
sensor.SensorIdentifier = item
} else {
log.Error("populateSensors: Failed to process ", k)
}
} else if k == "ty" {
if item, ok := v.(float64); ok {
sensor.SensorType = strconv.FormatFloat(item, 'f', -1, 64)
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
log.Error("populateSensors: Failed to process ", k)
}
} else {
sensor.SensorPropertyList = append(sensor.SensorPropertyList, k)
if item, ok := v.(string); ok {
sensor.SensorCharacteristicList = append(
sensor.SensorCharacteristicList,
SensorCharacteristic{
CharacteristicName: k,
CharacteristicValue: string(item),
})
} else if item, ok := v.(float64); ok {
sensor.SensorCharacteristicList = append(
sensor.SensorCharacteristicList,
SensorCharacteristic{
CharacteristicName: k,
CharacteristicValue: strconv.FormatFloat(item, 'f', -1, 64),
})
} else if item, ok := v.(int64); ok {
sensor.SensorCharacteristicList = append(
sensor.SensorCharacteristicList,
SensorCharacteristic{
CharacteristicName: k,
CharacteristicValue: strconv.FormatInt(item, 10),
})
} else if item, ok := v.(bool); ok {
sensor.SensorCharacteristicList = append(
sensor.SensorCharacteristicList,
SensorCharacteristic{
CharacteristicName: k,
CharacteristicValue: strconv.FormatBool(item),
})
} else if item, ok := v.([]string); ok {
sensor.SensorCharacteristicList = append(
sensor.SensorCharacteristicList,
SensorCharacteristic{
CharacteristicName: k,
CharacteristicValue: strings.Join(item, ","),
})
} else if item, ok := v.([]int64); ok {
log.Error("populateSensors: Failed to convert list of int64 into string: ", item)
} else if item, ok := v.([]interface{}); ok {
log.Debug("populateSensors: Got []interface {} for ", k)
log.Debug("populateSensors: ValueOf ", reflect.ValueOf(item))
s := SensorCharacteristic{
CharacteristicName: k,
var buf bytes.Buffer
fmt.Fprintf(&buf, "%T", reflect.ValueOf(item))
s.CharacteristicValue = buf.String()
sensor.SensorCharacteristicList = append(sensor.SensorCharacteristicList, s)
} else {
log.Error("populateSensors: Failed to process ", k)
} // End of 'for' statement
log.Info("populateSensors: sensor: ", sensor)
sensorsMap[sensor.SensorIdentifier] = sensor
sensorsPerPlatformMap[sensor.IotPlatformId] = append(sensorsPerPlatformMap[sensor.IotPlatformId], sensor.SensorIdentifier)
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// // 1. Get the list of the AE
// // Build the URL
// url := "http://" + iotPlatformInfo.Address + ":" + strconv.Itoa(int(iotPlatformInfo.Port)) + "/" + iotPlatformInfo.Name
// log.Debug("populateSensors: url=", url)
// // Build the headers
// var headers = http.Header{}
// headers["Accept"] = []string{headerAccept}
// headers["Content-Type"] = []string{headerContentType}
// headers["X-M2M-Origin"] = []string{"CAdmin"} // FIXME FSCOM How to get it
// headers["X-M2M-RI"] = []string{uuid.New().String()}
// headers["X-M2M-RVI"] = []string{"4"} // FIXME FSCOM How to get it
// // Build the queries
// queries := map[string]string{}
// queries["fu"] = "1" // Filter usage
// queries["ty"] = "4" // Filter on oneM2M CIN for sensors
// // Send the request
// response, err := sendRequest("GET", url, headers, nil, nil, queries, 200)
// if err != nil {
// log.Error("populateSensors: ", err.Error())
// return err
// }
// log.Debug("populateSensors: response: ", string(response))
// var oneM2M_uril map[string][]string
// err = json.Unmarshal(response, &oneM2M_uril)
// if err != nil {
// log.Error("populateSensors: ", err.Error())
// return err
// }
// log.Debug("populateSensors: oneM2M_uril: ", len(oneM2M_uril))
// log.Debug(oneM2M_uril)
// if _, ok := oneM2M_uril["m2m:uril"]; !ok {
// err := errors.New("populateSensors: CharacteristicName not found: m2m:uril")
// log.Error(err.Error())
// return err
// }
// // Loop for each CIN and build the sensor list
// for _, v := range oneM2M_uril["m2m:uril"] {
// log.Debug("populateSensors: Processing key: ", v)
// url := "http://" + iotPlatformInfo.Address + ":" + strconv.Itoa(int(iotPlatformInfo.Port)) + "/" + v
// log.Debug("populateSensors: url=", url)
// // Build the headers
// var headers = http.Header{}
// headers["Accept"] = []string{headerAccept}
// headers["Content-Type"] = []string{headerContentType}
// headers["X-M2M-Origin"] = []string{"CAdmin"} // FIXME FSCOM How to get it
// headers["X-M2M-RI"] = []string{uuid.New().String()}
// headers["X-M2M-RVI"] = []string{"4"} // FIXME FSCOM How to get it
// // Build the queries
// queries := map[string]string{}
// queries["fu"] = "2" // Filter usage
// // Send the request
// response, err := sendRequest("GET", url, headers, nil, nil, queries, 200)
// if err != nil {
// log.Error("populateSensors: ", err.Error())
// return err
// }
// log.Debug("populateSensors: response: ", string(response))
// var oneM2M_cin map[string]map[string]interface{}
// err = json.Unmarshal(response, &oneM2M_cin)
// if err != nil {
// log.Error("populateSensors: ", err.Error())
// continue
// }
// log.Debug("populateSensors: type(oneM2M_cin): ", reflect.TypeOf(oneM2M_cin))
// log.Debug("populateSensors: len(oneM2M_cin): ", len(oneM2M_cin))
// log.Debug("populateSensors: oneM2M_cin: ", oneM2M_cin)
// for _, m := range oneM2M_cin {
// //log.Debug("==> ", i, " value is ", m)
// var sensor = SensorDiscoveryInfo{
// IotPlatformId: iotPlatformInfo.IotPlatformId,
// }
// // m is a map[string]interface.
// // loop over keys and values in the map.
// for k, v := range m {
// log.Debug(k, " value is ", v)
// log.Debug("populateSensors: type(v): ", reflect.TypeOf(v))
// if k == "ri" {
// if item, ok := v.(string); ok {
// sensor.SensorIdentifier = item
// } else {
// log.Error("populateSensors: Failed to process ", k)
// }
// } else if k == "ty" {
// if item, ok := v.(float64); ok {
// sensor.SensorType = strconv.FormatFloat(item, 'f', -1, 64)
// } else {
// log.Error("populateSensors: Failed to process ", k)
// }
// } else {
// sensor.SensorPropertyList = append(sensor.SensorPropertyList, k)
// if item, ok := v.(string); ok {
// sensor.SensorCharacteristicList = append(
// sensor.SensorCharacteristicList,
// SensorCharacteristic{
// CharacteristicName: k,
// CharacteristicValue: string(item),
// })
// } else if item, ok := v.(float64); ok {
// sensor.SensorCharacteristicList = append(
// sensor.SensorCharacteristicList,
// SensorCharacteristic{
// CharacteristicName: k,
// CharacteristicValue: strconv.FormatFloat(item, 'f', -1, 64),
// })
// } else if item, ok := v.(int64); ok {
// sensor.SensorCharacteristicList = append(
// sensor.SensorCharacteristicList,
// SensorCharacteristic{
// CharacteristicName: k,
// CharacteristicValue: strconv.FormatInt(item, 10),
// })
// } else if item, ok := v.(bool); ok {
// sensor.SensorCharacteristicList = append(
// sensor.SensorCharacteristicList,
// SensorCharacteristic{
// CharacteristicName: k,
// CharacteristicValue: strconv.FormatBool(item),
// })
// } else if item, ok := v.([]string); ok {
// sensor.SensorCharacteristicList = append(
// sensor.SensorCharacteristicList,
// SensorCharacteristic{
// CharacteristicName: k,
// CharacteristicValue: strings.Join(item, ","),
// })
// } else if item, ok := v.([]int64); ok {
// log.Error("populateSensors: Failed to convert list of int64 into string: ", item)
// } else if item, ok := v.([]interface{}); ok {
// log.Debug("populateSensors: Got []interface {} for ", k)
// log.Debug("populateSensors: ValueOf ", reflect.ValueOf(item))
// s := SensorCharacteristic{
// CharacteristicName: k,
// }
// var buf bytes.Buffer
// fmt.Fprintf(&buf, "%T", reflect.ValueOf(item))
// s.CharacteristicValue = buf.String()
// sensor.SensorCharacteristicList = append(sensor.SensorCharacteristicList, s)
// } else {
// log.Error("populateSensors: Failed to process ", k)
// }
// }
// // if k == "rn" {
// // if item, ok := v.(string); ok {
// // sensor.DeviceId = item
// // } else {
// // log.Error("populateSensors: Failed to process ", k)
// // }
// // } else if k == "ri" {
// // if item, ok := v.(string); ok {
// // sensor.SensorIdentifier = item
// // } else {
// // log.Error("populateSensors: Failed to process ", k)
// // }
// // } else if k == "ty" {
// // if item, ok := v.(float64); ok {
// // sensor.SensorStatusType = strconv.FormatFloat(item, 'f', -1, 64)
// // } else {
// // log.Error("populateSensors: Failed to process ", k)
// // }
// // } else { // default: if k == "lt" || k == "et" || k == "ct" || k == "st" || k == "pi" || k == "lbl" {
// // if item, ok := v.(string); ok {
// // sensor.SensorCharacteristicList = append(
// // sensor.SensorCharacteristicList,
// // SensorCharacteristic{
// // CharacteristicName: k,
// // CharacteristicValue: string(item),
// // CharacteristicUnitOfMeasure: nil,
// // })
// // } else if item, ok := v.(float64); ok {
// // sensor.SensorCharacteristicList = append(
// // sensor.SensorCharacteristicList,
// // SensorCharacteristic{
// // CharacteristicName: k,
// // CharacteristicValue: strconv.FormatFloat(item, 'f', -1, 64),
// // CharacteristicUnitOfMeasure: nil,
// // })
// // } else if item, ok := v.([]string); ok {
// // sensor.SensorCharacteristicList = append(
// // sensor.SensorCharacteristicList,
// // SensorCharacteristic{
// // CharacteristicName: k,
// // CharacteristicValue: strings.Join(item, ","),
// // CharacteristicUnitOfMeasure: nil,
// // })
// // } else {
// // log.Error("populateSensors: Failed to process ", k)
// // }
// // }
// } // End of 'for' loop
// log.Info("populateSensors: sensor: ", sensor)
// sensorsMap[sensor.SensorIdentifier] = sensor
// sensorsPerPlatformMap[sensor.IotPlatformId] = append(sensorsPerPlatformMap[sensor.IotPlatformId], sensor.SensorIdentifier)
// } // End of 'for' loop
// } // End of 'for' statement
log.Info("populateSensors: sensorsMap: ", sensorsMap)
log.Info("populateSensors: sensorsPerPlatformMap: ", sensorsPerPlatformMap)
log.Debug("populateSensors: ", now.Sub(profilingTimers["populateSensors"]))
func (tm *SssMgr) oneM2M_create(sensor SensorDiscoveryInfo, requestedIotPlatformId string, type_ string) (sensorResp SensorDiscoveryInfo, err error) {
// FIXME FSCOM: requestedIotPlatformId should be useless
var bodyMap = map[string]map[string]interface{}{}
// Initialize the entry
if type_ == "AE" { // FIXME FSCOM Clarify how to map Deviceinfo with oneM2M AE/CNT/fexContainer
bodyMap["m2m:ae"] = make(map[string]interface{}, 0)
bodyMap["m2m:ae"]["api"] = "Norg.etsi." + requestedIotPlatformId + "." + sensor.SensorIdentifier
bodyMap["m2m:ae"]["rn"] = sensor.SensorIdentifier
bodyMap["m2m:ae"]["srv"] = []string{"4"}
} else if type_ == "CNT" {
bodyMap["m2m:cnt"] = make(map[string]interface{}, 0)
bodyMap["m2m:cnt"]["mbs"] = 10000
bodyMap["m2m:cnt"]["mni"] = 10
bodyMap["m2m:cnt"]["rn"] = sensor.SensorIdentifier
bodyMap["m2m:cnt"]["srv"] = []string{"4"}
if len(sensor.SensorCharacteristicList) != 0 {
for _, val := range sensor.SensorCharacteristicList {
log.Debug("oneM2M_create: Adding CNT metadata: ", val)
// FIXME FSCOM Add metadata
} // End of 'for' statement
}
} else {
err = errors.New("oneM2M_create: Invalid type")
log.Error("oneM2M_create: ", err.Error())
// Send it and get the result
var ctx = SssMgrBindingProtocolContext{
host: registeredIotPlatformsMap[requestedIotPlatformId].Address,
port: registeredIotPlatformsMap[requestedIotPlatformId].Port,
name: registeredIotPlatformsMap[requestedIotPlatformId].Name,
to: registeredIotPlatformsMap[requestedIotPlatformId].Name,
from: requestedIotPlatformId,
op: 1, // CREATE
rqi: uuid.New().String(),
rvi: []string{"4"}, // FIXME FSCOM How to get it
body: bodyMap,
code: 201,
if type_ == "AE" {
ctx.ty = 2
} else if type_ == "CNT" {
ctx.ty = 4
} else {
err = errors.New("oneM2M_create: Invalid type")
log.Error("send: ", err.Error())
//var resp = map[string]map[string]interface{}{}
err, resp := protocol.send(ctx)
if err != nil {
log.Error("oneM2M_create: ", err.Error())
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
// Build the headers
// var headers = http.Header{}
// headers["Accept"] = []string{headerAccept}
// headers["X-M2M-Origin"] = []string{"C" + requestedIotPlatformId} // FIXME FSCOM How to get it
// headers["X-M2M-RI"] = []string{uuid.New().String()}
// headers["X-M2M-RVI"] = []string{"4"}
// var s string
// if type_ == "AE" {
// s = headerContentType + ";ty=2"
// } else if type_ == "CNT" {
// s = headerContentType + ";ty=4"
// }
// headers["Content-Type"] = []string{s}
// // Build the url and the body
// var url string
// var bodyMap = map[string]map[string]interface{}{}
// Initialize the entry
// if type_ == "AE" { // FIXME FSCOM Clarify how to map Deviceinfo with oneM2M AE/CNT/fexContainer
// bodyMap["m2m:ae"] = make(map[string]interface{}, 0)
// bodyMap["m2m:ae"]["api"] = "Norg.etsi." + requestedIotPlatformId + "." + sensor.SensorIdentifier
// bodyMap["m2m:ae"]["rn"] = sensor.SensorIdentifier
// bodyMap["m2m:ae"]["rr"] = false
// bodyMap["m2m:ae"]["srv"] = []string{"4"}
// url = "http://" + registeredIotPlatformsMap[requestedIotPlatformId].Address + ":" + strconv.Itoa(int(registeredIotPlatformsMap[requestedIotPlatformId].Port)) + "/" + registeredIotPlatformsMap[requestedIotPlatformId].Name
// } else if type_ == "CNT" {
// bodyMap["m2m:cnt"] = make(map[string]interface{}, 0)
// bodyMap["m2m:cnt"]["mbs"] = 10000
// bodyMap["m2m:cnt"]["mni"] = 10
// bodyMap["m2m:cnt"]["rn"] = sensor.SensorIdentifier
// bodyMap["m2m:cnt"]["srv"] = []string{"4"}
// // Add metadata
// if len(sensor.SensorCharacteristicList) != 0 {
// for _, val := range sensor.SensorCharacteristicList {
// log.Debug("oneM2M_create: Adding CNT metadata: ", val)
// // FIXME FSCOM Add metadata
// } // End of 'for' statement
// }
// url = "http://" + registeredIotPlatformsMap[requestedIotPlatformId].Address + ":" + strconv.Itoa(int(registeredIotPlatformsMap[requestedIotPlatformId].Port)) + "/" + registeredIotPlatformsMap[requestedIotPlatformId].Name
// } else {
// err = errors.New("oneM2M_create: Invalid type")
// log.Error("oneM2M_create: ", err.Error())
// return sensorResp, err
// }
// log.Debug("oneM2M_create: url=", url)
// log.Debug("oneM2M_create: bodyMap=", bodyMap)
// body, err := json.Marshal(bodyMap)
// if err != nil {
// log.Error("oneM2M_create: ", err.Error())
// return sensorResp, err
// }
// log.Debug("oneM2M_create: Request body: ", string(body))
// Send the request
// response, err := sendRequest("POST", url, headers, bytes.NewBuffer(body), nil, nil, 201)
// if err != nil {
// log.Error("oneM2M_create: ", err.Error())
// return sensorResp, err
// }
// log.Debug("oneM2M_create: response: ", string(response))
// var d map[string]map[string]interface{}
// err = json.Unmarshal(response, &d)
// if err != nil {
// log.Error("oneM2M_create: ", err.Error())
// return sensorResp, err
// }
log.Debug("oneM2M_create: d: ", resp)
log.Debug("oneM2M_create: TypeOf(d): ", reflect.TypeOf(resp))
// sensorResp, err = tm.oneM2M_deserialize(sensorResp, resp)
// if err != nil {
// log.Error("oneM2M_create: ", err.Error())
// return sensorResp, err
// }
// log.Debug("oneM2M_create: sensorResp: ", sensorResp)
func (tm *SssMgr) oneM2M_discovery(sensor SensorDiscoveryInfo, requestedIotPlatformId string, type_ string) (sensorResp SensorDiscoveryInfo, err error) {
// FIXME FSCOM: requestedIotPlatformId should be useless
// 1. Get the list of the AE
// Build the URL
url := "http://" + registeredIotPlatformsMap[requestedIotPlatformId].Address + ":" + strconv.Itoa(int(registeredIotPlatformsMap[requestedIotPlatformId].Port)) + "/" + registeredIotPlatformsMap[requestedIotPlatformId].Name
log.Debug("oneM2M_discovery: url=", url)
// Build the headers
var headers = http.Header{}
headers["Accept"] = []string{headerAccept}
headers["Content-Type"] = []string{headerContentType}
headers["X-M2M-Origin"] = []string{"CAdmin"} // FIXME FSCOM How to get it
headers["X-M2M-RI"] = []string{uuid.New().String()}
headers["X-M2M-RVI"] = []string{"4"} // FIXME FSCOM How to get it
// Build the queries
queries := map[string]string{}
if type_ == "CN" {
queries["fu"] = "1" // Filter usage
queries["ty"] = "4" // Filter on oneM2M CIN for sensor
} else {
err = errors.New("oneM2M_discovery: Invalid type")
log.Error("oneM2M_discovery: ", err.Error())
}
// Send the request
response, err := sendRequest("GET", url, headers, nil, nil, queries, 200)
if err != nil {
log.Error("oneM2M_discovery: ", err.Error())
}
log.Debug("oneM2M_discovery: response: ", string(response))
var d map[string]map[string]interface{}
err = json.Unmarshal(response, &d)
if err != nil {
log.Error("oneM2M_discovery: ", err.Error())
}
log.Debug("oneM2M_discovery: d: ", d)
// Add additional entries
sensorResp, err = tm.oneM2M_deserialize(sensor, d)
if err != nil {
log.Error("oneM2M_discovery: ", err.Error())
// log.Debug("oneM2M_discovery: sensorResp: ", sensorResp)
return sensorResp, nil
func (tm *SssMgr) oneM2M_get(sensor SensorDiscoveryInfo, requestedIotPlatformId string, type_ string) (sensorResp SensorDiscoveryInfo, err error) {
// FIXME FSCOM: requestedIotPlatformId should be useless
if sensor.SensorIdentifier == "" {
err = errors.New("oneM2M_get: Cannot find \"ri\" value")
log.Error("oneM2M_get: ", err.Error())
}
// 1. Get the list of the AE
// Build the URL
url := "http://" + registeredIotPlatformsMap[requestedIotPlatformId].Address + ":" + strconv.Itoa(int(registeredIotPlatformsMap[requestedIotPlatformId].Port)) + "/" + sensor.SensorIdentifier
log.Debug("oneM2M_get: url=", url)
// Build the headers
var headers = http.Header{}
headers["Accept"] = []string{headerAccept}
headers["Content-Type"] = []string{headerContentType}
headers["X-M2M-Origin"] = []string{"CAdmin"} // FIXME FSCOM How to get it
headers["X-M2M-RI"] = []string{uuid.New().String()}
headers["X-M2M-RVI"] = []string{"4"} // FIXME FSCOM How to get it
// Build the queries
queries := map[string]string{}
if type_ == "AE" {
queries = nil
} else if type_ == "CN" {
queries["fu"] = "1" // Filter usage
queries["ty"] = "4" // Filter on oneM2M CIN for sensor
} else {
err = errors.New("oneM2M_get: Invalid type")
log.Error("oneM2M_get: ", err.Error())
}
// Send the request
response, err := sendRequest("GET", url, headers, nil, nil, queries, 200)
if err != nil {
log.Error("oneM2M_get: ", err.Error())
}
log.Debug("oneM2M_get: response: ", string(response))
var d map[string]map[string]interface{}
err = json.Unmarshal(response, &d)
if err != nil {
log.Error("oneM2M_get: ", err.Error())
}
log.Debug("oneM2M_get: d: ", d)
// Add additional entries
sensorResp, err = tm.oneM2M_deserialize(sensor, d)
if err != nil {
log.Error("oneM2M_get: ", err.Error())
log.Debug("oneM2M_get: sensorResp: ", sensorResp)
return sensorResp, nil
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
func (tm *SssMgr) oneM2M_subscribe(sensor SensorDiscoveryInfo, requestedIotPlatformId string, type_ string) (subscription string, err error) {
// FIXME FSCOM: requestedIotPlatformId should be useless
// Build the headers
var headers = http.Header{}
headers["Accept"] = []string{headerAccept}
headers["X-M2M-Origin"] = []string{"C" + requestedIotPlatformId} // FIXME FSCOM How to get it
headers["X-M2M-RI"] = []string{uuid.New().String()}
headers["X-M2M-RVI"] = []string{"4"}
s := headerContentType + ";ty=23"
headers["Content-Type"] = []string{s}
// Build the url and the body
var url string
var bodyMap = map[string]map[string]interface{}{}
bodyMap["m2m:sub"] = make(map[string]interface{}, 0)
net := make(map[string][4]int)
net["net"] = [4]int{1, 2, 3, 4}
bodyMap["m2m:sub"]["enc"] = net
bodyMap["m2m:sub"]["nu"] = "" // FIXME The URI of the listener
bodyMap["m2m:sub"]["rn"] = sensor.SensorIdentifier
url = "http://" + registeredIotPlatformsMap[requestedIotPlatformId].Address + ":" + strconv.Itoa(int(registeredIotPlatformsMap[requestedIotPlatformId].Port)) + "/" + registeredIotPlatformsMap[requestedIotPlatformId].Name
log.Debug("oneM2M_subscribe: url=", url)
log.Debug("oneM2M_subscribe: bodyMap=", bodyMap)
body, err := json.Marshal(bodyMap)
if err != nil {
log.Error("oneM2M_subscribe: ", err.Error())
return "", err
}
log.Debug("oneM2M_subscribe: Request body: ", string(body))
// Send the request
response, err := sendRequest("POST", url, headers, bytes.NewBuffer(body), nil, nil, 201)
if err != nil {
log.Error("oneM2M_subscribe: ", err.Error())
return "", err
}
log.Debug("oneM2M_subscribe: response: ", string(response))
var d map[string]map[string]interface{}
err = json.Unmarshal(response, &d)
if err != nil {
log.Error("oneM2M_subscribe: ", err.Error())
return "", err
}
log.Debug("oneM2M_subscribe: d: ", d)
err = errors.New("oneM2M_subscribe: To be implemented")
return "", err /*nil*/
}
func (tm *SssMgr) oneM2M_delete(sensor SensorDiscoveryInfo, requestedIotPlatformId string, type_ string) (err error) {
// FIXME FSCOM: requestedIotPlatformId should be useless
if sensor.SensorIdentifier == "" {
err = errors.New("oneM2M_delete: Cannot find \"ri\" value")
log.Error("oneM2M_delete: ", err.Error())
return err
}
// Build the URL
url := "http://" + registeredIotPlatformsMap[requestedIotPlatformId].Address + ":" + strconv.Itoa(int(registeredIotPlatformsMap[requestedIotPlatformId].Port)) + "/" + sensor.SensorIdentifier
log.Debug("oneM2M_delete: url=", url)
// Build the headers
var headers = http.Header{}
headers["Accept"] = []string{headerAccept}
headers["Content-Type"] = []string{headerContentType}
headers["X-M2M-Origin"] = []string{"C" + requestedIotPlatformId} // FIXME FSCOM How to get it
headers["X-M2M-RI"] = []string{uuid.New().String()}
headers["X-M2M-RI"] = []string{uuid.New().String()}
headers["X-M2M-RVI"] = []string{"4"}
// Send the request
_, err = sendRequest("DELETE", url, headers, nil, nil, nil, 200)
if err != nil {
log.Error("oneM2M_delete: ", err.Error())
return err
}
return nil
}
func (tm *SssMgr) oneM2M_subscribe_discovery_event(requestedIotPlatformId string) (err error) {
return nil
}
func (tm *SssMgr) oneM2M_deserialize(sensor SensorDiscoveryInfo, response map[string]map[string]interface{}) (sensorResp SensorDiscoveryInfo, err error) {
sensorResp = sensor // Same data structure
log.Debug("oneM2M_deserialize: type(response): ", reflect.TypeOf(response))
log.Debug("oneM2M_deserialize: len(response): ", len(response))
log.Debug("oneM2M_deserialize: response: ", response)
if val, ok := response["m2m:ae"]; ok {
log.Debug("oneM2M_deserialize: val: ", val)