// spaceapi.go package spaceapi import ( "encoding/json" "time" ) const max_num_events int = 4 type SpaceInfo map[string]interface{} type SpaceTempSensor struct { value string unit string location string name string description string } type SpaceLightSensor struct { value string //~ unit string location string name string description string } type SpacePowerConsumptionSensor struct { value string unit string location string name string description string } type SpaceNetworkConnectionsSensor struct { value string nettype string machines string location string name string description string } type SpaceMemberCountSensor struct { value string location string name string description string } type SpaceDoorLockSensor struct { value bool location string name string description string } type SpaceDoorAjarSensor struct { value bool location string name string description string } func (nsi SpaceTempSensor) MakeTempSensor(what, name, where, value, unit string) { } func (nsi SpaceInfo) MergeInSensorData(sensortype interface{}) { //todo check if what equals either "temperature", "door_locked", "barometer", "humidity", "beverage_supply", "power_consumption", "wind", "network_connections", "account_balance", "total_member_count", "people_now_present" or starts with "ext_". Else prepend "ext_" var what string switch sensortype := sensortype.(type) { case SpaceTempSensor: what = "temperature" case SpaceLightSensor: what = "ext_illumination" case SpacePowerConsumptionSensor: what = "power_consumption" case SpaceNetworkConnectionsSensor: what = "network_connections" case SpaceMemberCountSensor: what = "total_member_count" case SpaceDoorLockSensor: what = "door_locked" case SpaceDoorAjarSensor: what = "ext_door_ajar" default: panic("Unknown Sensor Type") } if nsi["sensors"] == nil { listofwhats := make([]SpaceInfo, 1) listofwhats[0] = sensortype.(SpaceInfo) sensorobj := SpaceInfo{what: listofwhats} nsi["sensors"] = sensorobj } else { sensorobj, ok := nsi["sensors"].(SpaceInfo) //type assertion (panics if false) if ok { if sensorobj[what] != nil { for _, sensor := range sensorobj { //~ 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 sensorobj[what] = make([]SpaceInfo, 1) sensorobj[what].([]SpaceInfo)[0] = sensortype.(SpaceInfo) } else { panic("Wrong Type of sensorobj: Should never happen") } } } func (nsi SpaceInfo) AddSpaceContactInfo(phone, irc, email, ml, jabber, issuemail string) SpaceInfo { nsi["contact"] = SpaceInfo{ "phone": phone, "email": email, "ml": ml, "jabber": jabber, "issue_mail": issuemail} nsi["issue_report_channels"] = [3]string{"issue_mail","email","ml"} return nsi } func (nsi SpaceInfo) AddSpaceFeed(feedtype, mimetype, url string) SpaceInfo { newfeed := SpaceInfo{"type": mimetype, "url": url} if nsi["feeds"] == nil { nsi["feeds"] = SpaceInfo{feedtype: newfeed} } else { feedobj, ok := nsi["feeds"].(SpaceInfo) //type assertion (panics if false) if ok { feedobj[feedtype] = newfeed } else { panic("Wrong Type of feedobj: Should never happen") } } return nsi } func (nsi SpaceInfo) AddSpaceEvent(name, eventtype, extra string) SpaceInfo { newevent := SpaceInfo{"name": name, "type": eventtype, "timestamp": 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 if nsi["location"] != nil { location, ok := nsi["location"].(SpaceInfo) if ok { location["address"] = address } } return nsi } func (nsi SpaceInfo) SetStatus(open bool, status string) { nsi["status"] = status nsi["open"] = open nsi["lastchange"] = time.Now().Unix() state, ok := nsi["state"].(SpaceInfo) if ok { state["message"] = status state["open"] = open state["lastchange"] = nsi["lastchange"] } } func NewSpaceInfo(space string, url string, logo string, open_icon string, closed_icon string, lat float64, lon float64) SpaceInfo { nsi := map[string]interface{}{ "api": "0.13", "space": space, "url": url, "logo": logo, "open": false, "lastchange": time.Now().Unix(), "icon": SpaceInfo{ "open": open_icon, "closed": closed_icon, }, "state": SpaceInfo{ "open": false, "lastchange":time.Now().Unix(), "icon": SpaceInfo{ "open": open_icon, "closed": closed_icon}, }, "location": SpaceInfo{ "lat": lat, "lon": lon}, "contact" : SpaceInfo {}, } return nsi } func (data SpaceInfo) MakeJSON() ([]byte, error) { msg, err := json.Marshal(data) if err == nil { return msg, nil } return nil, err }