Monday, May 20, 2013

Tuesday, March 12, 2013

Disable sitecore cahe

To tern off sitecore caching set following values in sitecore config.
<!--  CACHING ENABLED
 Determines if caching should be enabled at all
 Specify 'true' to enable caching and 'false' to disable all caching
-->
<setting name="Caching.Enabled" value="false" />
<!--  DISABLE BROWSER CACHING
 If true, all pages will have:
   Cache-Control: no-cache, no-store
   Pragma: no-cache
 in the http header
-->
<setting name="DisableBrowserCaching" value="true" />

Monday, March 11, 2013

MSBuild copying all files in folder to destination folder

Typical task as for me, after build I want to copy some files from one location to other. All files that exist in folder. The not working approach looks like:
I would think that it should work, but it doesn't
<Target Name="AfterBuild">
<Copy SourceFiles="Foo\*.*" DestinationFolder="$(OutputPath)" ContinueOnError="true" />
</Target>
The working solution is below
<ItemGroup>
    <_CopyDeployableAssemblies Include="Foo\*.*" />
</ItemGroup>
<Target Name="AfterBuild">
<Copy SourceFiles="@(_CopyDeployableAssemblies)" DestinationFolder="$(OutputPath)" ContinueOnError="true" />
</Target>
It will copy all files from folder Foo to Bin folder.

Sunday, March 10, 2013

Connecting lm298n with arduino


The connection is really strait forward. From arduino pins 6,7,5,4 connect accordingly to inputs IN1, IN2, IN3, IN4 in lm298n. And that's it, it will work. In terms of power supply, there are 2 power inputs in lm298n (5V, VCC), 5V can be connected to arduino, but if your motor requires more power, then you need to connect additional power supply to VCC. It is a really good practice to connect all GND cable together, I mean from additional power supply and from arduino. A code below increase a speed of two motors connected to lm298n and then decrease it.

I started recently my new mobile robotics project, and a picture shows a mobile framework connected to lm298n and arduino.
// Initialize
int PWM1   = 6; // PWM Pin Motor 1
int PoM1 = 7;   // Polarity Pin Motor 1
int PWM2   = 5; // PWM Pin Motor 2  
int PoM2 = 4;   // Polarity Pin Motor 2
 
int ValM1   = 0; // Initial Value for PWM Motor 1 
int ValM2   = 0; // Initial Value for PWM Motor 2
 
int i = 25;     // increment
// Used to detect acceleration or deceleration
boolean goUp = true ; 
 
void setup() 
{
  pinMode(PWM1,   OUTPUT); 
  pinMode(PoM1,   OUTPUT); 
  pinMode(PWM2, OUTPUT);   
  pinMode(PoM2, OUTPUT);   
  digitalWrite(PoM1, LOW) ;   // Both motor with same polarity
  digitalWrite(PoM2, LOW) ;
  analogWrite(PWM1, ValM1);   // Stop both motors => ValMx = 0
  analogWrite(PWM2, ValM2);    
  Serial.begin(9600);         // Used to check value 
}
 
// Main program
void loop()
{
  // give some time to the motor to adapt to new value
  delay (500) ;               
  if ((ValM1  < 250) && goUp) // First phase of acceleration
  {
      ValM1 = ValM1 + i ;     // increase PWM value => Acceleration
      ValM2 = ValM2 + i ;      
  }
  else
  {
    goUp = false ;            // Acceleration completed
    ValM1 = ValM1 - i ;       // Decrease PWM => deceleration
    ValM2 = ValM2 - i ;   
    // My motor made fanzy noise below 70  
    if (ValM1  < 75)           
    {                         // One below 75, I set to 0 = STOP
       ValM1 = 0 ;
       ValM2 = 0 ;
       goUp = true ;          // deceleration completed
    }
  }
  // If PWM values are OK, I send to motor controller
  if ((ValM1 > 75) && (ValM1 < 255))  
  {
    analogWrite(PWM1, ValM1);  
    analogWrite(PWM2, ValM2);
  }  
  Serial.print(ValM1);        // Debug. Print Value Motor 1
  Serial.print("\t");         // Print tab
  Serial.println(ValM2);      // Print Value Motor 2 to Serial
}
// End.

Wednesday, March 6, 2013

Reseting admin password in Sitecore

