Repeating a GET request without re-running script - json

I am trying to create a script that will show the bus times from my local stop on a scrollphatHD on raspberry pi zero w.
I can get the script running and I get the correct bus times, but I am having trouble getting the new bus times without restarting the script.
How can I loop the bus times so that I always get the updated times?
import requests
import signal
import scrollphathd
from scrollphathd.fonts import font3x5
r = requests.get("https://skyss.giantleap.no/public/departures?Hours=1&StopIdentifiers=12015491")
dest = r.json()["PassingTimes"][0]["TripDestination"]
avgang = r.json()["PassingTimes"][0]["DisplayTime"]
dest2 = r.json()["PassingTimes"][1]["TripDestination"]
avgang2 = r.json()["PassingTimes"][1]["DisplayTime"]
linje2 = dest + " " + avgang + ", " + dest2 + " " + avgang2 + ", "
while True:
scrollphathd.write_string(str1, y=1, font=font3x5, brightness=0.5)
scrollphathd.flip(x,y)
scrollphathd.scroll()
scrollphathd.show()
print(linje2)
I want the result to be updated about every 20 seconds or so, but I end up just getting the time from when I start the script over and over.

Related

How can I fix an error in the MCMChregress function that seems to stem from the R argument

I'm attempting to run a Bayesian Hierarchical model using MCMChregress, but don't know to fix the problem expressed by this error. My code is below. I believe the problem has to do with the R and r argument and how they relate to the number of parameters. So I didn't include the data set. Let me know if any additional information would be helpful!
"Error: in Wishart(r,rR) prior, R not comformable [q times q].
Error in form.wishart.prior.hmodels(r, R, nq) :
Please respecify and call MCMChregress() again."
#bayesian hierarchical
bhlm.2 = MCMChregress(
fixed = nip_wk_avg ~ unique_elements + vbs.theme_relevance +
vbs.total_score + vbs.product_attributes +
Super_Segment + vbs.play_promise +
vbs.visual_appeal + outgoing + pp79.99 + pp19.99,
random = ~ unique_elements + vbs.theme_relevance +
vbs.total_score + vbs.product_attributes +
Super_Segment + vbs.play_promise +
vbs.visual_appeal + outgoing + pp79.99 + pp19.99,
group = "Super_Theme", data = train.data,
r = 20, R = diag(20))

OpenWeatherMap JSON API forecast for rain and snow

I am facing problems forcing my code run smoothly when there is no value for Parameters rain and snow. Actually it loops as long as there are values for these parameters and if not it ends.
I have included gson library for helping me get the values as JSON objects/strings/double etc.
for (int l=0; l<40; l++) {
Forecast fcath = new Forecast();
fcath.setMain_temp(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("main").getAsJsonObject().get("temp").getAsDouble());
fcath.setWeather_description(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("weather").getAsJsonArray().get(0).getAsJsonObject().get("description").getAsString());
fcath.setClouds_all(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("clouds").getAsJsonObject().get("all").getAsInt());
fcath.setWind_speed(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("wind").getAsJsonObject().get("speed").getAsDouble());
fcath.setDt_txt(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("dt_txt").getAsString());
fcath.setRain(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("rain").getAsJsonObject().get("3h").getAsDouble());
fcath.setSnow(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("snow").getAsJsonObject().get("3h").getAsDouble());
f.add(fcath);
System.out.println("Date: " + f.get(l).getDt_txt() + " Temp: " + f.get(l).getMain_temp() + " Celsius " + " Chance of Rain: " + f.get(l).getRain() + " Chance of Snow: " + f.get(l).getSnow());
}
Without fcath.setRain and fcath.setSnow code lines the output is the following:
Working output photo
Assuming that there is no rain chance in the forecast
e.g.
fcath.setRain(obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("rain").getAsJsonObject().get("3h").getAsDouble()); == {}
it gives me NullPointerException
NullPointerException photo
Is still hard to say with the information provided, but seems that if the is no rain chance, then probably
obj_fcath.get("list").getAsJsonArray().get(l).getAsJsonObject().get("rain").getAsJsonObject()
is null. Then you cannot do a get("3h").getAsDouble() from a null object.

