Tuesday, October 31, 2017

Golang JSON Marshal And Unmarshal With Interface

Problem:


Cannot deserialize to an interface.

Example:

Pet is an interface with a method called Type() that returns the type of pet.  Dog and Cat implement Type() and return "dog" and "cat" respectively.  House has one pet.

package main

import (
"encoding/json"
"fmt"
)

func main() {
// House has a pet dog.
h := House {
Pet: &Dog{},
}

b, err := json.Marshal(h)
if err != nil {
panic(err)
}


h2 := House{}
err = json.Unmarshal(b, &h2)
if err != nil {
panic(err)
}

// Expect dog
fmt.Println(h2.Pet.Type())


// House has a pet cat.
h = House {
Pet: &Cat{},
}

b, err = json.Marshal(h)
if err != nil {
panic(err)
}

h2 = House{}
err = json.Unmarshal(b, &h2)
if err != nil {
panic(err)
}

// Expect cat
fmt.Println(h2.Pet.Type())
}

type House struct {
Pet Pet
}

type Pet interface {
Type() string
}

type Dog struct {
}

func (d *Dog) Type() string {
return `dog`
}

type Cat struct {
}

func (c *Cat) Type() string {
return `cat`
}

https://play.golang.org/p/mYYK7L7IQK


When you run this code, you get this error  "json: cannot unmarshal object into Go struct field House.Pet of type main.Pet" because it doesn't how to unmarshal it an interface Pet.

Solution:


package main

import (
"encoding/json"
"fmt"
)

func main() {
// House has a pet dog.
h := House {
Pet: &Dog{},
}

b, err := json.Marshal(h)
if err != nil {
panic(err)
}


h2 := House{}
err = json.Unmarshal(b, &h2)
if err != nil {
panic(err)
}

// Expect dog
fmt.Println(h2.Pet.Type())


// House has a pet cat.
h = House {
Pet: &Cat{},
}

b, err = json.Marshal(h)
if err != nil {
panic(err)
}

h2 = House{}
err = json.Unmarshal(b, &h2)
if err != nil {
panic(err)
}

// Expect cat
fmt.Println(h2.Pet.Type())
}

type House struct {
Pet Pet
}

func (h *House) UnmarshalJSON(data []byte) error {
var m map[string]interface{}
err := json.Unmarshal(data, &m)
if err != nil {
return err
}

pet := m["Pet"]
if pet == nil {
return fmt.Errorf("cannot unmarshal pet")
}

m = pet.(map[string]interface{})
data, err = json.Marshal(m)
if err != nil {
return err
}

name := m["PetName"]
if name == nil {
return fmt.Errorf("cannot unmarshal pet")
}


p, ok := pets[name.(string)]
if !ok {
return fmt.Errorf("cannot unmarshal pet %q", name)
}

err = json.Unmarshal(data, p)
if err != nil {
return err
}

h.Pet = p
return nil
}

var pets = map[string]Pet {`Dog`: &Dog{}, `Cat`: &Cat{}}

type Pet interface {
Type() string
}

type Dog struct {
}

func (d *Dog) Type() string {
return `dog`
}

func (d *Dog) MarshalJSON() ([]byte, error) {
type MyCopy Dog
return json.Marshal(&struct {
PetName string
*MyCopy
}{
PetName: `Dog`,
MyCopy:      (*MyCopy)(d),
})
}

type Cat struct {
}

func (c *Cat) Type() string {
return `cat`
}

func (c *Cat) MarshalJSON() ([]byte, error) {
type MyCopy Cat
return json.Marshal(&struct {
PetName string
*MyCopy
}{
PetName: `Cat`,
MyCopy:      (*MyCopy)(c),
})
}

https://play.golang.org/p/Wc_eMVtOpy

Wednesday, July 6, 2016

Best Lenses for Sony a6000 or Sony a6300

Problem:

I need great lenses for Sony a6300.



Solution:


I've been doing some research, and here's what I've gathered so far.

Prime:


These are excellent, affordable primes.

Sigma 30mm f/1.4 DC DN Contemporary Lens for Sony E

Price: $339.00 (Amazon)
DxOMark Score: 29
Weight: 265 grams


Sigma 30mm F2.8 EX DN Art for Sony SE

Price: $169.00 (Amazon)
DxOMark Score: 23
Weight: 130 grams


Sony E 35mm f/1.8 OSS

Price: $398.00 (Amazon)
DxOMark Score: 23
Weight: 155 grams


Sony E 50mm f/1.8 OSS

Price: $248.00 (Amazon)
DxOMark Score: 25
Weight: 202 grams



Sony 55mm F1.8 Sonnar T* FE ZA 

