public static Dictionary <string, string> getAmerasStatus() { Dictionary <string, string> AmeraStatus = new Dictionary <string, string>(); HashSet <string> AmeraIds = AmeraTasks.getAmeraIds(); Task <List <AmeraDeviceStatusReport> > task = getAmerasStatusAsync(AmeraIds); task.Wait(); List <AmeraDeviceStatusReport> reports = task.Result; foreach (AmeraDeviceStatusReport report in reports) { //format time //amera reports in epoch microseconds UTC //expected: 2016-04-05 19:20:36.0000000 DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); double timeDoubleMillis = Double.Parse(report.time) / 1000; DateTime time = epoch.AddMilliseconds(timeDoubleMillis); //detect sensor offline //because 'Connected' always return true for now //we detect offline status when the more recent update is more than 45 seconds (2 updates missed) //by default, sensor updates each 15 seconds DateTime utcNow = DateTime.UtcNow; TimeSpan t = utcNow - time; string status = (t.TotalSeconds < SensorTimeoutSecs ? "Online" : "Offline"); AmeraStatus.Add(report.amera, status); } /** * Dictionary<string, Task<List<AmeraDeviceStatusReport>>> Tasks = new Dictionary<string, Task<List<AmeraDeviceStatusReport>>>(); * foreach (string AmeraId in AmeraIds) * { * Tasks.Add(AmeraId, getAmerasStatusAsync(new HashSet<string>() { AmeraId })); * } * foreach (string AmeraId in AmeraIds) * { * Tasks[AmeraId].Wait(); * List<AmeraDeviceStatusReport> report = Tasks[AmeraId].Result; * if (report.Count == 1) * { * DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); * double timeDoubleMillis = Double.Parse(report.First().time) / 1000; * DateTime time = epoch.AddMilliseconds(timeDoubleMillis); * DateTime utcNow = DateTime.UtcNow; * * TimeSpan t = utcNow - time; * string status = (t.TotalSeconds < SensorTimeoutSecs ? "Online" : "Offline"); * AmeraStatus.Add(report[0].amera, status); * } * else * { * Console.WriteLine("Error - No status report replied! {0}",report.Count); * AmeraStatus.Add(AmeraId, "-Not Avail-"); * } * } **/ return(AmeraStatus); }
public static Dictionary <string, string> getAmeraNames() { Dictionary <string, string> AmeraNames = new Dictionary <string, string>(); HashSet <string> AmeraIds = AmeraTasks.getAmeraIds(); Dictionary <string, Task <string> > Tasks = new Dictionary <string, Task <string> >(); foreach (string AmeraId in AmeraIds) { Tasks.Add(AmeraId, getAmeraNamesAsync(AmeraId)); } foreach (string AmeraId in AmeraIds) { Tasks[AmeraId].Wait(); string jsonResult = Tasks[AmeraId].Result; //Console.WriteLine(jsonResult); Dictionary <string, string> AmeraName = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonResult); AmeraNames.Add(AmeraId, AmeraName["name"]); } return(AmeraNames); }
static void Main(string[] args) { if (args.Length != 2 && args.Length != 3) { Console.WriteLine("Amera sensor API (version " + VERSION + ")"); Console.WriteLine("Usage: AmeraSensorApi DoTask 1stAmeraId,2ndAmeraId,... [HTTP_PROXY]"); Console.WriteLine(" List of tasks:"); foreach (string task in TASKS) { if (!String.IsNullOrEmpty(task)) { Console.WriteLine(" {0}-{1}", TASKS.IndexOf(task), task); } } Console.WriteLine(Environment.NewLine); Console.WriteLine("Example: $ AmeraTasker LTEmonitor 0123af178e7f06b9ee http://127.0.0.1:8888"); Console.WriteLine(" $ AmeraTasker 1 0123af178e7f06b9ee http://127.0.0.1:8888"); Console.WriteLine(Environment.NewLine); Console.WriteLine("The ID for a given Amera sensor can be found using the Amera Web Portal"); Console.WriteLine("https://manager.meetamera.com/ameras"); Console.WriteLine(Environment.NewLine); Console.WriteLine("Press a Key to exit"); Console.ReadKey(); System.Environment.Exit(1); } // The task to perform string DoTask = args[0]; List <string> tasknames = (from s in TASKS select s.ToLower()).ToList(); if (!tasknames.Contains(DoTask.ToLower())) { Console.Error.WriteLine("Error: no task exist! {0}", DoTask); System.Environment.Exit(1); } //target sensor HashSet <string> Sensors = new HashSet <string>(args[1].ToLower().Split(',').ToList()); //optional proxy string proxy = null; if (args.Length == 3) { proxy = args[2]; } Console.WriteLine("AmeraSensorAPI task {0} started...", DoTask); Console.WriteLine("Sensors: " + String.Join(",", Sensors)); AmeraTasks ameraTasks = new AmeraTasks(proxy); // // is logged in bool Ok = AmeraTasks.isLoggedIn(); if (!Ok) { Console.Error.WriteLine("Error: not logged in!"); System.Environment.Exit(1); } // // get registered sensors HashSet <string> AmeraIds = AmeraTasks.getAmeraIds(); Console.WriteLine(Environment.NewLine); Console.WriteLine("Number of registered sensors: {0}", AmeraIds.Count); //Console.WriteLine(String.Join(",", AmeraIds)); // // get sensor names Dictionary <string, string> AmeraNames = AmeraTasks.getAmeraNames(); //Console.WriteLine(Environment.NewLine); Console.WriteLine("Number of named sensors: {0}", AmeraNames.Count); //Console.WriteLine(" "+String.Join("\n ", AmeraNames)); // // get sensor status Dictionary <string, string> AmerasStatus = AmeraTasks.getAmerasStatus(); //Console.WriteLine(Environment.NewLine); Console.WriteLine("Number of sensors that report their status: {0}", AmerasStatus.Count); //Console.WriteLine(" " + String.Join("\n ", AmerasStatus)); // // Generate new list (Id, Name, Status) List <cAmeraStatus> AmeraStatusList = new List <cAmeraStatus>(); foreach (string AmeraId in AmeraIds) { cAmeraStatus AmeraStatus = new cAmeraStatus(); AmeraStatus.Id = AmeraId; if (AmeraNames.ContainsKey(AmeraId)) { AmeraStatus.Name = AmeraNames[AmeraId]; } else { AmeraStatus.Name = "-NotAvail-"; } if (AmerasStatus.ContainsKey(AmeraId)) { AmeraStatus.Status = AmerasStatus[AmeraId]; } else { AmeraStatus.Status = "-NotAvail-"; } AmeraStatusList.Add(AmeraStatus); } //Console.WriteLine(Environment.NewLine); Console.WriteLine("Sensor details: {0}", AmeraStatusList.Count); Console.WriteLine("Id Name State"); foreach (cAmeraStatus AmeraStatus in AmeraStatusList.OrderBy(n => n.Name).ToList()) { Console.WriteLine("{0} {1} {2}", AmeraStatus.Id, AmeraStatus.Name, AmeraStatus.Status); } //#if IGNORE_FOR_NOW // // do LTE monitor bool SensorSelect = AmeraTasks.setAmeraIds(Sensors); Console.WriteLine(Environment.NewLine); Console.WriteLine("Status of setAmeraIds:" + SensorSelect.ToString()); //TBD Dictionary<string, List<AmeraTasks.LteMonitorReport>> LteMonitorReports = AmeraTasks.doLteMonitor(1); Dictionary <string, string> LteMonitorReports = AmeraTasks.doLteMonitor(1); Dictionary <string, List <AmeraTasks.FreqCellPair> > FreqCellPairs = AmeraTasks.getFreqCellPairs(); //#endif Console.WriteLine(Environment.NewLine); Console.WriteLine("Press a Key to exit"); Console.ReadKey(); System.Environment.Exit(0); }