EventScan OpenSource

I wrote a prototype application that was based on my whole consultancy time whilst supporting Acer at the London Olympics 2012.

I have since decided to push this original prototype that was written in .NET MVC and Xcode (earlier posts) to a more open technology and plan to push this to the wider Javascript community. Im keen to get people involved in this adventure and will be starting the bare bones of it in the not too distant future. Currently, the tech stack is looking like:

Web – NodeJs, React, Redux, MongoDB
Data capture units – iPhone (to start with).

If you would like to get involved in this project, drop me a comment and lets get a team together.

Posted in Uncategorized

Replicating c# String.Format with JavaScript

I was getting really sick of not having a way to populate placeholder text in JavaScript so i took 5 minutes out to write a small script to replicate the functionality in c#.

The code below is an initial prototype and does not have any validation etc. but i wanted to get something down.

See it working on JSFiddle

Feel free to use or alter this in your own work.

if (!String.prototype.stringFormat) {
String.prototype.stringFormat = function() {

var initialString = this.valueOf();
for (var i = 0, maxlength = arguments.length; i < maxlength; i++) {

var indexStr = '{'+ i + '}';
initialString = initialString.replace(indexStr, arguments[i]);
};
return initialString;
}
};

Posted in Mad ramblings

Javascript Data Cache

I was looking for a way to cache data in JavaScript so i wrote this little ditty.

I need to spend a little time optimising it but here is the code in its initial form.

This works as part of my mean.js service class stack.

‘use strict’;

/**
* Module dependencies.
*/
var cacheStore = [];
/**
* Update user details
*/
exports.add = function(key,value,cacheTimeinSeconds) {
// Init Variables
var cachedate = new Date().getTime();

key = key.toUpperCase();

if (!cacheTimeinSeconds)
{
// default 60 seconds
cachedate = cachedate + (60 * 1000);
}
else
{
cachedate = cachedate + (cacheTimeinSeconds * 1000);
}
var dataitem = {};
dataitem.key = key;
dataitem.value = value;
dataitem.expiry = cachedate;
dataitem.hasExpired = false;
var emptySlotFound = false;
for (var i = 0; i < cacheStore.length; i++)
{
if (cacheStore[i].hasExpired)
{
cacheStore[i] = dataitem;
emptySlotFound = true;
break;
}
}

if (!emptySlotFound)
{
cacheStore.push(dataitem);
}
console.log(‘>>>>>>>> Cache item added : ‘ + key + ‘ <<<<<<<<<<‘);

};

exports.get = function(key) {

key = key.toUpperCase();

var currentdate = new Date().getTime();
// Init Variables
for (var i = 0; i < cacheStore.length; i++)
{
if (cacheStore[i].expiry < currentdate)
{
cacheStore[i].key = ”;
cacheStore[i].value = null;
cacheStore[i].hasExpired = true;
}
}

for (i = 0; i < cacheStore.length; i++)
{
if (cacheStore[i].key === key && !cacheStore[i].hasExpired)
{
console.log(‘ Cache item retrieved : ‘ + key + ‘ <<<<<<<<<<‘);
return cacheStore[i].value;
}

}

return undefined;
};
exports.RemoveAllExpiredItems = function() {
var arrayOfExpiredIndexes = [];

for (var i = 0; i < cacheStore.length; i++)
{
if (cacheStore[i].hasExpired === true)
{
arrayOfExpiredIndexes.push(i);
}
}

for (i = 0; i < arrayOfExpiredIndexes.length; i++)
{
cacheStore.splice(arrayOfExpiredIndexes[i],1);
}
};

exports.ClearAllCache = function() {

cacheStore = [];

};

Posted in Uncategorized

Joining the dots….

I did some work a while ago regarding sockets using Node.js which can be seen here.

I have also been doing some work on Angular consuming WebAPI through .NET MVC and SQL Server.

I decided that I would go the whole hog and create a test piece that ties the following together.

Node.js running ExpressJS and Jade templating.

The served up pages are going to be managed by Angular and all the data is initially going to be handles through MongoDB although i may have ot switch this out to MS Table storage when i put this on the Azure platform.

The git repo is here if you are interested and the live azure site is here.

I shall post more about it when I have time.

— Update —

One little annoyance was that express aotumatically creates a www folder in the bin directory which Azure has real issues with so that took a little while to solve. It was simply a case of creating a web.config file which forces iis to use the bin directory.

The code is below if you are interested.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>

<!--
By default IIS will block requests going to the bin directory for security reasons.
We need to disable this since that's where Express has put the application entry point.
-->
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin" />
</hiddenSegments>
</requestFiltering>
</security>

<handlers>
<!-- Indicates that the www file is a node.js entry point -->
<add name="iisnode" path="/bin/www" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^bin\/www\/debug[\/]?" />
</rule>

<!--
First we consider whether the incoming URL matches a physical file in the /public folder.
This means IIS will handle your static resources, and you don't have to use express.static
-->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>

<!-- All other URLs are mapped to the node.js entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="/bin/www"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Posted in Uncategorized

Currently Reading – Success in Programming

Just thought I would share what I am currently reading whilst on the daily commute.

I’m about half way through the book but it does make for a good read and allows you to increase “your brand”.

Amazon Link.

download

Posted in Uncategorized

New idea for a website –

Just had a quick thought – i’m looking to get the public to test my fridgemagnet test project but could not find anywhere centrally to say “hey come and take a look”.

Is there any value in a website that people can post assets/urls and others can review/post comments etc?

Or have i just not found it yet – I know you can use groups but is there something else out there?

Posted in Uncategorized

My annoying ‘gotcha’ in Azure Table Storage and Node.js

I have been dabbling in a little pet project on and off which recreates an on-line fridge magnet scenario. http://fridgemagnet.azurewebsites.net/client1.html.

Don’t get excited about the code structure, this is a work in progress/proof of concept project so please, no comments.

Anyway, the issue I was facing was that I was using Azure simple table storage to persist the state between sessions but for some reason, I wasn’t  able to save to the table storage.

Everything was reporting correctly so i assumed that it was saving the data but it wasn’t.

The original code was this

var entGen = azure.TableUtilities.entityGenerator;


var task = {
PartitionKey: entGen.String(partkey),
RowKey: entGen.String('1'),
data: stringrep
};

Where stringrep was a string representation of my array of tiles.

The system always reported that the task was successful and no errors were reported.

I finally found a little comment written by another which highlighted a ‘gotcha’.

Essentially, the code above needed to be written as follow:

var task = {
PartitionKey: entGen.String(partkey),
RowKey: entGen.String('1'),
data: entGen.String(stringrep)
};

Note the change “entGen.String(stringrep)” – doing this was the silver bullet and suddenly the data actually saved.

I hope this helps anyone else out there that is having starter issues with Azure table storage.

Posted in Uncategorized