Skip to content

Instantly share code, notes, and snippets.

public class PropertyParser : IPropertyParser
{
public IEnumerable<string> Parse<TEntity>(Expression<Func<TEntity, object>> expression, bool validate = true) where TEntity : new()
{
return ParseProperty<TEntity>(expression.Body, validate);
}
public IEnumerable<string> Parse<TEntity,UEntity>(Expression<Func<TEntity, UEntity, object>> expression, bool validate = true) where TEntity : new()
{
return ParseProperty<TEntity>(expression.Body, validate);
}
@avisiboni
avisiboni / GenerateSasToken
Created December 23, 2019 09:41
Generate Sas Token function
public static string GenerateSasToken(string resourceUri, string key, string policyName = null, int expiryInSeconds = 3600)
{
TimeSpan fromEpochStart = DateTime.UtcNow - new DateTime(1970, 1, 1);
string expiry = Convert.ToString((int)fromEpochStart.TotalSeconds + expiryInSeconds);
string stringToSign = WebUtility.UrlEncode(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(key));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
@avisiboni
avisiboni / Main
Created December 23, 2019 09:43
Main function
static void Main(string[] args)
{
string deviceId = "YOUR-DEVICE-ID".ToLower();
string deviceKey = "YOUR-DEVICE PK OR SK";
string hostname = $"YOUR-ENDPOINT-NAMESPACE.azure-devices.net";
string endpoint = $"{hostname}/devices/{deviceId}";
var token = GenerateSasToken(endpoint, deviceKey);
var conn = $"HostName={hostname};CredentialType=SharedAccessSignature;DeviceId={deviceId};SharedAccessSignature={token}";
var deviceClient = DeviceClient.CreateFromConnectionString(conn, TransportType.Amqp_WebSocket_Only);
SendEvent(deviceClient).ConfigureAwait(false).GetAwaiter().GetResult();
@avisiboni
avisiboni / SendEvent
Created December 23, 2019 09:44
Send event from device function
static async Task SendEvent(DeviceClient deviceClient)
{
var dataBuffer = JsonConvert.SerializeObject(
new
{
Wind = 10,
Humidity = 70,
Precipitation = 0
});
const a = 1
//this service for catching all error in place
import { Injectable, ErrorHandler } from '@angular/core';
import { LogService } from './log.service';
@Injectable()
export class ErrorHandlerService extends ErrorHandler {
constructor(private logService: LogService) {
super();
}
@avisiboni
avisiboni / id_number_validtion.js
Created November 29, 2020 15:53
Israel id number validation
function validation(id) {
id = String(id).trim();
if (id.length > 9 || isNaN(id)) return false;
id = id.length < 9 ? ("00000000" + id).slice(-9) : id;
return Array.from(id, Number).reduce((counter, digit, i) => {
const step = digit * ((i % 2) + 1);
return counter + (step > 9 ? step - 9 : step);
}) % 10 === 0;
}
@avisiboni
avisiboni / module.cs
Created November 30, 2020 14:48
cache resolver
builder.Register(c =>
{
var cacheType = ConfigurationManager.AppSettings["Cache.Type"];
var configCache = (CacheType)Enum.Parse(typeof(CacheType), cacheType);
switch (configCache)
{
case CacheType.Context:
default:
return (ICacheService) c.Resolve<ContextCacheService>();
case CacheType.Redis:
public interface ICacheService
{
T GetData<T>(string key);
Task<T> GetDataAsync<T>(string key);
Task SetDataAsync<T>(string key, T data);
bool ClearData(string key);
}
public class ContextCacheService : ICacheService
{
public T GetData<T>(string key)
{
try
{
var value = HttpContext.Current.Cache[key];