Author Archives: Rex Hardin

About Rex Hardin

Some say... I am the stig! Others just call me a geek/nerd/dork. :D

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

I bought a 3D printer!

Hi folks.. I bought a 3D printer – a Lulzbot Mini.
lulzbot_mini_box
battlestation

I’ve only made a few prints with it thus far – the Rocktopus of course:
rocktopus

I also made a spinning top in high speed / low quality, scaled down to 30% to use the rest of the Lulzbot provided HIPS filament. I didn’t have glue for the tip, and the low quality didn’t let ’em snap in – so I manually extruded filament to ‘glue’ one of the tips on – and it worked!

The first real ‘work’ I’m putting effort into is creating a side-mount panel for a Raspberry Pi 2 (Model B) to run Octoprint. I know others have done this, but it seems like everyone resorts to using screws. I checked my baggies of mixed computer/electronics screws, standoffs, etc and I don’t have anything that will work off-hand – so screw the screws! Instead, I’m going to design snap-in plugs. I’ve had some initial success, and I’ll update this post with the STL and/or a link to thingiverse with the final product. I learned the hard way that rapid protoyping means making very small, plastic-frugal test prints. My first attempt at this was the entire bracket in one go. What a waste!
lulzbot_test_plug

For now, I figured I’d share the dimensions of the left side plate. As it’s open source and open hardware, the schematic for the Lulzbot Mini is available on the USB drive provided with purchase. The PDF that shows the schematics and dimensions is a bit of a pain in the ass to read, though. Here’s my translation to something human-readable for my purposes:
lulzbot-mini-sideplate

I finally arrived at a decent snap design for the holes in the Lulzbot mini. I have been using Autodesk Fusion 360 to do my modeling.
lulzbot_mini_sideplate_v1

I actually own an old copy of Autodesk Inventor, but with Fusion 360 (free for broke-ass startups!) exporting via the 3d print function allows me to easily save an STL. I am syncing the files to my laptop using OneDrive, and loading the STL into Cura.

lulzbot_mini_sideplate_v1_cura

This is just a test frame. I need to validate that my dimensions are accurate – so this is a test print to see if the four snaps actually fit into the holes correctly:

lulzbot_mini_sideplate_v1_3dprint

And… success!
lulzbot_mini_sideplate_v1_attached

lulzbot_mini_sideplate_v1_semi_attached

1,479 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

Automic Introspective REST Agent + Small life update

Hi folks,

Long time no post! I believe the last thing I posted was some sort of Craigslist apartment-scanning script I was using to sort through flats in NYC. Though I was offered the job, I ended up turning it down for a couple of reasons:

1. Though it would have paid well more than enough to cover the COLA difference, NYC is wicked pricey and honestly I think I’m a bit scared of big city living.

2. I had a more interesting competing offer.

Late last year I accepted an offer to be an automation consultant through a consultancy that services Automic, Chef, and Cloudbees technologies. I’ve been sent around the USA and Europe so far and have had the opportunity to work on great things for great companies, with great people! I am proud to say I’ve had a direct hand in creating the CAOS framework used by Nordea (Danish Bank)  and have automated the software build/deployment pipeline for a number of other companies.

Anyhoo, something I have realized is that Automic’s ARA product, while quite feature rich, is missing something… a REST API! So I prototyped one in PowerShell. Here’s a demo:

4,205 total views, 1 views today

PowerShell + Craigslist for advanced apartment hunting

I was very, very tired of copying/pasting a template saying “Hi I’m Rex and I’m thinking about moving to NYC because of a job offer.. no pets, no kids, no wife, etc” to each place I found appealing. I decided to scrape craigslist and build PowerShell objects that I could sort and use to automagically spam my potential new roommates! 🙂

Here’s the script that’ll do the scraping and build an array of objects:

And here’s what a sample object looks like, though you could probably guess by the object declaration:
Sample object

Playing with data is fun, right?
Sample list of objects

There’s a ton that COULD be done with this, I’m only scratching the surface! 🙂

———————————- UPDATE ———————————-

I’ve semi-wrapped it in a function for ease of use:

Using Find-Apartment

14,517 total views, 7 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