Newer
Older
locationInfo.Longitude = append(locationInfo.Longitude, geoDataInfo.Location.Coordinates[0])
locationInfo.Shape = 2
seconds := time.Now().Unix()
var timestamp TimeStamp
timestamp.Seconds = int32(seconds)
locationInfo.Timestamp = ×tamp
terminalLocation.CurrentLocation = &locationInfo
retrievalStatus := RETRIEVED
terminalLocation.LocationRetrievalStatus = &retrievalStatus
terminalLocationList = append(terminalLocationList, terminalLocation)
}
periodicNotif.IsFinalNotification = false
periodicNotif.Link = periodicCheck.Subscription.Link
subsIdStr := strconv.Itoa(subsId)
periodicNotif.CallbackData = periodicCheck.Subscription.CallbackReference.CallbackData
periodicNotif.TerminalLocation = terminalLocationList
var inlinePeriodicSubscriptionNotification InlineSubscriptionNotification
inlinePeriodicSubscriptionNotification.SubscriptionNotification = &periodicNotif
sendSubscriptionNotification(periodicCheck.Subscription.CallbackReference.NotifyURL, inlinePeriodicSubscriptionNotification)
log.Info("Periodic Notification"+"("+subsIdStr+") For ", periodicCheck.Subscription.Address)
}
}
}
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
func deregisterDistance(subsIdStr string) {
subsId, err := strconv.Atoi(subsIdStr)
if err != nil {
log.Error(err)
}
mutex.Lock()
defer mutex.Unlock()
distanceSubscriptionMap[subsId] = nil
}
func registerDistance(distanceSub *DistanceNotificationSubscription, subsIdStr string) {
subsId, err := strconv.Atoi(subsIdStr)
if err != nil {
log.Error(err)
}
mutex.Lock()
defer mutex.Unlock()
var distanceCheck DistanceCheck
distanceCheck.Subscription = distanceSub
//checkImmediate ignored, will be hit on next check anyway
//if distanceSub.CheckImmediate {
distanceCheck.NextTts = 0 //next time periodic trigger hits, will be forced to trigger
//} else {
// distanceCheck.NextTts = distanceSub.Frequency
// }
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
distanceSubscriptionMap[subsId] = &distanceCheck
}
func deregisterAreaCircle(subsIdStr string) {
subsId, err := strconv.Atoi(subsIdStr)
if err != nil {
log.Error(err)
}
mutex.Lock()
defer mutex.Unlock()
areaCircleSubscriptionMap[subsId] = nil
}
func registerAreaCircle(areaCircleSub *CircleNotificationSubscription, subsIdStr string) {
subsId, err := strconv.Atoi(subsIdStr)
if err != nil {
log.Error(err)
}
mutex.Lock()
defer mutex.Unlock()
var areaCircleCheck AreaCircleCheck
areaCircleCheck.Subscription = areaCircleSub
//checkImmediate ignored, will be hit on next check anyway
//if areaCircleSub.CheckImmediate {
areaCircleCheck.NextTts = 0 //next time periodic trigger hits, will be forced to trigger
//} else {
// areaCircleCheck.NextTts = areaCircleSub.Frequency
// }
areaCircleSubscriptionMap[subsId] = &areaCircleCheck
}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
func deregisterPeriodic(subsIdStr string) {
subsId, err := strconv.Atoi(subsIdStr)
if err != nil {
log.Error(err)
}
mutex.Lock()
defer mutex.Unlock()
periodicSubscriptionMap[subsId] = nil
}
func registerPeriodic(periodicSub *PeriodicNotificationSubscription, subsIdStr string) {
subsId, err := strconv.Atoi(subsIdStr)
if err != nil {
log.Error(err)
}
mutex.Lock()
defer mutex.Unlock()
var periodicCheck PeriodicCheck
periodicCheck.Subscription = periodicSub
periodicCheck.NextTts = periodicSub.Frequency
periodicSubscriptionMap[subsId] = &periodicCheck
}
func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersInAP int32, nbUsersInZone int32, previousNbUsersInAP int32, previousNbUsersInZone int32) {
Simon Pastor
committed
Simon Pastor
committed
//check all that applies
for subsId, zoneStatus := range zoneStatusSubscriptionMap {
Kevin Di Lallo
committed
if zoneStatus == nil {
continue
}
Simon Pastor
committed
Kevin Di Lallo
committed
if zoneStatus.ZoneId == zoneId {
Simon Pastor
committed
zoneWarning := false
apWarning := false
if nbUsersInZone != -1 {
if previousNbUsersInZone != nbUsersInZone && nbUsersInZone >= zoneStatus.NbUsersInZoneThreshold {
Simon Pastor
committed
zoneWarning = true
}
Simon Pastor
committed
}
if nbUsersInAP != -1 {
if previousNbUsersInAP != nbUsersInAP && nbUsersInAP >= zoneStatus.NbUsersInAPThreshold {
Simon Pastor
committed
apWarning = true
}
}
Simon Pastor
committed
Simon Pastor
committed
if zoneWarning || apWarning {
subsIdStr := strconv.Itoa(subsId)
jsonInfo, _ := rc.JSONGetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".")
if jsonInfo == "" {
return
}
Simon Pastor
committed
subscription := convertJsonToZoneStatusSubscription(jsonInfo)
Simon Pastor
committed
var zoneStatusNotif ZoneStatusNotification
zoneStatusNotif.ZoneId = zoneId
if apWarning {
zoneStatusNotif.AccessPointId = apId
Simon Pastor
committed
}
if zoneWarning {
Simon Pastor
committed
}
seconds := time.Now().Unix()
var timestamp TimeStamp
timestamp.Seconds = int32(seconds)
zoneStatusNotif.Timestamp = ×tamp
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)
Simon Pastor
committed
} else {
log.Info("Zone Status Notification" + "(" + subsIdStr + "): " + "For event in zone " + zoneId + " which has " + strconv.Itoa(int(nbUsersInZone)) + " users in total")
Simon Pastor
committed
}
}
}
}
}
func checkNotificationRegisteredUsers(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {
//check all that applies
for subsId, value := range userSubscriptionMap {
if value == userId {
subsIdStr := strconv.Itoa(subsId)
Kevin Di Lallo
committed
jsonInfo, _ := rc.JSONGetEntry(baseKey+typeUserSubscription+":"+subsIdStr, ".")
if jsonInfo == "" {
return
}
subscription := convertJsonToUserSubscription(jsonInfo)
var zonal ZonalPresenceNotification
seconds := time.Now().Unix()
var timestamp TimeStamp
timestamp.Seconds = int32(seconds)
zonal.Timestamp = ×tamp
if newZoneId != oldZoneId {
if oldZoneId != "" {
if userSubscriptionLeavingMap[subsId] != "" {
zonal.ZoneId = oldZoneId
zonal.CurrentAccessPointId = oldApId
event := new(UserEventType)
*event = LEAVING_EVENT
zonal.UserEventType = event
var inlineZonal InlineZonalPresenceNotification
inlineZonal.ZonalPresenceNotification = &zonal
sendZonalPresenceNotification(subscription.CallbackReference.NotifyURL, inlineZonal)
log.Info("User Notification" + "(" + subsIdStr + "): " + "Leaving event in zone " + oldZoneId + " for user " + userId)
}
if userSubscriptionEnteringMap[subsId] != "" && newZoneId != "" {
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
event := new(UserEventType)
*event = ENTERING_EVENT
var inlineZonal InlineZonalPresenceNotification
inlineZonal.ZonalPresenceNotification = &zonal
sendZonalPresenceNotification(subscription.CallbackReference.NotifyURL, inlineZonal)
log.Info("User Notification" + "(" + subsIdStr + "): " + "Entering event in zone " + newZoneId + " for user " + userId)
}
} else {
if newApId != oldApId {
if userSubscriptionTransferringMap[subsId] != "" {
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.PreviousAccessPointId = oldApId
event := new(UserEventType)
*event = TRANSFERRING_EVENT
Simon Pastor
committed
zonal.UserEventType = event
var inlineZonal InlineZonalPresenceNotification
inlineZonal.ZonalPresenceNotification = &zonal
sendZonalPresenceNotification(subscription.CallbackReference.NotifyURL, inlineZonal)
log.Info("User Notification" + "(" + subsIdStr + "): " + " Transferring event within zone " + newZoneId + " for user " + userId + " from Ap " + oldApId + " to " + newApId)
}
}
}
}
}
}
func sendZonalPresenceNotification(notifyUrl string, notification InlineZonalPresenceNotification) {
jsonNotif, err := json.Marshal(notification)
if err != nil {
log.Error(err)
return
}
Kevin Di Lallo
committed
resp, err := http.Post(notifyUrl, "application/json", bytes.NewBuffer(jsonNotif))
Kevin Di Lallo
committed
duration := float64(time.Since(startTime).Microseconds()) / 1000.0
_ = httpLog.LogTx(notifyUrl, "POST", string(jsonNotif), resp, startTime)
Kevin Di Lallo
committed
if err != nil {
log.Error(err)
Kevin Di Lallo
committed
met.ObserveNotification(sandboxName, serviceName, notifZonalPresence, notifyUrl, nil, duration)
Kevin Di Lallo
committed
return
}
Kevin Di Lallo
committed
met.ObserveNotification(sandboxName, serviceName, notifZonalPresence, notifyUrl, resp, duration)
func sendStatusNotification(notifyUrl string, notification InlineZoneStatusNotification) {
jsonNotif, err := json.Marshal(notification)
Simon Pastor
committed
if err != nil {
log.Error(err)
return
}
Kevin Di Lallo
committed
resp, err := http.Post(notifyUrl, "application/json", bytes.NewBuffer(jsonNotif))
Kevin Di Lallo
committed
duration := float64(time.Since(startTime).Microseconds()) / 1000.0
_ = httpLog.LogTx(notifyUrl, "POST", string(jsonNotif), resp, startTime)
Kevin Di Lallo
committed
if err != nil {
log.Error(err)
Kevin Di Lallo
committed
met.ObserveNotification(sandboxName, serviceName, notifZoneStatus, notifyUrl, nil, duration)
Kevin Di Lallo
committed
return
}
Kevin Di Lallo
committed
met.ObserveNotification(sandboxName, serviceName, notifZoneStatus, notifyUrl, resp, duration)
Simon Pastor
committed
}
func sendSubscriptionNotification(notifyUrl string, notification InlineSubscriptionNotification) {
startTime := time.Now()
jsonNotif, err := json.Marshal(notification)
if err != nil {
log.Error(err)
return
}
resp, err := http.Post(notifyUrl, "application/json", bytes.NewBuffer(jsonNotif))
duration := float64(time.Since(startTime).Microseconds()) / 1000.0
_ = httpLog.LogTx(notifyUrl, "POST", string(jsonNotif), resp, startTime)
if err != nil {
log.Error(err)
met.ObserveNotification(sandboxName, serviceName, notifSubscription, notifyUrl, nil, duration)
return
}
met.ObserveNotification(sandboxName, serviceName, notifSubscription, notifyUrl, resp, duration)
defer resp.Body.Close()
}
func checkNotificationRegisteredZones(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {
//check all that applies
for subsId, value := range zonalSubscriptionMap {
if value == newZoneId {
if newZoneId != oldZoneId {
if zonalSubscriptionEnteringMap[subsId] != "" {
subsIdStr := strconv.Itoa(subsId)
Kevin Di Lallo
committed
jsonInfo, _ := rc.JSONGetEntry(baseKey+typeZonalSubscription+":"+subsIdStr, ".")
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
var zonal ZonalPresenceNotification
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.Address = userId
event := new(UserEventType)
*event = ENTERING_EVENT
Simon Pastor
committed
zonal.UserEventType = event
seconds := time.Now().Unix()
var timestamp TimeStamp
timestamp.Seconds = int32(seconds)
zonal.Timestamp = ×tamp
var inlineZonal InlineZonalPresenceNotification
inlineZonal.ZonalPresenceNotification = &zonal
sendZonalPresenceNotification(subscription.CallbackReference.NotifyURL, inlineZonal)
log.Info("Zonal Notify Entering event in zone " + newZoneId + " for user " + userId)
}
}
} else {
if newApId != oldApId {
if zonalSubscriptionTransferringMap[subsId] != "" {
subsIdStr := strconv.Itoa(subsId)
Kevin Di Lallo
committed
jsonInfo, _ := rc.JSONGetEntry(baseKey+typeZonalSubscription+":"+subsIdStr, ".")
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
var zonal ZonalPresenceNotification
zonal.ZoneId = newZoneId
zonal.CurrentAccessPointId = newApId
zonal.PreviousAccessPointId = oldApId
zonal.Address = userId
event := new(UserEventType)
*event = TRANSFERRING_EVENT
Simon Pastor
committed
zonal.UserEventType = event
seconds := time.Now().Unix()
var timestamp TimeStamp
timestamp.Seconds = int32(seconds)
zonal.Timestamp = ×tamp
var inlineZonal InlineZonalPresenceNotification
inlineZonal.ZonalPresenceNotification = &zonal
sendZonalPresenceNotification(subscription.CallbackReference.NotifyURL, inlineZonal)
log.Info("Zonal Notify Transferring event in zone " + newZoneId + " for user " + userId + " from Ap " + oldApId + " to " + newApId)
}
}
}
} else {
if value == oldZoneId {
if zonalSubscriptionLeavingMap[subsId] != "" {
subsIdStr := strconv.Itoa(subsId)
Kevin Di Lallo
committed
jsonInfo, _ := rc.JSONGetEntry(baseKey+typeZonalSubscription+":"+subsIdStr, ".")
if jsonInfo != "" {
subscription := convertJsonToZonalSubscription(jsonInfo)
var zonal ZonalPresenceNotification
zonal.ZoneId = oldZoneId
zonal.CurrentAccessPointId = oldApId
zonal.Address = userId
event := new(UserEventType)
*event = LEAVING_EVENT
Simon Pastor
committed
zonal.UserEventType = event
seconds := time.Now().Unix()
var timestamp TimeStamp
timestamp.Seconds = int32(seconds)
zonal.Timestamp = ×tamp
var inlineZonal InlineZonalPresenceNotification
inlineZonal.ZonalPresenceNotification = &zonal
sendZonalPresenceNotification(subscription.CallbackReference.NotifyURL, inlineZonal)
log.Info("Zonal Notify Leaving event in zone " + oldZoneId + " for user " + userId)
}
}
}
}
}
}
func usersGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
Kevin Di Lallo
committed
var userData UeUserData
Kevin Di Lallo
committed
// Retrieve query parameters
u, _ := url.Parse(r.URL.String())
log.Info("url: ", u.RequestURI())
q := u.Query()
userData.queryZoneId = q["zoneId"]
userData.queryApId = q["accessPointId"]
userData.queryAddress = q["address"]
validQueryParams := []string{"zoneId", "accessPointId", "address"}
//look for all query parameters to reject if any invalid ones
found := false
for queryParam := range q {
found = false
for _, validQueryParam := range validQueryParams {
if queryParam == validQueryParam {
found = true
break
}
}
if !found {
log.Error("Query param not valid: ", queryParam)
w.WriteHeader(http.StatusBadRequest)
return
}
}
Kevin Di Lallo
committed
// Get user list from DB
userList.ResourceURL = hostUrl.String() + basePath + "queries/users"
response.UserList = &userList
Kevin Di Lallo
committed
userData.userList = &userList
Kevin Di Lallo
committed
keyName := baseKey + typeUser + ":*"
err := rc.ForEachJSONEntry(keyName, populateUserList, &userData)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Kevin Di Lallo
committed
// Send response
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
Kevin Di Lallo
committed
func populateUserList(key string, jsonInfo string, userData interface{}) error {
// Get query params & userlist from user data
data := userData.(*UeUserData)
if data == nil || data.userList == nil {
return errors.New("userList not found in userData")
}
Kevin Di Lallo
committed
// Retrieve user info from DB
var userInfo UserInfo
err := json.Unmarshal([]byte(jsonInfo), &userInfo)
if err != nil {
return err
}
Kevin Di Lallo
committed
// Ignore entries with no zoneID or AP ID
if userInfo.ZoneId == "" || userInfo.AccessPointId == "" {
return nil
Kevin Di Lallo
committed
//query parameters looked through using OR within same query parameter and AND between different query parameters
//example returning users matching zoneId : (zone01 OR zone02) AND accessPointId : (ap1 OR ap2 OR ap3) AND address: (ipAddress1 OR ipAddress2)
foundAMatch := false
Kevin Di Lallo
committed
// Filter using query params
if len(data.queryZoneId) > 0 {
foundAMatch = false
for _, queryZoneId := range data.queryZoneId {
if userInfo.ZoneId == queryZoneId {
foundAMatch = true
}
}
if !foundAMatch {
return nil
}
if len(data.queryApId) > 0 {
foundAMatch = false
for _, queryApId := range data.queryApId {
if userInfo.AccessPointId == queryApId {
foundAMatch = true
}
}
if !foundAMatch {
return nil
}
Kevin Di Lallo
committed
if len(data.queryAddress) > 0 {
foundAMatch = false
for _, queryAddress := range data.queryAddress {
if userInfo.Address == queryAddress {
foundAMatch = true
}
}
if !foundAMatch {
return nil
}
Kevin Di Lallo
committed
// Add user info to list
data.userList.User = append(data.userList.User, userInfo)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
Kevin Di Lallo
committed
var userData ApUserData
vars := mux.Vars(r)
// Retrieve query parameters
u, _ := url.Parse(r.URL.String())
log.Info("url: ", u.RequestURI())
q := u.Query()
Kevin Di Lallo
committed
userData.queryInterestRealm = q.Get("interestRealm")
validQueryParams := []string{"interestRealm"}
//look for all query parameters to reject if any invalid ones
found := false
for queryParam := range q {
found = false
for _, validQueryParam := range validQueryParams {
if queryParam == validQueryParam {
found = true
break
}
}
if !found {
log.Error("Query param not valid: ", queryParam)
w.WriteHeader(http.StatusBadRequest)
return
}
}
Kevin Di Lallo
committed
// Get user list from DB
var response InlineAccessPointList
var apList AccessPointList
apList.ZoneId = vars["zoneId"]
apList.ResourceURL = hostUrl.String() + basePath + "queries/zones/" + vars["zoneId"] + "/accessPoints"
Kevin Di Lallo
committed
response.AccessPointList = &apList
userData.apList = &apList
//make sure the zone exists first
jsonZoneInfo, _ := rc.JSONGetEntry(baseKey+typeZone+":"+vars["zoneId"], ".")
if jsonZoneInfo == "" {
w.WriteHeader(http.StatusNotFound)
return
}
Kevin Di Lallo
committed
keyName := baseKey + typeZone + ":" + vars["zoneId"] + ":*"
err := rc.ForEachJSONEntry(keyName, populateApList, &userData)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Kevin Di Lallo
committed
// Send response
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func apByIdGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
var response InlineAccessPointInfo
var apInfo AccessPointInfo
response.AccessPointInfo = &apInfo
Kevin Di Lallo
committed
jsonApInfo, _ := rc.JSONGetEntry(baseKey+typeZone+":"+vars["zoneId"]+":"+typeAccessPoint+":"+vars["accessPointId"], ".")
if jsonApInfo == "" {
w.WriteHeader(http.StatusNotFound)
return
}
err := json.Unmarshal([]byte(jsonApInfo), &apInfo)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func zonesGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
zoneList.ResourceURL = hostUrl.String() + basePath + "queries/zones"
response.ZoneList = &zoneList
Kevin Di Lallo
committed
keyName := baseKey + typeZone + ":*"
err := rc.ForEachJSONEntry(keyName, populateZoneList, &zoneList)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
func zonesByIdGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
var zoneInfo ZoneInfo
response.ZoneInfo = &zoneInfo
Kevin Di Lallo
committed
jsonZoneInfo, _ := rc.JSONGetEntry(baseKey+typeZone+":"+vars["zoneId"], ".")
if jsonZoneInfo == "" {
w.WriteHeader(http.StatusNotFound)
return
}
err := json.Unmarshal([]byte(jsonZoneInfo), &zoneInfo)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
Kevin Di Lallo
committed
func populateZoneList(key string, jsonInfo string, userData interface{}) error {
zoneList := userData.(*ZoneList)
var zoneInfo ZoneInfo
// Format response
err := json.Unmarshal([]byte(jsonInfo), &zoneInfo)
if err != nil {
return err
}
if zoneInfo.ZoneId != "" {
zoneList.Zone = append(zoneList.Zone, zoneInfo)
}
return nil
}
Kevin Di Lallo
committed
func populateApList(key string, jsonInfo string, userData interface{}) error {
// Get query params & aplist from user data
data := userData.(*ApUserData)
if data == nil || data.apList == nil {
return errors.New("apList not found in userData")
}
Kevin Di Lallo
committed
// Retrieve AP info from DB
var apInfo AccessPointInfo
err := json.Unmarshal([]byte(jsonInfo), &apInfo)
if err != nil {
return err
}
Kevin Di Lallo
committed
// Ignore entries with no AP ID
if apInfo.AccessPointId == "" {
return nil
}
// Filter using query params
if data.queryInterestRealm != "" && apInfo.InterestRealm != data.queryInterestRealm {
return nil
Kevin Di Lallo
committed
// Add AP info to list
data.apList.AccessPoint = append(data.apList.AccessPoint, apInfo)
func distanceSubDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
present, _ := rc.JSONGetEntry(baseKey+typeDistanceSubscription+":"+vars["subscriptionId"], ".")
if present == "" {
w.WriteHeader(http.StatusNotFound)
return
}
err := rc.JSONDelEntry(baseKey+typeDistanceSubscription+":"+vars["subscriptionId"], ".")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
func distanceSubListGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var response InlineNotificationSubscriptionList
var distanceSubList NotificationSubscriptionList
distanceSubList.ResourceURL = hostUrl.String() + basePath + "subscriptions/distance"
response.NotificationSubscriptionList = &distanceSubList
keyName := baseKey + typeDistanceSubscription + "*"
err := rc.ForEachJSONEntry(keyName, populateDistanceList, &distanceSubList)
Kevin Di Lallo
committed
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
var response InlineDistanceNotificationSubscription
var distanceSub DistanceNotificationSubscription
response.DistanceNotificationSubscription = &distanceSub
jsonDistanceSub, _ := rc.JSONGetEntry(baseKey+typeDistanceSubscription+":"+vars["subscriptionId"], ".")
if jsonDistanceSub == "" {
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
func distanceSubPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
decoder := json.NewDecoder(r.Body)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Error("Body not present")
http.Error(w, "Body not present", http.StatusBadRequest)
Simon Pastor
committed
//checking for mandatory properties
if distanceSub.CallbackReference == nil || distanceSub.CallbackReference.NotifyURL == "" {
log.Error("Mandatory CallbackReference parameter not present")
http.Error(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
Simon Pastor
committed
return
}
if distanceSub.Criteria == nil {
log.Error("Mandatory DistanceCriteria parameter not present")
http.Error(w, "Mandatory DistanceCriteria parameter not present", http.StatusBadRequest)
Simon Pastor
committed
return
}
if distanceSub.Frequency == 0 {
log.Error("Mandatory Frequency parameter not present")
http.Error(w, "Mandatory Frequency parameter not present", http.StatusBadRequest)
return
}
if distanceSub.MonitoredAddress == nil {
log.Error("Mandatory MonitoredAddress parameter not present")
http.Error(w, "Mandatory MonitoredAddress parameter not present", http.StatusBadRequest)
return
}
/*
if distanceSub.TrackingAccuracy == 0 {
log.Error("Mandatory TrackingAccuracy parameter not present")
http.Error(w, "Mandatory TrackingAccuracy parameter not present", http.StatusBadRequest)
return
}
*/
Simon Pastor
committed
newSubsId := nextDistanceSubscriptionIdAvailable
nextDistanceSubscriptionIdAvailable++
subsIdStr := strconv.Itoa(newSubsId)
distanceSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/distance/" + subsIdStr
_ = rc.JSONSetEntry(baseKey+typeDistanceSubscription+":"+subsIdStr, ".", convertDistanceSubscriptionToJson(distanceSub))
registerDistance(distanceSub, subsIdStr)
response.DistanceNotificationSubscription = distanceSub
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, string(jsonResponse))
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
decoder := json.NewDecoder(r.Body)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error("Body not present")
http.Error(w, "Body not present", http.StatusBadRequest)
Simon Pastor
committed
//checking for mandatory properties
if distanceSub.CallbackReference == nil || distanceSub.CallbackReference.NotifyURL == "" {
log.Error("Mandatory CallbackReference parameter not present")
http.Error(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
Simon Pastor
committed
return
}
if distanceSub.Criteria == nil {
log.Error("Mandatory DistanceCriteria parameter not present")
http.Error(w, "Mandatory DistanceCriteria parameter not present", http.StatusBadRequest)
Simon Pastor
committed
return
}
if distanceSub.Frequency == 0 {
log.Error("Mandatory Frequency parameter not present")
http.Error(w, "Mandatory Frequency parameter not present", http.StatusBadRequest)
return
}
if distanceSub.MonitoredAddress == nil {
log.Error("Mandatory MonitoredAddress parameter not present")
http.Error(w, "Mandatory MonitoredAddress parameter not present", http.StatusBadRequest)
return
}
/*
if distanceSub.TrackingAccuracy == 0 {
log.Error("Mandatory TrackingAccuracy parameter not present")
http.Error(w, "Mandatory TrackingAccuracy parameter not present", http.StatusBadRequest)
return
}
*/
if distanceSub.ResourceURL == "" {
log.Error("Mandatory ResourceURL parameter not present")
http.Error(w, "Mandatory ResourceURL parameter not present", http.StatusBadRequest)
subsIdParamStr := vars["subscriptionId"]
log.Error("SubscriptionId in endpoint and in body not matching")
http.Error(w, "SubscriptionId in endpoint and in body not matching", http.StatusBadRequest)
distanceSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/distance/" + subsIdStr
subsId, err := strconv.Atoi(subsIdStr)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNotFound)
return
}
_ = rc.JSONSetEntry(baseKey+typeDistanceSubscription+":"+subsIdStr, ".", convertDistanceSubscriptionToJson(distanceSub))
//store the dynamic states of the subscription
notifSent := distanceSubscriptionMap[subsId].NbNotificationsSent
deregisterDistance(subsIdStr)
registerDistance(distanceSub, subsIdStr)
jsonResponse, err := json.Marshal(response)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, string(jsonResponse))
func populateDistanceList(key string, jsonInfo string, userData interface{}) error {
distanceList := userData.(*NotificationSubscriptionList)
var distanceInfo DistanceNotificationSubscription
if err != nil {
return err
}
distanceList.DistanceNotificationSubscription = append(distanceList.DistanceNotificationSubscription, distanceInfo)
func areaCircleSubDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
present, _ := rc.JSONGetEntry(baseKey+typeAreaCircleSubscription+":"+vars["subscriptionId"], ".")
if present == "" {
w.WriteHeader(http.StatusNotFound)
return
}