1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package device
- import (
- "confrontation-training/global"
- "confrontation-training/http"
- deviceModel "confrontation-training/models/gateway"
- "gorm.io/gorm"
- 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)
- var count int64
- if deviceType == "" {
- count = global.Db.Find(&deviceList).RowsAffected
- } else {
- 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,
- AliasName: param.AliasName,
- }
- 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
- //}
- return result, nil
- }
- func (d *DeviceService) DisconnectDevice(url string) *netHttp.Response {
- return http.DeleteReq(url)
- }
|