Newer
Older
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
}
err := rc.JSONDelEntry(baseKey+typeAreaCircleSubscription+":"+vars["subscriptionId"], ".")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
func areaCircleSubListGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
var response InlineNotificationSubscriptionList
var areaCircleSubList NotificationSubscriptionList
areaCircleSubList.ResourceURL = hostUrl.String() + basePath + "subscriptions/area/circle"
response.NotificationSubscriptionList = &areaCircleSubList
keyName := baseKey + typeAreaCircleSubscription + "*"
err := rc.ForEachJSONEntry(keyName, populateAreaCircleList, &areaCircleSubList)
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))
}
func areaCircleSubGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
var response InlineCircleNotificationSubscription
var areaCircleSub CircleNotificationSubscription
response.CircleNotificationSubscription = &areaCircleSub
jsonAreaCircleSub, _ := rc.JSONGetEntry(baseKey+typeAreaCircleSubscription+":"+vars["subscriptionId"], ".")
if jsonAreaCircleSub == "" {
w.WriteHeader(http.StatusNotFound)
return
}
err := json.Unmarshal([]byte(jsonAreaCircleSub), &areaCircleSub)
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 areaCircleSubPost(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 areaCircleSub.CallbackReference == nil || areaCircleSub.CallbackReference.NotifyURL == "" {
log.Error("Mandatory CallbackReference parameter not present")
http.Error(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
Simon Pastor
committed
return
}
if areaCircleSub.Address == nil {
log.Error("Mandatory Address parameter not present")
http.Error(w, "Mandatory Address parameter not present", http.StatusBadRequest)
return
}
if areaCircleSub.Latitude == 0 {
log.Error("Mandatory Latitude parameter not present")
http.Error(w, "Mandatory Latitude parameter not present", http.StatusBadRequest)
return
}
if areaCircleSub.Longitude == 0 {
log.Error("Mandatory Longitude parameter not present")
http.Error(w, "Mandatory Longitude parameter not present", http.StatusBadRequest)
return
}
if areaCircleSub.Radius == 0 {
log.Error("Mandatory Radius parameter not present")
http.Error(w, "Mandatory Radius parameter not present", http.StatusBadRequest)
Simon Pastor
committed
return
}
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
if areaCircleSub.EnteringLeavingCriteria == nil {
log.Error("Mandatory EnteringLeavingCriteria parameter not present")
http.Error(w, "Mandatory EnteringLeavingCriteria parameter not present", http.StatusBadRequest)
return
} else {
switch *areaCircleSub.EnteringLeavingCriteria {
case ENTERING_CRITERIA, LEAVING_CRITERIA:
default:
log.Error("Invalid Mandatory EnteringLeavingCriteria parameter value")
http.Error(w, "Invalid Mandatory EnteringLeavingCriteria parameter value", http.StatusBadRequest)
return
}
}
if areaCircleSub.Frequency == 0 {
log.Error("Mandatory Frequency parameter not present")
http.Error(w, "Mandatory Frequency parameter not present", http.StatusBadRequest)
return
}
/*
if areaCircleSub.CheckImmediate == nil {
log.Error("Mandatory CheckImmediate parameter not present")
http.Error(w, "Mandatory CheckImmediate parameter not present", http.StatusBadRequest)
return
}
*/
/*
if areaCircleSub.TrackingAccuracy == 0 {
log.Error("Mandatory TrackingAccuracy parameter not present")
http.Error(w, "Mandatory TrackingAccuracy parameter not present", http.StatusBadRequest)
return
}
*/
Simon Pastor
committed
newSubsId := nextAreaCircleSubscriptionIdAvailable
nextAreaCircleSubscriptionIdAvailable++
subsIdStr := strconv.Itoa(newSubsId)
/*
if zonalTrafficSub.Duration > 0 {
//TODO start a timer mecanism and expire subscription
}
//else, lasts forever or until subscription is deleted
*/
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
if areaCircleSub.Duration != 0 { //used to be string -> zonalTrafficSub.Duration != "" && zonalTrafficSub.Duration != "0" {
//TODO start a timer mecanism and expire subscription
log.Info("Non zero duration")
}
//else, lasts forever or until subscription is deleted
areaCircleSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/area/circle/" + subsIdStr
_ = rc.JSONSetEntry(baseKey+typeAreaCircleSubscription+":"+subsIdStr, ".", convertAreaCircleSubscriptionToJson(areaCircleSub))
registerAreaCircle(areaCircleSub, subsIdStr)
response.CircleNotificationSubscription = areaCircleSub
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))
}
func areaCircleSubPut(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
vars := mux.Vars(r)
var response InlineCircleNotificationSubscription
var body InlineCircleNotificationSubscription
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&body)
if err != nil {
log.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
areaCircleSub := body.CircleNotificationSubscription
if areaCircleSub == nil {
log.Error("Body not present")
http.Error(w, "Body not present", http.StatusBadRequest)
return
}
//checking for mandatory properties
if areaCircleSub.CallbackReference == nil || areaCircleSub.CallbackReference.NotifyURL == "" {
log.Error("Mandatory CallbackReference parameter not present")
http.Error(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
return
}
if areaCircleSub.Address == nil {
log.Error("Mandatory Address parameter not present")
http.Error(w, "Mandatory Address parameter not present", http.StatusBadRequest)
return
}
if areaCircleSub.Latitude == 0 {
log.Error("Mandatory Latitude parameter not present")
http.Error(w, "Mandatory Latitude parameter not present", http.StatusBadRequest)
return
}
if areaCircleSub.Longitude == 0 {
log.Error("Mandatory Longitude parameter not present")
http.Error(w, "Mandatory Longitude parameter not present", http.StatusBadRequest)
return
}
if areaCircleSub.Radius == 0 {
log.Error("Mandatory Radius parameter not present")
http.Error(w, "Mandatory Radius parameter not present", http.StatusBadRequest)