Sitecore inside Core db stores users information. In order to reset password to 'b' for a user 'admin' one has to run a following script
UPDATE [aspnet_Membership] SET Password='8dC23rEIsvuttG3Np1L4hJmJAOA=', PasswordSalt=' joeLPwcwMq6L7kyuVfVS7g=='   
WHERE UserId IN (SELECT UserId FROM [aspnet_Users] WHERE UserName = 'sitecore\Admin') 
It is important to set both Password and PasswordSalt.

Monday, February 25, 2013

Cloning objects in .NET

In .NET I want to do something like:
// This is illegal in C#
public class PdfDetails : ICloneable<PdfDetails>
{
    public int Id { get; set; }
    public string FileName { get; set; }

    public PdfDetails Clone()
    {
        return MemberwiseClone();
    }
}
But I can't because IClonable is not a generic interface. I remember reading explanation for it in Skeets book or Eric Lipper blog, and it was because in .NET framework before 2.0 there was no generics and since then some of the old interfaces were replaced with generic once, in other cases new generics interfaces were created, and in some cases nothing happened - and ICloneable is an example of this group, there is no support or replacement for generics. So I need to do something like:
public class PdfDetails : ICloneable
{
    public int Id { get; set; }
    public string FileName { get; set; }

    public object Clone()
    {
        return MemberwiseClone();
    }
}
Unfortunately I do not want to return an object type I want to return a PdfDetails type but I can't do it with a non generic interface. So I need to do overloading to create a method that returns a PdfDetails type, the thing that I am really thinking of is something like:
public class PdfDetails : ICloneable
{
    public int Id { get; set; }
    public string FileName { get; set; }

    public object Clone()
    {
        return MemberwiseClone();
    }

    // This is illegal in C#
    public PdfDetails Clone()
    {
     return (PdfDetails) Clone();
    }
}
But I can't do it in C#, because it is not how overloading works here. So I need to do something like:
public class PdfDetails : ICloneable
{
    public int Id { get; set; }
    public string FileName { get; set; }

    // unfortunately this method can not be public
    object ICloneable.Clone()
    {
       return MemberwiseClone();
    }

    public PdfDetails Clone()
    {
        // because ICloneable.Clone() can not be public I can not call it from here
        // instad I need to call again MemberwiseClone() method
        return (PdfDetails)MemberwiseClone();
    }
}
But when you look at the code above you ask a question then why do I really want to implement a ICloneable interface in first place. And I belive, that you don't have to if you don't need to. What I mean is you can go and safely write a code like:
public class PdfDetails 
{
    public int Id { get; set; }
    public string FileName { get; set; }

    public PdfDetails Clone()
    {
        return (PdfDetails)MemberwiseClone();
    }
}
It does the job, but it doesn't inform other programmers that PdfDetails class is supposed to Clone objects. That's why I enjoy writing my own interface:
public interface ICloneable<out T>
{
    T Clone();
}
And than implement it
public class PdfDetails : ICloneable<PdfDetails>
{
    public PdfDetails Clone()
    {
 return (PdfDetails)MemberwiseClone();
    }
}

Thursday, February 14, 2013

How ASP.NET lifecycle works

Today I had to explain it to few people, so I thought that it would be good to have a place to write it down, so I don't need to do it next time:)

Global.asax.cs includes a class (usually called Global) that inherits from HttpApplication. The hard part is that HttpApplication can only handle one request per time. The confusing part is that this class includes methods that should be executed just once per application lifecycle, and if HttpApplication is able to handle only one request per time, how is it possible that everything works, and how is it possible that IIS is able to handle multiple requests pert time?

First things first, methods like Application_Start, Application_End are called once per AppDomain lifecycle, programmer should not be concerned that they exist in a scope of a class (usually called Global) that can be created many times. So these methods are different than for example modules that are executed once per HttpApplication. And it leads us to a second thought, class that exists in Global.asax (usually called Global) should be treated as an ordinary class, it means that its object can be created and destroyed at any time.

When a new request is send to IIS, and Global class object is busy handling other request, IIS can create a new thread and create a new instance of Global class. IIS can create such thread, but doesn't have to, it can stack a request and wait to reuse Global object that will finish handling previous requests.

To make it even more complicated each AppDomain that was created by ASP.NET can have multiple Global objects, so it is extremely easy to 'configure' something wrong, especially when you use static keyword, or a singleton pattern! Now it is also important to notice that one application can run as many AppDomains, but it is not very useful configuration (in majority of cases), but one IIS can host multiple applications, and each of them 'should' run in a separate AppDomain.

Hope it clarifies something :)