Skip to content

Commit

Permalink
refactor(plc4go): avoid panics if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed May 2, 2023
1 parent ab8bfd8 commit 7cc564f
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 164 deletions.
5 changes: 3 additions & 2 deletions plc4go/internal/ads/Discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"encoding/binary"
"fmt"
"github.com/pkg/errors"
"net"
"net/url"
"strconv"
Expand Down Expand Up @@ -131,11 +132,11 @@ func (d *Discoverer) Discover(ctx context.Context, callback func(event apiModel.
for _, discoveryItem := range discoveryItems {
responseAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", discoveryItem.localAddress, model.AdsDiscoveryConstants_ADSDISCOVERYUDPDEFAULTPORT))
if err != nil {
panic(err)
return errors.Wrap(err, "error resolving udp")
}
socket, err := net.ListenUDP("udp4", responseAddr)
if err != nil {
panic(err)
return errors.Wrap(err, "error listening udp")
}
discoveryItem.socket = socket

Expand Down
2 changes: 1 addition & 1 deletion plc4go/internal/ads/model/Tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (m DirectPlcTag) SerializeWithWriteBuffer(writeBuffer utils.WriteBuffer) er
}

func (m DirectPlcTag) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
panic(name)
return xml.Attr{}, errors.Errorf("%s", name) // TODO: why did this panic before
}

type SymbolicPlcTag struct {
Expand Down
45 changes: 30 additions & 15 deletions plc4go/internal/cbus/CBusMessageMapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ func TagToCBusMessage(tag apiModel.PlcTag, value apiValues.PlcValue, alphaGenera
var salData readWriteModel.SALData
switch tagType.application.ApplicationId() {
case readWriteModel.ApplicationId_FREE_USAGE:
panic("Not yet implemented") // TODO: implement
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_TEMPERATURE_BROADCAST:
var temperatureBroadcastData readWriteModel.TemperatureBroadcastData
switch salCommand {
Expand All @@ -114,7 +115,8 @@ func TagToCBusMessage(tag apiModel.PlcTag, value apiValues.PlcValue, alphaGenera
}
salData = readWriteModel.NewSALDataTemperatureBroadcast(temperatureBroadcastData, nil)
case readWriteModel.ApplicationId_ROOM_CONTROL_SYSTEM:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case
readWriteModel.ApplicationId_LIGHTING,
readWriteModel.ApplicationId_VENTILATION,
Expand Down Expand Up @@ -166,35 +168,48 @@ func TagToCBusMessage(tag apiModel.PlcTag, value apiValues.PlcValue, alphaGenera
lightingData = readWriteModel.NewLightingDataTerminateRamp(group, commandTypeContainer)
supportsWrite = true
case readWriteModel.LightingCommandType_LABEL.PLC4XEnumName():
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
default:
return nil, false, false, false, errors.Errorf("Unsupported command %s for %s", salCommand, tagType.application.ApplicationId())
}
salData = readWriteModel.NewSALDataLighting(lightingData, nil)
case readWriteModel.ApplicationId_AIR_CONDITIONING:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_TRIGGER_CONTROL:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_ENABLE_CONTROL:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_SECURITY:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_METERING:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_ACCESS_CONTROL:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_CLOCK_AND_TIMEKEEPING:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_TELEPHONY_STATUS_AND_CONTROL:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_MEASUREMENT:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_TESTING:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_MEDIA_TRANSPORT_CONTROL:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
case readWriteModel.ApplicationId_ERROR_REPORTING:
panic("Implement me")
err = errors.New("Not yet implemented") // TODO: implement
return
default:
return nil, false, false, false, errors.Errorf("No support for %s", tagType.application)
}
Expand Down
16 changes: 8 additions & 8 deletions plc4go/internal/cbus/TagHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ func (m TagHandler) handleStatusRequestPattern(match map[string]string) (apiMode
} else if levelArgument := match["startingGroupAddressLabel"]; levelArgument != "" {
statusRequestType = StatusRequestTypeLevel
decodedHex, _ := hex.DecodeString(levelArgument)
if len(decodedHex) != 1 {
panic("invalid state. Should have exactly 1")
if hexLength := len(decodedHex); hexLength != 1 {
return nil, errors.Errorf("invalid state. Should have exactly 1. Actual length %d", hexLength)
}
startingGroupAddressLabel = &decodedHex[0]
} else {
Expand Down Expand Up @@ -177,7 +177,7 @@ func (m TagHandler) handleCalPattern(match map[string]string) (apiModel.PlcTag,
calTypeArgument := match["calType"]
switch {
case strings.HasPrefix(calTypeArgument, "reset"):
panic("Not implemented") // TODO: implement me
return nil, errors.New("Not implemented") // TODO: implement me
case strings.HasPrefix(calTypeArgument, "recall="):
var recalParamNo readWriteModel.Parameter
recallParamNoArgument := match["recallParamNo"]
Expand Down Expand Up @@ -263,15 +263,15 @@ func (m TagHandler) handleCalPattern(match map[string]string) (apiModel.PlcTag,
count = uint8(atoi)
return NewCALGetStatusTag(unitAddress, bridgeAddresses, recalParamNo, count, 1), nil
case strings.HasPrefix(calTypeArgument, "write="):
panic("Not implemented") // TODO: implement me
return nil, errors.New("Not implemented") // TODO: implement me
case strings.HasPrefix(calTypeArgument, "identifyReply="):
panic("Not implemented") // TODO: implement me
return nil, errors.New("Not implemented") // TODO: implement me
case strings.HasPrefix(calTypeArgument, "reply="):
panic("Not implemented") // TODO: implement me
return nil, errors.New("Not implemented") // TODO: implement me
case strings.HasPrefix(calTypeArgument, "status="):
panic("Not implemented") // TODO: implement me
return nil, errors.New("Not implemented") // TODO: implement me
case strings.HasPrefix(calTypeArgument, "statusExtended="):
panic("Not implemented") // TODO: implement me
return nil, errors.New("Not implemented") // TODO: implement me
default:
return nil, errors.Errorf("Invalid cal type %s", calTypeArgument)
}
Expand Down
Loading

0 comments on commit 7cc564f

Please sign in to comment.