Skip to content

Commit

Permalink
feat: add Test method to verify parsing blackout value
Browse files Browse the repository at this point in the history
  • Loading branch information
jkdevito committed Jul 21, 2024
1 parent 6ad5bc9 commit 3d4bb67
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions src/MegapixelHeliosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class MegapixelHeliosController : EssentialsBridgeableDevice
#region IRestfulComms

private readonly IRestfulComms _client;

private int _responseCode;
public int ResponseCode
{
Expand Down Expand Up @@ -160,7 +160,7 @@ public MegapixelHeliosController(string key, string name, MegapixelHeliosPropert
//StatusFeedback = new IntFeedback(() => (int)_commsMonitor.Status);

PowerIsOnFeedback = new BoolFeedback(() => PowerIsOn);
CurrentPresetIdFeedback = new IntFeedback(()=> CurrentPresetId);
CurrentPresetIdFeedback = new IntFeedback(() => CurrentPresetId);
CurrentPresetNameFeedback = new StringFeedback(() => CurrentPresetName);

ResponseCodeFeedback = new IntFeedback(() => ResponseCode);
Expand Down Expand Up @@ -284,6 +284,30 @@ private JToken IsValidJson(string contentString)
}
}

public void TestOnResponseReceived()
{
var jsonObject = new RootDevObject
{
Dev = new DevObject
{
Display = new DisplayObject
{
Blackout = true
}
}
};
var contentString = JsonConvert.SerializeObject(jsonObject, Formatting.Indented);

var sender = new object();
var args = new GenericClientResponseEventArgs
{
Code = 200,
ContentString = contentString
};

OnResponseReceived(sender, args);
}

private void OnResponseReceived(object sender, GenericClientResponseEventArgs args)
{
try
Expand All @@ -292,7 +316,7 @@ private void OnResponseReceived(object sender, GenericClientResponseEventArgs ar
"OnResponseReceived: Code = {0} | ContentString = {1}",
args.Code, args.ContentString);

ResponseCode = args.Code;
ResponseCode = args.Code;

if (string.IsNullOrEmpty(args.ContentString))
{
Expand All @@ -305,11 +329,12 @@ private void OnResponseReceived(object sender, GenericClientResponseEventArgs ar
var jToken = IsValidJson(args.ContentString);
if (jToken == null)
{
Debug.Console(MegapixelHeliosDebug.Notice, this, "OnResponseReceived: IsValidJson failed, passing ContentString as string");
Debug.Console(MegapixelHeliosDebug.Notice, this, "OnResponseReceived: IsValidJson failed, passing ContentString as string");
return;
}

ProcessJToken(jToken);
ProcessJToken(jToken, "dev.display.blackout");
//ProcessJToken(jToken, "presetName");
}
catch (Exception ex)
{
Expand All @@ -320,22 +345,16 @@ private void OnResponseReceived(object sender, GenericClientResponseEventArgs ar
}

// process JToken
private void ProcessJToken(JToken jToken)
private void ProcessJToken(JToken jToken, string path)
{
var token = jToken.SelectToken("dev");
if (token == null)
{
Debug.Console(MegapixelHeliosDebug.Notice, this, "ProcessJToken: selectToken 'dev' failed");
}

token = jToken.SelectToken("presetName");
var token = jToken.SelectToken(path);
if (token == null)
{
Debug.Console(MegapixelHeliosDebug.Notice, this, "ProcessJToken: selectToken 'presetName' failed");
Debug.Console(MegapixelHeliosDebug.Notice, this, "ProcessJToken: selectToken '{0}' failed", path);
return;
}

Debug.Console(MegapixelHeliosDebug.Notice, this, "ProcessJToken: token.Type is '{0}'", token.Type.ToString());
Debug.Console(MegapixelHeliosDebug.Notice, this, "ProcessJToken: '{0}' token.Type is '{1}'", path, token.Type.ToString());

try
{
Expand All @@ -350,7 +369,18 @@ private void ProcessJToken(JToken jToken)
{
// [ ] TODO - update to process JSON arrays
break;
}
}
case JTokenType.Boolean:
{
// [ ] TODO - update to process JSON objects
if (path == "dev.display.blackout")
{
Debug.Console(MegapixelHeliosDebug.Verbose, this, "ProcessJToken: '{0}' = {1}", path, token.Value<bool>());
PowerIsOn = token.Value<bool>();
}

break;
}
default:
{
Debug.Console(MegapixelHeliosDebug.Notice, this, "ProcessJToken: unhandled JTokenType '{0}'", token.Type.ToString());
Expand Down Expand Up @@ -402,7 +432,7 @@ public void PowerOn()
Display = new DisplayObject
{
Blackout = false
}
}
}
};

Expand Down Expand Up @@ -448,7 +478,7 @@ public void PowerOff()
Debug.Console(MegapixelHeliosDebug.Trace, this, "PowerOff: content-'{0}'", content);
_client.SendRequest("PATCH", "/api/v1/public", content);
}

/// <summary>
/// Queries device for preset list
/// </summary>
Expand Down

0 comments on commit 3d4bb67

Please sign in to comment.