trendit
This commit is contained in:
156
platformapi/trenditapi/trendit.go
Normal file
156
platformapi/trenditapi/trendit.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package trenditapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//增加打印机
|
||||
func (a *API) AddPrinter(sn, key, name string) error {
|
||||
params := []AddPrinterReq{{
|
||||
Sn: sn,
|
||||
Key: key,
|
||||
Name: name,
|
||||
}}
|
||||
resp := a.HttpPostJson("addPrinter", params)
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return fmt.Errorf("添加打印机错误: %v", resp.BaseRes.Message)
|
||||
}
|
||||
retVal, _ := resp.BaseRes.Data.(map[string]interface{})
|
||||
failMsg := ""
|
||||
if len(retVal["fail"].([]interface{})) > 0 {
|
||||
for _, v := range retVal["fail"].([]interface{}) {
|
||||
t := v.(FailItem)
|
||||
failMsg += t.Sn + ":" + t.Reason + " "
|
||||
}
|
||||
return fmt.Errorf("添加打印机错误: %v", failMsg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//修改打印机信息
|
||||
func (a *API) EditPrinter(sn, name string) (string, error) {
|
||||
resp := a.HttpPostJson("editPrinter", []EditPrinterReq{{
|
||||
Sn: sn,
|
||||
Name: name,
|
||||
},
|
||||
})
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return "", errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return "", fmt.Errorf("修改打印机信息错误:%v", resp.BaseRes.Message)
|
||||
}
|
||||
retVal, _ := resp.BaseRes.Data.(map[string]interface{})
|
||||
failMsg := ""
|
||||
if len(retVal["fail"].([]interface{})) > 0 {
|
||||
for _, v := range retVal["fail"].([]interface{}) {
|
||||
t := v.(FailItem)
|
||||
failMsg += t.Sn + ":" + t.Reason + " "
|
||||
}
|
||||
return fmt.Sprintf("修改打印机信息错误:%v", failMsg), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
//删除打印机
|
||||
func (a *API) DelPrinter(snList []string) error {
|
||||
resp := a.HttpPostJson("delPrinter", snList)
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return fmt.Errorf("删除打印机错误:%v", resp.BaseRes.Message)
|
||||
}
|
||||
retVal, _ := resp.BaseRes.Data.(map[string]interface{})
|
||||
failMsg := ""
|
||||
if len(retVal["fail"].([]interface{})) > 0 {
|
||||
for _, v := range retVal["fail"].([]interface{}) {
|
||||
t := v.(FailItem)
|
||||
failMsg += t.Sn + ":" + t.Reason + " "
|
||||
}
|
||||
return fmt.Errorf("删除打印机错误:%v", failMsg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//设置打印机浓度
|
||||
func (a *API) SetDensity(sn string, density int) error {
|
||||
resp := a.HttpPostJson("setDensity", SetDensityReq{
|
||||
Sn: sn,
|
||||
Density: density,
|
||||
})
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return fmt.Errorf("设置打印机浓度错误:%v", resp.BaseRes.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//设置音量
|
||||
func (a *API) SetVolume(sn string, volume int) error {
|
||||
resp := a.HttpPostJson("setVolume", SetVolumeReq{
|
||||
Sn: sn,
|
||||
Volume: volume,
|
||||
})
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return fmt.Errorf("设置打印机音量错误:%v", resp.BaseRes.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//查询打印机状态
|
||||
func (a *API) GetDeviceStatus(sn string) (float64, float64, error) {
|
||||
resp := a.HttpPostJson("getDeviceStatus", GetDeviceStatusReq{Sn: sn})
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return 0, 0, errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return 0, 0, fmt.Errorf("查询打印机状态错误:%v", resp.BaseRes.Message)
|
||||
}
|
||||
retVal, _ := resp.BaseRes.Data.(map[string]interface{})
|
||||
return retVal["onlineStatus"].(float64), retVal["workStatus"].(float64), nil
|
||||
}
|
||||
|
||||
//清空设备待打印队列
|
||||
func (a *API) CleanWaitingQueue(sn string) error {
|
||||
resp := a.HttpPostJson("cleanWaitingQueue", sn)
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return fmt.Errorf("清空设备待打印队列错误:%v", resp.BaseRes.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//打印小票
|
||||
func (a *API) Print(sn, content, voice string) (string, error) {
|
||||
resp := a.HttpPostJson("print", PrintReq{
|
||||
Sn: sn,
|
||||
Content: content,
|
||||
Voice: voice,
|
||||
VoicePlayTimes: 1,
|
||||
VoicePlayInterval: 3,
|
||||
Copies: 1,
|
||||
})
|
||||
if resp.HttpStatusCode != HttpStatusSuccessCode {
|
||||
return "", errors.New("HTTP请求错误,请检查重试")
|
||||
}
|
||||
if resp.BaseRes.Code != ResponseCodeSuccess {
|
||||
return "", fmt.Errorf("打印错误:%v", resp.BaseRes.Message)
|
||||
}
|
||||
retVal := resp.BaseRes.Data.(map[string]interface{})
|
||||
if retVal["printId"].(string) != "" {
|
||||
return retVal["printId"].(string), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
Reference in New Issue
Block a user