ffmpeg azure function consumption plan low CPU availability for high volume requests

I am running an azure queue function on a consumption plan; my function starts an FFMpeg process and accordingly is very CPU intensive. When I run the function with less than 100 items in the queue at once it works perfectly, azure scales up and gives me plenty of servers and all of the tasks complete very quickly. My problem is once I start doing more than 300 or 400 items at once, it starts fine but after a while the CPU slowly goes from 80% utilisation to only around 10% utilisation - my functions cant finish in time with only 10% CPU. This can be seen in the image shown below.
Does anyone know why the CPU useage is going lower the more instances my function creates? Thanks in advance Cuan
edit: the function is set to only run one at a time per instance, but the problem exists when set to 2 or 3 concurrent processes per instance in the host.json
edit: the CPU drops get noticeable at 15-20 servers, and start causing failures at around 60. After that the CPU bottoms out at an average of 8-10% with individuals reaching 0-3%, and the server count seems to increase without limit (which would be more helpful if I got some CPU with the servers)
Thanks again, Cuan.
I've also added the function code to the bottom of this post in case it helps.
using System.Net;
using System;
using System.Diagnostics;
using System.ComponentModel;
public static void Run(string myQueueItem, TraceWriter log)
{
log.Info($"C# Queue trigger function processed a request: {myQueueItem}");
//Basic Parameters
string ffmpegFile = #"D:\home\site\wwwroot\CommonResources\ffmpeg.exe";
string outputpath = #"D:\home\site\wwwroot\queue-ffmpeg-test\output\";
string reloutputpath = "output/";
string relinputpath = "input/";
string outputfile = "video2.mp4";
string dir = #"D:\home\site\wwwroot\queue-ffmpeg-test\";
//Special Parameters
string videoFile = "1 minute basic.mp4";
string sub = "1 minute sub.ass";
//guid tmp files
// Guid g1=Guid.NewGuid();
// Guid g2=Guid.NewGuid();
// string f1 = g1 + ".mp4";
// string f2 = g2 + ".ass";
string f1 = videoFile;
string f2 = sub;
//guid output - we will now do this at the caller level
string g3 = myQueueItem;
string outputGuid = g3+".mp4";
//get input files
//argument
string tmp = subArg(f1, f2, outputGuid );
//String.Format("-i \"" + #"input/tmp.mp4" + "\" -vf \"ass = '" + sub + "'\" \"" + reloutputpath +outputfile + "\" -y");
log.Info("ffmpeg argument is: "+tmp);
//startprocess parameters
Process process = new Process();
process.StartInfo.FileName = ffmpegFile;
process.StartInfo.Arguments = tmp;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = dir;
//output handler
process.OutputDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
log.Info("O: "+e.Data);
}
);
process.ErrorDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
log.Info("E: "+e.Data);
}
);
//start process
process.Start();
log.Info("process started");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
public static void getFile(string link, string fileName, string dir, string relInputPath){
using (var client = new WebClient()){
client.DownloadFile(link, dir + relInputPath+ fileName);
}
}
public static string subArg(string input1, string input2, string output1){
return String.Format("-i \"" + #"input/" +input1+ "\" -vf \"ass = '" + #"input/"+input2 + "'\" \"" + #"output/" +output1 + "\" -y");
}
When you use the D:\home directory you are writing to the virtual function, which means each instance has to continually try to write to the same spot as the functions run which causes the massive I/O block. Instead writing to D:\local and then sending the finished file somewhere else solves that issue, this way rather than each instance constantly writing to a location they only write when completed, and write to a location designed to handle high throughput.
The easiest way I could find to manage the input and output after writing to D:\local was just to hook up the function to an azure storage container and handle the ins and outs that way. Doing so made the average CPU stay at 90-100% for upwards of 70 concurrent Instances.

How to deal with information received in two packets

This is the case. I want to make a game, client being made in flash and server on java. From server side, the first byte i write on the stream is the protocol id, like this:
try
{
Output.writeByte(LOGIN);
Output.writeByte((byte)ID);
Output.writeByte(new_position.x);
Output.writeByte(new_position.y);
Output.flush();
}
After the 'onResponse' event is triggered, the socket is read like this:
type:int = socket_client.readByte();
if (type == 0x1)
FP.console.log("You are logged as " + socket_client.readByte() + " in x:" + socket_client.readByte() + " y:" + socket_client.readByte() );
else if (type == 0x2)
FP.console.log("You are now in x:" + socket_client.readByte() + " y:" + socket_client.readByte());
As you probably have guessed by now, this gives me some problems. Sometimes, server sends the information split in two, so the above code throws an EOF exception. Tracing the following code gives me sometimes this result:
trace("SIZE: " + socket_client.bytesAvailable);
//var type:int = socket_client.readByte();
var values:String = "";
while (socket_client.bytesAvailable > 0)
values += socket_client.readByte() + " ";
trace(values);`
Values:
SIZE: 1
2
SIZE: 2
2 6
The first '2' is the protocol id, the second and the third stands for x and y values.
Now, the question is, how can i prevent this to happen? How could i 'wait' until i have all the information needed?
Btw, on java this never happens, but i have no more control than on as3.
Add BufferedOutputStream in output initialization like this:
Output = new DataOutputStream(new BufferedOutputStream(connection.getOutputStream()));
Basically you need to switch your message format from [type, data] to [type, length, data]. Then, wait to process the data until bytesAvailable >= length, otherwise put it into a buffer.
Here is an example SOCKET_DATA handler that uses this logic:
https://github.com/magicalhobo/Flash-CS5-mobile-proxy/blob/master/com/magicalhobo/mobile/proxy/MobileClient.as#L110

"CalendarApp: Mismatch: etags" when adding reminders - Google Apps Scripts

I have a small Google Apps Script that processes a date column in a spreadsheet and generates entries in a Calendar (birthdays).
Work is fine, but when adding reminders to the (recently-created) CalendarEvent, an error is thrown :
Service error: CalendarApp: Mismatch: etags = ["GUQKRgBAfip7JGA6WhJb"], version = [63489901413]
I've tried to perform 1 second sleep after creating event (wait for changes to be done in calendar), but no luck on this...
BTW, events are created succesfully, only reminders cannot be added.
PD: the calendar is one I own, but not my primary calendar.
Here is part of the code:
try
{
birthday = new Date(Data[i][BirthColumn]);
birthday.setFullYear(today.getFullYear());
birthday.setUTCHours(12);
birthlist += Data[i][NameColumn] + " --> " + birthday + "\n";
calendarevent = cal.createAllDayEventSeries("¡Cumpleaños " + Data[i][NameColumn] + "!", birthday, CalendarApp.newRecurrence().addYearlyRule().times(YearsInAdvance));
if (calendarevent == null)
success = false;
else
{
//This sentence fails every single time.
calendarevent.addEmailReminder(0);
calendarevent.addPopupReminder(0);
calendarevent.addSmsReminder(0);
}
}
catch (ee)
{
var row = i + 1;
success = false;
errlist += "Error on row " + row + ": check name and birth date. Exception Error: " + ee.message + "\n";
}
This is the portion of the code I finally change to make it work, as Serge insas suggest me before:
if (calendarevent == null)
success = false;
else
{
cal.getEventSeriesById(calendarevent.getId()).addEmailReminder(0);
cal.getEventSeriesById(calendarevent.getId()).addPopupReminder(0);
cal.getEventSeriesById(calendarevent.getId()).addSmsReminder(0);
}
This is a known issue
See comment nr 67 for a working workaround : the trick is to re-call the event for every item you want to add (reminder, popup...) using cal.getEventSeriesById(eventID) after you get the Id simply with .getId()
I use it in some scripts and it solved the issue for me.