Category Archives: Uncategorized

TypeScript Compiler API – Syntax Parsing

I’ve been working with the TypeScript compiler API to build a custom compiler for my current employer, who effectively wants to abstract strings into resource files automatically. It’s a similar concept to what Android and iOS development environments provide structure for OOB.

For example, given a simple class in an input file:

Abstracting the properties on the class into a structured resource file might be done with regular expressions in such a simple example – but to do it the “right” way you’d end up re-writing the compiler that interprets the TypeScript anyway!

The TypeScript compiler API exposes you to the ability to walk the Abstract Syntax Tree (AST) and identify each type of “node” in the tree. The node types are listed in the SyntaxKind enum.

It’s easy enough to take a TypeScript file as input and output the syntax of every node, and you could build logic on top of this syntax identification.

To use this code, it must be transpiled to javascript before running on node. The entire nodejs/typescript runtime and environment setup is a mess, I find it fickle to deal with compared to the ease of other languages. Note that you need to npm install both typescript and typings.

Your output may look something like this:
tsc  showSyntax.ts console output

This process generates the showSyntax.js in the same folder.

You can run the output showSyntax.js on node, passing one or more filenames as input. Here’s the example helloworld.ts above:

showSyntax of helloworld.ts

I think it is interesting to read the code through the eyes of the compiler, seeing the SyntaxKind description of each node. It’s a useful learning aid.

998 total views, 2 views today

Playing with Django-Channels

Django-channels is a library touted to bring websockets to django in an incredibly simple / easy-to-implement fashion. I’ve listened to presentations, read some documentation and started exploring the code samples. One of the very first example apps provided by the project maintainers is a chat application – it’s a rudimentary implementation of live chat for authenticated users. It works well, but it’s far from feature-rich. To deep-dive and learn more about django-channels, I wanted to make the app more feature rich by implementing a “user count”. One would think this should be simple – just count the number of open websocket connections associated with a given chat room! And while the premise is simple, the implementation doesn’t really exist yet.

I researched the issue and submitted a ticket on the GitHub project here: https://github.com/django/channels/issues/388

Currently, retrieving this count requires getting the RedisChannelLayer instance and asking for all of the key/value pairs from the Group zset, then getting the length of the returned list:

This also works:

It feels dirty to retrieve all the data when you just need the count. When using redis as a backend, we can issue the zcard command to retrieve row count for a given zset, i.e:

zcard asgi::group:room-1

Redis Screenshot

My knowledge/research falls short at this point. Could we extend Group class this enhancement be generic enough to implement for all backends? The hacky way can be implemented easily:

We can use the native connection in a backend-specific (asgi_redis.core.RedisChannelLayer) fashion:

750 total views, 1 views today

Django DateTimeZoneField

In a scheduling project I’ve been working on, I’ve come across the need to ingest and present a lot of datetime information. The input will include many different timezones and may not reflect the user’s timezone. Thus the user must specify the timezone in the datetime stamp field. You’d think Django would handle this out of the box, but it really doesn’t. My solution is to have the user explicitly specify the timezone in the datetime field via a neat widget, and have the server side coerce that data into UTC before storing it. As long as your django instance has timezone settings configured, this will let you store a naïve datetime that is guaranteed to be UTC. There are benefits to this – datetime arithmetic becomes simple and the presentation layer can handle converting to other timezones.

My solution is to create a new field class that inherits from forms.DateTimeField, and simply override the way it initially converts the string value to a datetime stamp. I use the dateutil parser to give the datetime string a bit of wiggle room , though in most cases the javascript widget will produce the input. The parsed datetime object will be timezone aware, but is then converted to UTC before django handles form validation / input cleaning. I didn’t need to, but you could also explicitly remove the tzinfo at this stage.

Define a new field type in your forms.py:

When defining your form / modelform, explicitly specify the field and widget, i.e:

In the template you render your form in, use the class you’ve specified to enable a jquery-ui datepicker. In my case, I’m using jquery-ui-timepicker:

Don’t forget the css/js dependencies:

1,442 total views, no views today

The jb0x project

I am starting on a new project that I have dubbed jb0x. The domain (jb0x.com) will redirect to this page for the time being.

So, what is jb0x?

It’s a good listener and event triggering mechanism. The premise is that you’ll snap your fingers and it does something for you. It’s that simple! It’ll be a little cube or sphere shaped device that actively listens for snaps, and determines the pattern of your snap, and uses that pattern to launch a particular command or perform a particular action on your computer. It’s a way to truly “remotely” interface with your computer in a totally analog fashion. Ever want to skip a song while the remote is out of reach? Snap snap… song skipped.

Here’s an itty bitty taste of how I think it’ll work:

