作者 yangfu

feat:设备数据建模

@@ -2,6 +2,7 @@ package main @@ -2,6 +2,7 @@ package main
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/mqtt"
5 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/task" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/task"
6 7
7 "github.com/beego/beego/v2/server/web" 8 "github.com/beego/beego/v2/server/web"
@@ -9,7 +10,6 @@ import ( @@ -9,7 +10,6 @@ import (
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
12 - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/mqtt"  
13 13
14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/crontab" 14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/crontab"
15 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 15 _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CommonStatisticsQuery struct {
  12 + Action string `cname:"查询类别" json:"actionType" valid:"Required"`
  13 + QueryOptions map[string]interface{} `json:"queryOptions"`
  14 +}
  15 +
  16 +func (checkUndertakerQuery *CommonStatisticsQuery) Valid(validation *validation.Validation) {
  17 + //validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (checkUndertakerQuery *CommonStatisticsQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(checkUndertakerQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(checkUndertakerQuery).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package service
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/core/application"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/statistics/query"
  7 +)
  8 +
  9 +// CommonStatisticsService 通用的统计服务
  10 +type CommonStatisticsService struct {
  11 +}
  12 +
  13 +// CommonStatisticsService 通用的统计服务
  14 +func (svr *CommonStatisticsService) CommonStatisticsService(contractStatisticsQuery *query.CommonStatisticsQuery) (interface{}, error) {
  15 + if err := contractStatisticsQuery.ValidateQuery(); err != nil {
  16 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  17 + }
  18 + var err error
  19 + transactionContext, err := factory.CreateTransactionContext(nil)
  20 + if err != nil {
  21 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  22 + }
  23 + if err := transactionContext.StartTransaction(); err != nil {
  24 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  25 + }
  26 + defer func() {
  27 + _ = transactionContext.RollbackTransaction()
  28 + }()
  29 +
  30 + //statisticsService, err := factory.CreateCooperationStatisticsService(map[string]interface{}{
  31 + // "transactionContext": transactionContext,
  32 + //})
  33 + //var res interface{}
  34 + //switch contractStatisticsQuery.Action {
  35 + //case domain_service.SearchContractDividends:
  36 + // res, err = statisticsService.SearchContractDividends(contractStatisticsQuery.QueryOptions)
  37 + //case domain_service.GetContractDividends:
  38 + // res, err = statisticsService.GetContractDividends(contractStatisticsQuery.QueryOptions)
  39 + //case domain_service.CooperationGoodsStatistics:
  40 + // res, err = statisticsService.CooperationGoodsStatistics(contractStatisticsQuery.QueryOptions)
  41 + //case domain_service.CooperationModeStatistics:
  42 + // res, err = statisticsService.CooperationModeStatistics(contractStatisticsQuery.QueryOptions)
  43 + //case domain_service.CompanyDividendsStatistics:
  44 + // res, err = statisticsService.CompanyDividendsStatistics(contractStatisticsQuery.QueryOptions)
  45 + //case domain_service.CompanyCooperationUsersStatistics:
  46 + // res, err = statisticsService.CompanyCooperationUsersStatistics(contractStatisticsQuery.QueryOptions)
  47 + //case domain_service.CompanyPaymentHistoryStatistics:
  48 + // res, err = statisticsService.CompanyPaymentHistoryStatistics(contractStatisticsQuery.QueryOptions)
  49 + //case domain_service.CompanyCooperationProjectContracts:
  50 + // res, err = statisticsService.CompanyCooperationProjectContracts(contractStatisticsQuery.QueryOptions)
  51 + //case domain_service.PaymentHistoryHistogramStatistics:
  52 + // res, err = statisticsService.PaymentHistoryHistogramStatistics(contractStatisticsQuery.QueryOptions)
  53 + //case domain_service.CooperationUserModeStatistics:
  54 + // res, err = statisticsService.CooperationUserModeStatistics(contractStatisticsQuery.QueryOptions)
  55 + //case domain_service.DividendsStatistics:
  56 + // res, err = statisticsService.DividendsStatistics(contractStatisticsQuery.QueryOptions)
  57 + //case domain_service.SearchDividendsEstimates:
  58 + // res, err = statisticsService.SearchDividendsEstimates(contractStatisticsQuery.QueryOptions)
  59 + //case domain_service.CooperationCompanyStatistics:
  60 + // res, err = statisticsService.CooperationCompanyStatistics(contractStatisticsQuery.QueryOptions)
  61 + //case domain_service.PersonCooperationContractStatistics:
  62 + // res, err = statisticsService.PersonCooperationContractStatistics(contractStatisticsQuery.QueryOptions)
  63 + //case domain_service.PersonCompanyPaymentHistoryStatistics:
  64 + // res, err = statisticsService.PersonCompanyPaymentHistoryStatistics(contractStatisticsQuery.QueryOptions)
  65 + //case domain_service.PersonCooperationProjectSharedInfo:
  66 + // res, err = statisticsService.PersonCooperationProjectSharedInfo(contractStatisticsQuery.QueryOptions)
  67 + //case domain_service.PersonCooperationProjectSharedInfoAttachment:
  68 + // res, err = statisticsService.PersonCooperationProjectSharedInfoAttachment(contractStatisticsQuery.QueryOptions)
  69 + //case domain_service.PersonCooperationCompany:
  70 + // res, err = statisticsService.PersonCooperationCompany(contractStatisticsQuery.QueryOptions)
  71 + //case domain_service.CreditAccountStatistics:
  72 + // res, err = statisticsService.CreditAccountStatistics(contractStatisticsQuery.QueryOptions)
  73 + //case domain_service.RelevantCooperationContractNumbers:
  74 + // res, err = statisticsService.RelevantCooperationContractNumbers(contractStatisticsQuery.QueryOptions)
  75 + //}
  76 + if err != nil {
  77 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  78 + }
  79 + if err := transactionContext.CommitTransaction(); err != nil {
  80 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  81 + }
  82 + return struct {
  83 + }{}, nil
  84 +}
@@ -3,6 +3,7 @@ package constant @@ -3,6 +3,7 @@ package constant
3 import "os" 3 import "os"
4 4
5 var MQTT_TOPIC = "/MQTT" 5 var MQTT_TOPIC = "/MQTT"
  6 +
6 //设备商提供的测试地址 7 //设备商提供的测试地址
7 //var MQTT_HOST = "175.24.122.87" 8 //var MQTT_HOST = "175.24.122.87"
8 //var MQTT_PORT = "1883" 9 //var MQTT_PORT = "1883"
@@ -14,8 +15,7 @@ var MQTT_PORT = "1883" @@ -14,8 +15,7 @@ var MQTT_PORT = "1883"
14 var MQTT_USER = "admin" 15 var MQTT_USER = "admin"
15 var MQTT_PASSWORD = "123456" 16 var MQTT_PASSWORD = "123456"
16 17
17 -  
18 -func init(){ 18 +func init() {
19 if os.Getenv("MQTT_HOST") != "" { 19 if os.Getenv("MQTT_HOST") != "" {
20 MQTT_HOST = os.Getenv("MQTT_HOST") 20 MQTT_HOST = os.Getenv("MQTT_HOST")
21 } 21 }
@@ -28,4 +28,4 @@ func init(){ @@ -28,4 +28,4 @@ func init(){
28 if os.Getenv("MQTT_PASSWORD") != "" { 28 if os.Getenv("MQTT_PASSWORD") != "" {
29 MQTT_PASSWORD = os.Getenv("MQTT_PASSWORD") 29 MQTT_PASSWORD = os.Getenv("MQTT_PASSWORD")
30 } 30 }
31 -}  
  31 +}
@@ -7,7 +7,7 @@ type DeviceRunningData struct { @@ -7,7 +7,7 @@ type DeviceRunningData struct {
7 // 通讯状态:1:通讯正常,0:设备未上电或与采集端通讯故障 7 // 通讯状态:1:通讯正常,0:设备未上电或与采集端通讯故障
8 ComStatus int `json:"comStatus"` 8 ComStatus int `json:"comStatus"`
9 // 匹配数目 9 // 匹配数目
10 - Count int `json:"count,string"` 10 + Count int `json:"count"`
11 // 炸机前段温度:炸机前段当前温度 YZJ1 油炸机 11 // 炸机前段温度:炸机前段当前温度 YZJ1 油炸机
12 FrontTemp float64 `json:"frontTemp"` 12 FrontTemp float64 `json:"frontTemp"`
13 // 炸机前段温度:炸机前段当前温度 YZJ2 油炸机 13 // 炸机前段温度:炸机前段当前温度 YZJ2 油炸机
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
  7 +)
  8 +
  9 +const (
  10 + // 时段产能统计
  11 + HourProductiveStatistics = "HourProductiveStatistics"
  12 +)
  13 +
  14 +type PGCommonStatisticsService struct {
  15 + transactionContext *pgTransaction.TransactionContext
  16 +}
  17 +
  18 +func (ptr *PGCommonStatisticsService) CommonStatistics(actionType, queryOptions map[string]interface{}) (interface{}, error) {
  19 + switch actionType {
  20 +
  21 + }
  22 + return nil, nil
  23 +}
  24 +
  25 +// 时段产能-统计 (传串设备)
  26 +func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[string]interface{}) (interface{}, error) {
  27 + var request = &HourProductiveStatisticsRequest{}
  28 + if err := utils.LoadQueryObject(queryOptions, &request); err != nil {
  29 + return nil, err
  30 + }
  31 + return nil, nil
  32 +}
  33 +
  34 +type HourProductiveStatisticsRequest struct {
  35 + CompanyId int `json:"companyId" valid:"Required"`
  36 + OrgId int `json:"orgId" valid:"Required"`
  37 + WorkshopId int `json:"workshopId" valid:"Required"`
  38 +}
  39 +
  40 +func NewPGCommonStatisticsService(transactionContext *pgTransaction.TransactionContext) (*PGCommonStatisticsService, error) {
  41 + if transactionContext == nil {
  42 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  43 + } else {
  44 + return &PGCommonStatisticsService{
  45 + transactionContext: transactionContext,
  46 + }, nil
  47 + }
  48 +}
@@ -62,7 +62,7 @@ func SendWorkshopWorkTimeStaticJob(productRecord *domain.ProductAttendanceRecord @@ -62,7 +62,7 @@ func SendWorkshopWorkTimeStaticJob(productRecord *domain.ProductAttendanceRecord
62 return SendAsyncJob(domain.TaskKeyWorkshopWorkTimeRecordStatics(), productRecord) 62 return SendAsyncJob(domain.TaskKeyWorkshopWorkTimeRecordStatics(), productRecord)
63 } 63 }
64 64
65 -func SenDeviceZkTecoReportJob(productRecord *domain.DeviceZkTeco) error { 65 +func SendDeviceZkTecoReportJob(productRecord *domain.DeviceZkTeco) error {
66 return SendAsyncJob(domain.TaskDeviceZkTecoReport(), productRecord) 66 return SendAsyncJob(domain.TaskDeviceZkTecoReport(), productRecord)
67 } 67 }
68 68
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "fmt" 4 "fmt"
5 pahomqtt "github.com/eclipse/paho.mqtt.golang" 5 pahomqtt "github.com/eclipse/paho.mqtt.golang"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
7 "time" 8 "time"
8 ) 9 )
9 10
@@ -26,21 +27,26 @@ func (subscribeClient *SubscribeClient) options() *pahomqtt.ClientOptions { @@ -26,21 +27,26 @@ func (subscribeClient *SubscribeClient) options() *pahomqtt.ClientOptions {
26 opts.SetKeepAlive(2 * time.Second) 27 opts.SetKeepAlive(2 * time.Second)
27 opts.SetPingTimeout(1 * time.Second) 28 opts.SetPingTimeout(1 * time.Second)
28 opts.CleanSession = false 29 opts.CleanSession = false
29 - opts.SetClientID("test") 30 + //opts.SetClientID("test")
30 //opts.Order = true 31 //opts.Order = true
31 return opts 32 return opts
32 } 33 }
33 34
34 -func (subscribeClient *SubscribeClient) Connect() *SubscribeClient{ 35 +func (subscribeClient *SubscribeClient) Connect() *SubscribeClient {
35 opts := subscribeClient.options() 36 opts := subscribeClient.options()
36 fmt.Println("start connect......") 37 fmt.Println("start connect......")
37 opts.OnConnectionLost = func(c pahomqtt.Client, err error) { 38 opts.OnConnectionLost = func(c pahomqtt.Client, err error) {
  39 + defer func() {
  40 + if r := recover(); r != nil {
  41 + log.Logger.Error(fmt.Sprintf("%v", r))
  42 + }
  43 + }()
38 fmt.Println("Connect error:", err) 44 fmt.Println("Connect error:", err)
39 for { 45 for {
40 fmt.Println("reconnect server") 46 fmt.Println("reconnect server")
41 token := subscribeClient.client.Connect() 47 token := subscribeClient.client.Connect()
42 token.Wait() 48 token.Wait()
43 - fmt.Println("server Connect status:",subscribeClient.client.IsConnectionOpen()) 49 + fmt.Println("server Connect status:", subscribeClient.client.IsConnectionOpen())
44 if subscribeClient.client.IsConnectionOpen() { 50 if subscribeClient.client.IsConnectionOpen() {
45 break 51 break
46 } 52 }
@@ -48,7 +54,7 @@ func (subscribeClient *SubscribeClient) Connect() *SubscribeClient{ @@ -48,7 +54,7 @@ func (subscribeClient *SubscribeClient) Connect() *SubscribeClient{
48 } 54 }
49 } 55 }
50 opts.OnConnect = func(c pahomqtt.Client) { 56 opts.OnConnect = func(c pahomqtt.Client) {
51 - c.Subscribe(subscribeClient.topic,0,subscribeClient.handler) 57 + c.Subscribe(subscribeClient.topic, 0, subscribeClient.handler)
52 } 58 }
53 subscribeClient.client = pahomqtt.NewClient(opts) 59 subscribeClient.client = pahomqtt.NewClient(opts)
54 token := subscribeClient.client.Connect() 60 token := subscribeClient.client.Connect()
@@ -56,21 +62,21 @@ func (subscribeClient *SubscribeClient) Connect() *SubscribeClient{ @@ -56,21 +62,21 @@ func (subscribeClient *SubscribeClient) Connect() *SubscribeClient{
56 return subscribeClient 62 return subscribeClient
57 } 63 }
58 64
59 -func (subscribeClient *SubscribeClient) Subscribe(topic string, messageHandler pahomqtt.MessageHandler){ 65 +func (subscribeClient *SubscribeClient) Subscribe(topic string, messageHandler pahomqtt.MessageHandler) {
60 subscribeClient.topic = topic 66 subscribeClient.topic = topic
61 subscribeClient.handler = messageHandler 67 subscribeClient.handler = messageHandler
62 - token := subscribeClient.client.Subscribe(topic,0,messageHandler) 68 + token := subscribeClient.client.Subscribe(topic, 0, messageHandler)
63 token.Wait() 69 token.Wait()
64 token.Done() 70 token.Done()
65 } 71 }
66 72
67 -func StartSubscribe(topic string,handler MessageHandler){ 73 +func StartSubscribe(topic string, handler MessageHandler) {
68 defer func() { 74 defer func() {
69 - if err := recover();err != nil { 75 + if err := recover(); err != nil {
70 fmt.Println(err) 76 fmt.Println(err)
71 - StartSubscribe(topic,handler) 77 + StartSubscribe(topic, handler)
72 } 78 }
73 }() 79 }()
74 fmt.Println("start subscribe...") 80 fmt.Println("start subscribe...")
75 - NewSubscribeClient().Connect().Subscribe(topic,pahomqtt.MessageHandler(handler))  
76 -}  
  81 + NewSubscribeClient().Connect().Subscribe(topic, pahomqtt.MessageHandler(handler))
  82 +}
@@ -28,7 +28,7 @@ func (controller *DeviceZKTecoController) PostCdata() { @@ -28,7 +28,7 @@ func (controller *DeviceZKTecoController) PostCdata() {
28 data.ActionTime = mTime 28 data.ActionTime = mTime
29 //mBytes, _ := json.Marshal(data) 29 //mBytes, _ := json.Marshal(data)
30 //redis.GetRedis().LPush(domain.TaskDeviceZkTecoReport(), mBytes) 30 //redis.GetRedis().LPush(domain.TaskDeviceZkTecoReport(), mBytes)
31 - domainService.SenDeviceZkTecoReportJob(data) 31 + domainService.SendDeviceZkTecoReportJob(data)
32 } 32 }
33 } 33 }
34 controller.Response(data, nil) 34 controller.Response(data, nil)