Skip to content

Commit

Permalink
connect to Kerberos Hub if required, even if camera is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricve committed May 9, 2023
1 parent 99cc7d4 commit e9ea34c
Show file tree
Hide file tree
Showing 7 changed files with 658 additions and 71 deletions.
137 changes: 72 additions & 65 deletions machinery/src/cloud/Cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ func HandleUpload(configuration *models.Configuration, communication *models.Com
log.Log.Error("HandleUpload: " + err.Error())
}
} else {
delay = 5 * time.Second // slow down
log.Log.Error("HandleUpload: " + err.Error())
delay = 20 * time.Second // slow down
if err != nil {
log.Log.Error("HandleUpload: " + err.Error())
}
}

time.Sleep(delay)
Expand Down Expand Up @@ -215,36 +217,36 @@ func GetSystemInfo() (models.System, error) {
func HandleHeartBeat(configuration *models.Configuration, communication *models.Communication, uptimeStart time.Time) {
log.Log.Debug("HandleHeartBeat: started")

config := configuration.Config
loop:
for {

if config.Offline == "true" {
log.Log.Debug("HandleHeartBeat: stopping as Offline is enabled.")
} else {
config := configuration.Config

url := config.HeartbeatURI
key := ""
username := ""
vaultURI := ""
if config.Offline == "true" {
log.Log.Debug("HandleHeartBeat: stopping as Offline is enabled.")
} else {

username = config.S3.Username
if config.Cloud == "s3" && config.S3 != nil && config.S3.Publickey != "" {
username = config.S3.Username
key = config.S3.Publickey
} else if config.Cloud == "kstorage" && config.KStorage != nil && config.KStorage.CloudKey != "" {
key = config.KStorage.CloudKey
username = config.KStorage.Directory
}
url := config.HeartbeatURI
key := ""
username := ""
vaultURI := ""

// This is the new way ;)
if config.HubURI != "" {
url = config.HubURI + "/devices/heartbeat"
}
if config.HubKey != "" {
key = config.HubKey
}
username = config.S3.Username
if config.Cloud == "s3" && config.S3 != nil && config.S3.Publickey != "" {
username = config.S3.Username
key = config.S3.Publickey
} else if config.Cloud == "kstorage" && config.KStorage != nil && config.KStorage.CloudKey != "" {
key = config.KStorage.CloudKey
username = config.KStorage.Directory
}

loop:
for {
// This is the new way ;)
if config.HubURI != "" {
url = config.HubURI + "/devices/heartbeat"
}
if config.HubKey != "" {
key = config.HubKey
}

if key != "" {
// Check if we have a friendly name or not.
Expand Down Expand Up @@ -292,39 +294,44 @@ func HandleHeartBeat(configuration *models.Configuration, communication *models.
// Congert to string
macs, _ := json.Marshal(system.MACs)
ips, _ := json.Marshal(system.IPs)
cameraConnected := "true"
if communication.CameraConnected == false {
cameraConnected = "false"
}

var object = fmt.Sprintf(`{
"key" : "%s",
"version" : "3.0.0",
"release" : "%s",
"cpuid" : "%s",
"clouduser" : "%s",
"cloudpublickey" : "%s",
"cameraname" : "%s",
"enterprise" : %t,
"hostname" : "%s",
"architecture" : "%s",
"totalMemory" : "%d",
"usedMemory" : "%d",
"freeMemory" : "%d",
"processMemory" : "%d",
"mac_list" : %s,
"ip_list" : %s,
"board" : "",
"disk1size" : "%s",
"disk3size" : "%s",
"diskvdasize" : "%s",
"uptime" : "%s",
"boot_time" : "%s",
"siteID" : "%s",
"onvif" : "%s",
"numberoffiles" : "33",
"timestamp" : 1564747908,
"cameratype" : "IPCamera",
"docker" : true,
"kios" : false,
"raspberrypi" : false
}`, config.Key, system.Version, system.CPUId, username, key, name, isEnterprise, system.Hostname, system.Architecture, system.TotalMemory, system.UsedMemory, system.FreeMemory, system.ProcessUsedMemory, macs, ips, "0", "0", "0", uptimeString, boottimeString, config.HubSite, onvifEnabled)
"key" : "%s",
"version" : "3.0.0",
"release" : "%s",
"cpuid" : "%s",
"clouduser" : "%s",
"cloudpublickey" : "%s",
"cameraname" : "%s",
"enterprise" : %t,
"hostname" : "%s",
"architecture" : "%s",
"totalMemory" : "%d",
"usedMemory" : "%d",
"freeMemory" : "%d",
"processMemory" : "%d",
"mac_list" : %s,
"ip_list" : %s,
"board" : "",
"disk1size" : "%s",
"disk3size" : "%s",
"diskvdasize" : "%s",
"uptime" : "%s",
"boot_time" : "%s",
"siteID" : "%s",
"onvif" : "%s",
"cameraConnected": "%s",
"numberoffiles" : "33",
"timestamp" : 1564747908,
"cameratype" : "IPCamera",
"docker" : true,
"kios" : false,
"raspberrypi" : false
}`, config.Key, system.Version, system.CPUId, username, key, name, isEnterprise, system.Hostname, system.Architecture, system.TotalMemory, system.UsedMemory, system.FreeMemory, system.ProcessUsedMemory, macs, ips, "0", "0", "0", uptimeString, boottimeString, config.HubSite, onvifEnabled, cameraConnected)

var jsonStr = []byte(object)
buffy := bytes.NewBuffer(jsonStr)
Expand Down Expand Up @@ -365,14 +372,14 @@ func HandleHeartBeat(configuration *models.Configuration, communication *models.
} else {
log.Log.Error("HandleHeartBeat: Disabled as we do not have a public key defined.")
}
}

// This will check if we need to stop the thread,
// because of a reconfiguration.
select {
case <-communication.HandleHeartBeat:
break loop
case <-time.After(15 * time.Second):
}
// This will check if we need to stop the thread,
// because of a reconfiguration.
select {
case <-communication.HandleHeartBeat:
break loop
case <-time.After(15 * time.Second):
}
}

Expand Down
16 changes: 10 additions & 6 deletions machinery/src/components/Kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func Bootstrap(configuration *models.Configuration, communication *models.Commun
subDecoder := &ffmpeg.VideoDecoder{}
cameraSettings := &models.Camera{}

// Handle heartbeats
go cloud.HandleHeartBeat(configuration, communication, uptimeStart)

// Run the agent and fire up all the other
// goroutines which do image capture, motion detection, onvif, etc.

Expand All @@ -75,13 +78,16 @@ func Bootstrap(configuration *models.Configuration, communication *models.Commun
}
// We will re open the configuration, might have changed :O!
OpenConfig(configuration)

// We will override the configuration with the environment variables
OverrideWithEnvironmentVariables(configuration)
}
log.Log.Debug("Bootstrap: finished")
}

func RunAgent(configuration *models.Configuration, communication *models.Communication, uptimeStart time.Time, cameraSettings *models.Camera, decoder *ffmpeg.VideoDecoder, subDecoder *ffmpeg.VideoDecoder) string {
log.Log.Debug("RunAgent: bootstrapping agent")

log.Log.Debug("RunAgent: bootstrapping agent")
config := configuration.Config

// Currently only support H264 encoded cameras, this will change.
Expand Down Expand Up @@ -185,9 +191,6 @@ func RunAgent(configuration *models.Configuration, communication *models.Communi
communication.HandleONVIF = make(chan models.OnvifAction, 1)
mqttClient := routers.ConfigureMQTT(configuration, communication)

// Handle heartbeats
go cloud.HandleHeartBeat(configuration, communication, uptimeStart)

// Handle the camera stream
go capture.HandleStream(infile, queue, communication)

Expand Down Expand Up @@ -241,14 +244,14 @@ func RunAgent(configuration *models.Configuration, communication *models.Communi

// Here we are cleaning up everything!
if configuration.Config.Offline != "true" {
communication.HandleHeartBeat <- "stop"
communication.HandleUpload <- "stop"
}
communication.HandleStream <- "stop"
if subStreamEnabled {
communication.HandleSubStream <- "stop"
}
time.Sleep(time.Second * 1)

time.Sleep(time.Second * 3)

infile.Close()
infile = nil
Expand Down Expand Up @@ -278,6 +281,7 @@ func RunAgent(configuration *models.Configuration, communication *models.Communi
// Waiting for some seconds to make sure everything is properly closed.
log.Log.Info("RunAgent: waiting 3 seconds to make sure everything is properly closed.")
time.Sleep(time.Second * 3)

} else {
log.Log.Error("Something went wrong while opening RTSP: " + err.Error())
time.Sleep(time.Second * 3)
Expand Down
1 change: 1 addition & 0 deletions machinery/src/models/Communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ type Communication struct {
Decoder *ffmpeg.VideoDecoder
SubDecoder *ffmpeg.VideoDecoder
Image string
CameraConnected bool
}
Loading

0 comments on commit e9ea34c

Please sign in to comment.