From 416b6413bdc6cb2172594375f3e71072ddb47bfb Mon Sep 17 00:00:00 2001 From: MaMe82 Date: Wed, 26 Sep 2018 01:08:51 +0200 Subject: [PATCH] More work on TriggerActions --- service/triggerAction.go | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/service/triggerAction.go b/service/triggerAction.go index 68439d5..25d5765 100644 --- a/service/triggerAction.go +++ b/service/triggerAction.go @@ -29,6 +29,7 @@ func (tam *TriggerActionManager) processing_loop() { break Outer // abort loop on "nil" event, as this indicates the EventQueue channel has been closed } fmt.Println("TriggerActionManager received unfiltered event", evt) + tam.dispatchTriggerEvent(evt) // check if relevant and dispatch to triggers case <- tam.evtRcv.Ctx.Done(): // evvent Receiver cancelled or unregistered @@ -38,7 +39,56 @@ func (tam *TriggerActionManager) processing_loop() { fmt.Println("TAM processing loop finished") } +func (tam *TriggerActionManager) dispatchTriggerEvent(evt *pb.Event) { + if evt.Type != common_web.EVT_TRIGGER { return } + triggerTypeRcv := common_web.EvtTriggerType(evt.Values[0].GetTint64()) + + tam.registeredTriggerActionMutex.Lock() + defer tam.registeredTriggerActionMutex.Unlock() + for _,ta := range tam.registeredTriggerAction { + switch triggerType := ta.Trigger.(type) { + case *pb.TriggerAction_ServiceStarted: + if triggerTypeRcv == common_web.EVT_TRIGGER_TYPE_SERVICE_STARTED { + // trigger action of ta + tam.fireAction(ta.Trigger, ta.Action) + } + case *pb.TriggerAction_UsbGadgetConnected: + if triggerTypeRcv == common_web.EVT_TRIGGER_TYPE_USB_GADGET_CONNECTED { + // trigger action of ta + tam.fireAction(triggerType.UsbGadgetConnected, ta.Action) + } + case *pb.TriggerAction_UsbGadgetDisconnected: + if triggerTypeRcv == common_web.EVT_TRIGGER_TYPE_USB_GADGET_DISCONNECTED { + // trigger action of ta + tam.fireAction(triggerType.UsbGadgetDisconnected, ta.Action) + } + case *pb.TriggerAction_DhcpLeaseGranted: + if triggerTypeRcv == common_web.EVT_TRIGGER_TYPE_USB_GADGET_DISCONNECTED { + // trigger action of ta + tam.fireAction(triggerType.DhcpLeaseGranted, ta.Action) + } + } + } +} + +func (tam *TriggerActionManager) fireAction(trigger interface{}, action interface{}) (err error ) { + switch actionType := action.(type) { + case *pb.TriggerAction_BashScript: + bs := actionType.BashScript + fmt.Printf("Fire bash script '%s'\n", bs.ScriptPath) + case *pb.TriggerAction_HidScript: + hs := actionType.HidScript + fmt.Printf("Starting HID script '%s'\n", hs.ScriptName) + case *pb.TriggerAction_DeploySettingsTemplate: + st := actionType.DeploySettingsTemplate + strType := pb.ActionDeploySettingsTemplate_TemplateType_name[int32(st.Type)] + fmt.Printf("Deploy settings template of type [%s] with name '%s'\n", strType, st.TemplateName) + case *pb.TriggerAction_Log: + fmt.Printf("Logging trigger '%+v'\n", trigger) + } + return nil +} func (tam *TriggerActionManager) AddTriggerAction(ta *pb.TriggerAction) (err error) { tam.registeredTriggerActionMutex.Lock() @@ -64,7 +114,16 @@ func (tam *TriggerActionManager) Start() { }, }, } + serviceUpLog := &pb.TriggerAction{ + Trigger: &pb.TriggerAction_ServiceStarted{ + ServiceStarted: &pb.TriggerServiceStarted{}, + }, + Action: &pb.TriggerAction_Log{ + Log: &pb.ActionLog{}, + }, + } tam.AddTriggerAction(serviceUpRunScript) + tam.AddTriggerAction(serviceUpLog) fmt.Printf("TEST TRIGGER ACTION: Service up %+v\n", serviceUpRunScript) go tam.processing_loop()