Commit 782e604f authored by Ikram Haq's avatar Ikram Haq
Browse files

Implement logic for zoneStatusNotification

parent b91c86ea
Loading
Loading
Loading
Loading
+39 −30
Original line number Diff line number Diff line
@@ -3988,48 +3988,57 @@ components:
      x-etsi-ref: 6.4.6

    ZoneStatusNotification:
      description: A type containing zone status notification.
      properties:
        _links:
          $ref: '#/components/schemas/Link'
        accessPointId:
          description: Identifier of an access point.
          description: Identifier of an access point (e.g. ap01). Shall be included when userNumEvent related with access point or operationStatus is included.
          type: string
          x-etsi-mec-cardinality: 0..1
          x-etsi-mec-origin-type: string
        callbackData:
          description: CallBackData if passed by the application during the associated ZoneStatusSubscription operation. See [REST_NetAPI_Common].
          x-etsi-mec-origin-type: String
        notificationType:
          description: Shall be set to "ZoneStatusNotification".
          type: string
          x-etsi-mec-cardinality: 0..1
          x-etsi-mec-origin-type: string
        link:
          description: Link to other resources that are in relationship with this notification. The server SHOULD include a link to the related subscription. No other links are required or suggested by this specification
          items:
            $ref: '#/components/schemas/Link'
          type: array
          x-etsi-mec-cardinality: 0..N
          x-etsi-mec-origin-type: Link
        numberOfUsersInAP:
          description: This element shall be present when ZoneStatusSubscription includes numberOfUsersAPThreshold element and the number of users in an access point exceeds the threshold defined in the subscription.
          type: integer
          x-etsi-mec-cardinality: 0..1
          x-etsi-mec-origin-type: unsignedInt
        numberOfUsersInZone:
          description: This element shall be present when ZoneStatusSubscription includes numberOfUsersZoneThreshold element and the number of users in a zone exceeds the threshold defined in this subscription.
          type: integer
          x-etsi-mec-cardinality: 0..1
          x-etsi-mec-origin-type: unsignedInt
          x-etsi-mec-cardinality: '1'
          x-etsi-mec-origin-type: String
        operationStatus:
          description': Shall be present when ZoneStatusSubscription includes operationStatus and the operation status value of an access point meets Serviceable or Unserviceable or Unknown defined in the subscription. See note 1.
          x-etsi-mec-cardinality': 0..1
          x-etsi-mec-origin-type': OperationStatus
          $ref: '#/components/schemas/OperationStatus'
        timestamp:
        timeStamp:
          description': Time stamp.
          x-etsi-mec-cardinality': 0..1
          x-etsi-mec-origin-type': TimeStamp
          $ref: '#/components/schemas/TimeStamp'
        userNumEvent:
          description: 'Shall be present when ZoneStatusSubscription includes upperNumberOfUsersZoneThreshold, lowerNumberOfUsersZoneThreshold, upperNumberOfUsersAPThreshold or lowerNumberOfUsersAPThreshold, and the number of users in a zone or an access point crosses the threshold defined in the subscription:
            1 = OVER_ZONE_UPPER_THD.
            2 = UNDER_ZONE_LOWER_THD.
            3 = OVER_AP_UPPER_THD.
            4 = UNDER_AP_LOWER_THD.'
          enum:
            - 1
            - 2
            - 3
            - 4
          type: integer
          x-etsi-mec-cardinality: 0..1
          x-etsi-mec-origin-type: Enum(inlined)
        zoneId:
          description: Identifier of zone
          description: 'The identity of the zone. '
          type: string
          x-etsi-mec-cardinality: 1
          x-etsi-mec-origin-type: string
          x-etsi-mec-cardinality: '1'
          x-etsi-mec-origin-type: String
      required:
      - notificationType
      - zoneId
        - timestamp
      - _links
      type: object
      x-etsi-notes: "NOTE:\tAs specified in [5], clause 5.2.3.2."
      x-etsi-ref: 6.4.7


    # ZoneStatusSubscription:
    #   description: A type containing zone status subscription.
    #   properties:
