X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=r3-webstatus-spaceapi%2Fspaceapi%2Fspaceapi.go;fp=r3-webstatus-spaceapi%2Fspaceapi%2Fspaceapi.go;h=7e088198d16de5969215f3516508cc1d87dd72c2;hb=7aa8d6094a6e2e98a5e2ee1e9e69a58e42678ad6;hp=0000000000000000000000000000000000000000;hpb=c897b0e80868e68719d977b623ee24b98b121b0d;p=svn42.git diff --git a/r3-webstatus-spaceapi/spaceapi/spaceapi.go b/r3-webstatus-spaceapi/spaceapi/spaceapi.go new file mode 100644 index 0000000..7e08819 --- /dev/null +++ b/r3-webstatus-spaceapi/spaceapi/spaceapi.go @@ -0,0 +1,123 @@ +// spaceapi.go +package spaceapi + +import ( + "encoding/json" + "time" +) + +const max_num_events int = 4 + +type SpaceInfo map[string]interface{} + +func (nsi SpaceInfo) UpdateSensorData(what, where, value string) { + if nsi["sensors"] == nil { + sensorlist := make([]SpaceInfo, 1) + sensorlist[0] = SpaceInfo{what: SpaceInfo{where: value}} + nsi["sensors"] = sensorlist + } else { + sensorlist, ok := nsi["sensors"].([]SpaceInfo) //type assertion (panics if false) + if ok { + for _, sensor := range sensorlist { + if sensor[what] != nil { + sensorinfo, ok2 := sensor[what].(SpaceInfo) + if ok2 { + sensorinfo[where] = value + return + } else { + panic("Wrong Type of sensorinfo: Should never happen") + } + } + } + //else + nsi["sensors"] = append(sensorlist, SpaceInfo{what: SpaceInfo{where: value}}) + } else { + panic("Wrong Type of sensorlist: Should never happen") + } + } +} + +func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber string) SpaceInfo { + nsi["contact"] = SpaceInfo{ + "phone": phone, + "email": email, + "ml": ml, + "jabber": jabber} + return nsi +} + +func (nsi SpaceInfo) AddSpaceFeed(name, mimetype, url string) SpaceInfo { + newfeed := SpaceInfo{"name": name, "type": mimetype, "url": url} + if nsi["feeds"] == nil { + feedlist := make([]SpaceInfo, 1) + feedlist[0] = newfeed + nsi["feeds"] = feedlist + } else { + feedlist, ok := nsi["feeds"].([]SpaceInfo) //type assertion (panics if false) + if ok { + nsi["feeds"] = append(feedlist, newfeed) + } else { + panic("Wrong Type of feedlist: Should never happen") + } + } + return nsi +} + +func (nsi SpaceInfo) AddSpaceEvent(name, eventtype, extra string) SpaceInfo { + newevent := SpaceInfo{"name": name, "type": eventtype, "t": time.Now().Unix(), "extra": extra} + if nsi["events"] == nil { + eventlist := make([]SpaceInfo, 1) + eventlist[0] = newevent + nsi["events"] = eventlist + } else { + eventlist, ok := nsi["events"].([]SpaceInfo) //type assertion + if ok { + if len(eventlist) >= max_num_events { + eventlist = eventlist[1:] + } + nsi["events"] = append(eventlist, newevent) + } else { + panic("Wrong Type of eventlist: Should never happen") + } + } + return nsi +} + +func (nsi SpaceInfo) AddSpaceAddress(address string) SpaceInfo { + nsi["address"] = address + return nsi +} + +func (nsi SpaceInfo) AddSpaceLatLon(lat float64, lon float64) SpaceInfo { + nsi["lat"] = lat + nsi["lon"] = lon + return nsi +} + +func (nsi SpaceInfo) SetStatus(open bool, status string) { + nsi["status"] = status + nsi["open"] = open + nsi["lastchange"] = time.Now().Unix() +} + +func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string) SpaceInfo { + nsi := map[string]interface{}{ + "api": "0.13", + "space": space, + "url": url, + "logo": logo, + "open": false, + "lastchange": time.Now().Unix(), + "icon": map[string]interface{}{ + "open": open_icon, + "closed": closed_icon}} + return nsi +} + +func (data SpaceInfo) MakeJSON() ([]byte, error) { + msg, err := json.Marshal(data) + if err == nil { + return msg, nil + } + return nil, err +}