Tuesday, March 18, 2014

Scala and XML

I don't use Scala very often and consider myself to be quite a beginner with it.  It has been a long while since I worked with it.  So when I heard a recent request to help someone with the generation of XML data for a project, I decided to use the opportunity to brush up on my Scala and try out the support for XML.

In short, the solution I came up with is not bad and relatively short.  It likely could be done better though.  What I found interesting is that it took me an hour or two to sort out some of the details.  The somewhat embarrassing part is that someone did some work in MS Word and produced a 90% solution in a much shorter time though.  In this case, the elegance of the language and solution was a bigger draw to me than the much lower tech but practical solution.  I find myself somewhat embarrassed by this but at least it did help me brush up on things a bit.

Another somewhat negative note is that it sounds like (per some messages on the Scala site) the XML support is somewhat an unwanted stepchild.  It sounded like those closely involved with the languages evolution would prefer to move to either the string interpolation or something similar/related to it.  I think a number of people indicated problems with the way it works now.  I didn't really take the time to understand the details of the arguments.

I will say that the XML support was not 100% straight forward.  Things went easier once I gave up trying to perform what I think would be described as partial String interpolation in attribute text values.  In my final result, I end up pushing in "completed" attribute values instead of trying to do something like replacements in the middle of the overall text attribute value. 

In other words, in the user_template definition below;
      DisplayName={sdisplayName}
works while something like:
     DisplayName="{lastName}, {firstName}"
 did not work.

So my final code had to generate each attribute value in its entirety which was then passed into the function and used to replace the attribute text place holder in the XML.

So even if this didn't work exactly as I had hopped, it was still useful.  If the group managing the language are able to provide functionality that is more natural and powerful though, it would be immensely useful.  I am very grateful for the work they are doing.

I also want to note that I was working on this in Eclipse Kepler Release 2 and it worked pretty well.  More nice work..

import scala.io.Source._
import scala.xml.Node
object fmtdata extends App
{
  def outer_template(users : List[Node]) =
   <PrincipalMappingConfiguration
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Entries>
        <PrincipalMappingEntry
        SourceAddress="https://test.org/"
        DestinationAddress="https://test.org/teams/">
          <MappingInformation>
            <Mappings>
              {users}
            </Mappings>
          </MappingInformation>
        </PrincipalMappingEntry>
      </Entries>
      </PrincipalMappingConfiguration>;

  def user_template(sacctname : String, dacctname : String, email: String, sdisplayName : String, ddisplayName : String) =
      <PrincipalMapping>
        <SourcePrincipal AccountName={sacctname}
              UserInfoId="0"
              DisplayName={sdisplayName}
              Email={email} Title="" PrincipalType="User" />
            <DestinationPrincipal AccountName={dacctname}
                        UserInfoId="0"
                        DisplayName={ddisplayName}
                        Email={email}
                        Title=""
                        PrincipalType="User"
                        Department="" />
          </PrincipalMapping>;

    val header = "<?xml version=\"1.0\"?>";
    val infile = fromFile("c:\\temp\\users.csv")
    val lines = infile.getLines().toList map { _.split(",") }
    infile.close()

    var users: List[Node] = List[Node]()

    // lname, fname, vcid, eadid
    lines.foreach{ f =>
         users = users ++
           (user_template("i:0#.w|org\\"++f(2),     /*    source id    */
                   "i:0#.w|dir\\" ++ f(3),  /*    dest id        */
                   f(2) ++ "@test.org",      /*    email    */
                   f(1) ++ " " ++ f(0),     /* source name */
                   f(0) ++ ", " ++ f(1)));  /*    dest name*/
    }

    println( header )
    println( outer_template(users) )
}

The input file, users.csv would look something like this (dummy data):

myLastName1,myFirstName1,myDomainAcctA1,myDomainAcctB1
myLastName2,myFirstName2,myDomainAcctA2,myDomainAcctB2
myLastName3,myFirstName3,myDomainAcctA3,myDomainAcctB3
myLastName4,myFirstName4,myDomainAcctA4,myDomainAcctB4
smith,myfirstname,mysmith,smithm


