// writePump pumps messages from the hub to the websocket connection. func (c *Conn) writePump() { ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() c.ws.Close() }() for { select { case message, ok := <-c.send: if !ok { c.write(websocket.CloseMessage, []byte{}) return } if err := c.write(websocket.TextMessage, message); err != nil { say.Fatalf("writePump: could not write text message %s with error: %s", string(message), err.Error()) return } case <-ticker.C: if err := c.write(websocket.PingMessage, []byte{}); err != nil { say.Fatalf("writePump: could not write ping message with error: %s", err.Error()) return } } } }
func (c *Conn) postCommand(command map[string]interface{}) { b, err := json.Marshal(command) if err != nil { say.Fatalf("postCommand: Could not generate JSON from %+v with error %s", command, err) } c.send <- b }
func main() { say.SetLevelWithConfName("info") configFile, config, err := conf.FromArgs() switch { case err != nil: say.Fatalf("Cannot read configuration in `%s` with error: %s", configFile, err.Error()) case configFile == "": say.Infof("You should specify configuration file. Starting with test configuration: %+v", config) default: say.Infof("Starting DeviceHive gateway with configuration in '%s': %+v", configFile, config) } say.SetLevelWithConfName(config.LoggingLevel) bus, err := dbus.SystemBus() if err != nil { say.Infof("Cannot get system bus with error: %s", err.Error()) say.Infof("Trying to use session bus for testing purposes...") if bus, err = dbus.SessionBus(); err != nil { say.Fatalf("Cannot get session bus with error: %s\n", err.Error()) return } } reply, err := bus.RequestName(DBusConnName, dbus.NameFlagDoNotQueue) switch { case err != nil: say.Fatalf("Cannot request name '%s' with error: %s\n", DBusConnName, err.Error()) case reply != dbus.RequestNameReplyPrimaryOwner: say.Fatalf("The name '%s' already taken.", DBusConnName) } if config.DeviceNotificationReceive == conf.DeviceNotificationReceiveByWS { say.Infof("Starting as websocket...") wsImplementation(bus, config) return } if config.DeviceNotificationReceive == conf.DeviceNotificationReceiveByREST { say.Infof("Starting as rest...") restImplementation(bus, config) return } }
func wsImplementation(bus *dbus.Conn, config conf.Conf) { var conn *ws.Conn for { info, err := rest.GetApiInfo(config.URL) if err == nil { say.Verbosef("API info: %+v", info) c := ws.New(info.WebSocketServerUrl, config.DeviceID, config.SendNotificationQueueCapacity, func(m map[string]interface{}) { p := m["parameters"] params := "" if p != nil { b, err := json.Marshal(p) if err != nil { say.Fatalf("Could not generete JSON from command %+v\nWith error %s", m, err.Error()) } params = string(b) } say.Verbosef("COMMAND %s -> %s(%v)", info.WebSocketServerUrl, m["command"].(string), params) bus.Emit("/com/devicehive/cloud", "com.devicehive.cloud.CommandReceived", uint32(m["id"].(float64)), m["command"].(string), params) }) conn = &c if err == nil { break } } say.Infof("API info error: %s", err.Error()) time.Sleep(5 * time.Second) } w := NewDbusObjectWrapper(conn) go conn.Run(config.AccessKey, func() { conn.RegisterDevice(config.DeviceID, config.DeviceName, config.DeviceKey, config.NetworkName, config.NetworkKey, config.NetworkDesc) conn.Authenticate(config.DeviceID, config.DeviceKey) conn.SubscribeCommands() }) bus.Export(w, "/com/devicehive/cloud", DBusConnName) // Introspectable n := &introspect.Node{ Name: "/com/devicehive/cloud", Interfaces: []introspect.Interface{ introspect.IntrospectData, prop.IntrospectData, { Name: "com.devicehive.cloud", Methods: introspect.Methods(w), }, }, } bus.Export(introspect.NewIntrospectable(n), "/com/devicehive/cloud", "org.freedesktop.DBus.Introspectable") root := &introspect.Node{ Children: []introspect.Node{ { Name: "com/devicehive/cloud", }, }, } bus.Export(introspect.NewIntrospectable(root), "/", "org.freedesktop.DBus.Introspectable") select {} }