123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package device
- import (
- errors "confrontation-training/err"
- "confrontation-training/global"
- "confrontation-training/http"
- deviceModel "confrontation-training/models/gateway"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "io/ioutil"
- netHttp "net/http"
- )
- type DeviceService struct {
- }
- func (d *DeviceService) FindDeviceByMac(mac string) (deviceModel.DeviceInfo, int64) {
- info := deviceModel.DeviceInfo{}
- count := global.Db.Where(" mac = ?", mac).First(&info).RowsAffected
- return info, count
- }
- func (d *DeviceService) FindDeviceByType(deviceType string) ([]deviceModel.DeviceInfo, int64) {
- var deviceList = make([]deviceModel.DeviceInfo, 0)
- count := global.Db.Where("type = ?", deviceType).Find(&deviceList).RowsAffected
- return deviceList, count
- }
- func (d *DeviceService) CreateDevice(param deviceModel.DeviceAddParam) *gorm.DB {
- addParam := deviceModel.DeviceInfo{
- Type: param.Type,
- Mac: param.Mac,
- }
- return global.Db.Create(&addParam)
- }
- func (d *DeviceService) RemoveDevice(mac string) int64 {
- deviceInfo, count := d.FindDeviceByMac(mac)
- if count > 0 {
- count = global.Db.Delete(&deviceModel.DeviceInfo{}, deviceInfo.ID).RowsAffected
- }
- return count
- }
- func (d *DeviceService) DeviceConnectedList(url string) (map[string]interface{}, error) {
- httpResponse := http.GetReq(url)
- var result map[string]interface{}
- if httpResponse.StatusCode != 200 {
- fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
- return result, nil
- } else {
- body, err := ioutil.ReadAll(httpResponse.Body)
- if err == nil {
- err = json.Unmarshal(body, &result)
- return result, err
- }
- return result, err
- }
- }
- func (d *DeviceService) DisconnectDevice(url string) *netHttp.Response {
- return http.DeleteReq(url)
- }
|