The output would then look something like:
<?xml version="1.0"?>
<PrincipalMappingConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Entries>
        <PrincipalMappingEntry SourceAddress="https://test.org/" DestinationAddress="https://test.org/teams/">
          <MappingInformation>
            <Mappings>
              <PrincipalMapping>
        <SourcePrincipal AccountName="i:0#.w|org\myDomainAcctA1" UserInfoId="0" DisplayName="myFirstName1 myLastName1" Email="myDomainAcctA1@test.org" Title="" PrincipalType="User"/>
            <DestinationPrincipal AccountName="i:0#.w|dir\myDomainAcctB1" UserInfoId="0" DisplayName="myLastName1, myFirstName1" Email="myDomainAcctA1@test.org" Title="" PrincipalType="User" Department=""/>
          </PrincipalMapping><PrincipalMapping>
        <SourcePrincipal AccountName="i:0#.w|org\myDomainAcctA2" UserInfoId="0" DisplayName="myFirstName2 myLastName2" Email="myDomainAcctA2@test.org" Title="" PrincipalType="User"/>
            <DestinationPrincipal AccountName="i:0#.w|dir\myDomainAcctB2" UserInfoId="0" DisplayName="myLastName2, myFirstName2" Email="myDomainAcctA2@test.org" Title="" PrincipalType="User" Department=""/>
          </PrincipalMapping><PrincipalMapping>
        <SourcePrincipal AccountName="i:0#.w|org\myDomainAcctA3" UserInfoId="0" DisplayName="myFirstName3 myLastName3" Email="myDomainAcctA3@test.org" Title="" PrincipalType="User"/>
            <DestinationPrincipal AccountName="i:0#.w|dir\myDomainAcctB3" UserInfoId="0" DisplayName="myLastName3, myFirstName3" Email="myDomainAcctA3@test.org" Title="" PrincipalType="User" Department=""/>
          </PrincipalMapping><PrincipalMapping>
        <SourcePrincipal AccountName="i:0#.w|org\myDomainAcctA4" UserInfoId="0" DisplayName="myFirstName4 myLastName4" Email="myDomainAcctA4@test.org" Title="" PrincipalType="User"/>
            <DestinationPrincipal AccountName="i:0#.w|dir\myDomainAcctB4" UserInfoId="0" DisplayName="myLastName4, myFirstName4" Email="myDomainAcctA4@test.org" Title="" PrincipalType="User" Department=""/>
          </PrincipalMapping><PrincipalMapping>
        <SourcePrincipal AccountName="i:0#.w|org\mysmith" UserInfoId="0" DisplayName="myfirstname smith" Email="mysmith@test.org" Title="" PrincipalType="User"/>
            <DestinationPrincipal AccountName="i:0#.w|dir\smithm" UserInfoId="0" DisplayName="smith, myfirstname" Email="mysmith@test.org" Title="" PrincipalType="User" Department=""/>
          </PrincipalMapping>
            </Mappings>
          </MappingInformation>
        </PrincipalMappingEntry>
      </Entries>
      </PrincipalMappingConfiguration>

Wednesday, March 12, 2014

Chick-fil-a : Fun with the cow

I find so much fun making jokes with the person wandering around in the cow outfit at our local Chick-fil-a.  My kids groan when we are there and the cow is roaming because they know what I MUST do.  I really don't know why they think the jokes are cheesy.. :)

Howdy cow friend; do you like music?  I really like to listen to Moooszart.

What is your favorite moovie?

Did you study Mooossolini in your history class?

When my children are not feeling well, they milk it for all it is worth.

How about a hug out of udderly love?

If you want a cow glare, you can do the:  "You are working really hard, do you have a steak in the business?"

Kid:  "I want chicken nuggets! "
me:  "I don't have a beef with that."

Some days we are just a bit off and act moooody.

When we start talking about various illnesses and such, the kids just can't stomach it.

Cud I have a shake please?

When I nearly backed into the ditch, the family about had a cow.

You are really a-grazing!

Brother, cud you spare a dime?

worker:  Sorry, we are out of unsweet tea.
me:  Are you sear-ious!

Another one good for a glare:  Can I get calf a sandwitch?

Occasionally, the kids cud use a good tanning of their hides for sear-ious misbehavior.

I need calf-inated coffee in the morning!

Heiffer-got to tie his shoe.

Giving the cow some spare change:  Cow tipping

You're quiet, cow got your tongue?

You're panting, have you been hoofing it?

I know I forgot a bunch of them now.  I guess I should start writing these down quicker instead of sitting around and chewing the cud with folks.

Will hopefully remember some more and update later. 


Hope you found these aMOOsing!
May God bless your day.
Scott




Wednesday, March 5, 2014

Research for home shop: Iron Workers, Presses, Mills and Welders

My general list of brands of mills/presses/iron workers  and research info is currently at:

https://docs.google.com/document/d/1WApJOZUBsJ_a8FNN6cx69BuRYD10-3Zvb_Z0Z2iWw0w/edit

I would really like to put together a nice home shop (need a little more space though).  Maybe a nice enough setup to do small jobs for $.  Could be a nice post retirement activity if retirement is ever possible.

Other than the space issue, it is somewhat hard to define what is the appropriate machine capacity for a home shop.  This is especially true without some experience with this type of equipment and a good understanding of what types of projects I may want to tackle.

Still thinking about the types of possible projects - ranging from mundane carts/storage, large rebar type trellises all the way up to aluminum greenhouse frames, etc. 

Cost is also a factor.  What features on a mill would I regret not having if I either have a desire to do lots of hobby work or decide to try and make a few dollars with it?  What features are easily added later?  What is best purchased versus self-built?  I believe I could safely construct a press but I am less sure whether I can do it cost effectively - less mechanical engineering knowledge will translate into overbuilding to stay safe.  I do think it would be a fun project though.

There is also the risk with used equipment.  Without a knowledgeable friend, it seems somewhat difficult to determine the state of used mills.  I did see a few blog posts with some good practical advice - listen to your intuition and remember a "too good deal" is likely not a deal type things.  I don't have experience tramming a mill or working with DTI equipment so I would agree with some blogs stating it would be a mistake trying to sort that out when checking out a potential mill for purchase.