jb0x_initial_design

Update — I built a working prototype. I have the arduino doing a little processing/math to determine whether a snap event is detected as right, left or both. It’s not perfect yet, but I have some ideas on how to improve it.

jb0x_prototype

The arduino is spitting out data over serial to a raspberry pi, which will be running a celery worker that listens to the serial port and ingest the data (via Django ORM) into a database. I’ll do this to get some statistics and test data to mull over, and eventually I’ll use the Django-app with celery integration to both record and invoke the ‘tasks’ corresponding to snap events.

15,728 total views, 12 views today

VLC Multicast Streaming Audio

Ever wanted to synchronize audio playback over multiple devices? I have. VLC is the key player in this use case:

  • I have a laptop/pc in my bedroom and a server/media centre in the living area.
  • I have shared central storage, so both computers can access the media.
  • I also have RDP, VNC and PowerShell remoting enabled so that I can remotely control one from the other.
  • VLC is installed on both.

Edit: My little cousin didn’t get it at a glance, and he’s tech savvy – so maybe this will help explain why/how it’s used:

[4:40:36 PM] Kyle nolastname: pretty neat but, what for?
[4:47:18 PM] William (Rex) Hardin: Well, right now for instance
[4:47:30 PM] William (Rex) Hardin: I’m playing an audio stream on both my speaker setup in my bedroom
[4:47:36 PM] William (Rex) Hardin: and my speaker setup in the living room
[4:47:43 PM] William (Rex) Hardin: and they’re both in sync
[4:47:53 PM] William (Rex) Hardin: I can walk between the rooms, and not notice and difference

Here’s how we’ll utilize VLC’s multicast streaming services to synchronize audio playback on multiple networked devices.

Configure the Media Server

Step 1: Start a Stream

Start a Stream

Start a Stream

Add a file to stream. I’d suggest using an hour-long audio mix, the stream breaks when playlist items change. Click “Stream”.

Just click next. You don’t need to change anything here. Click next.

Make sure “display locally” is unchecked. Select RTP /MPEG Transport Stream from the dropdown list, and click Add.

In the address field, enter a multicast address. If in doubt, just use 239.1.1.1. Keep the default port. Don’t bother with naming the stream. Click next.

Uncheck “Activate Transcoding”, click next.

Check stream all elementary streams, click Stream.

Hurrah, it’s streaming! You should see VLC playing your audio file, but you shouldn’t hear anything – recall that we intentionally unselected “display locally”.

Step 2: Open the Stream

Open a SECOND instance of VLC. We’ll use this instance to listen to the stream via multicast, thus audio playback won’t be ahead of any other clients assuming similar network distance.

Open a Network Stream (Ctrl+N)

Enter rtp://239.1.1.1:5004 as the network URL & click Play.

With any luck, your stream should open and you should be able to hear your streaming music!

Additional Clients

In my case, I RDP to my other clients in the house/apartment and repeat “Step 2: Open the Stream”. This should go without saying, but you only need one instance of VLC on your additional computers where you want to listen to the stream.

RDP Configuration

Before we get into opening the streams, you should make sure RDP is configured to play audio locally on the remote device. This is a pain in the ass to configure for Linux, I’m pretty sure I had to use xfreerdp with some launch options to ensure audio playback remained on the remote device.

Repeat “Step 2: Open the Stream” to open the stream on your other computers!

Bonus: VLC on Android

Install and Open VLC for Android – click Stream in the context menu.


Enter the same multicast address as used in the previous example, 239.1.1.1 and hit enter/return to start listening to the stream. Audio playback for me was a bit choppy. How was your experience?

4,685 total views, 3 views today

PowerShell REST Service

Hi folks,

In a previous post I showed off a little service I had written in PowerShell to wrap Automic ARA’s ARATools.jar methods in a REST service with a neat page that allows you to test POST json data without writing any code. I have bundled these files for download and copied the source code below. Please let me know if you have any questions!

ForestAIR

3,146 total views, 6 views today

Find invalid Base64 characters

Here’s a PowerShell function to find non-base64 characters in a block of text.

2,231 total views, 1 views today

Spectrum – Javascript Color Picker

I’m building a website to sort of mimic the functionality of StudioStyl.es (awesome site for Visual Studio theme lovers!) for PowerShell ISE color themes!

I’ll be doing things a little differently. Colleagues have suggested using PowerShell as the primary scripting language for the site, and while that’s feasible it seems like overkill (if not convoluted) at this point. Not sure if I’ll head in that direction – in the meantime, I’m playing with javascript color pickers!

The Spectrum color picker is proving really handy: http://jsfiddle.net/W6WCD/1/

5,308 total views, 3 views today