| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package iotDownService
- import (
- "IotAdmin/core/sdk/config"
- "IotAdmin/core/tools/writer"
- iotProtocol "IotAdmin/iot/protocol"
- iotElProtocol "IotAdmin/iot/protocol/electric"
- iotStruct "IotAdmin/iot/struct"
- "encoding/json"
- "log"
- "strings"
- "time"
- )
- func init() {
- mp = make(map[string]*writer.FileWriter)
- }
- func reportData(dataArray *[]*iotStruct.CollectData) {
- if dataArray == nil || len(*dataArray) == 0 {
- return
- }
- var reportHandler iotProtocol.ReportHandler
- for _, data := range *dataArray {
- cfg := data.SlaveConfig
- if cfg == nil {
- continue
- }
- for _, rc := range *cfg.ReportConfig {
- switch rc.Protocol {
- case iotProtocol.PlatElHj212:
- reportHandler = iotElProtocol.NewElHj212ReportHandler()
- default:
- }
- go func(d *iotStruct.CollectData, cfg *iotStruct.ReportConfig) {
- reportHandler.Adapter(d)
- str := reportHandler.Report(d, cfg)
- go logReport(d, cfg, str)
- }(data, &rc)
- }
- }
- }
- var mp map[string]*writer.FileWriter
- func logReport(data *iotStruct.CollectData, cfg *iotStruct.ReportConfig, dataStr *string) {
- output := getWrite(data)
- logData := &iotStruct.LogData{
- Time: time.Now().Format("20060102150400"),
- No: data.SlaveConfig.No,
- Data: *data,
- ReportHost: cfg.Host,
- ReportProtocol: cfg.Protocol,
- ReportMn: cfg.MN,
- ReportUser: cfg.User,
- ReportPwd: cfg.Pwd,
- ReportStr: *dataStr,
- }
- str, err := json.Marshal(logData)
- if err != nil {
- log.Printf("Report Log Marshal error: %s \r\n", err.Error())
- return
- }
- _, err = output.Write(str)
- if err != nil {
- log.Printf("Report Log Write error: %s \r\n", err.Error())
- return
- }
- _, err = output.Write([]byte("\r\n"))
- }
- func getWrite(data *iotStruct.CollectData) *writer.FileWriter {
- filePath := config.ApplicationConfig.DataLogFile
- if filePath == "" {
- filePath = "./_data/iot/"
- } else if filePath[len(filePath)-1] != '/' {
- filePath += "/"
- }
- filePath += strings.ReplaceAll(data.SlaveConfig.No, "_", "/")
- var output *writer.FileWriter
- var ok bool
- if output, ok = mp[filePath]; !ok {
- var err error
- output, err = writer.NewFileWriter(
- writer.WithPath(filePath),
- // 不切割文件
- writer.WithCap(0),
- writer.WithSuffix(".data"),
- )
- if err != nil {
- log.Printf("Report Log setup error: %s \r\n", err.Error())
- }
- mp[filePath] = output
- }
- return output
- }
|