2019-12-18 23:08:07 +00:00
package main
import (
"flag"
2021-11-01 10:59:22 +00:00
"fmt"
2019-12-18 23:08:07 +00:00
"github.com/cyrilix/robocar-base/cli"
2021-10-12 16:54:37 +00:00
"github.com/cyrilix/robocar-camera/pkg/camera"
2021-10-12 16:26:57 +00:00
"go.uber.org/zap"
2019-12-18 23:08:07 +00:00
"gocv.io/x/gocv"
"log"
"os"
)
const DefaultClientId = "robocar-camera"
func main ( ) {
2021-11-01 10:59:22 +00:00
var mqttBroker , username , password , clientId , topicBase , topicRoi string
var pubFrequency , horizon int
2019-12-18 23:08:07 +00:00
var device , videoWidth , videoHeight int
mqttQos := cli . InitIntFlag ( "MQTT_QOS" , 0 )
_ , mqttRetain := os . LookupEnv ( "MQTT_RETAIN" )
cli . InitMqttFlags ( DefaultClientId , & mqttBroker , & username , & password , & clientId , & mqttQos , & mqttRetain )
2021-11-01 10:59:22 +00:00
err := cli . SetIntDefaultValueFromEnv ( & horizon , "HORIZON" , 0 )
if err != nil {
log . Printf ( "unable to parse horizon value arg: %v" , err )
}
2019-12-18 23:08:07 +00:00
flag . StringVar ( & topicBase , "mqtt-topic" , os . Getenv ( "MQTT_TOPIC" ) , "Mqtt topic to publish camera frames, use MQTT_TOPIC if args not set" )
2021-11-01 10:59:22 +00:00
flag . StringVar ( & topicRoi , "mqtt-topic-roi" , os . Getenv ( "MQTT_TOPIC_ROI" ) , "Mqtt topic to publish camera frames cropped to horizon value, mqtt-topic value with '-roi' suffix if args not set" )
2019-12-18 23:08:07 +00:00
flag . IntVar ( & pubFrequency , "mqtt-pub-frequency" , 25. , "Number of messages to publish per second" )
flag . IntVar ( & device , "video-device" , 0 , "Video device number" )
flag . IntVar ( & videoWidth , "video-width" , 160 , "Video pixels width" )
flag . IntVar ( & videoHeight , "video-height" , 128 , "Video pixels height" )
2021-11-01 10:59:22 +00:00
flag . IntVar ( & horizon , "horizon" , horizon , "Limit region of interest to horizon in pixels from top, use HORIZON if args not set" )
2022-01-03 18:15:51 +00:00
logLevel := zap . LevelFlag ( "log" , zap . InfoLevel , "log level" )
2019-12-18 23:08:07 +00:00
flag . Parse ( )
2022-01-03 18:15:51 +00:00
2019-12-18 23:08:07 +00:00
if len ( os . Args ) <= 1 {
flag . PrintDefaults ( )
os . Exit ( 1 )
}
2021-10-12 16:26:57 +00:00
config := zap . NewDevelopmentConfig ( )
2022-01-03 18:15:51 +00:00
config . Level = zap . NewAtomicLevelAt ( * logLevel )
2021-10-12 16:26:57 +00:00
lgr , err := config . Build ( )
if err != nil {
log . Fatalf ( "unable to init logger: %v" , err )
}
defer func ( ) {
if err := lgr . Sync ( ) ; err != nil {
log . Printf ( "unable to Sync logger: %v\n" , err )
}
} ( )
zap . ReplaceGlobals ( lgr )
2019-12-31 16:14:05 +00:00
client , err := cli . Connect ( mqttBroker , username , password , clientId )
if err != nil {
2021-10-12 16:26:57 +00:00
zap . S ( ) . Fatalf ( "unable to connect to mqtt broker: %v" , err )
2019-12-31 16:14:05 +00:00
}
defer client . Disconnect ( 10 )
2019-12-18 23:08:07 +00:00
videoProperties := make ( map [ gocv . VideoCaptureProperties ] float64 )
videoProperties [ gocv . VideoCaptureFrameWidth ] = float64 ( videoWidth )
videoProperties [ gocv . VideoCaptureFrameHeight ] = float64 ( videoHeight )
2021-11-01 10:59:22 +00:00
if topicRoi == "" {
2022-01-03 18:15:51 +00:00
topicRoi = fmt . Sprintf ( "%s-roi" , topicBase )
2021-11-01 10:59:22 +00:00
}
c := camera . New ( client , topicBase , topicRoi , pubFrequency , videoProperties , horizon )
2019-12-18 23:08:07 +00:00
defer c . Stop ( )
cli . HandleExit ( c )
2019-12-31 16:14:05 +00:00
err = c . Start ( )
2019-12-18 23:08:07 +00:00
if err != nil {
2021-10-12 16:26:57 +00:00
zap . S ( ) . Fatalf ( "unable to start service: %v" , err )
2019-12-18 23:08:07 +00:00
}
}