Started to work on TriggerAction implementation

This commit is contained in:
MaMe82 2018-09-26 00:04:48 +02:00
parent 4e7d1cda40
commit 84320719c0
5 changed files with 2261 additions and 202 deletions

View File

@ -1,26 +1,37 @@
package common_web
const (
EVT_ANY = int64(0)
EVT_LOG = int64(1)
EVT_HID = int64(2)
EVT_ANY = int64(0)
EVT_LOG = int64(1)
EVT_HID = int64(2)
EVT_TRIGGER = int64(2)
)
type EvtTriggerType int64
const (
EVT_TRIGGER_TYPE_SERVICE_STARTED = EvtTriggerType(0)
EVT_TRIGGER_TYPE_USB_GADGET_CONNECTED = EvtTriggerType(1)
EVT_TRIGGER_TYPE_USB_GADGET_DISCONNECTED = EvtTriggerType(2)
EVT_TRIGGER_TYPE_WIFI_AP_STARTED = EvtTriggerType(3)
EVT_TRIGGER_TYPE_WIFI_CONNECTED_AS_STA = EvtTriggerType(4)
EVT_TRIGGER_TYPE_SSH_LOGIN = EvtTriggerType(5)
EVT_TRIGGER_TYPE_DHCP_LEASE_GRANTED = EvtTriggerType(6)
)
const (
HidEventType_JOB_STARTED = int64(0)
HidEventType_JOB_STOPPED = int64(1)
HidEventType_CONTROLLER_ABORTED = int64(2)
HidEventType_JOB_CANCELLED = int64(3)
HidEventType_JOB_SUCCEEDED = int64(4)
HidEventType_JOB_SUCCEEDED_NO_RESULT = int64(5)
HidEventType_JOB_FAILED = int64(6)
HidEventType_JOB_WAIT_LED_FINISHED = int64(7)
HidEventType_JOB_STARTED = int64(0)
HidEventType_JOB_STOPPED = int64(1)
HidEventType_CONTROLLER_ABORTED = int64(2)
HidEventType_JOB_CANCELLED = int64(3)
HidEventType_JOB_SUCCEEDED = int64(4)
HidEventType_JOB_SUCCEEDED_NO_RESULT = int64(5)
HidEventType_JOB_FAILED = int64(6)
HidEventType_JOB_WAIT_LED_FINISHED = int64(7)
HidEventType_JOB_WAIT_LED_REPEATED_FINISHED = int64(8)
HidEventType_JOB_NO_FREE_VM = int64(9)
HidEventType_JOB_NO_FREE_VM = int64(9)
)
var EventType_name = map[int64]string{
0: "JOB STARTED",
1: "JOB STOPPED",
@ -42,4 +53,3 @@ var EventType_value = map[string]int32{
"JOB_CANCELLED": 3,
}
*/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -50,6 +50,69 @@ service P4WNP1 {
rpc ListStoredWifiSettings(Empty) returns (StringMessageArray) {}
}
/* Triggers, Actions and resulting TriggerActions */
message TriggerActionSettings {
repeated TriggerAction registeredTriggerActions = 1;
}
message TriggerAction {
uint32 id = 1; // assigned by service, used as identifier to allow deletion of trigger actions
oneof Trigger {
TriggerServiceStarted serviceStarted = 2;
TriggerUSBGadgetConnected usbGadgetConnected = 3;
TriggerUSBGadgetDisconnected usbGadgetDisconnected = 4;
TriggerWifiAPStarted wifiAPStarted = 5;
TriggerWifiConnectedAsSta wifiConnectedAsSta = 6;
TriggerSSHLogin sshLogin = 7;
TriggerDHCPLeaseGranted dhcpLeaseGranted = 8;
}
oneof Action {
ActionStartBashScript bashScript = 9;
ActionStartHIDScript hidScript = 10;
ActionDeploySettingsTemplate deploySettingsTemplate = 11;
ActionLog log = 12;
}
}
message TriggerServiceStarted {} //no fields
message TriggerGPIO{} // input and output params have to be defined
message TriggerUSBGadgetConnected{}
message TriggerUSBGadgetDisconnected{}
message TriggerWifiAPStarted{} // fired when an Access Point is running
message TriggerWifiConnectedAsSta{} // fired when successfully connected to an existing AP
message TriggerSSHLogin{ // fired when a user logs in
string resLoginUser = 1;
}
message TriggerDHCPLeaseGranted { // fired when a client receives a lease
string resInterface = 1;
string resClientIP = 2;
string resClientMac = 3;
}
message ActionStartBashScript {
string scriptPath = 1;
}
message ActionStartHIDScript {
string scriptName = 1; //could be combined into oneof with script path, to allow starting scripts from arbitrary filepath (avoids storing scripts in DB or fixed folder)
}
message ActionDeploySettingsTemplate {
string templateName = 1;
enum TemplateType {
FULL_SETTINGS = 0;
NETWORK = 1;
WIFI = 2;
USB = 3;
BLUETOOTH = 4;
}
TemplateType type = 2;
}
message ActionLog {}
message WifiRequestSettingsStorage {
string TemplateName = 1;
WiFiSettings settings = 2;

View File

@ -1 +1,85 @@
package service
import (
"fmt"
"github.com/mame82/P4wnP1_go/common_web"
pb "github.com/mame82/P4wnP1_go/proto"
"sync"
)
type TriggerActionManager struct {
rootSvc *Service
evtRcv *EventReceiver
registeredTriggerActionMutex *sync.Mutex
registeredTriggerAction []*pb.TriggerAction
nextID uint32
}
func (tam *TriggerActionManager) processing_loop() {
// (un)register event listener(s)
tam.evtRcv = tam.rootSvc.SubSysEvent.RegisterReceiver(common_web.EVT_ANY) // ToDo: change to trigger event type, once defined
fmt.Println("TAM processing loop started")
Outer:
for {
select {
case evt := <- tam.evtRcv.EventQueue:
// avoid consuming empty messages, because channel is closed
if evt == nil {
break Outer // abort loop on "nil" event, as this indicates the EventQueue channel has been closed
}
fmt.Println("TriggerActionManager received unfiltered event", evt)
// check if relevant and dispatch to triggers
case <- tam.evtRcv.Ctx.Done():
// evvent Receiver cancelled or unregistered
break Outer
}
}
fmt.Println("TAM processing loop finished")
}
func (tam *TriggerActionManager) AddTriggerAction(ta *pb.TriggerAction) (err error) {
tam.registeredTriggerActionMutex.Lock()
defer tam.registeredTriggerActionMutex.Unlock()
ta.Id = tam.nextID
tam.nextID++
tam.registeredTriggerAction = append(tam.registeredTriggerAction, ta)
return nil
}
func (tam *TriggerActionManager) Start() {
// create test trigger
serviceUpRunScript := &pb.TriggerAction{
Trigger: &pb.TriggerAction_ServiceStarted{
ServiceStarted: &pb.TriggerServiceStarted{},
},
Action: &pb.TriggerAction_BashScript{
BashScript: &pb.ActionStartBashScript{
ScriptPath: "/usr/local/P4wnP1/scripts/servicestart.sh",
},
},
}
tam.AddTriggerAction(serviceUpRunScript)
fmt.Printf("TEST TRIGGER ACTION: Service up %+v\n", serviceUpRunScript)
go tam.processing_loop()
}
func (tam *TriggerActionManager) Stop() {
tam.rootSvc.SubSysEvent.UnregisterReceiver(tam.evtRcv) // should end the processing loop, as the context of the event receiver is closed
}
func NewTriggerActionManager(rootService *Service) (tam *TriggerActionManager) {
tam = &TriggerActionManager{
registeredTriggerAction: []*pb.TriggerAction{},
registeredTriggerActionMutex: &sync.Mutex{},
rootSvc: rootService,
}
return tam
}