+111 −23
Original line number Diff line number Diff line
@@ -120,6 +120,10 @@ type ZoneStatusCheck struct {
	Unknown                         bool
	NbUsersInZoneThreshold          int32
	NbUsersInAPThreshold            int32
	upperNumberOfUsersZoneThreshold int32
	lowerNumberOfUsersZoneThreshold int32
	upperNumberOfUsersAPThreshold   int32
	lowerNumberOfUsersAPThreshold   int32
}

type DistanceCheck struct {
@@ -608,7 +612,7 @@ func deregisterZoneStatus(subsIdStr string) {
	zoneStatusSubscriptionMap[subsId] = nil
}

func registerZoneStatus(zoneId string, nbOfUsersZoneThreshold int32, nbOfUsersAPThreshold int32, opStatus []OperationStatus, subsIdStr string) {
func registerZoneStatus(zoneId string, upNbOfUsersZoneThreshold int32, upNbOfUsersAPThreshold int32, opStatus []OperationStatus, subsIdStr string, loNbOfUsersZoneThreshold int32, loNbOfUsersAPThreshold int32) {

	subsId, err := strconv.Atoi(subsIdStr)
	if err != nil {
@@ -629,8 +633,10 @@ func registerZoneStatus(zoneId string, nbOfUsersZoneThreshold int32, nbOfUsersAP
			}
		}
	}
	zoneStatus.NbUsersInZoneThreshold = nbOfUsersZoneThreshold
	zoneStatus.NbUsersInAPThreshold = nbOfUsersAPThreshold
	zoneStatus.upperNumberOfUsersZoneThreshold = upNbOfUsersZoneThreshold
	zoneStatus.upperNumberOfUsersAPThreshold = upNbOfUsersAPThreshold
	zoneStatus.lowerNumberOfUsersZoneThreshold = loNbOfUsersZoneThreshold
	zoneStatus.lowerNumberOfUsersAPThreshold = loNbOfUsersAPThreshold
	zoneStatus.ZoneId = zoneId
	mutex.Lock()
	defer mutex.Unlock()
@@ -1258,8 +1264,7 @@ func registerPeriodic(periodicSub *PeriodicNotificationSubscription, subsIdStr s
	periodicSubscriptionMap[subsId] = &periodicCheck
}

func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersInAP int32, nbUsersInZone int32, previousNbUsersInAP int32, previousNbUsersInZone int32) {

func checkNotificationRegisteredZoneStatus1(zoneId string, apId string, nbUsersInAP int32, nbUsersInZone int32, previousNbUsersInAP int32, previousNbUsersInZone int32) {
	mutex.Lock()
	defer mutex.Unlock()

@@ -1272,18 +1277,31 @@ func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersIn
		if zoneStatus.ZoneId == zoneId {
			zoneWarning := false
			apWarning := false
			zoneWarning_Lower := false
			apWarning_Lower := false

			if nbUsersInZone != -1 {
				if previousNbUsersInZone != nbUsersInZone && nbUsersInZone >= zoneStatus.NbUsersInZoneThreshold {
				if previousNbUsersInZone != nbUsersInZone && nbUsersInZone >= zoneStatus.upperNumberOfUsersZoneThreshold {
					zoneWarning = true
				}
			}
			if nbUsersInZone != -1 {
				if previousNbUsersInZone != nbUsersInZone && nbUsersInZone <= zoneStatus.lowerNumberOfUsersZoneThreshold {
					zoneWarning_Lower = true
				}
			}
			if nbUsersInAP != -1 {
				if previousNbUsersInAP != nbUsersInAP && nbUsersInAP >= zoneStatus.NbUsersInAPThreshold {
				if previousNbUsersInAP != nbUsersInAP && nbUsersInAP >= zoneStatus.upperNumberOfUsersAPThreshold {
					apWarning = true
				}
			}
			if nbUsersInAP != -1 {
				if previousNbUsersInAP != nbUsersInAP && nbUsersInAP <= zoneStatus.lowerNumberOfUsersAPThreshold {
					apWarning_Lower = true
				}
			}

			if zoneWarning || apWarning {
			if zoneWarning || apWarning || apWarning_Lower || zoneWarning_Lower {
				subsIdStr := strconv.Itoa(subsId)
				jsonInfo, _ := rc.JSONGetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".")
				if jsonInfo == "" {
@@ -1296,10 +1314,17 @@ func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersIn
				zoneStatusNotif.ZoneId = zoneId
				if apWarning {
					zoneStatusNotif.AccessPointId = apId
					zoneStatusNotif.NumberOfUsersInAP = nbUsersInAP
					zoneStatusNotif.UserNumEvent = "OVER_AP_UPPER_THD"
				}
				if apWarning_Lower {
					zoneStatusNotif.AccessPointId = apId
					zoneStatusNotif.UserNumEvent = "UNDER_AP_LOWER_THD"
				}
				if zoneWarning {
					zoneStatusNotif.NumberOfUsersInZone = nbUsersInZone
					zoneStatusNotif.UserNumEvent = "OVER_ZONE_UPPER_THD"
				}
				if zoneWarning_Lower {
					zoneStatusNotif.UserNumEvent = "UNDER_ZONE_LOWER_THD"
				}
				seconds := time.Now().Unix()
				var timestamp TimeStamp
@@ -1316,7 +1341,68 @@ func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersIn
			}
		}
	}

}

// func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersInAP int32, nbUsersInZone int32, previousNbUsersInAP int32, previousNbUsersInZone int32) {

// 	mutex.Lock()
// 	defer mutex.Unlock()

// 	//check all that applies
// 	for subsId, zoneStatus := range zoneStatusSubscriptionMap {
// 		if zoneStatus == nil {
// 			continue
// 		}

// 		if zoneStatus.ZoneId == zoneId {
// 			zoneWarning := false
// 			apWarning := false
// 			if nbUsersInZone != -1 {
// 				if previousNbUsersInZone != nbUsersInZone && nbUsersInZone >= zoneStatus.NbUsersInZoneThreshold {
// 					zoneWarning = true
// 				}
// 			}
// 			if nbUsersInAP != -1 {
// 				if previousNbUsersInAP != nbUsersInAP && nbUsersInAP >= zoneStatus.NbUsersInAPThreshold {
// 					apWarning = true
// 				}
// 			}

// 			if zoneWarning || apWarning {
// 				subsIdStr := strconv.Itoa(subsId)
// 				jsonInfo, _ := rc.JSONGetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".")
// 				if jsonInfo == "" {
// 					return
// 				}

// 				subscription := convertJsonToZoneStatusSubscription(jsonInfo)

// 				var zoneStatusNotif ZoneStatusNotification
// 				zoneStatusNotif.ZoneId = zoneId
// 				if apWarning {
// 					zoneStatusNotif.AccessPointId = apId
// 					zoneStatusNotif.NumberOfUsersInAP = nbUsersInAP
// 				}
// 				if zoneWarning {
// 					zoneStatusNotif.NumberOfUsersInZone = nbUsersInZone
// 				}
// 				seconds := time.Now().Unix()
// 				var timestamp TimeStamp
// 				timestamp.Seconds = int32(seconds)
// 				zoneStatusNotif.Timestamp = &timestamp
// 				var inlineZoneStatusNotification InlineZoneStatusNotification
// 				inlineZoneStatusNotification.ZoneStatusNotification = &zoneStatusNotif
// 				sendStatusNotification(subscription.CallbackReference.NotifyURL, inlineZoneStatusNotification)
// 				if apWarning {
// 					log.Info("Zone Status Notification" + "(" + subsIdStr + "): " + "For event in zone " + zoneId + " which has " + strconv.Itoa(int(nbUsersInAP)) + " users in AP " + apId)
// 				} else {
// 					log.Info("Zone Status Notification" + "(" + subsIdStr + "): " + "For event in zone " + zoneId + " which has " + strconv.Itoa(int(nbUsersInZone)) + " users in total")
// 				}
// 			}
// 		}
// 	}
// }
func checkNotificationRegisteredUsers1(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {
	mutex.Lock()
	defer mutex.Unlock()
@@ -4016,7 +4102,7 @@ func handleZoneStatusSubscription(w http.ResponseWriter, requestBody []map[strin
			zoneStatusSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/zones/" + subsIdStr
			_ = rc.JSONSetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(&zoneStatusSub))
			registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.UpperNumberOfUsersZoneThreshold, zoneStatusSub.UpperNumberOfUsersAPThreshold,
				zoneStatusSub.OperationStatus, subsIdStr)
				zoneStatusSub.OperationStatus, subsIdStr, zoneStatusSub.LowerNumberOfUsersZoneThreshold, zoneStatusSub.LowerNumberOfUsersAPThreshold)

			// Prepare response
			var response InlineZoneStatusSubscription
@@ -4460,8 +4546,8 @@ func zoneStatusSubPost(w http.ResponseWriter, r *http.Request) {

	_ = rc.JSONSetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(zoneStatusSub))

	registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.NumberOfUsersZoneThreshold, zoneStatusSub.NumberOfUsersAPThreshold,
		zoneStatusSub.OperationStatus, subsIdStr)
	registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.UpperNumberOfUsersZoneThreshold, zoneStatusSub.UpperNumberOfUsersAPThreshold,
		zoneStatusSub.OperationStatus, subsIdStr, zoneStatusSub.LowerNumberOfUsersZoneThreshold, zoneStatusSub.LowerNumberOfUsersAPThreshold)

	response.ZoneStatusSubscription = zoneStatusSub

@@ -4543,8 +4629,8 @@ func zoneStatusSubPut(w http.ResponseWriter, r *http.Request) {
	_ = rc.JSONSetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(zoneStatusSub))

	deregisterZoneStatus(subsIdStr)
	registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.NumberOfUsersZoneThreshold, zoneStatusSub.NumberOfUsersAPThreshold,
		zoneStatusSub.OperationStatus, subsIdStr)
	registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.UpperNumberOfUsersZoneThreshold, zoneStatusSub.UpperNumberOfUsersAPThreshold,
		zoneStatusSub.OperationStatus, subsIdStr, zoneStatusSub.LowerNumberOfUsersZoneThreshold, zoneStatusSub.LowerNumberOfUsersAPThreshold)

	response.ZoneStatusSubscription = zoneStatusSub

@@ -4725,7 +4811,8 @@ func updateZoneInfo(zoneId string, nbAccessPoints int, nbUnsrvAccessPoints int,

	// Update Zone info in DB & Send notifications
	_ = rc.JSONSetEntry(baseKey+typeZone+":"+zoneId, ".", convertZoneInfoToJson(zoneInfo))
	checkNotificationRegisteredZoneStatus(zoneId, "", int32(-1), int32(nbUsers), int32(-1), previousNbUsers)
	// checkNotificationRegisteredZoneStatus(zoneId, "", int32(-1), int32(nbUsers), int32(-1), previousNbUsers)
	checkNotificationRegisteredZoneStatus1(zoneId, "", int32(-1), int32(nbUsers), int32(-1), previousNbUsers)
}

func updateAccessPointInfo(zoneId string, apId string, conTypeStr string, opStatusStr string, nbUsers int, longitude *float32, latitude *float32) {
@@ -4781,7 +4868,8 @@ func updateAccessPointInfo(zoneId string, apId string, conTypeStr string, opStat

	// Update AP info in DB & Send notifications
	_ = rc.JSONSetEntry(baseKey+typeZone+":"+zoneId+":"+typeAccessPoint+":"+apId, ".", convertAccessPointInfoToJson(apInfo))
	checkNotificationRegisteredZoneStatus(zoneId, apId, int32(nbUsers), int32(-1), previousNbUsers, int32(-1))
	// checkNotificationRegisteredZoneStatus(zoneId, apId, int32(nbUsers), int32(-1), previousNbUsers, int32(-1))
	checkNotificationRegisteredZoneStatus1(zoneId, apId, int32(nbUsers), int32(-1), previousNbUsers, int32(-1))
}

func zoneStatusReInit() {
+4 −0
Original line number Diff line number Diff line
@@ -25,6 +25,10 @@ package server

// A type containing zone status notification.
type ZoneStatusNotification struct {
	// Shall be present when ZoneStatusSubscription includes specific thresholds.
	UserNumEvent string `json:"userNumEvent,omitempty"`
	// Shall be set to \"ZoneStatusNotification\".
	NotificationType string `json:"notificationType,omitempty"`
	// Identifier of an access point.
	AccessPointId string `json:"accessPointId,omitempty"`
	// CallBackData if passed by the application during the associated ZoneStatusSubscription operation. See [REST_NetAPI_Common].