Price: $898.00 (Amazon)  (This is the most expensive one on the list.  I won't be getting it! :)
DxOMark Score: 27
Weight: 281 grams



Sigma 60mm F2.8 EX DN Art for Sony SE

Price: $209.00 (Amazon)
DxOMark Score: 25
Weight: 190 grams


Zoom:


Sony Vario-Tessar T E 16-70mm F4 ZA OSS

Price: $998.00 (Amazon)
DxOMark Score: 17
Weight: 308 grams


Sony E PZ 18-105mm F4 G OSS

Price: $598.00 (Amazon)
DxOMark Score: 15
Weight: 427 grams



Conclusion:


I'm leaning toward getting the Sigma 30mm 1.4 for street photography/portrait, the Sony 50mm or Sigma 60mm for portrait photography, and Sony 18-105mm for everything else.

Note that the sigma lenses do not have image stabilization.  The sigma 30mm 1.4 is similar in size to Sony 55mm.  The Sony 18-105mm has internal zoom, meaning that it doesn't protrude.

Wednesday, May 4, 2016

Force quit and force restart a unresponsive application in MacOS

Problem:

Cannot force quit a not responding application in MacOS.  Try force quit from the Dock.  Try Command+Option+Esc to bring up the dialog to force quit there.  Try restart the computer.  The computer doesn't restart and does nothing

Solution:

Open up Terminal and restart there

sudo shutdown -r now

WeMo LED Lighting Starter Set - Bulbs Not Detected

Context:

WeMo Lighting Starter Set
iOS Wemo App

Problem:

 The WeMo Lighting Starter Set comes with two bulbs, but they cannot be detected.  It is one of the most frustrating things!  I've lost hours trying to get them to work.  Stupid thing!  I've tried resetting the link, reinstalling the app, resetting the bulbs so many times, but the bulbs do not get detected during scan.  The link somehow does not see them.

Solution:

After a lot of tries, I have finally been able to detect them and add them.  Victory at last!

Ok, I'm not sure if this will help you, but you can try my steps:
  1. Reset the link
    1. Press and hold the little reset button on the link for about 5 seconds while plugging it in (same as instructions written on it) to restore to factory settings.
    2.  You will first see the amber light blinking.  This may take some time, and then, you will see the light blinks amber and green alternately.  When you see this, it is ready to be hooked up.
    3. Go WiFi into Settings (iPhone) and connect to the Wemo link.  You should see an SSID something .Link.
    4. Connect to it
    5. Open Wemo app.  It should say that you are connected to the link and that it's trying to open a connection.
    6. It will try to connect to your WiFi network next.
    7. If all successful, it will go into the scanning mode to detect the bulbs.
    8. If your bulbs are found, great!  (For me, nothing is found!)
  2. If your bulbs are not detected, follow the steps below to reset the bulbs.  You can try one bulb, and then add another later.  Note that you do not have to reset the link every time you add a bulb.  You can go to the "Add a device" in the settings of the WeMo App to scan and add new devices.
    1. The instructions on the support page as listed below.  However, I followed exactly that and it DID NOT work for me.  What worked for me was that instead of 1 second as instructed in step 2, 4, 6, I did about 3 seconds for each of the steps.  If the bulb flashes, it's been reset successfully.  If it succeeds, open the WeMo app, go to add a device and scan.  If all is good, you should be able to detect the bulb.  Repeat the same process to add the other bulb.
      Step 1:
      Start with the WEMO LED Lightbulb being in the ON position.
      User-added image

      Step 2:
      Turn it OFF for one (1) second.

      Step 3:
      Turn it ON for 3-4 seconds.

      Step 4:
      Turn it OFF for one (1) second.

      Step 5:
      Turn it ON for 3-4 seconds.

      Step 6:
      Turn it OFF for one (1) second.

      Step 7:
      Turn it ON and leave it as it is.  After about five (5) seconds, the WEMO LED Lightbulb will flash which shows a successful reset.

Wednesday, December 2, 2015

WIFI Information in Mac OS


What's my connection mode: 802.11g or 802.11n or whatnot?  What's my current transmission speed?

Hold down the "alt/option" key on the keyboard and click on the Wifi indicator as shown in the picture below.  As you can see, you ca see more information.  Right now, I'm connecting with 802.11n at 52Mbps.  The speed fluctuates.  



WD My Cloud EX2 - Fan Problem - Manual Control

Context:


WD My Cloud EX2 8TB

Problem:


Fan speed is always at 0 RPM even when the temperature gets around 56 degrees Celsius.  I thought it was just a defective unit, so I called Western Digital to get a replacement.  Unfortunately, the replacement had the same problem.

Side note:  you have to pay for shipping to return the old unit to them.  I thought that they would send me a prepaid label, but that wasn't the case.  I paid around $20 for UPS shipping.

Solution:


The problem is a software one.  To manually control the fan, you will have to ssh in and run the fan.

First to enable SSH, go to Settings -> Network, under Network Services, you will find SSH.  Turn that on.  You will get a warning about voiding the warranty.  This warning stopped me first time for not going in.

Anyway, if you decide to proceed and configure your SSH.

I'm using a Mac, so from my terminal, I run

ssh sshd@[name of your unit here]

Once you're in, there's a command call "fan_control"

fan_control -h

Will give you a list of options

*** Fan Cobtrol Help Message ***

fan_control b c: for NAS booting
fan_control 0 d : [auto] open debug msg
fan_control 0 c : [auto] close debug msg
fan_control -L [value] : set Lower (THYST)
fan_control -H [value] : set Upper(TOS)
fan_control -g 0 : get current temperature
fan_control -g 1 : get Lower temperature
fan_control -g 2 : get Upper temperature
fan_control -g 3 : get fan state
fan_control -g 4 : get fan rpm
fan_control -f 0 : set fan stop
fan_control -f 1 : set fan rpm4000
fan_control -f 2 : set fan rpm5000
fan_control -f 3 : set fan rpm6000
fan_control -f 4 : set fan rpm7000
fan_control -f 5 : set fan rpm8000
fan_control -f 6 : set fan rpm9000

Now as you can see, to get the current temperature, you run

fan_control -g 0

To set the fan speed at rpm4000, you can run

fan_control -f 1

To stop, run

fan_control -f 0

And friends, that's how you can manually control it.

If you want to monitor the temperature, say every minute, included is another utility called "watch" that you can use.

watch -n 60 fan_control -g 0

Here, I'm saying that gives me the temperature every minute.  60 there is 60 seconds.  To quit, you can simply do Control + C

Hope that they will fix the issue in the next firmware update.

Friday, January 25, 2013

iPad Mini 2? When?

I can't wait for iPad Mini 2 to come out with Retina Display.  I played around with the current iPad Mini, and the display really bothered me.  Since I'm using a Retina Display MacPro, a Retina Display iPad and a Retina Display iPhone, I notice the resolution difference right away, and I can't stand it.  If you're coming from "Retina Display" (Apple's Marketing Term), you better wait for a next generation of the iPad Mini.  If you buy one now, you will be greatly disappointed.  Don't know when that will come out though.  There have been a lot of rumors around the March time frame.

Read Zip and Look At XML in Zip To Count Elements With A Specific Attribute

import xml.sax
import zipfile
import re

counter = 0

class MyContentHandler(xml.sax.ContentHandler):
    def __init__(self):
        xml.sax.ContentHandler.__init__(self)

    def startElement(self, name, attrs):
        if attrs.has_key("color"): # search for the element with the attribute of color
            global counter
            counter += 1

def count(myZip):
    zf = zipfile.ZipFile(myZip)  # read in zip
    files = zf.namelist() # get the files in zip
    for file in files:
        if re.search(".*myXML.xml", file) is not None:  # using regular exp to find file
            f = zf.open(file) # open file
            xml.sax.parse(f, MyContentHandler()) # parse the xml
            print counter # print counter
            break

if __name__ == '__main__':
    import sys
    if len(sys.argv) == 2:
        try:
            count(sys.argv[1])
        except IOError:
            print 'Filename not found.'
    else:
        import os
        print 'usage: %s zip' % os.path.basename(sys.argv[0])

Wednesday, January 23, 2013

Free Online Storage in the Clouds


Here are some free ones and their sizes:

Box Dropbox Google Drive iCloud MediaFire Mega SkyDrive
5 GB 2 GB 5 GB 5 GB 50 GB 50 GB 7 GB

iPhone Messages App Doesn't Rotate To Landscape

Context:

iPhone 5
iOS 6

Problem:

The Messages app doesn't rotate to landscape when I want to text in landscape mode.

Solution:

Reboot the phone. Power off and power on the phone again using the power button.

Monday, January 14, 2013

Windows Keeps Rebooting in VirtualBox

Context:

VirtualBox
Windows 2008 Server R2

Problem:

Windows keeps restarting in VirtualBox without giving the chance to log in.  You see the screen where it asks you to do CTRL+ALT+DELETE to log in, but it will be restarted over and over again.

Solution:

While it keeps rebooting, do F8.  Hopefully, you get the screen where you can choose the last known configuration of Windows.

The action can't be completed because the file is open in DHCP Client

Context:

Windows 2008 Server

Problem:

Cannot delete an event log

Solution:


  • Make sure that your service is not running
  • You can uninstall it or force a deletion (sc delete <service name>)
  • Remove the log registry entry under [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog]
  • Restart the machine
  • Delete the log file

Friday, January 4, 2013

Windows Server 2008 R2 Random Shutdowns

Context:

Windows Server 2008 R2
VirtualBox

Problem:

Windows Server randomly shuts itself down every hour.

System log shows:

The process wlms.exe has initiated the power off of computer WIN-P4EMG3VI42J on behalf of user NT AUTHORITY\SYSTEM for the following reason: Other (Unplanned)
 Reason Code: 0x0
 Shutdown Type: power off
 Comment:


Solution:

Windows is unactivated and needs to be activated.

If you look in the Application log, you should see this message "The license period for this installation of Windows has expired.  The operating system will shutdown every hour."

Sunday, December 2, 2012

Random

dầu em có ở nơi đâu, bên cạnh một người nào, chỉ cần em hạnh phúc vui vẻ...

Wednesday, November 28, 2012

Mac OS Wi-Fi Settings

Context:

Mac OS

Problem:

Looking for Wi-Fi settings such as speed and whatnot

Solution:

Here are some ways:

  • Hold down "option" key on the keyboard and click on the top place where you connect to the wifi network
  • Apple -> About This Mac -> More Info...
    • Lion:  -> System Report -> Network -> Wi-Fi
    • Snow Leopard: -> Network ->AirPort
  • Command+Space to bring up the spotlight search
    • Type in "Network Utility"


Thursday, November 22, 2012

iPad Retina Display (4th Generation) and Nexus 7 32G - No Waiting In Line - Get It Now Online

Yay!  Got my tablets.  I don't have to waste my precious sleep and wait in crazy long lines tonight.

Here's where I got my tablets:

Best Buy:

Two new iPads with Retina Display (4th Generation ) for $439.99 (excluding tax) a piece.  (free shipping)

Office Depot:

Google Nexus 7, 32G for $229.99 (excluding tax).  (free shipping)

Happy Thanksgiving, Everyone!

Eat, sleep, laugh, love...

Monday, November 19, 2012

Black Friday 2012 Best iPad 2, iPad 4 and Google Nexus 7 Deals

If you're looking for an iPad 2, iPad 4 or a Google Nexus 7 this Black Friday, here are the places with the best deals that I know of at this time.

iPad 2:  Walmart  $399 with $75 gift card
iPad 4:  Target $499 with $60 gift card
Google Nexus 7:  Office Depot $229 ($20 off from $249 for a 32G)

Thursday, October 18, 2012

Reading ZipEntry content from ZipInputStream

Context:

Java
MySQL
Zip

Problem:

I needed to search the content of a specific file in the Zip which was read in from the database.  I had no direct access to the Zip file except to the input stream of the Zip file.

Solution:

InputStream inStream = blob.getBinaryStream();  
// the blob contains the zip

ZipInputStream zipInStream = new ZipInputStream(inStream);

ZipEntry zipEntry = null;
while((zipEntry = zipInStream.getNextEntry()) != null) {
  String filename = zipEntry.getName();
  if(filename.matches("file_name_that_i_want_to_look_at") {
    StringWriter writer = new StringWriter();
    int i;
    while((i = zipInStream.read()) !=  -1) {
         writer.write(i);   
    } 

    String content = writer.toString();  // content of the file
  }
}

Saturday, October 13, 2012

How to remove Facebook synced contacts from iPhone running iOS 6

1. On your iPhone, go to Settings, you should see the Facebook app there.

2. In Facebook settings, turn "Contacts" off.




3. Hit the "Update all contacts"

Tuesday, October 2, 2012

Macally Folio Stand Case (Red/White) For iPhone 5 (SLIMCOVER5R)

 

 My order of this case arrived today from Macmall.  Here are some of my pros and cons:

Pros:
  • The case looks really nice.
  • The leather is shining.
  • The inside cover is soft.
  • The feel is good when holding in hand.
  • Not bulky.
  • Magnetic cover, holding the cover together when closed.
  • Feeling that phone is safe and secured in case.
Cons:
  • Unnecessary latch:  The pictures from the Macally don''t show it, but there's a little red plastic latch.  If you look at the picture above, there's that red latch in the middle.  There's one like that on the opposite side to hold the phone secured.  Why do I think it's a con?  Well, because I like to use it as a stand and having it there prevents me from doing so.  Having it there is unnecessary in my opinion (the other three you see in the picture above should be enough).  The latch makes it hard to quickly put it in stand mode.  You have to unlatch your phone first.  Maybe, it's just me, but I'm afraid that by taking phone out and putting it in may scratch that side of my phone.
  • The leather is very nice but not strong enough I think.  I was holding the case with the phone in my hand along with some boxes of hair dye (I was shopping for my mom :).  Unintentionally, I must have held it at such an angle that one of the corners of a box was poking at the case; now, I have a small cut on the case.  It was my fault.  Not sure if other kinds of leather would have had the same damage by the same force.  Since I got the white case, the cut is more visible.  Well, I guess the case did its job of protecting.  LOL
Overall:
  •  So far, I like it.  I know there's no perfect case.  I would have liked it a lot more if it were not for those two cons mentioned above.