device.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package device
  2. import (
  3. "confrontation-training/global"
  4. "confrontation-training/http"
  5. deviceModel "confrontation-training/models/gateway"
  6. "gorm.io/gorm"
  7. netHttp "net/http"
  8. )
  9. type DeviceService struct {
  10. }
  11. func (d *DeviceService) FindDeviceByMac(mac string) (deviceModel.DeviceInfo, int64) {
  12. info := deviceModel.DeviceInfo{}
  13. count := global.Db.Where(" mac = ?", mac).First(&info).RowsAffected
  14. return info, count
  15. }
  16. func (d *DeviceService) FindDeviceByType(deviceType string) ([]deviceModel.DeviceInfo, int64) {
  17. var deviceList = make([]deviceModel.DeviceInfo, 0)
  18. var count int64
  19. if deviceType == "" {
  20. count = global.Db.Find(&deviceList).RowsAffected
  21. } else {
  22. count = global.Db.Where("type = ?", deviceType).Find(&deviceList).RowsAffected
  23. }
  24. return deviceList, count
  25. }
  26. func (d *DeviceService) CreateDevice(param deviceModel.DeviceAddParam) *gorm.DB {
  27. addParam := deviceModel.DeviceInfo{
  28. Type: param.Type,
  29. Mac: param.Mac,
  30. AliasName: param.AliasName,
  31. }
  32. return global.Db.Create(&addParam)
  33. }
  34. func (d *DeviceService) RemoveDevice(mac string) int64 {
  35. deviceInfo, count := d.FindDeviceByMac(mac)
  36. if count > 0 {
  37. count = global.Db.Delete(&deviceModel.DeviceInfo{}, deviceInfo.ID).RowsAffected
  38. }
  39. return count
  40. }
  41. func (d *DeviceService) DeviceConnectedList(url string) (map[string]interface{}, error) {
  42. //httpResponse := http.GetReq(url)
  43. var result map[string]interface{}
  44. //if httpResponse.StatusCode != 200 {
  45. // fmt.Printf("%s:%s", errors.StartTransFailed, httpResponse.Body)
  46. // return result, nil
  47. //} else {
  48. // body, err := ioutil.ReadAll(httpResponse.Body)
  49. // if err == nil {
  50. // err = json.Unmarshal(body, &result)
  51. // return result, err
  52. // }
  53. // return result, err
  54. //}
  55. return result, nil
  56. }
  57. func (d *DeviceService) DisconnectDevice(url string) *netHttp.Response {
  58. return http.DeleteReq(url)
  59. }