gosdk/device/ljt_voice.go

49 lines
1.0 KiB
Go

package device
import (
"fmt"
"git.u8t.cn/open/goutil"
mqtt "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
type LJTVoice struct {
deviceId string
client mqtt.Client
callback Callback
brand string
model string
}
func NewLJTVoice(deviceId string, client mqtt.Client, callback Callback) Voice {
return &LJTVoice{
deviceId: deviceId,
client: client,
callback: callback,
brand: "ljt",
model: "zkb01",
}
}
func (v *LJTVoice) KeepAlive() error {
return nil
}
func (v *LJTVoice) Operate(string) error {
return nil
}
func (v *LJTVoice) Play(content string) error {
volume := cast.ToInt(100)
params := make(map[string]interface{})
params["action"] = "PlayTTS"
params["content"] = content
params["vol"] = int(goutil.If(volume > 100 || volume < 0, 50, volume) / 10)
topic := fmt.Sprintf("cmd/%s/%s/%s", v.brand, v.model, v.deviceId)
v.client.Publish(topic, 2, false, goutil.EncodeJSON(params))
log.Infof("LJT topic[%s] message[%s]", topic, goutil.EncodeJSON(params))
return nil
}