An interesting reference regarding checking out a used mill.
http://www.mermac.com/freemill2.html

It may turn out to be an adventure.

The below lists are by no way complete but these are the most common sites I have looked at for much of the larger equipment I have an interest in.

Some metal working tool, equipment and accessory manufacturers:
  1. http://www.aceronline.net/acergroup/mills.htm
  2. http://www.americanmachinetools.com/milling_machines.htm
  3. http://metal.baileighindustrial.com/
  4. http://www.birminghammillingmachines.com/
  5. http://www.bpt.com/
  6. http://www.boltonhardware.com/category/bolton-tools/
  7. http://www.grizzly.com/
  8. www.harborfreight.com/
  9. http://www.jettools.com/us/en/home/ 
  10. http://kbctoolsandmachinery.com/category/show/Milling+Machines/Manual/
  11. http://www.kentusa.com/mills/
  12. http://www.lagun.com/vertical-milling-machines.php
  13. http://www.littlemachineshop.com/
  14. http://www.manford-usa.com/
  15. http://www.mscdirect.com/ 
  16. http://www.micromark.com/machine-tools.html
  17. http://www.northerntool.com/shop/tools/category_metal-fabrication
  18. http://www.proxxontools.com/store/pc/home.asp
  19. http://www.robotshop.com/en/tabletop-milling-machines.html
  20. http://www.rongfu.com/en/milling-drilling-machine.html
  21. http://www.sharp-industries.com/mills.html
  22. http://www.sherline.com/tools.htm
  23. http://www.siegind.com/products.html
  24. http://www.smithy.com/
  25. http://www.southern-tool.com/store/metalworking_mills.php
  26. http://www.taigtools.com/
  27. www.use-enco.com
  28. http://www.wells-index.com/
  29. http://tool.wttool.com/tools/Milling%20Machine
Great resources I lurk at a lot.  Internet searches on metal working and welding help end up here for me a lot.
Some sites which handle used (and new lots of the time) equipment.  I have not bought anything from any of these sites but do a lot of window shopping.

Monday, March 3, 2014

Raspberry Pi bluetooth

Just noting some success with setting up a Raspberry Pi with Bluetooth support.  My goal is to get it working with the Wii Remote for projects that I and my son find.

Raspberry Pi Model B
Cirago Bluetooth 3.0 High Speed & Wi-Fi combo USB mini adapter; BTA7300

Linux version 3.10.32+ (dc4@dc4-arm-01)
(gcc version 4.7.2 20120731 (prerelease) (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08) ) #648 PREEMPT Fri Feb 28 16:29:56 GMT 2014

Notes:
  • Plugged directly into PI.
  • Had previously used a different Wi-fi adapter but unplugged it, plugged this in and wi-fi worked (once I added the MAC to the router filter to allow access)
  • I keep the os/firmware updated fairly regularly – updated before these installs
  • Using the default available Bluez modules (4.x) instead of the 5.0 which I would have to compile.  I may try to compile 5.0 at some point it appears this should work for some things.  Here is what I installed.
    • sudo apt-get install bluez
    • sudo apt-get install bluez-tools
    • sudo apt-get install bluez-utils
    • sudo apt-get install bluez-firmware
  • I have not fully played with the Bluetooth but was able to see my cell phone and briefly had it paired (left a registration on the phone) but I couldn’t think of anything useful at the time to try other than list out some info.
    • bt-device --connect <mac>
    • bt-device --services <mac> 
I have not fully decided what language to use when playing with the Bluetooth.  It appears that C, Python and Java are possible.  I will likely install the wrapper libs and such for Python and try that since that is likely easier for my son.  The Java support appears to be via JSR-82 but it sounds like that has some shortcomings.

Python bindings
sudo apt-get install python-smbus python-cwiid python-scipy python-numpy python-pygame python-setuptools libsdl-dev

Successful testing of wii Remote!
Using the site below related to Python & Wii Remote, I was able to see the button values changing and mess with the LEDs.

Partial excerpts from the www.cl.cam.ac.uk site [mentioned below].  Pulling the example in here just to have a small but functional one in a single place.  Refer to the source site referenced for more details on what the steps are doing.

sudo hciconfig hci0 piscan

python

import cwiid
 
# I found it most reliable to press buttons 1 & 2 just
# prior to executing the next statement interactively.
 
wm = cwiid.Wiimote()
 
import time
for i in range(16):
  wm.led = i
  time.sleep(0.5)
 
# Interactively, I pressed an extra return to get it running. I am
# not a Python programmer so even little things are nice to note...
  
Resources
I found this helpful for the Bluetooth.

man –k bt was helpful in finding the related commands after installing everything.
bt-device
bt-adapter
bt-agent
bt-input
bt-monitor
bt-network
bt-obex
bt-serial

JSR 82 - Bluetooth

Some Bluetooth/Python info
http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/downloads/

Python interacting with Wiimote
http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/wiimote/

Another good reference & example is:
http://www.raspberrypi-spy.co.uk/2013/02/nintendo-wii-remote-python-and-the-raspberry-pi/