-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: partway implement connectors and more
did quite a lot today, very happy with current progress. hope to finish connectors tomorrow, would be very nice.
- Loading branch information
Showing
12 changed files
with
239 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package camera | ||
|
||
import "github.com/SumeruCCTV/sumeru/service/database/models" | ||
|
||
type Connector interface { | ||
TestConnection() error | ||
} | ||
|
||
type ConnectorData struct { | ||
uuid string | ||
ipAddress string | ||
port int | ||
credentials models.CameraCredentials | ||
} | ||
|
||
func NewConnector(cameraType models.CameraType, svc *Service, data *ConnectorData) Connector { | ||
switch cameraType { | ||
case models.CameraTypeONVIF: | ||
return NewONVIFConnector(svc, data) | ||
case models.CameraTypeRTSP: | ||
return NewRTSPConnector(svc, data) | ||
default: | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package camera | ||
|
||
import ( | ||
"github.com/SumeruCCTV/sumeru/service/database/models" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (svc *Service) accept(cam *models.Camera) { | ||
svc.log.Debugw("adding camera", zap.String("uuid", cam.Uuid)) | ||
c := NewConnector(cam.Type, svc, &ConnectorData{ | ||
uuid: cam.Uuid, | ||
ipAddress: cam.IPAddress, | ||
port: cam.Port, | ||
credentials: cam.Credentials, | ||
}) | ||
if c == nil { | ||
return // invalid camera type? | ||
} | ||
if err := c.TestConnection(); err != nil { | ||
svc.log.Debugw("failed to test connection for camera", zap.String("uuid", cam.Uuid), zap.Error(err)) | ||
if cam.Status != models.CameraStatusInvalid { | ||
if err := svc.database.UpdateCameraStatus(cam.Uuid, models.CameraStatusInvalid); err != nil { | ||
svc.log.Warnw("failed to update camera status", zap.String("uuid", cam.Uuid), zap.Error(err)) | ||
} | ||
} | ||
return | ||
} | ||
svc.cameras[cam.Uuid] = c | ||
if err := svc.database.UpdateCameraStatus(cam.Uuid, models.CameraStatusDisconnected); err != nil { | ||
svc.log.Warnw("failed to update camera status", zap.String("uuid", cam.Uuid), zap.Error(err)) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package camera | ||
|
||
import ( | ||
"fmt" | ||
"github.com/SumeruCCTV/go-onvif" | ||
"github.com/SumeruCCTV/sumeru/pkg/utils" | ||
) | ||
|
||
// maybe switch to https://github.com/videonext/onvif | ||
// go-onvif is a mess | ||
|
||
const protocol = "http" | ||
|
||
type ONVIFConnector struct { | ||
svc *Service | ||
log *utils.Logger | ||
|
||
data *ConnectorData | ||
rtsp *RTSPConnector | ||
} | ||
|
||
func (c *ONVIFConnector) TestConnection() error { | ||
dvc := c.device() | ||
if _, err := dvc.GetInformation(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *ONVIFConnector) device() *onvif.Device { | ||
return &onvif.Device{ | ||
XAddr: fmt.Sprintf("%s://%s:%d/onvif/services", protocol, c.data.ipAddress, c.data.port), | ||
User: c.data.credentials.Username, | ||
Password: c.data.credentials.Password, | ||
} | ||
} | ||
|
||
func NewONVIFConnector(svc *Service, data *ConnectorData) *ONVIFConnector { | ||
return &ONVIFConnector{ | ||
svc: svc, | ||
log: svc.log.Named("onvif"), | ||
data: data, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package camera | ||
|
||
import ( | ||
"fmt" | ||
"github.com/SumeruCCTV/sumeru/pkg/utils" | ||
"github.com/SumeruCCTV/transcoder/ffmpeg" | ||
"net" | ||
) | ||
|
||
// TODO: see https://github.com/andrewlfw/joy4/tree/main/format/rtspv2 | ||
// maybe use this instead of ffmpeg? | ||
|
||
type RTSPConnector struct { | ||
svc *Service | ||
log *utils.Logger | ||
|
||
data *ConnectorData | ||
} | ||
|
||
func (c *RTSPConnector) TestConnection() error { | ||
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", c.data.ipAddress, c.data.port)) | ||
if err != nil { | ||
return err | ||
} | ||
_ = conn.Close() | ||
|
||
_ = ffmpeg.Config{} | ||
|
||
return nil | ||
} | ||
|
||
func NewRTSPConnector(svc *Service, data *ConnectorData) *RTSPConnector { | ||
return &RTSPConnector{ | ||
svc: svc, | ||
log: svc.log.Named("rtsp"), | ||
data: data, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.