Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outline reference code for use cases that use long polling e.g printing or synchronisation with other systems #61

Open
devraj opened this issue Oct 7, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@devraj
Copy link
Member

devraj commented Oct 7, 2024

Is your feature request related to a problem? Please describe.
One of the obvious use cases for the long poll functionality is calling other systems as events progress on the command centre e.g printing of labels or synchronisation with other systems. It would be useful to provide a reference implementation of how this could work for

Describe the solution you'd like
Ideally we would like to provide this as a complete solution for printing and or synchronisation, but for this exercise we would just want to implement something that sits in the samples folder and provides a starting point for how this could work.

Additionally provide a list of hardware that the users can use with the library.

Describe alternatives you've considered
NA

Additional context

  • brother_ql python package to print labels without needing drivers (for example from a Rapsberry Pi)
  • broher-label fork of the repository above
@devraj devraj added the enhancement New feature or request label Oct 7, 2024
@devraj devraj self-assigned this Oct 7, 2024
@devraj
Copy link
Member Author

devraj commented Nov 10, 2024

I tried brother_ql with a Brother QL-700 and got the following result.

Note: On a QL-700 there is a button called Editor Lite if this is turned on the printer seems to be map as a USB disk. The printer will not work as expected if this is turned on.

See this YouTube video it did eventually end up saving the day for me.

Once the printer was installed, this is a basic discovery command:

brother_ql -b pyusb discover
deprecation warning: brother_ql.devicedependent is deprecated and will be removed in a future release
INFO:brother_ql.output_helpers:  Found a label printer: usb://0x04f9:0x2042_Љ  (model: unknown)
usb://0x04f9:0x2042_Љ

if you have say a 696x200 image, you can use the CLI to print and cut the label:

brother_ql -b pyusb -m QL-700 -p usb://0x04f9:0x2042 print -l 62 ~/Downloads/sample_label.jpg 

Haven't cleaned this example up, but here's an extremely simplistic example of printing the same JPG via a python script:

from brother_ql import BrotherQLRaster, create_label
from brother_ql.backends.helpers import send
from brother_ql.backends import backend_factory, guess_backend


from PIL import Image, ImageDraw, ImageFont

# Printer model and label size setup
PRINTER_MODEL = 'QL-700'  # Adjust to your printer model
LABEL_WIDTH = 696  # Common width in pixels for 62mm labels
LABEL_HEIGHT = 200  # Adjust for the height of your label

def create_basic_label(text):
    # Read the image from ~/Downloads/sample_label_mta_australia.jpg
    image = Image.open('/Users/devraj/Downloads/sample_label_mta_australia.jpg')
    return image

def print_label(text, printer_model=PRINTER_MODEL):
    qlr = BrotherQLRaster(printer_model)
    # qlr.exception_on_warning = True  # Enable warnings if needed

    # Create label image and render to raster data
    label_image = create_basic_label(text)
    create_label(qlr, label_image, "62")

    # Define backend and specify the printer connection
    backend = 'pyusb'  # 'pyusb' for USB, 'network' if using network
    printer_identifier = 'usb://0x04f9:0x2042'  # Replace with actual printer USB ID or network IP

    # Send the print job
    send(instructions=qlr.data, printer_identifier=printer_identifier, backend_identifier=backend)

# Print a label with simple text
print_label("Hello, World!")

@devraj
Copy link
Member Author

devraj commented Nov 11, 2024

The following are some observations regarding events raised in the Command Centre when a door access is granted. This is to support the research I am doing around printing.

For our demo command centre if we get a list of groups and read through the event types we will find that Event type we are looking for is Card Event:

{
    "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/groups/23",
    "id": "23",
    "name": "Card Event",
    "eventTypes": [
      {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/types/15800",
        "id": "15800",
        "name": "Access Granted - Mobile Reader"
      },
      {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/types/15815",
        "id": "15815",
        "name": "Card presented (Controlled Challenge) – Mobile Reader"
      },
      {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/types/15816",
        "id": "15816",
        "name": "Card Exit Granted - Mobile Reader"
      },
      {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/types/20001",
        "id": "20001",
        "name": "Door Access Granted"
      },
      {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/types/20002",
        "id": "20002",
        "name": "Level Access Granted"
      }
    ]
}

In particular we want to track Door Access Granted, Sample Event for entering a door that uses a 700 Single Door Controller:

{
    "card": {
        "facilityCode": "P61405",
        "issueLevel": 1,
        "number": "Card App"
    },
    "cardholder": {
        "firstName": "Devraj",
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/cardholders/9985",
        "id": "9985",
        "lastName": "Mukherjee",
        "name": "Mukherjee, Devraj"
    },
    "division": {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/divisions/2",
        "id": "2",
        "name": "Root Division"
    },
    "entryAccessZone": {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/access_zones/9992",
        "id": "9992",
        "name": "Anomaly Demo Access Zone"
    },
    "group": {
        "id": "23",
        "name": "Card Event"
    },
    "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/4715",
    "id": "4715",
    "message": "Mukherjee, Devraj was granted entry into Anomaly Demo Access Zone through Anomaly Demo Door.",
    "priority": 1,
    "source": {
        "href": "https://commandcentre-api-au.security.gallagher.cloud/api/doors/9991",
        "id": "9991",
        "name": "Anomaly Demo Door"
    },
    "time": "2024-07-29T10:32:21Z",
    "type": {
        "id": "20001",
        "name": "Door Access Granted"
    }
}

the command used to produce the above is:

http get https://commandcentre-api-au.security.gallagher.cloud/api/events\?group\=23 "Authorization: GGL-API-KEY $GACC_API_KEY"

this will produce next and previous links

@devraj
Copy link
Member Author

devraj commented Nov 11, 2024

To conclude the example of printing to a label printer on an event. Here's a simulated event to trigger the polling script to trigger a print.

Quoting Gallagher's documentation there are limitations (understandably) on what sort of events you can create via the API:

  • A new event must contain either this or (if your server is running 8.90 or later) eventType. Without one of them, the POST will fail.
  • The event type is mandatory because the server cannot assume a reasonable default. It can for the other fields.

Take this href from the list of event types. Note that you can only use event types in one of the thirty external event groups with IDs 57-66 and 190-209. Command Centre ships with one event type per group, IDs 4000-4009 and 6010-6029, but you can create 970 more using the External Event Type Configuration utility.

This is a POST request that will trigger something acceptable for you to test that the event poll system receives a log event and has a cardholder attached to it:

echo -n '{
  "type": {
    "href": "https://commandcentre-api-au.security.gallagher.cloud/api/events/types/4000"
  },
  "source": {
    "href": "https://commandcentre-api-au.security.gallagher.cloud/api/doors/9991"
  },
  "priority": 1,
  "message": "Via API Mukherjee, Devraj was granted entry into Anomaly Demo Access Zone through Anomaly Demo Door.",
  "details": "",
  "cardholder": {
    "href": "https://commandcentre-api-au.security.gallagher.cloud/api/cardholders/9985"
  },
  "entryAccessZone": {
    "href": "https://commandcentre-api-au.security.gallagher.cloud/api/access_zones/9992"
  },
  "door": {
    "href": "https://commandcentre-api-au.security.gallagher.cloud/api/doors/9991"
  }
}' | http post https://commandcentre-api-au.security.gallagher.cloud/api/events "Authorization: GGL-API-KEY $GACC_API_KEY"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant