Trigger & Action API Tutorial
Run the examplePermalink
- Install and run the gateway
- Download the Python client library
- Copy the root certificate of the gateway to your working directory. As default, the certificate(ca.crt) resides in cert of the installation directory.
-
Change the server and the device information in example/action/test/test.py as needed.
# the path of the root certificate GATEWAY_CA_FILE = '../../../../cert/gateway/ca.crt' # the ip address of the gateway GATEWAY_IP = '192.168.0.2' GATEWAY_PORT = 4000 # the ip address of the target device DEVICE_IP = '192.168.0.110' DEVICE_PORT = 51211 USE_SSL = False
-
Run.
cd example/action/test python test.py
1. Connect to the gateway and the devicePermalink
The example assumes you use the device gateway. For the master gateway or other connection options, refer to the Connect or ConnectMaster examples.
client = GatewayClient(GATEWAY_IP, GATEWAY_PORT, GATEWAY_CA_FILE)
channel = client.getChannel()
connectSvc = ConnectSvc(channel)
connInfo = connect_pb2.ConnectInfo(IPAddr=DEVICE_IP, port=DEVICE_PORT, useSSL=USE_SSL)
devID = connectSvc.connect(connInfo)
2. Make triggersPermalink
You can make several types of triggers. The example shows how to make event triggers among these.
cardFailEventTrigger = action_pb2.EventTrigger(eventCode=BS2_EVENT_VERIFY_FAIL | BS2_SUB_EVENT_CREDENTIAL_CARD)
cardFailTrigger = action_pb2.Trigger(deviceID=deviceID, type=action_pb2.TRIGGER_EVENT, event=cardFailEventTrigger)
fingerFailEventTrigger = action_pb2.EventTrigger(eventCode=BS2_EVENT_IDENTIFY_FAIL | BS2_SUB_EVENT_CREDENTIAL_FINGER)
fingerFailTrigger = action_pb2.Trigger(deviceID=deviceID, type=action_pb2.TRIGGER_EVENT, event=fingerFailEventTrigger)
3. Make actionsPermalink
The example shows how to make a relay action. Refer to ActionType for available types of actions.
relaySignal = action_pb2.Signal(count=3, onDuration=500, offDuration=500)
relayAction = action_pb2.RelayAction(relayIndex=0, signal=relaySignal)
failAction = action_pb2.Action(deviceID=deviceID, type=action_pb2.ACTION_RELAY, relay=relayAction)
4. Make TriggerActionConfigPermalink
You can configure up to 128 pairs of trigger and action in TriggerActionConfig.
cardTriggerAction = action_pb2.TriggerActionConfig.TriggerAction(trigger=cardFailTrigger, action=failAction)
fingerTriggerAction = action_pb2.TriggerActionConfig.TriggerAction(trigger=fingerFailTrigger, action=failAction)
config = action_pb2.TriggerActionConfig(triggerActions=[cardTriggerAction, fingerTriggerAction])
self.actionSvc.setConfig(deviceID, config)