question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Examples on integrating with Django Channels ?

See original GitHub issue

Are there any examples on integrating this library with Django Channels ?

I see examples on how to integrate with WebSockets library, but Django Channels and it’s “Consumers” would be also nice. At least small example on how to create few handlers on Consumer. I’m trying something, but it turned out to be a really long method on JsonWebsocketConsumer

Thank you.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
dinko-peharcommented, Mar 8, 2022

I managed to create basic “Consumer” for OCPP. If anyone searches for Django Channels integration, I have used something like (only “ocpp1.6” is accepted):

from dataclasses import asdict
from datetime import datetime

from channels.generic.websocket import JsonWebsocketConsumer
from ocpp.messages import pack, unpack
from ocpp.v16 import call_result, enums


class ChargepointConsumer(JsonWebsocketConsumer):
    def connect(self):
        # TODO: Check if CP exists.

        if "ocpp1.6" not in self.scope["subprotocols"]:
            self.close(1002)

        self.accept("ocpp1.6")

    def decode_json(self, text_data):
        return unpack(text_data)

    def encode_json(self, content):
        return pack(content)

    def receive_json(self, content):

        # For related "Call", create proper "CallResult".
        if content.action == "BootNotification":
            # Handle received payload.
            # TODO: Save to database ?

            # Create result payload.
            payload = call_result.BootNotificationPayload(
                current_time=datetime.utcnow().isoformat(),
                interval=10,
                status=enums.RegistrationStatus.accepted,
            )
        elif content.action == "Heartbeat":
            # Create result payload.
            payload = call_result.HeartbeatPayload(
                current_time=datetime.utcnow().isoformat()
            )

        # Send "CallResult"
        self.send_json(content.create_call_result(asdict(payload)))

There are some pieces missing, but if anyone has better approach, feel free to write.

1reaction
OrangeTuxcommented, Feb 9, 2022

The connection argument of ChargePoint must satisfy the interface async def send(msg: str) and async def recv() -> str. See https://github.com/mobilityhouse/ocpp/issues/94#issuecomment-643911096 . I don’t know Django Channels, but I guess it would be easy to create a wrapper around this type that satisfies the interface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to Django Channels - TestDriven.io
In this tutorial, we'll build a real-time chat application with Django Channels, focusing on how to integrate Django with Django Channels.
Read more >
Django Channels and WebSockets - LogRocket Blog
Learn how to use Django Channels and WebSockets, a new protocol that provides full-duplex communication, by building a real-time game app.
Read more >
Getting Started with Django Channels - Real Python
In this tutorial, we will use Django Channels to create a real-time application that updates a list of users as they log in...
Read more >
Django Channels — Channels 4.0.0 documentation
Channels is comprised of several packages: Channels, the Django integration layer; Daphne, the HTTP and Websocket termination server; asgiref, the base ASGI ...
Read more >
Django Channels - Introduction and Basic Setup
Channels preserve the synchronous behavior of Django and add a layer of asynchronous protocols allowing users to write the views that are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found